summaryrefslogtreecommitdiff
path: root/recipes/matchbox-applet-inputmanager/files/mbinputmgr-libgtkinput.patch
blob: 5d1ccdc0c7bb0c427f0ed6b77052697ee20c7c14 (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
347
348
349
350
Index: mb-applet-input-manager-0.6/mbinputmgr-tray.c
===================================================================
--- mb-applet-input-manager-0.6/mbinputmgr-tray.c	(revision 1292)
+++ mb-applet-input-manager-0.6/mbinputmgr-tray.c	(working copy)
@@ -32,8 +32,20 @@
 MBInpmgrState *Inpmgr_state = NULL;
 Bool           ButtonIsDown = False;
 int            ButtonActive;
-Atom           AtomIMActivate;
 
+Atom	       atoms[2];
+char *atom_names[] =
+  {
+    "_MB_INPUT_REQUEST",
+    "_GPE_INPUT_MANAGER"
+  };
+
+#define	       _MB_INPUT_REQUEST	0
+#define	       _GPE_INPUT_MANAGER	1
+
+#define _GPE_INPUT_MANAGER_OPEN	1
+#define _GPE_INPUT_MANAGER_CLOSE 2
+
 typedef struct ButtonImgs {
   
   MBPixbufImage *active;
@@ -41,6 +53,17 @@
 
 } ButtonImgs;
 
+struct window_record
+{
+  Window w;
+  struct window_record *next;
+};
+
+struct window_record *requests;
+
+Display	 *dpy;
+Bool	 auto_enabled;
+
 void
 paint_callback ( MBTrayApp *app, Drawable drw )
 {
@@ -262,14 +285,190 @@
 }
 
 void
+really_close (void)
+{
+  if (requests == NULL && mbinputmgr_method_active (Inpmgr_state) && auto_enabled)
+    {    
+      mbinputmgr_toggle_selected_method (Inpmgr_state);
+      auto_enabled = False;
+    }
+}
+
+Bool timer_active;
+struct timeval expires;
+
+Bool
+get_timeout (struct timeval *tv)
+{
+  int sec, usec;
+  struct timeval now;
+
+  if (!timer_active)
+    return False;
+  
+  gettimeofday (&now, NULL);
+  
+  sec = expires.tv_sec - now.tv_sec;
+  usec = expires.tv_usec - now.tv_usec;
+  if (usec < 0)
+    {
+      sec--;
+      usec += 1000000;
+    }
+
+  if (sec < 0)
+    {
+      /* timer expired */
+      really_close ();
+      timer_active = False;
+      return False;
+    }
+
+  tv->tv_sec = sec;
+  tv->tv_usec = usec;
+
+  return True;
+}
+
+Bool
+process_close_request (Window w)
+{
+  struct window_record *r, *pr = NULL;
+  unsigned long tv;
+
+  for (r = requests; r != NULL && r->w != w; )
+    {
+      pr = r;
+      r = r->next;
+    }
+  
+  if (r)
+    {
+      if (pr)
+	pr->next = r->next;
+      else
+	requests = r->next;
+
+      free (r);
+      
+      if (requests == NULL && mbinputmgr_method_active (Inpmgr_state) && auto_enabled)
+	{
+	  timer_active = True;
+	  gettimeofday (&expires, NULL);
+	  expires.tv_usec += 100000;
+	  if (expires.tv_usec >= 1000000)
+	    {
+	      expires.tv_sec++;
+	      expires.tv_usec -= 1000000;
+	    }
+	}
+
+      return True;
+    }
+  
+  return False;
+}
+
+int trapped_error_code;
+int (*old_error_handler) (Display *d, XErrorEvent *e);
+
+static int
+error_handler(Display     *display,
+	      XErrorEvent *error)
+{
+   trapped_error_code = error->error_code;
+   return 0;
+}
+
+static void
+trap_errors(void)
+{
+   trapped_error_code = 0;
+   old_error_handler = XSetErrorHandler(error_handler);
+}
+
+static int
+untrap_errors(void)
+{
+   XSetErrorHandler(old_error_handler);
+   return trapped_error_code;
+}
+
+Bool
+process_open_request (Window w)
+{
+  struct window_record *r;
+
+  for (r = requests; r != NULL && r->w != w; r = r->next)
+    ;
+
+  if (r)
+    return True;
+
+  trap_errors ();
+  XSelectInput (dpy, w, StructureNotifyMask);
+  XSync (dpy, False);
+  if (untrap_errors ())
+    return False;
+
+  r = malloc (sizeof (*r));
+  r->next = requests;
+  r->w = w;
+  requests = r;
+
+  if (!mbinputmgr_method_active(Inpmgr_state))
+    {
+      mbinputmgr_toggle_selected_method (Inpmgr_state);
+      auto_enabled = True;
+    }
+
+  return False;
+}
+
+Bool docked_already;
+
+void
+is_docked (void)
+{
+  Window	 tray_w;
+
+  tray_w = mb_tray_app_xwin (app);
+
+  if (XGetSelectionOwner (dpy, atoms[_GPE_INPUT_MANAGER]) == None)
+    {
+      XSetSelectionOwner (dpy, atoms[_GPE_INPUT_MANAGER], tray_w, CurrentTime);
+    } 
+  else 
+    {
+      fprintf (stderr, "Unable to claim _GPE_INPUT_MANAGER selection.\n");
+    }
+}
+
+void
 xevent_callback (MBTrayApp *app, XEvent *ev)
 {
   if (ev->type == ClientMessage)
     {
       XClientMessageEvent *cmev = (XClientMessageEvent *)&ev->xconfigure;
 
-      if (cmev->message_type == AtomIMActivate)
+      if (cmev->message_type == atoms[_GPE_INPUT_MANAGER])
 	{
+	  switch (cmev->data.l[0])
+	    {
+	    case _GPE_INPUT_MANAGER_OPEN:
+	      process_open_request (cmev->data.l[1]);
+	      break;
+	    case _GPE_INPUT_MANAGER_CLOSE:
+	      process_close_request (cmev->data.l[1]);
+	      break;
+	    default:
+	      fprintf (stderr, "received unknown _GPE_INPUT_MANAGER request %d\n", cmev->data.l[0]);
+	      break;
+	    }
+	} 
+#ifndef DISABLE_OLD_PROTOCOL
+      else if (cmev->message_type == atoms[_MB_INPUT_REQUEST])
+	{
 	  /* De Activate */
 	  if (cmev->data.l[0] == 0 && mbinputmgr_method_active(Inpmgr_state))
 	    mbinputmgr_toggle_selected_method (Inpmgr_state);
@@ -278,8 +477,20 @@
 		   && !mbinputmgr_method_active(Inpmgr_state))
 	    mbinputmgr_toggle_selected_method (Inpmgr_state);
 	}
+#endif
     }
+  else if (ev->type == UnmapNotify)
+    {
+      XUnmapEvent *uev = &ev->xunmap;
 
+      process_close_request (uev->window);
+    }
+  else if (ev->type == ConfigureNotify && !docked_already)
+    {
+      docked_already = TRUE;
+      is_docked ();
+    }
+
   mb_menu_handle_xevent (PopupMenu, ev);
 }
 
@@ -347,11 +558,42 @@
   free(icon_path);
 }
 
+static Bool
+get_xevent_timed(Display* dpy, XEvent* event_return, struct timeval *tv)
+{
+  if (tv == NULL) 
+    {
+      XNextEvent(dpy, event_return);
+      return True;
+    }
+
+  XFlush(dpy);
+
+  if (XPending(dpy) == 0) 
+    {
+      int fd = ConnectionNumber(dpy);
+      fd_set readset;
+      FD_ZERO(&readset);
+      FD_SET(fd, &readset);
+      if (select(fd+1, &readset, NULL, NULL, tv) == 0) 
+	{
+	  return False;
+	} else {
+	  XNextEvent(dpy, event_return);
+	  return True;
+	}
+    } else {
+      XNextEvent(dpy, event_return);
+      return True;
+    }
+}
+
 int 
 main(int argc, char **argv)
 {
   int            i;
   MBPixbufImage *app_icon_img  = NULL;
+  XEvent xevent;
 
   app = mb_tray_app_new ( "Input Selector",
 			  resize_callback,
@@ -359,13 +601,13 @@
 			  &argc,
 			  &argv );  
 
+  dpy = mb_tray_app_xdisplay (app);
+
   Pixbuf = mb_pixbuf_new(mb_tray_app_xdisplay(app), 
 			 mb_tray_app_xscreen(app));
 
-  AtomIMActivate = XInternAtom(mb_tray_app_xdisplay(app), 
-			       "_MB_INPUT_REQUEST", False);
+  XInternAtoms (dpy, atom_names, sizeof (atom_names) / sizeof (atom_names[0]), False, atoms);
 
-
   PopupMenu = mb_menu_new (mb_tray_app_xdisplay(app),
 			   mb_tray_app_xscreen(app));
 
@@ -399,17 +641,30 @@
 
   mb_tray_app_set_xevent_callback (app, xevent_callback );
 
+  mb_tray_app_set_xevent_mask (app, SubstructureNotifyMask);
+
   mb_tray_app_set_button_callback (app, button_callback );
 
   /* XXX set up dnotify to reload entrys only on _addition_  */
 
+#ifndef DISABLE_OLD_PROTOCOL
   XSelectInput(mb_tray_app_xdisplay(app),
 	       mb_tray_app_xrootwin(app),
 	       SubstructureNotifyMask);
+#endif 
 
+  /* Not using mb_tray_app_main() to avoid libmb's broken get_xevent_timed() */
+  mb_tray_app_main_init (app);
+  for (;;)
+    {
+      struct timeval tv, *tvp = NULL;
 
-  mb_tray_app_main (app);
+      if (get_timeout (&tv))
+	tvp = &tv;
 
+      if (get_xevent_timed (dpy, &xevent, tvp))
+	mb_tray_handle_xevent (app, &xevent);
+    }
+
   return 0;
 }
-