summaryrefslogtreecommitdiff
path: root/libxklavier/xklavier_util.c
blob: 1ed0196e43637aba261a151bdd26b8fa5de907f3 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (C) 2002-2006 Sergey V. Udaltsov <svu@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <time.h>
#include <string.h>

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "xklavier_private.h"

XklState *
_xkl_state_copy(XklState * state)
{
	XklState * copy;

	copy = g_new(XklState, 1);
	copy->group = state->group;
	copy->indicators = state->indicators;

	return copy;
}

G_DEFINE_BOXED_TYPE (XklState, xkl_state, _xkl_state_copy, g_free);

XklState *
xkl_engine_get_current_state(XklEngine * engine)
{
	return &xkl_engine_priv(engine, curr_state);
}

const gchar *
xkl_get_last_error()
{
	return xkl_last_error_message;
}

gchar *
xkl_engine_get_window_title(XklEngine * engine, Window w)
{
	Atom type_ret;
	int format_ret;
	unsigned long nitems, rest;
	unsigned char *prop;

	if ((w == xkl_engine_priv(engine, root_window))
	    || (w == PointerRoot))
		return g_strdup("ROOT");

	if (Success ==
	    XGetWindowProperty(xkl_engine_get_display(engine), w,
			       xkl_engine_priv(engine, atoms)[WM_NAME], 0L,
			       -1L, False, XA_STRING, &type_ret,
			       &format_ret, &nitems, &rest, &prop))
		return (gchar *) prop;
	else
		return NULL;
}

gboolean
xkl_engine_is_window_from_same_toplevel_window(XklEngine * engine,
					       Window win1, Window win2)
{
	Window app1, app2;
	return xkl_engine_find_toplevel_window(engine, win1, &app1) &&
	    xkl_engine_find_toplevel_window(engine, win2, &app2)
	    && app1 == app2;
}

gboolean
xkl_engine_get_state(XklEngine * engine, Window win, XklState * state_out)
{
	Window app_win;

	if (!xkl_engine_find_toplevel_window(engine, win, &app_win)) {
		if (state_out != NULL)
			state_out->group = -1;
		return FALSE;
	}

	return xkl_engine_get_toplevel_window_state(engine, app_win,
						    state_out);
}

void
xkl_engine_delete_state(XklEngine * engine, Window win)
{
	Window app_win;

	if (xkl_engine_find_toplevel_window(engine, win, &app_win))
		xkl_engine_remove_toplevel_window_state(engine, app_win);
}

void
xkl_engine_save_state(XklEngine * engine, Window win, XklState * state)
{
	Window app_win;

	if (!
	    (xkl_engine_is_listening_for
	     (engine, XKLL_MANAGE_WINDOW_STATES)))
		return;

	if (xkl_engine_find_toplevel_window(engine, win, &app_win))
		xkl_engine_save_toplevel_window_state(engine, app_win,
						      state);
}

/*
 *  Prepares the name of window suitable for debugging (32characters long).
 */
gchar *
xkl_get_debug_window_title(XklEngine * engine, Window win)
{
	static gchar sname[33];
	gchar *name;
	strcpy(sname, "NULL");
	if (win != (Window) NULL) {
		name = xkl_engine_get_window_title(engine, win);
		if (name != NULL) {
			g_snprintf(sname, sizeof(sname), "%.32s", name);
			g_free(name);
		}
	}
	return sname;
}

Window
xkl_engine_get_current_window(XklEngine * engine)
{
	return xkl_engine_priv(engine, curr_toplvl_win);
}

/*
 * Loads subtree. 
 * All the windows with WM_STATE are added.
 * All the windows within level 0 are listened for focus and property
 */
gboolean
xkl_engine_load_subtree(XklEngine * engine, Window window, gint level,
			XklState * init_state)
{
	Window rwin = (Window) NULL,
	    parent = (Window) NULL, *children = NULL, *child;
	guint num = 0;
	gboolean retval = True;

	xkl_engine_priv(engine, last_error_code) =
	    xkl_engine_query_tree(engine, window, &rwin, &parent,
				  &children, &num);

	if (xkl_engine_priv(engine, last_error_code) != Success) {
		return FALSE;
	}

	child = children;
	while (num) {
		if (xkl_engine_if_window_has_wm_state(engine, *child)) {
			xkl_debug(160,
				  "Window " WINID_FORMAT
				  " '%s' has WM_STATE so we'll add it\n",
				  *child,
				  xkl_get_debug_window_title(engine,
							     *child));
			xkl_engine_add_toplevel_window(engine, *child,
						       window, TRUE,
						       init_state);
		} else {
			xkl_debug(200,
				  "Window " WINID_FORMAT
				  " '%s' does not have have WM_STATE so we'll not add it\n",
				  *child,
				  xkl_get_debug_window_title(engine,
							     *child));

			if (level == 0) {
				xkl_debug(200,
					  "But we are at level 0 so we'll spy on it\n");
				xkl_engine_select_input_merging(engine,
								*child,
								FocusChangeMask
								|
								PropertyChangeMask);
			} else
				xkl_debug(200,
					  "And we are at level %d so we'll not spy on it\n",
					  level);

			retval =
			    xkl_engine_load_subtree(engine, *child,
						    level + 1, init_state);
		}

		child++;
		num--;
	}

	if (children != NULL)
		XFree(children);

	return retval;
}

/*
 * Checks whether given window has WM_STATE property (i.e. "App window").
 */
gboolean
xkl_engine_if_window_has_wm_state(XklEngine * engine, Window win)
{				/* ICCCM 4.1.3.1 */
	Atom type = None;
	int format;
	unsigned long nitems;
	unsigned long after;
	unsigned char *data = NULL;	/* Helps in the case of BadWindow error */

	XGetWindowProperty(xkl_engine_get_display(engine), win,
			   xkl_engine_priv(engine, atoms)[WM_STATE], 0, 0,
			   False, xkl_engine_priv(engine, atoms)[WM_STATE],
			   &type, &format, &nitems, &after, &data);
	if (data != NULL)
		XFree(data);	/* To avoid an one-byte memory leak because after successfull return
				 * data array always contains at least one nul byte (NULL-equivalent) */
	return type != None;
}

/*
 * Finds out the official parent window (accortind to XQueryTree)
 */
Window
xkl_engine_get_registered_parent(XklEngine * engine, Window win)
{
	Window parent = (Window) NULL, rw = (Window) NULL, *children =
	    NULL;
	guint nchildren = 0;

	xkl_engine_priv(engine, last_error_code) =
	    xkl_engine_query_tree(engine, win, &rw, &parent, &children,
				  &nchildren);

	if (children != NULL)
		XFree(children);

	return xkl_engine_priv(engine, last_error_code) ==
	    Success ? parent : (Window) NULL;
}

/*
 * Make sure about the result. Origial XQueryTree is pretty stupid beast:)
 */
Status
xkl_engine_query_tree(XklEngine * engine, Window w,
		      Window * root_out,
		      Window * parent_out,
		      Window ** children_out, guint * nchildren_out)
{
	gboolean result;
	unsigned int nc;

	result = (gboolean) XQueryTree(xkl_engine_get_display(engine),
				       w,
				       root_out,
				       parent_out, children_out, &nc);
	*nchildren_out = nc;

	if (!result) {
		xkl_debug(160,
			  "Could not get tree info for window "
			  WINID_FORMAT ": %d\n", w, result);
		xkl_last_error_message = "Could not get the tree info";
	}

	return result ? Success : FirstExtensionError;
}

const gchar *
xkl_event_get_name(gint type)
{
	/* Not really good to use the fact of consecutivity
	   but X protocol is already standartized so... */
	static const gchar *evt_names[] = {
		"KeyPress",
		"KeyRelease",
		"ButtonPress",
		"ButtonRelease",
		"MotionNotify",
		"EnterNotify",
		"LeaveNotify",
		"FocusIn",
		"FocusOut",
		"KeymapNotify",
		"Expose",
		"GraphicsExpose",
		"NoExpose",
		"VisibilityNotify",
		"CreateNotify",
		"DestroyNotify",
		"UnmapNotify",
		"MapNotify",
		"MapRequest",
		"ReparentNotify",
		"ConfigureNotify",
		"ConfigureRequest",
		"GravityNotify",
		"ResizeRequest",
		"CirculateNotify",
		"CirculateRequest",
		"PropertyNotify",
		"SelectionClear",
		"SelectionRequest",
		"SelectionNotify",
		"ColormapNotify", "ClientMessage", "MappingNotify",
		"LASTEvent"
	};
	type -= KeyPress;
	if (type < 0 || type >= (sizeof(evt_names) / sizeof(evt_names[0])))
		return "UNKNOWN";
	return evt_names[type];
}

void
xkl_engine_update_current_state(XklEngine * engine, int group,
				unsigned indicators, const char reason[])
{
	xkl_debug(150,
		  "Updating the current state with [g:%d/i:%u], reason: %s\n",
		  group, indicators, reason);
	xkl_engine_priv(engine, curr_state).group = group;
	xkl_engine_priv(engine, curr_state).indicators = indicators;
}