1// This is an open source non-commercial project. Dear PVS-Studio, please check
2// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
4#include "nvim/os/os.h"
5#include "nvim/fileio.h"
6#include "nvim/vim.h"
7#include "nvim/main.h"
8#include "nvim/ui.h"
9#include "nvim/aucmd.h"
10#include "nvim/eval.h"
11
12#ifdef INCLUDE_GENERATED_DECLARATIONS
13# include "aucmd.c.generated.h"
14#endif
15
16void do_autocmd_uienter(uint64_t chanid, bool attached)
17{
18 static bool recursive = false;
19
20 if (recursive) {
21 return; // disallow recursion
22 }
23 recursive = true;
24
25 dict_T *dict = get_vim_var_dict(VV_EVENT);
26 assert(chanid < VARNUMBER_MAX);
27 tv_dict_add_nr(dict, S_LEN("chan"), (varnumber_T)chanid);
28 tv_dict_set_keys_readonly(dict);
29 apply_autocmds(attached ? EVENT_UIENTER : EVENT_UILEAVE,
30 NULL, NULL, false, curbuf);
31 tv_dict_clear(dict);
32
33 recursive = false;
34}
35
36static void focusgained_event(void **argv)
37{
38 bool *gainedp = argv[0];
39 do_autocmd_focusgained(*gainedp);
40 xfree(gainedp);
41}
42void aucmd_schedule_focusgained(bool gained)
43{
44 bool *gainedp = xmalloc(sizeof(*gainedp));
45 *gainedp = gained;
46 loop_schedule_deferred(&main_loop,
47 event_create(focusgained_event, 1, gainedp));
48}
49
50static void do_autocmd_focusgained(bool gained)
51{
52 static bool recursive = false;
53
54 if (recursive) {
55 return; // disallow recursion
56 }
57 recursive = true;
58 apply_autocmds((gained ? EVENT_FOCUSGAINED : EVENT_FOCUSLOST),
59 NULL, NULL, false, curbuf);
60 recursive = false;
61}
62