summaryrefslogtreecommitdiff
path: root/event2.c
blob: 10d9ae642b6cd4b9976787b4cc6b918297238c94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <X11/Xlib.h>
#include <time.h>

typedef struct s_elock_win_item
{
    Window                  win;
    time_t                  create_time;
    struct s_elock_win_item *next;
} t_elock_win_item;

typedef struct s_elock_win_list
{
    Display                *disp;
    struct s_elock_win_item *first;
    struct s_elock_win_item *last;
} t_elock_win_list;

t_elock_win_list        list;

Display                *elock_open_display ();
void                    elock_error (char *message);
void                    elock_echo (char *message);
t_elock_win_list       *elock_init_list ();
static void             elock_add_to_list (Window win);
static void             elock_select_events (Window wind, Bool substructureOnly);


Display                *elock_open_display ()
{
    Display                *disp;

	printf ("> elock_open_display\n");

    disp = XOpenDisplay (getenv ("DISPLAY"));

    if (disp == NULL)
    {
        fprintf (stderr, "Can't open Display\n");
        exit (1);
    }
    fprintf (stdout, "DISPLAY OPENED\n");
	printf ("< elock_open_display\n");

    return disp;
}

void elock_error (char *message)
{
    fprintf (stderr, "%s\n", message);
    exit (1);
}

void elock_echo (char *message)
{
    fprintf (stdout, "%s\n", message);
    return;
}

t_elock_win_list       *elock_init_list ()
{
    int                     loop;
    t_elock_win_list        list;

	printf ("> elock_init_list\n");

    list.disp = elock_open_display ();
    list.first = 0;
    list.last = 0;

    for (loop = -1; ++loop < ScreenCount (list.disp);)
    {
        Window                  root = RootWindowOfScreen (ScreenOfDisplay (list.disp, loop));

        elock_add_to_list (root);
        elock_select_events (root, True);
    }
    t_elock_win_list       *p_list = &list;
	
	printf ("< elock_init_list\n");

    return p_list;
}

static void elock_add_to_list (Window win)
{
    t_elock_win_item       *new_win_item = malloc (sizeof (t_elock_win_item));

	printf ("> elock_add_to_list\n");

    if (new_win_item == NULL)
        elock_error ("MALLOC ERROR");

    new_win_item->win = win;
    new_win_item->create_time = time (0);
    new_win_item->next = 0;

    if (!list.first)
    {
        list.first = new_win_item;
    }
    if (list.last)
    {
        list.last->next = new_win_item;
    }

	printf ("< elock_add_to_list\n");

    list.last = new_win_item;
}

static void elock_select_events (Window wind, Bool substructureOnly)
{
    Window                  elock_root;
    Window                  elock_parent;
    Window                 *elock_child;
    unsigned                elock_nb_child = 0;
    unsigned                loop_count;
    XWindowAttributes       attribs;

	printf ("> elock_select_events\n");

    if (!XQueryTree (list.disp, wind, &elock_root, &elock_parent, &elock_child, &elock_nb_child))
    {
        elock_error ("XQueryTree ERROR");
    }

    if (elock_nb_child)
	{
		printf ("elock_nb_child EXIST\n");
        XFree ((char *)elock_child);
	}

    if (substructureOnly)
	{
		printf ("substructureOnly EXIST\n");
        XSelectInput (list.disp, wind, SubstructureNotifyMask);
	}
    else
    {
		printf ("substructureOnly DOESN'T EXIST\n");
        if (elock_parent == None)
        {
			printf ("elock_parent == None\n");
            attribs.all_event_masks = attribs.do_not_propagate_mask = KeyPressMask;
        }
        else if (!XGetWindowAttributes (list.disp, wind, &attribs))
        {
            elock_error ("XGetWindowAttributes ERROR");
        }
        XSelectInput (list.disp,
                      wind,
                      SubstructureNotifyMask |
                      ((attribs.all_event_masks | attribs.do_not_propagate_mask) & KeyPressMask));
    }

    if (!XQueryTree (list.disp, wind, &elock_root, &elock_parent, &elock_child, &elock_nb_child))
    {
        elock_error ("XQueryTree ERROR");
    }

    for (loop_count = 0; loop_count < elock_nb_child; ++loop_count)
    {
		printf (">> for\n");
        elock_select_events (elock_child[loop_count], substructureOnly);
    }

    if (elock_nb_child)
	{
		printf ("elock_nb_child EXIST\n");
        XFree ((char *)elock_child);
	}

	printf ("< elock_select_events\n");

}


int main ()
{
    elock_init_list ();

	return 0;
}