A
download global-keys.c
Language: C
LOC: 278
Project Info
gnome-core
Server: Gnome
Type: cvs
...nome‑core\gnome‑core\panel\
   .cvsignore
   aligned-widget.c
   aligned-widget.h
   applet.c
   applet.h
   basep-widget.c
   basep-widget.h
   border-widget.c
   border-widget.h
   button-widget.c
   button-widget.h
   distribution.c
   distribution.h
   drawer-widget.c
   drawer-widget.h
   drawer.c
   drawer.h
   edge-widget.c
   edge-widget.h
   floating-widget.c
   floating-widget.h
   foobar-widget.c
   foobar-widget.h
   global-keys.c
   global-keys.h
   gnome-panel-add-launcher.c
   gnome-panel-properties.c
   ...l-properties.desktop.in
   ...-panel-properties.glade
   gnome-panel-screenshot.c
   ...-panel-screenshot.glade
   gnome-run.c
   gnome-run.h
   GNOME_Panel.server.in
   launcher.c
   launcher.h
   logout.c
   logout.h
   main.c
   Makefile.am
   menu-fentry.c
   menu-fentry.h
   menu-properties.c
   menu-properties.h
   menu-util.c
   menu-util.h
   menu.c
   menu.h
   multiscreen-stuff.c
   multiscreen-stuff.h
   nothing.cP
   nothing.h
   panel-applet-frame.c
   panel-applet-frame.h
   panel-config-global.h
   panel-config.c
   panel-config.h
   panel-gconf.c
   panel-gconf.h
   ...l-global-config.schemas
   panel-main.h
   panel-marshal.list
   ...er-panel-config.schemas
   panel-shell.c
   panel-shell.h
   panel-types.h
   panel-util.c
   panel-util.h
   panel-widget.c
   panel-widget.h
   panel.c
   panel.h
   panel.hints
   quick-desktop-reader.c
   quick-desktop-reader.h
   rgb-stuff.c
   rgb-stuff.h
   session.c
   session.h
   sliding-widget.c
   sliding-widget.h
   status-docklet.c
   status-docklet.h
   status.c
   status.h
   swallow.c
   swallow.h
   TODO.old
   xstuff.c
   xstuff.h

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
#include <config.h>

#include <X11/keysym.h>
#include <gdk/gdkx.h>
#include <gdk/gdk.h>

#include <libgnome/libgnome.h>

#include "global-keys.h"

#include "applet.h"
#include "gnome-run.h"
#include "panel.h"
#include "panel-util.h"
#include "panel-config-global.h"

extern GlobalConfig global_config;
extern GSList *panels;

#define N_BITS 32 /*all modifier masks fit into it */

typedef struct {
	guint mods;
	guint key;
} ModAndKey;
typedef void (*BitsCallback) (guint value, ModAndKey *mod_and_key);


static void
all_combinations (guint mask_to_traverse,
		  BitsCallback callback,
		  ModAndKey *mod_and_key)
{
	int indexes[N_BITS];/*indexes of bits we need to flip*/
	int i, bit, bits_set_cnt;
	int uppervalue;

	bit = 0;
	for (i = 0; i < N_BITS; i++) {
		if (mask_to_traverse & (1<<i))
			indexes[bit++]=i;
	}

	bits_set_cnt = bit;

	uppervalue = 1<<bits_set_cnt;
	for (i = 0; i < uppervalue; i++) {
		int j, result = 0;

		for (j = 0; j < bits_set_cnt; j++) {
			if (i & (1<<j))
				result |= (1<<indexes[j]);
		}
		callback (result, mod_and_key);
	}
}

static void
do_grab_key (guint mod, ModAndKey* data)
{
	XGrabKey (GDK_DISPLAY(), data->key, (mod | data->mods),
		  GDK_ROOT_WINDOW(), True, GrabModeAsync, GrabModeAsync);
}

static void
grab_key (guint mod, guint key)
{
	ModAndKey data;
        int other_mods = IGNORED_MODS & ~mod;
    
	data.mods = mod;
        data.key = key;
    
	all_combinations (other_mods, do_grab_key, &data);
}

static void
do_ungrab_key (guint mod, ModAndKey* data)
{
	XUngrabKey(GDK_DISPLAY(), data->key, (mod | data->mods),
		   GDK_ROOT_WINDOW());
}

static void 
ungrab_key (guint mod,guint key)
{
	ModAndKey data;
	int other_mods = IGNORED_MODS & ~mod;    
    
	data.mods = mod;
	data.key = key;
    
	all_combinations(other_mods, do_ungrab_key, &data);
}

void
panel_global_keys_setup (void)
{
	static guint lastkey_menu = 0;
	static guint laststate_menu = 0;
	static guint lastkey_run = 0;
	static guint laststate_run = 0;
	static guint lastkey_screenshot = 0;
	static guint laststate_screenshot = 0;
	static guint lastkey_window_screenshot = 0;
	static guint laststate_window_screenshot = 0;

	/* FIXME: the if trees are horrible, this shoul dbe cleaned up with
	 * lists or something */

	gdk_error_trap_push();
	if (lastkey_menu != 0) {
		ungrab_key (laststate_menu, lastkey_menu);
	}
	if (lastkey_run != 0 &&
	    (lastkey_menu != lastkey_run ||
	     laststate_menu != laststate_run)) {
		ungrab_key (laststate_run, lastkey_run);
	}
	if (lastkey_run != 0 &&
	    (lastkey_menu != lastkey_screenshot ||
	     laststate_menu != laststate_screenshot) &&
	    (lastkey_run != lastkey_screenshot ||
	     laststate_run != laststate_screenshot)) {
		ungrab_key (laststate_screenshot, lastkey_screenshot);
	}
	if (lastkey_run != 0 &&
	    (lastkey_menu != lastkey_window_screenshot ||
	     laststate_menu != laststate_window_screenshot) &&
	    (lastkey_run != lastkey_window_screenshot ||
	     laststate_run != laststate_window_screenshot) &&
	    (lastkey_screenshot != lastkey_window_screenshot ||
	     laststate_screenshot != laststate_window_screenshot)) {
		ungrab_key (laststate_window_screenshot,
			    lastkey_window_screenshot);
	}
	
	if (global_config.keys_enabled && 
	    global_config.menu_keysym) {
		lastkey_menu = XKeysymToKeycode(GDK_DISPLAY(),
						global_config.menu_keysym);
		laststate_menu = global_config.menu_state;
		if (lastkey_menu != 0)
			grab_key (laststate_menu, lastkey_menu);
	} else {
		lastkey_menu = 0;
	}

	if (global_config.keys_enabled && 
	    global_config.run_keysym) {
		lastkey_run = XKeysymToKeycode (GDK_DISPLAY (),
						global_config.run_keysym);
		laststate_run = global_config.run_state;
		if (lastkey_run != 0 &&
		    (lastkey_menu != lastkey_run ||
		     laststate_menu != laststate_run))
			grab_key (laststate_run, lastkey_run);
	} else {
		lastkey_run = 0;
	}

	if (global_config.keys_enabled && 
	    global_config.screenshot_keysym) {
		lastkey_screenshot = XKeysymToKeycode
			(GDK_DISPLAY (), global_config.screenshot_keysym);
		laststate_screenshot = global_config.screenshot_state;
		if (lastkey_screenshot != 0 &&
		    (lastkey_menu != lastkey_screenshot ||
		     laststate_menu != laststate_screenshot) &&
		    (lastkey_run != lastkey_screenshot ||
		     laststate_run != laststate_screenshot))
			grab_key (laststate_screenshot, lastkey_screenshot);
	} else {
		lastkey_screenshot = 0;
	}

	if (global_config.keys_enabled && 
	    global_config.window_screenshot_keysym) {
		lastkey_window_screenshot = XKeysymToKeycode
			(GDK_DISPLAY (), global_config.window_screenshot_keysym);
		laststate_window_screenshot = global_config.window_screenshot_state;
		if (lastkey_window_screenshot != 0 &&
		    (lastkey_menu != lastkey_window_screenshot ||
		     laststate_menu != laststate_window_screenshot) &&
		    (lastkey_run != lastkey_window_screenshot ||
		     laststate_run != laststate_window_screenshot) &&
		    (lastkey_screenshot != lastkey_window_screenshot ||
		     laststate_screenshot != laststate_window_screenshot))
			grab_key (laststate_window_screenshot,
				  lastkey_window_screenshot);
	} else {
		lastkey_window_screenshot = 0;
	}

	gdk_flush ();
	gdk_error_trap_pop();
}

static gboolean
check_for_grabs (void)
{
	if (gdk_pointer_grab (gdk_get_default_root_window (), FALSE, 
			      0, NULL, NULL, GDK_CURRENT_TIME)
	    != GrabSuccess) {
		return TRUE;
	} else {
		gdk_pointer_ungrab (GDK_CURRENT_TIME);
		return FALSE;
	}
}

GdkFilterReturn
panel_global_keys_filter (GdkXEvent *gdk_xevent,
			  GdkEvent *event,
			  gpointer data)
{
	XEvent *xevent = (XEvent *)gdk_xevent;
	guint keycode, state;
	guint menu_keycode, menu_state;
	guint run_keycode, run_state;
	guint screenshot_keycode, screenshot_state;
	guint window_screenshot_keycode, window_screenshot_state;

	if(xevent->type != KeyPress)
		return GDK_FILTER_CONTINUE;

	keycode = xevent->xkey.keycode;
	state = xevent->xkey.state;

	menu_keycode = XKeysymToKeycode (GDK_DISPLAY (),
					 global_config.menu_keysym);
	menu_state = global_config.menu_state;

	run_keycode = XKeysymToKeycode (GDK_DISPLAY (),
					global_config.run_keysym);
	run_state = global_config.run_state;

	screenshot_keycode = XKeysymToKeycode (GDK_DISPLAY (),
					       global_config.screenshot_keysym);
	screenshot_state = global_config.screenshot_state;

	window_screenshot_keycode =
		XKeysymToKeycode (GDK_DISPLAY (),
				  global_config.window_screenshot_keysym);
	window_screenshot_state = global_config.window_screenshot_state;

	if (keycode == menu_keycode &&
	    (state & USED_MODS) == menu_state) {
		PanelWidget *panel;
		GtkWidget *menu, *basep;
		/* check if anybody else has a grab */
		if (check_for_grabs ()) {
			return GDK_FILTER_CONTINUE;
		}

		panel = panels->data;
		menu = make_popup_panel_menu (panel);
		basep = panel->panel_parent;
		if (BASEP_IS_WIDGET(basep)) {
			BASEP_WIDGET(basep)->autohide_inhibit = TRUE;
			basep_widget_autohide (BASEP_WIDGET (basep));
		}
		gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
				NULL, NULL, 0, GDK_CURRENT_TIME);
		return GDK_FILTER_REMOVE;
	} else if (keycode == run_keycode &&
		   (state & USED_MODS) == run_state) {
		/* check if anybody else has a grab */
		if (check_for_grabs ()) {
			return GDK_FILTER_CONTINUE;
		}

		show_run_dialog ();
		return GDK_FILTER_REMOVE;
	} else if (keycode == screenshot_keycode &&
		   (state & USED_MODS) == screenshot_state) {
		char *argv[2];
		char *proggie;

		/* check if anybody else has a grab */
		if (check_for_grabs ()) {
			return GDK_FILTER_CONTINUE;
		}

		proggie = g_find_program_in_path  ("gnome-panel-screenshot");
		if (proggie == NULL) {
			panel_error_dialog ("cannot_find_ss_program",
					    _("Can't find the screenshot "
					      "program"));
			return GDK_FILTER_REMOVE;
		}
		argv[0] = proggie;
		argv[1] = NULL;

		if (gnome_execute_async (g_get_home_dir (), 1, argv)<0)
			panel_error_dialog ("cannot_exec_ss_program",
					    _("Can't execute the screenshot "
					      "program"));

		g_free (proggie);

		return GDK_FILTER_REMOVE;
	} else if (keycode == window_screenshot_keycode &&
		   (state & USED_MODS) == window_screenshot_state) {
		char *argv[3];
		char *proggie;

		/* check if anybody else has a grab */
		if (check_for_grabs ()) {
			return GDK_FILTER_CONTINUE;
		}

		proggie = g_find_program_in_path  ("gnome-panel-screenshot");
		if (proggie == NULL) {
			panel_error_dialog ("cannot_find_ss_program",
					    _("Can't find the screenshot "
					      "program"));
			return GDK_FILTER_REMOVE;
		}
		argv[0] = proggie;
		argv[1] = "--window";
		argv[2] = NULL;

		if (gnome_execute_async (g_get_home_dir (), 2, argv)<0)
			panel_error_dialog ("cannot_exec_ss_program",
					    _("Can't execute the screenshot "
					      "program"));

		g_free (proggie);

		return GDK_FILTER_REMOVE;
	}

	return GDK_FILTER_CONTINUE;
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us