Debian Codegraph @

Composite-Manager/src/applet-composite-manager.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define _BSD_SOURCE
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <glib/gstdio.h>
26 #include <cairo-dock.h>
27
28 #include "applet-struct.h"
29 #include "applet-composite-manager.h"
30
31
32 ///////////////////
33 /// WM BACKENDS ///
34 ///////////////////
35
36 static CDWM *_get_wm_by_name (const gchar *cName)
37 {
38 int i;
39 for (i = 0; i < NB_WM; i ++)
40 {
41 if (strcmp (cName, myData.pWmList[i].cName) == 0)
42 return &myData.pWmList[i];
43 }
44 return NULL;
45 }
46 static CDWM *_get_wm_by_index (CDWMIndex n)
47 {
48 if (n < NB_WM)
49 return &myData.pWmList[n];
50 else
51 return NULL;
52 }
53
54 static void _set_metacity_composite (gboolean bActive)
55 {
56 int r;
57 if (bActive)
58 r = system ("gconftool-2 -s '/apps/metacity/general/compositing_manager' --type bool true");
59 else
60 r = system ("gconftool-2 -s '/apps/metacity/general/compositing_manager' --type bool false");
61 if (r < 0)
62 cd_warning ("Not able to launch this command: gconftool-2");
63 }
64 static void _set_xfwm_composite (gboolean bActive)
65 {
66 int r;
67 if (bActive)
68 r = system ("xfconf-query -c xfwm4 -p '/general/use_compositing' -t 'bool' -s 'true'");
69 else
70 r = system ("xfconf-query -c xfwm4 -p '/general/use_compositing' -t 'bool' -s 'false'");
71 if (r < 0)
72 cd_warning ("Not able to launch this command: xfconf-query");
73 }
74 static void _set_kwin_composite (gboolean bActive)
75 {
76 int r;
77 if (bActive)
78 r = system ("if test \"`qdbus org.kde.kwin /KWin compositingActive`\" = \"false\";then qdbus org.kde.kwin /KWin toggleCompositing; fi"); // not active, so activating
79 else
80 r = system ("if test \"`qdbus org.kde.kwin /KWin compositingActive`\" = \"true\"; then qdbus org.kde.kwin /KWin toggleCompositing; fi"); // active, so deactivating
81 if (r < 0)
82 cd_warning ("Not able to launch this command: qdbus");
83 }
84 static void _define_known_wms (void)
85 {
86 myData.pWmList[CD_COMPIZ].cName = "Compiz";
87 myData.pWmList[CD_COMPIZ].cCommand = "compiz --replace";
88 myData.pWmList[CD_COMPIZ].activate_composite = NULL;
89 myData.pWmList[CD_COMPIZ].cConfigTool = "ccsm";
90
91 myData.pWmList[CD_KWIN].cName = "KWin";
92 myData.pWmList[CD_KWIN].cCommand = "kwin --replace";
93 myData.pWmList[CD_KWIN].activate_composite = _set_kwin_composite;
94 myData.pWmList[CD_KWIN].cConfigTool = NULL; /// TODO: find the config tool...
95
96 myData.pWmList[CD_XFWM].cName = "Xfwm";
97 myData.pWmList[CD_XFWM].cCommand = "xfwm4 --replace";
98 myData.pWmList[CD_XFWM].activate_composite = _set_xfwm_composite;
99 myData.pWmList[CD_XFWM].cConfigTool = "xfwm4-settings"; // there is also xfwm4-tweaks-settings, wish they merge both ...
100
101 myData.pWmList[CD_METACITY].cName = "Metacity";
102 myData.pWmList[CD_METACITY].cCommand = "metacity --replace";
103 myData.pWmList[CD_METACITY].activate_composite = _set_metacity_composite;
104 myData.pWmList[CD_METACITY].cConfigTool = "gconf-editor /apps/metacity";
105
106 myData.pWmList[CD_CUSTOM_WMFB].cName = "Fallback";
107 myData.pWmList[CD_CUSTOM_WMFB].cCommand = NULL;
108 myData.pWmList[CD_CUSTOM_WMFB].activate_composite = NULL;
109 myData.pWmList[CD_CUSTOM_WMFB].bIsAvailable = TRUE;
110
111 myData.pWmList[CD_CUSTOM_WMC].cName = "Composite";
112 myData.pWmList[CD_CUSTOM_WMC].cCommand = NULL;
113 myData.pWmList[CD_CUSTOM_WMC].activate_composite = NULL;
114 myData.pWmList[CD_CUSTOM_WMC].bIsAvailable = TRUE;
115 }
116
117 static void _check_available_wms (gchar *cWhich)
118 {
119 if (cWhich == NULL) // no known WM is present, skip the check.
120 return;
121
122 CDWM *wm;
123 wm = _get_wm_by_index (CD_COMPIZ);
124 wm->bIsAvailable = (strstr (cWhich, "compiz") != NULL);
125 wm = _get_wm_by_index (CD_KWIN);
126 wm->bIsAvailable = (strstr (cWhich, "kwin") != NULL);
127 wm = _get_wm_by_index (CD_XFWM);
128 wm->bIsAvailable = (strstr (cWhich, "xfwm4") != NULL);
129 wm = _get_wm_by_index (CD_METACITY);
130 wm->bIsAvailable = (strstr (cWhich, "metacity") != NULL);
131 }
132
133 static CDWMIndex _check_current_wm (gchar *cPs)
134 {
135 if (cPs == NULL) // no known WM is present, skip the check.
136 return -1;
137
138 if (strstr (cPs, "compiz") != NULL)
139 return CD_COMPIZ;
140 if (strstr (cPs, "kwin") != NULL)
141 return CD_KWIN;
142 if (strstr (cPs, "xfwm4") != NULL)
143 return CD_XFWM;
144 if (strstr (cPs, "metacity") != NULL)
145 return CD_METACITY;
146
147 return -1;
148 }
149
150
151 ////////////////////////
152 /// COMPOSITE SIGNAL ///
153 ////////////////////////
154
155 static void _on_composited_changed (GdkScreen *pScreen, gpointer data)
156 {
157 myData.bIsComposited = gdk_screen_is_composited (pScreen);
158 cd_draw_current_state ();
159 }
160 static void _start_watching_composite_state (void)
161 {
162 // get the current state.
163 GdkScreen *pScreen = gdk_screen_get_default ();
164 myData.bIsComposited = gdk_screen_is_composited (pScreen);
165
166 // draw it.
167 cd_draw_current_state ();
168
169 // listen for future changes.
170 g_signal_connect (G_OBJECT (pScreen), "composited-changed", G_CALLBACK(_on_composited_changed), NULL);
171 }
172
173
174 ///////////////////
175 /// PREFERED WM ///
176 ///////////////////
177
178 static CDWM *_get_prefered_wmc (CDWMIndex iCurrentWm)
179 {
180 cd_debug ("%s (%s, %d)", __func__, myConfig.cWmCompositor, iCurrentWm);
181 CDWM *wm;
182 if (myConfig.cWmCompositor != NULL) // a composite WM is defined.
183 {
184 wm = _get_wm_by_name (myConfig.cWmCompositor);
185 if (wm == NULL) // not one of the known WM -> define and take the custom one.
186 {
187 wm = _get_wm_by_index (CD_CUSTOM_WMC);
188 g_free ((gchar*)wm->cCommand);
189 wm->cCommand = g_strdup (myConfig.cWmCompositor);
190 return wm;
191 }
192 else if (wm->bIsAvailable)
193 return wm;
194 }
195
196 // no WM defined, or the one defined is not available -> check if a suitable one is running.
197 if (iCurrentWm < NB_WM) // one of the know WM is running
198 {
199 if (myData.bIsComposited) // and it provides composite => let's take it!
200 {
201 wm = _get_wm_by_index (iCurrentWm);
202 if (wm->bIsAvailable) // just to be sure.
203 return wm;
204 }
205 }
206
207 // no succes so far, take the most suitable one.
208 int index[NB_COMPOSITE_WM] = {CD_COMPIZ, CD_KWIN, CD_XFWM, CD_METACITY}; // in this order by default.
209 switch (g_iDesktopEnv)
210 {
211 case CAIRO_DOCK_GNOME:
212 index[1] = CD_METACITY;
213 index[3] = CD_KWIN;
214 break;
215 case CAIRO_DOCK_XFCE:
216 index[1] = CD_XFWM;
217 index[2] = CD_KWIN;
218 break;
219 case CAIRO_DOCK_KDE:
220 default:
221 break;
222 }
223 int i;
224 for (i = 0; i < NB_COMPOSITE_WM; i ++)
225 {
226 wm = _get_wm_by_index (index[i]);
227 cd_debug (" %d) %s, %d", index[i], wm->cName, wm->bIsAvailable);
228 if (wm->bIsAvailable)
229 return wm;
230 }
231 return NULL;
232 }
233
234 static CDWM *_get_prefered_wmfb (CDWMIndex iCurrentWm)
235 {
236 cd_debug ("%s (%s, %d)", __func__, myConfig.cWmFallback, iCurrentWm);
237 CDWM *wm;
238 if (myConfig.cWmFallback != NULL) // a fallback WM is defined.
239 {
240 wm = _get_wm_by_name (myConfig.cWmFallback);
241 if (wm == NULL) // not one of the known WM -> define and take the custom one.
242 {
243 wm = _get_wm_by_index (CD_CUSTOM_WMFB);
244 g_free ((gchar*)wm->cCommand);
245 wm->cCommand = g_strdup (myConfig.cWmFallback);
246 return wm;
247 }
248 else if (wm->bIsAvailable)
249 return wm;
250 }
251
252 // no WM defined, or the one defined is not available -> check if a suitable one is running.
253 if (iCurrentWm < NB_WM) // one of the know WM is running
254 {
255 if (!myData.bIsComposited) // and it is a fallback => let's take it!
256 {
257 wm = _get_wm_by_index (iCurrentWm);
258 cd_debug ("current wm: %d, %d", iCurrentWm, wm->bIsAvailable);
259 if (wm->bIsAvailable) // just to be sure.
260 return wm;
261 }
262 }
263
264 // no succes so far, take the most suitable one.
265 int index[NB_FALLBACK_WM] = {CD_METACITY, CD_XFWM, CD_KWIN}; // in this order by default.
266 switch (g_iDesktopEnv)
267 {
268 case CAIRO_DOCK_GNOME:
269 index[0] = CD_METACITY;
270 index[1] = CD_XFWM;
271 break;
272 case CAIRO_DOCK_XFCE:
273 index[0] = CD_XFWM;
274 index[1] = CD_METACITY;
275 break;
276 case CAIRO_DOCK_KDE:
277 index[0] = CD_KWIN;
278 index[1] = CD_METACITY;
279 index[2] = CD_XFWM;
280 break;
281 case CAIRO_DOCK_UNKNOWN_ENV:
282 case CAIRO_DOCK_NB_DESKTOPS:
283 break;
284 }
285 int i;
286 for (i = 0; i < NB_FALLBACK_WM; i ++)
287 {
288 wm = _get_wm_by_index (index[i]);
289 cd_debug (" %s (%d)", wm->cName, wm->bIsAvailable);
290 if (wm->bIsAvailable)
291 return wm;
292 }
293 return NULL;
294 }
295
296 static inline gchar *_get_running_wm (void)
297 {
298 return cairo_dock_launch_command_sync ("pgrep -l \"kwin$|compiz$|xfwm4$|metacity$\""); // -l = write the name of the program (not the command next to the PID in 'ps -ef'. we add a '$' after the names to avoid listing things like compiz-decorator or xfwm4-settings
299 }
300 static void _define_prefered_wms (gchar *cPs)
301 {
302 // get the compositor and fallback WMs
303 CDWMIndex iCurrentWm = _check_current_wm (cPs);
304 myData.wmc = _get_prefered_wmc (iCurrentWm);
305 myData.wmfb = _get_prefered_wmfb (iCurrentWm);
306 cd_debug ("***** WM: %s / %s", myData.wmc?myData.wmc->cName:NULL, myData.wmfb?myData.wmfb->cName:NULL);
307 }
308 void cd_define_prefered_wms (void)
309 {
310 gchar *ps = _get_running_wm ();
311 _define_prefered_wms (ps);
312 g_free (ps);
313 }
314
315 /////////////////
316 /// INIT/STOP ///
317 /////////////////
318
319 static void _check_wms (CDSharedMemory *pSharedMemory)
320 {
321 pSharedMemory->which = cairo_dock_launch_command_sync ("which compiz kwin xfwm4 metacity");
322
323 pSharedMemory->ps = _get_running_wm ();
324 }
325 static void _update_from_data (CDSharedMemory *pSharedMemory)
326 {
327 _check_available_wms (pSharedMemory->which);
328
329 _define_prefered_wms (pSharedMemory->ps); // we do it once we know the current state.
330
331 gldi_task_discard (myData.pTask);
332 myData.pTask = NULL;
333 }
334
335 static void _free_shared_memory (CDSharedMemory *pSharedMemory)
336 {
337 g_free (pSharedMemory->which);
338 g_free (pSharedMemory->ps);
339 g_free (pSharedMemory);
340 }
341 void cd_init_wms (void)
342 {
343 _define_known_wms ();
344
345 _start_watching_composite_state ();
346
347 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
348 myData.pTask = gldi_task_new_full (0, // one-shot
349 (GldiGetDataAsyncFunc) _check_wms,
350 (GldiUpdateSyncFunc) _update_from_data,
351 (GFreeFunc) _free_shared_memory,
352 pSharedMemory);
353 gldi_task_launch_delayed (myData.pTask, 3000); // 3s delay, since we don't need these info right away.
354 }
355
356
357 void cd_stop_wms (void)
358 {
359 // discard task.
360 gldi_task_discard (myData.pTask);
361
362 // stop listening
363 GdkScreen *pScreen = gdk_screen_get_default ();
364 g_signal_handlers_disconnect_by_func (G_OBJECT(pScreen), _on_composited_changed, NULL);
365
366 // reset custom WMs.
367 CDWM *wm;
368 wm = _get_wm_by_index (CD_CUSTOM_WMC);
369 g_free ((gchar*)wm->cCommand);
370 wm = _get_wm_by_index (CD_CUSTOM_WMFB);
371 g_free ((gchar*)wm->cCommand);
372 }
373
374
375 ////////////
376 /// DRAW ///
377 ////////////
378
379 void cd_draw_current_state (void)
380 {
381 cd_debug ("%s (%d)", __func__, myData.bIsComposited);
382 if (myData.bIsComposited)
383 CD_APPLET_SET_USER_IMAGE_ON_MY_ICON (myConfig.cIconCompositeON, "composite-on.png");
384 else
385 CD_APPLET_SET_USER_IMAGE_ON_MY_ICON (myConfig.cIconCompositeOFF, "composite-off.png");
386 }
387
388
389 /////////////////////
390 /// TOGGLE ON/OFF ///
391 /////////////////////
392
393 static gboolean _activate_composite_delayed (gpointer data)
394 {
395 if (data)
396 {
397 if (myData.wmc->activate_composite != NULL)
398 myData.wmc->activate_composite (TRUE);
399 }
400 else
401 {
402 if (myData.wmfb->activate_composite != NULL)
403 myData.wmfb->activate_composite (FALSE);
404 }
405 return FALSE;
406 }
407
408 static gboolean _wm_is_running (CDWM *wm)
409 {
410 const gchar *cCommand = wm->cCommand;
411 gchar *cWhich = g_strdup_printf ("pgrep %s$", cCommand); // see above for the '$' character.
412 gchar *str = strchr (cWhich+6, ' '); // remove any parameter to the command, we just want the program name.
413 if (str) // a space is found.
414 {
415 *str = '$';
416 *(str+1) = '\0';
417 }
418 gchar *cResult = cairo_dock_launch_command_sync (cWhich);
419 gboolean bIsRunning = (cResult != NULL && *cResult != '\0');
420
421 g_free (cWhich);
422 g_free (cResult);
423 return bIsRunning;
424 }
425
426 static void cd_turn_composite_on (void)
427 {
428 if (myData.wmc == NULL) // no compositor.
429 {
430 gldi_dialog_show_temporary_with_icon (D_("No compositor is available."), myIcon, myContainer, 6000, "same icon");
431 return;
432 }
433
434 // if not already launched, launch it.
435 if (! _wm_is_running (myData.wmc)) // not running
436 {
437 cairo_dock_launch_command (myData.wmc->cCommand);
438 g_timeout_add_seconds (2, _activate_composite_delayed, GINT_TO_POINTER (1)); // let the WM start for 2s.
439 }
440 else // already running, just toggle composite ON.
441 {
442 if (myData.wmc->activate_composite != NULL)
443 myData.wmc->activate_composite (TRUE);
444 else
445 gldi_dialog_show_temporary_with_icon (D_("No compositor is available."), myIcon, myContainer, 6000, "same icon");
446 }
447 }
448
449 static void cd_turn_composite_off (void)
450 {
451 if (myData.wmfb == NULL) // no fallback.
452 {
453 gldi_dialog_show_temporary_with_icon (D_("No fallback is available."), myIcon, myContainer, 6000, "same icon");
454 return;
455 }
456
457 // if not already launched, launch it.
458 if (! _wm_is_running (myData.wmfb)) // not running
459 {
460 cairo_dock_launch_command (myData.wmfb->cCommand);
461 g_timeout_add_seconds (2, _activate_composite_delayed, 0); // let the WM start for 2s.
462 }
463 else // already running, just toggle composite OFF.
464 {
465 if (myData.wmfb->activate_composite != NULL)
466 myData.wmfb->activate_composite (FALSE);
467 else
468 gldi_dialog_show_temporary_with_icon (D_("No fallback is available."), myIcon, myContainer, 6000, "same icon");
469 }
470 }
471
472 void cd_toggle_composite (void)
473 {
474 if (myData.bIsComposited)
475 cd_turn_composite_off ();
476 else
477 cd_turn_composite_on ();
478 }
479
480
481 ///////////////////
482 /// CONFIG TOOL ///
483 ///////////////////
484
485 static const gchar *_get_config_tool (void)
486 {
487 if (myData.bIsComposited && myData.wmc)
488 {
489 return myData.wmc->cConfigTool;
490 }
491 else if (!myData.bIsComposited && myData.wmfb)
492 {
493 return myData.wmfb->cConfigTool;
494 }
495 return NULL;
496 }
497 void cd_open_wm_config (void)
498 {
499 const gchar *cConfigTool = _get_config_tool ();
500
501 if (cConfigTool != NULL)
502 {
503 gchar *cmd = g_strdup_printf ("which %s", cConfigTool);
504 gchar *cResult = cairo_dock_launch_command_sync (cmd);
505 g_free (cmd);
506 if (cResult == NULL || *cResult != '/')
507 {
508 gchar *msg = g_strdup_printf (D_("You need to install '%s'"), cConfigTool);
509 gldi_dialog_show_temporary_with_icon (msg, myIcon, myContainer, 6000, "same icon");
510 g_free (msg);
511 }
512 else
513 {
514 cairo_dock_launch_command (cConfigTool);
515 }
516 }
517 else
518 {
519 gldi_dialog_show_temporary_with_icon (D_("No configuration tool is available."), myIcon, myContainer, 6000, "same icon");
520 }
521 }
522
523
524 static const gchar *_get_command (void)
525 {
526 if (myData.bIsComposited && myData.wmc)
527 {
528 return myData.wmc->cCommand;
529 }
530 else if (!myData.bIsComposited && myData.wmfb)
531 {
532 return myData.wmfb->cCommand;
533 }
534 return NULL;
535 }
536
537 void cd_reload_wm (void)
538 {
539 const gchar *cCommand = _get_command ();
540 if (cCommand != NULL)
541 {
542 cairo_dock_launch_command (cCommand);
543 }
544 }