wck-utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* $Id$
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Copyright (C) 2013 Cedric Leporcq <cedl38@gmail.com>
  18. *
  19. * This code is derived from original 'Window Applets' from Andrej Belcijan.
  20. */
  21. #include "wck-utils.h"
  22. /* Prototypes */
  23. static WnckWindow *get_root_window(WnckScreen *screen);
  24. static WnckWindow *get_upper_maximized(WckUtils *);
  25. static void track_controled_window (WckUtils *);
  26. static void active_workspace_changed(WnckScreen *, WnckWorkspace *, WckUtils *);
  27. static void active_window_changed(WnckScreen *, WnckWindow *, WckUtils *);
  28. static void track_changed_max_state(WnckWindow *, WnckWindowState, WnckWindowState, WckUtils *);
  29. static void on_umaxed_window_state_changed(WnckWindow *, WnckWindowState, WnckWindowState, WckUtils *);
  30. static void on_viewports_changed(WnckScreen *, WckUtils *);
  31. static void on_window_closed(WnckScreen *, WnckWindow *, WckUtils *);
  32. static void on_window_opened(WnckScreen *, WnckWindow *, WckUtils *);
  33. gboolean wck_signal_handler_disconnect (GObject *object, gulong handler)
  34. {
  35. if (object && handler > 0)
  36. {
  37. if (g_signal_handler_is_connected(object, handler))
  38. {
  39. g_signal_handler_disconnect(object, handler);
  40. return TRUE;
  41. }
  42. }
  43. return FALSE;
  44. }
  45. static WnckWindow *get_root_window (WnckScreen *screen)
  46. {
  47. GList *winstack = wnck_screen_get_windows_stacked(screen);
  48. // we can't access data directly because sometimes we will get NULL or not desktop window
  49. if (winstack && wnck_window_get_window_type (winstack->data) == WNCK_WINDOW_DESKTOP)
  50. return winstack->data;
  51. else
  52. return NULL;
  53. }
  54. /* Trigger when activewindow's workspaces changes */
  55. static void umax_window_workspace_changed (WnckWindow *window, WckUtils *win)
  56. {
  57. track_controled_window (win);
  58. }
  59. /* Trigger when a specific window's state changes */
  60. static void track_changed_max_state (WnckWindow *window,
  61. WnckWindowState changed_mask,
  62. WnckWindowState new_state,
  63. WckUtils *win)
  64. {
  65. /* track the window max state only if it isn't the control window */
  66. if (window != win->controlwindow)
  67. {
  68. if (window
  69. && !wnck_window_is_minimized(window)
  70. && wnck_window_is_maximized(window))
  71. {
  72. track_controled_window (win);
  73. }
  74. }
  75. }
  76. /* Triggers when umaxedwindow's state changes */
  77. static void on_umaxed_window_state_changed (WnckWindow *window,
  78. WnckWindowState changed_mask,
  79. WnckWindowState new_state,
  80. WckUtils *win)
  81. {
  82. /* WARNING : only if window is unmaximized to prevent growing loop !!!*/
  83. if (!wnck_window_is_maximized(window)
  84. || wnck_window_is_minimized(window)
  85. || changed_mask & (WNCK_WINDOW_STATE_ABOVE))
  86. {
  87. track_controled_window (win);
  88. }
  89. else {
  90. on_wck_state_changed(win->controlwindow, win->data);
  91. }
  92. }
  93. /* Returns the highest maximized window */
  94. static WnckWindow *get_upper_maximized (WckUtils *win)
  95. {
  96. WnckWindow *umaxedwindow = NULL;
  97. GList *windows = wnck_screen_get_windows_stacked(win->activescreen);
  98. while (windows && windows->data)
  99. {
  100. if (!win->activeworkspace
  101. || wnck_window_is_in_viewport(windows->data, win->activeworkspace))
  102. if (wnck_window_is_maximized(windows->data)
  103. && !wnck_window_is_minimized(windows->data))
  104. {
  105. umaxedwindow = windows->data;
  106. }
  107. windows = windows->next;
  108. }
  109. /* NULL if no maximized window found */
  110. return umaxedwindow;
  111. }
  112. /* track the new controled window according to preferences */
  113. static void track_controled_window (WckUtils *win)
  114. {
  115. WnckWindow *previous_umax = NULL;
  116. WnckWindow *previous_control = NULL;
  117. previous_control = win->controlwindow;
  118. if (win->only_maximized)
  119. {
  120. previous_umax = win->umaxwindow;
  121. win->umaxwindow = get_upper_maximized(win);
  122. win->controlwindow = win->umaxwindow;
  123. }
  124. else if (win->activewindow
  125. && (!win->activeworkspace
  126. || wnck_window_is_in_viewport(win->activewindow, win->activeworkspace))
  127. && !wnck_window_is_minimized(win->activewindow)
  128. && !wnck_window_is_sticky(win->activewindow))
  129. {
  130. win->controlwindow = win->activewindow;
  131. }
  132. if (!win->umaxwindow || (win->umaxwindow != previous_umax))
  133. {
  134. wck_signal_handler_disconnect (G_OBJECT(previous_umax), win->msh);
  135. wck_signal_handler_disconnect (G_OBJECT(previous_umax), win->mwh);
  136. }
  137. if (win->only_maximized)
  138. {
  139. if (win->umaxwindow && (win->umaxwindow != previous_umax))
  140. {
  141. /* track the new upper maximized window state */
  142. win->msh = g_signal_connect(G_OBJECT(win->umaxwindow),
  143. "state-changed",
  144. G_CALLBACK (on_umaxed_window_state_changed),
  145. win);
  146. win->mwh = g_signal_connect(G_OBJECT (win->umaxwindow),
  147. "workspace-changed",
  148. G_CALLBACK (umax_window_workspace_changed),
  149. win);
  150. }
  151. else if (win->controlwindow == previous_control)
  152. {
  153. /* track previous upper maximized window state on desktop */
  154. win->umaxwindow = previous_umax;
  155. if (win->umaxwindow) {
  156. win->msh = g_signal_connect(G_OBJECT(win->umaxwindow),
  157. "state-changed",
  158. G_CALLBACK (track_changed_max_state),
  159. win);
  160. }
  161. }
  162. }
  163. if (!win->controlwindow)
  164. win->controlwindow = get_root_window(win->activescreen);
  165. if (win->controlwindow != previous_control)
  166. on_control_window_changed(win->controlwindow, previous_control, win->data);
  167. else
  168. on_wck_state_changed(win->controlwindow, win->data);
  169. }
  170. /* Triggers when a new window has been opened */
  171. static void on_window_opened (WnckScreen *screen,
  172. WnckWindow *window,
  173. WckUtils *win)
  174. {
  175. // track new maximized window
  176. if (wnck_window_is_maximized(window))
  177. track_controled_window (win);
  178. }
  179. /* Triggers when a window has been closed */
  180. static void on_window_closed (WnckScreen *screen,
  181. WnckWindow *window,
  182. WckUtils *win)
  183. {
  184. // track closed maximized window
  185. if (wnck_window_is_maximized(window))
  186. track_controled_window (win);
  187. }
  188. /* Triggers when a new active window is selected */
  189. static void active_window_changed (WnckScreen *screen,
  190. WnckWindow *previous,
  191. WckUtils *win)
  192. {
  193. win->activewindow = wnck_screen_get_active_window(screen);
  194. if (win->activewindow != previous)
  195. {
  196. wck_signal_handler_disconnect (G_OBJECT(previous), win->ash);
  197. track_controled_window (win);
  198. }
  199. if (win->activewindow
  200. && (win->activewindow != previous)
  201. && (wnck_window_get_window_type (win->activewindow) != WNCK_WINDOW_DESKTOP))
  202. {
  203. /* Start tracking the new active window */
  204. win->ash = g_signal_connect(G_OBJECT (win->activewindow), "state-changed", G_CALLBACK (track_changed_max_state), win);
  205. }
  206. }
  207. /* Triggers when user changes viewports on Compiz */
  208. // We ONLY need this for Compiz (Marco doesn't use viewports)
  209. static void on_viewports_changed (WnckScreen *screen, WckUtils *win)
  210. {
  211. reload_wnck (win, win->only_maximized, win->data);
  212. }
  213. /* Triggers when user changes workspace on Marco (?) */
  214. static void active_workspace_changed (WnckScreen *screen,
  215. WnckWorkspace *previous,
  216. WckUtils *win)
  217. {
  218. reload_wnck (win, win->only_maximized, win->data);
  219. }
  220. void toggle_maximize (WnckWindow *window)
  221. {
  222. if (window && wnck_window_is_maximized(window))
  223. wnck_window_unmaximize(window);
  224. else
  225. wnck_window_maximize(window);
  226. }
  227. void reload_wnck (WckUtils *win, gboolean only_maximized, gpointer data)
  228. {
  229. /* disconnect all signal handlers */
  230. wck_signal_handler_disconnect (G_OBJECT(win->controlwindow), win->ash);
  231. wck_signal_handler_disconnect (G_OBJECT(win->controlwindow), win->msh);
  232. wck_signal_handler_disconnect (G_OBJECT(win->controlwindow), win->mwh);
  233. wck_signal_handler_disconnect (G_OBJECT(win->activescreen), win->sch);
  234. wck_signal_handler_disconnect (G_OBJECT(win->activescreen), win->soh);
  235. wck_signal_handler_disconnect (G_OBJECT(win->activescreen), win->svh);
  236. wck_signal_handler_disconnect (G_OBJECT(win->activescreen), win->swh);
  237. init_wnck (win, only_maximized, data);
  238. }
  239. void init_wnck (WckUtils *win, gboolean only_maximized, gpointer data)
  240. {
  241. /* save data */
  242. win->data = data;
  243. /* get window proprieties */
  244. win->activescreen = wnck_screen_get_default();
  245. win->activeworkspace = wnck_screen_get_active_workspace(win->activescreen);
  246. if (!win->activeworkspace)
  247. win->activeworkspace = wnck_screen_get_workspace(win->activescreen, 0);
  248. win->activewindow = wnck_screen_get_active_window(win->activescreen);
  249. win->umaxwindow = NULL;
  250. win->controlwindow = NULL;
  251. win->only_maximized = only_maximized;
  252. /* Global window tracking */
  253. g_signal_connect(win->activescreen, "active-window-changed", G_CALLBACK (active_window_changed), win);
  254. if (win->only_maximized)
  255. {
  256. win->sch = g_signal_connect(win->activescreen, "window-closed", G_CALLBACK (on_window_closed), win);
  257. win->soh = g_signal_connect(win->activescreen, "window-opened", G_CALLBACK (on_window_opened), win);
  258. }
  259. win->svh = g_signal_connect(win->activescreen, "viewports-changed", G_CALLBACK (on_viewports_changed), win);
  260. win->swh = g_signal_connect(win->activescreen, "active-workspace-changed", G_CALLBACK (active_workspace_changed), win);
  261. /* Get controled window */
  262. track_controled_window (win);
  263. if (!win->controlwindow)
  264. on_control_window_changed (NULL, NULL, win->data);
  265. }