Debian Codegraph @

Global-Menu/src/applet-app.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * based on indicator-messages.c written by :
6 * Ted Gould <[email protected]>
7 * Cody Russell <[email protected]>
8 * E-mail : see the 'copyright' file.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29
30 #include "MwmUtil.h"
31
32 #include "applet-struct.h"
33 #include "applet-draw.h"
34 #include "applet-app.h"
35
36 #define CD_APP_MENU_REGISTRAR_ADDR "com.canonical.AppMenu.Registrar"
37 #define CD_APP_MENU_REGISTRAR_OBJ "/com/canonical/AppMenu/Registrar"
38 #define CD_APP_MENU_REGISTRAR_IFACE "com.canonical.AppMenu.Registrar"
39
40 static DBusGProxyCall *s_pDetectRegistrarCall = NULL;
41 static DBusGProxyCall *s_pGetMenuCall = NULL;
42
43
44 ///////////////////////
45 /// WINDOW CONTROLS ///
46 ///////////////////////
47
48 static void _get_window_allowed_actions (GldiWindowActor *actor)
49 {
50 if (actor == NULL)
51 {
52 myData.bCanMinimize = FALSE;
53 myData.bCanMaximize = FALSE;
54 myData.bCanClose = FALSE;
55 return;
56 }
57 gldi_window_can_minimize_maximize_close (actor, &myData.bCanMinimize, &myData.bCanMaximize, &myData.bCanClose);
58 }
59
60 static void _set_border (GldiWindowActor *actor, gboolean bWithBorder)
61 {
62 if (actor->bIsMaximized)
63 gldi_window_set_border (actor, bWithBorder);
64 }
65 void cd_app_menu_set_windows_borders (gboolean bWithBorder)
66 {
67 gldi_windows_foreach (FALSE, (GFunc)_set_border, GINT_TO_POINTER (bWithBorder));
68 }
69
70
71 ////////////////////////
72 /// APPLICATION MENU ///
73 ////////////////////////
74
75 static void cd_app_menu_launch_our_registrar (void)
76 {
77 cairo_dock_launch_command (CD_PLUGINS_DIR"/appmenu-registrar");
78 myData.bOwnRegistrar = TRUE;
79 }
80
81 static void _on_registrar_owner_changed (const gchar *cName, gboolean bOwned, gpointer data)
82 {
83 cd_debug ("Registrar is on the bus (%d)", bOwned);
84 CD_APPLET_ENTER;
85
86 if (bOwned)
87 {
88 // set up a proxy to the Registrar
89 myData.pProxyRegistrar = cairo_dock_create_new_session_proxy (
90 CD_APP_MENU_REGISTRAR_ADDR,
91 CD_APP_MENU_REGISTRAR_OBJ,
92 CD_APP_MENU_REGISTRAR_IFACE); // whenever it appears on the bus, we'll get it.
93
94 // get the controls and menu of the current window.
95 GldiWindowActor *actor = gldi_windows_get_active ();
96
97 cd_app_menu_set_current_window (actor);
98 }
99 else // no more registrar on the bus.
100 {
101 g_object_unref (myData.pProxyRegistrar);
102 myData.pProxyRegistrar = NULL;
103
104 cd_app_menu_launch_our_registrar ();
105 }
106 CD_APPLET_LEAVE ();
107 }
108
109 static void _on_detect_registrar (gboolean bPresent, gpointer data)
110 {
111 cd_debug ("Registrar is present: %d", bPresent);
112 CD_APPLET_ENTER;
113 s_pDetectRegistrarCall = NULL;
114 // if present, set up proxy.
115 if (bPresent)
116 {
117 _on_registrar_owner_changed (CD_APP_MENU_REGISTRAR_ADDR, TRUE, NULL);
118 }
119 else
120 {
121 cd_app_menu_launch_our_registrar (); // when it has been launched, we'll get notified by Dbus.
122 }
123
124 // watch whenever the Registrar goes up or down.
125 cairo_dock_watch_dbus_name_owner (CD_APP_MENU_REGISTRAR_ADDR,
126 (CairoDockDbusNameOwnerChangedFunc) _on_registrar_owner_changed,
127 NULL);
128 CD_APPLET_LEAVE ();
129 }
130 void cd_app_detect_registrar (void)
131 {
132 if (s_pDetectRegistrarCall == NULL)
133 s_pDetectRegistrarCall = cairo_dock_dbus_detect_application_async (CD_APP_MENU_REGISTRAR_ADDR,
134 (CairoDockOnAppliPresentOnDbus) _on_detect_registrar,
135 NULL);
136 }
137
138
139 void cd_app_disconnect_from_registrar (void)
140 {
141 // stop detecting/watching the registrar
142 cairo_dock_stop_watching_dbus_name_owner (CD_APP_MENU_REGISTRAR_ADDR,
143 (CairoDockDbusNameOwnerChangedFunc) _on_registrar_owner_changed);
144
145 if (s_pDetectRegistrarCall != NULL)
146 {
147 DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
148 dbus_g_proxy_cancel_call (pProxy, s_pDetectRegistrarCall);
149 s_pDetectRegistrarCall = NULL;
150 }
151
152 // discard the menu
153 if (s_pGetMenuCall != NULL)
154 {
155 DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
156 dbus_g_proxy_cancel_call (pProxy, s_pGetMenuCall);
157 s_pGetMenuCall = NULL;
158 }
159
160 if (myData.pMenu != NULL)
161 {
162 gtk_widget_destroy (GTK_WIDGET (myData.pMenu));
163 myData.pMenu = NULL;
164 }
165
166 if (myData.pTask != NULL)
167 {
168 gldi_task_discard (myData.pTask);
169 myData.pTask = NULL;
170 }
171
172 // kill the registrar if it's our own one
173 if (myData.bOwnRegistrar)
174 {
175 int r = system ("pkill appmenu-registr"); // 15 chars limit; 'pkill -f' doesn't work :-/ this is not very clean, we should get the PID when we spawn it, and use it.
176 if (r < 0)
177 cd_warning ("Not able to launch this command: pkill");
178 myData.bOwnRegistrar = FALSE;
179 }
180 }
181
182
183 typedef struct {
184 gchar *cService;
185 gchar *cMenuObject;
186 DbusmenuGtkMenu *pMenu;
187 } CDSharedMemory;
188
189 static void _on_menu_destroyed (GldiModuleInstance *myApplet, GObject *old_menu_pointer)
190 {
191 if (old_menu_pointer == (GObject*)myData.pMenu)
192 myData.pMenu = NULL;
193 }
194
195 /*static void _free_shared_memory (CDSharedMemory *pSharedMemory)
196 {
197 g_free (pSharedMemory->cService);
198 g_free (pSharedMemory->cMenuObject);
199 if (pSharedMemory->pMenu)
200 gtk_widget_destroy (GTK_WIDGET (pSharedMemory->pMenu));
201 g_free (pSharedMemory);
202 }
203 static void _get_menu_async (CDSharedMemory *pSharedMemory)
204 {
205 cd_debug ("%s()", __func__);
206 pSharedMemory->pMenu = dbusmenu_gtkmenu_new (pSharedMemory->cService, pSharedMemory->cMenuObject); /// can this object disappear by itself ? it seems to crash with 2 instances of inkscape, when closing one of them...
207 cd_debug ("menu built");
208 }
209 static gboolean _fetch_menu (CDSharedMemory *pSharedMemory)
210 {
211 cd_debug ("%s()", __func__);
212 CD_APPLET_ENTER;
213 myData.pMenu = pSharedMemory->pMenu;
214 pSharedMemory->pMenu = NULL;
215
216 g_object_weak_ref (G_OBJECT (myData.pMenu),
217 (GWeakNotify)_on_menu_destroyed,
218 myApplet);
219 CD_APPLET_LEAVE (TRUE);
220 }*/
221
222 static void _on_got_menu (DBusGProxy *proxy, DBusGProxyCall *call_id, GldiModuleInstance *myApplet)
223 {
224 cd_debug ("%s ()", __func__);
225 CD_APPLET_ENTER;
226 s_pGetMenuCall = NULL;
227
228 GError *erreur = NULL;
229 gchar *cService = NULL, *cMenuObject = NULL;
230
231 gboolean bSuccess = dbus_g_proxy_end_call (proxy,
232 call_id,
233 &erreur,
234 G_TYPE_STRING, &cService,
235 DBUS_TYPE_G_OBJECT_PATH, &cMenuObject,
236 G_TYPE_INVALID);
237 if (erreur)
238 {
239 cd_warning ("couldn't get the application menu (%s)", erreur->message);
240 g_error_free (erreur);
241 }
242 if (bSuccess)
243 {
244 cd_debug (" -> %s", cService);
245 cd_debug (" %s", cMenuObject);
246 if (cService && *cService != '\0')
247 {
248 /*if (myData.pTask != NULL)
249 {
250 gldi_task_discard (myData.pTask);
251 myData.pTask = NULL;
252 }
253 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
254 pSharedMemory->cService = cService;
255 pSharedMemory->cMenuObject = cMenuObject;
256 myData.pTask = gldi_task_new_full (0,
257 (GldiGetDataAsyncFunc) _get_menu_async,
258 (GldiUpdateSyncFunc) _fetch_menu,
259 (GFreeFunc) _free_shared_memory,
260 pSharedMemory);
261 gldi_task_launch_delayed (myData.pTask, 0);*/
262 /// TODO: it seems to hang he dock for a second, even with a task :-/
263 /// so maybe we need to cache the {window,menu} couples...
264 myData.pMenu = dbusmenu_gtkmenu_new (cService, cMenuObject); /// can this object disappear by itself ? it seems to crash with 2 instances of inkscape, when closing one of them...
265 if (g_object_is_floating (myData.pMenu)) // claim ownership on the menu.
266 g_object_ref_sink (myData.pMenu);
267 if (myData.pMenu)
268 {
269 g_object_weak_ref (G_OBJECT (myData.pMenu),
270 (GWeakNotify)_on_menu_destroyed,
271 myApplet);
272 gldi_menu_init (GTK_WIDGET(myData.pMenu), myIcon);
273 }
274 }
275 }
276
277 ///g_free (cService);
278 ///g_free (cMenuObject);
279 CD_APPLET_LEAVE ();
280 }
281 static void _get_application_menu (GldiWindowActor *actor)
282 {
283 // destroy the current menu
284 if (myData.pMenu != NULL)
285 {
286 gtk_widget_destroy (GTK_WIDGET (myData.pMenu));
287 myData.pMenu = NULL;
288 }
289
290 if (s_pGetMenuCall != NULL)
291 {
292 DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
293 dbus_g_proxy_cancel_call (pProxy, s_pGetMenuCall);
294 s_pGetMenuCall = NULL;
295 }
296
297 if (myData.pTask != NULL)
298 {
299 gldi_task_discard (myData.pTask);
300 myData.pTask = NULL;
301 }
302
303 // get the new one.
304 if (actor != NULL)
305 {
306 if (myData.pProxyRegistrar != NULL)
307 {
308 guint id = gldi_window_get_id (actor);
309
310 s_pGetMenuCall = dbus_g_proxy_begin_call (myData.pProxyRegistrar,
311 "GetMenuForWindow",
312 (DBusGProxyCallNotify)_on_got_menu,
313 myApplet,
314 (GDestroyNotify) NULL,
315 G_TYPE_UINT, id,
316 G_TYPE_INVALID);
317 }
318 }
319 }
320
321
322 ////////////////////
323 /// START / STOP ///
324 ////////////////////
325
326 static gboolean _get_current_window_idle (GldiModuleInstance *myApplet)
327 {
328 // get the controls and menu of the current window.
329 GldiWindowActor *actor = gldi_windows_get_active ();
330
331 cd_app_menu_set_current_window (actor);
332
333 myData.iSidInitIdle = 0;
334 return FALSE;
335 }
336 static gboolean _remove_windows_borders_idle (GldiModuleInstance *myApplet)
337 {
338 cd_app_menu_set_windows_borders (FALSE);
339
340 myData.iSidInitIdle2 = 0;
341 return FALSE;
342 }
343 void cd_app_menu_start (void)
344 {
345 // connect to the registrar or directly get the current window.
346 if (myConfig.bDisplayMenu)
347 {
348 cd_app_detect_registrar (); // -> will get the current window once connected to the registrar
349 }
350 else
351 {
352 myData.iSidInitIdle = g_idle_add ((GSourceFunc)_get_current_window_idle, myApplet); // in idle, because it's heavy + the applications-manager is started after the plug-ins.
353 }
354
355 // remove borders from all maximised windows
356 if (myConfig.bDisplayControls)
357 {
358 myData.iSidInitIdle2 = g_idle_add ((GSourceFunc)_remove_windows_borders_idle, myApplet); // in idle, because it's heavy + the applications-manager is started after the plug-ins.
359 }
360
361 if (myConfig.bDisplayControls)
362 {
363 cd_app_menu_resize ();
364 }
365 }
366
367
368 void cd_app_menu_stop (void)
369 {
370 // disconnect from the registrar.
371 if (myConfig.bDisplayMenu)
372 {
373 cd_app_disconnect_from_registrar ();
374 }
375
376 // set back the window border of maximized windows.
377 if (myConfig.bDisplayControls)
378 {
379 cd_app_menu_set_windows_borders (TRUE);
380 }
381
382 if (myData.iSidInitIdle != 0)
383 g_source_remove (myData.iSidInitIdle);
384 if (myData.iSidInitIdle2 != 0)
385 g_source_remove (myData.iSidInitIdle2);
386 gldi_icon_unset_appli (myIcon);
387 }
388
389
390 void cd_app_menu_set_current_window (GldiWindowActor *actor)
391 {
392 cd_debug ("%s (%p)", __func__, actor);
393 if (actor != myData.pCurrentWindow)
394 {
395 myData.pPreviousWindow = myData.pCurrentWindow;
396 myData.pCurrentWindow = actor;
397 gldi_icon_set_appli (myIcon, actor); // set the actor on our icon, so that the dock adds the usual actions in our right-click menu. // this takes a reference on the actor, and remove the ref on the previous one.
398
399 if (myConfig.bDisplayMenu)
400 _get_application_menu (actor);
401
402 if (myConfig.bDisplayControls)
403 _get_window_allowed_actions (actor);
404
405 // update the icon
406 CD_APPLET_SET_NAME_FOR_MY_ICON (actor ? actor->cName : NULL);
407
408 cd_app_menu_redraw_icon ();
409 }
410 }