ewmh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * ewmh.c
  3. * Copyright © 2008, 2009 Martin Duquesnoy <xorg62@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of the nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "wmfs.h"
  33. /* Taken From standards.freedesktop.org */
  34. #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
  35. #define _NET_WM_STATE_ADD 1 /* add/set property */
  36. #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
  37. /** Init ewmh atoms
  38. */
  39. void
  40. ewmh_init_hints(void)
  41. {
  42. int i = 1, s, j, showing_desk = 0;
  43. char root_name[] = WMFS_VERSION;
  44. char class[] = "wmfs", st[64];
  45. long pid = (long)getpid();
  46. s = screen_count();
  47. net_atom = emalloc(net_last + s, sizeof(Atom));
  48. /* EWMH hints */
  49. net_atom[net_supported] = ATOM("_NET_SUPPORTED");
  50. net_atom[net_client_list] = ATOM("_NET_CLIENT_LIST");
  51. net_atom[net_frame_extents] = ATOM("_NET_FRAME_EXTENTS");
  52. net_atom[net_number_of_desktops] = ATOM("_NET_NUMBER_OF_DESKTOPS");
  53. net_atom[net_current_desktop] = ATOM("_NET_CURRENT_DESKTOP");
  54. net_atom[net_desktop_names] = ATOM("_NET_DESKTOP_NAMES");
  55. net_atom[net_desktop_geometry] = ATOM("_NET_DESKTOP_GEOMETRY");
  56. net_atom[net_workarea] = ATOM("_NET_WORKAREA");
  57. net_atom[net_active_window] = ATOM("_NET_ACTIVE_WINDOW");
  58. net_atom[net_close_window] = ATOM("_NET_CLOSE_WINDOW");
  59. net_atom[net_wm_name] = ATOM("_NET_WM_NAME");
  60. net_atom[net_wm_pid] = ATOM("_NET_WM_PID");
  61. net_atom[net_wm_desktop] = ATOM("_NET_WM_DESKTOP");
  62. net_atom[net_showing_desktop] = ATOM("_NET_SHOWING_DESKTOP");
  63. net_atom[net_wm_icon_name] = ATOM("_NET_WM_ICON_NAME");
  64. net_atom[net_wm_window_type] = ATOM("_NET_WM_WINDOW_TYPE");
  65. net_atom[net_supporting_wm_check] = ATOM("_NET_SUPPORTING_WM_CHECK");
  66. net_atom[net_wm_window_type_normal] = ATOM("_NET_WM_WINDOW_TYPE_NORMAL");
  67. net_atom[net_wm_window_type_dock] = ATOM("_NET_WM_WINDOW_TYPE_DOCK");
  68. net_atom[net_wm_window_type_splash] = ATOM("_NET_WM_WINDOW_TYPE_SPLASH");
  69. net_atom[net_wm_window_type_dialog] = ATOM("_NET_WM_WINDOW_TYPE_DIALOG");
  70. net_atom[net_wm_icon] = ATOM("_NET_WM_ICON");
  71. net_atom[net_wm_state] = ATOM("_NET_WM_STATE");
  72. net_atom[net_wm_state_fullscreen] = ATOM("_NET_WM_STATE_FULLSCREEN");
  73. net_atom[net_wm_state_demands_attention] = ATOM("_NET_WM_STATE_DEMANDS_ATTENTION");
  74. net_atom[utf8_string] = ATOM("UTF8_STRING");
  75. /* WMFS hints */
  76. net_atom[wmfs_running] = ATOM("_WMFS_RUNNING");
  77. net_atom[wmfs_update_hints] = ATOM("_WMFS_UPDATE_HINTS");
  78. net_atom[wmfs_update_status] = ATOM("_WMFS_UPDATE_STATUS");
  79. net_atom[wmfs_set_screen] = ATOM("_WMFS_SET_SCREEN");
  80. net_atom[wmfs_screen_count] = ATOM("_WMFS_SCREEN_COUNT");
  81. net_atom[wmfs_current_tag] = ATOM("_WMFS_CURRENT_TAG");
  82. net_atom[wmfs_tag_list] = ATOM("_WMFS_TAG_LIST");
  83. net_atom[wmfs_current_screen] = ATOM("_WMFS_CURRENT_SCREEN");
  84. net_atom[wmfs_current_layout] = ATOM("_WMFS_CURRENT_LAYOUT");
  85. net_atom[wmfs_mwfact] = ATOM("_WMFS_MWFACT");
  86. net_atom[wmfs_nmaster] = ATOM("_WMFS_NMASTER");
  87. net_atom[wmfs_function] = ATOM("_WMFS_FUNCTION");
  88. net_atom[wmfs_cmd] = ATOM("_WMFS_CMD");
  89. /* Multi atom _WMFS_STATUSTEXT_<screennum> */
  90. for(j = 0; j < s; ++j)
  91. {
  92. sprintf(st, "_WMFS_STATUSTEXT_%d", j);
  93. net_atom[wmfs_statustext + j] = ATOM(st);
  94. }
  95. XChangeProperty(dpy, ROOT, net_atom[net_supported], XA_ATOM, 32,
  96. PropModeReplace, (uchar*)net_atom, net_last + s);
  97. XChangeProperty(dpy, ROOT, net_atom[wmfs_running], XA_CARDINAL, 32,
  98. PropModeReplace, (uchar*)&i, 1);
  99. /* Set _NET_SUPPORTING_WM_CHECK */
  100. XChangeProperty(dpy, ROOT, net_atom[net_supporting_wm_check], XA_WINDOW, 32,
  101. PropModeReplace, (uchar*)&ROOT, 1);
  102. XChangeProperty(dpy, ROOT, net_atom[net_wm_name], net_atom[utf8_string], 8,
  103. PropModeReplace, (uchar*)&root_name, strlen(root_name));
  104. XChangeProperty(dpy, ROOT, ATOM("WM_CLASS"), XA_STRING, 8,
  105. PropModeReplace, (uchar*)&class, strlen(class));
  106. /* Set _NET_WM_PID */
  107. XChangeProperty(dpy, ROOT, net_atom[net_wm_pid], XA_CARDINAL, 32,
  108. PropModeReplace, (uchar*)&pid, 1);
  109. /* Set _NET_SHOWING_DESKTOP */
  110. XChangeProperty(dpy, ROOT, net_atom[net_showing_desktop], XA_CARDINAL, 32,
  111. PropModeReplace, (uchar*)&showing_desk, 1);
  112. return;
  113. }
  114. /** Get the number of desktop (tag)
  115. */
  116. void
  117. ewmh_get_number_of_desktop(void)
  118. {
  119. int c = 0, i;
  120. for(i = 0; i < screen_count(); ++i)
  121. c += conf.ntag[i];
  122. XChangeProperty(dpy, ROOT, net_atom[net_number_of_desktops], XA_CARDINAL, 32,
  123. PropModeReplace, (uchar*)&c, 1);
  124. return;
  125. }
  126. /** Get the current desktop
  127. */
  128. void
  129. ewmh_update_current_tag_prop(void)
  130. {
  131. int t;
  132. char *s = NULL;
  133. screen_get_sel();
  134. t = seltag[selscreen] - 1;
  135. s = emalloc(8, sizeof(char));
  136. /* Get current desktop (tag) */
  137. XChangeProperty(dpy, ROOT, net_atom[net_current_desktop], XA_CARDINAL, 32,
  138. PropModeReplace, (uchar*)&t, 1);
  139. /* Current tag name */
  140. XChangeProperty(dpy, ROOT, net_atom[wmfs_current_tag], net_atom[utf8_string], 8,
  141. PropModeReplace, (uchar*)tags[selscreen][seltag[selscreen]].name,
  142. strlen(tags[selscreen][seltag[selscreen]].name));
  143. sprintf(s, "%.3f", tags[selscreen][t + 1].mwfact);
  144. /* Current tag mwfact */
  145. XChangeProperty(dpy, ROOT, net_atom[wmfs_mwfact], XA_STRING, 8,
  146. PropModeReplace, (uchar*)s, strlen(s));
  147. /* Current nmaster */
  148. XChangeProperty(dpy, ROOT, net_atom[wmfs_nmaster], XA_CARDINAL, 32,
  149. PropModeReplace, (uchar*)&tags[selscreen][t + 1].nmaster, 1);
  150. /* Current layout */
  151. XChangeProperty(dpy, ROOT, net_atom[wmfs_current_layout], net_atom[utf8_string], 8,
  152. PropModeReplace, (uchar*)tags[selscreen][seltag[selscreen]].layout.symbol,
  153. strlen(tags[selscreen][seltag[selscreen]].layout.symbol));
  154. free(s);
  155. return;
  156. }
  157. /** Get _NET_CLIENT_LIST
  158. */
  159. void
  160. ewmh_get_client_list(void)
  161. {
  162. Window *list;
  163. Client *c;
  164. int win_n;
  165. for(win_n = 0, c = clients; c; c = c->next, ++win_n);
  166. list = emalloc(win_n, sizeof(Window));
  167. for(win_n = 0, c = clients; c; c = c->next, ++win_n)
  168. list[win_n] = c->win;
  169. XChangeProperty(dpy, ROOT, net_atom[net_client_list], XA_WINDOW, 32,
  170. PropModeReplace, (uchar *)list, win_n);
  171. free(list);
  172. return;
  173. }
  174. /** The desktop names
  175. */
  176. void
  177. ewmh_get_desktop_names(void)
  178. {
  179. char *str = NULL;
  180. int S, s, i, len = 0, pos = 0;
  181. S = screen_count();
  182. for(s = 0 ; s < S; ++s)
  183. for(i = 1; i < conf.ntag[s] + 1; ++i)
  184. len += strlen(tags[s][i].name);
  185. str = emalloc(len + i + 1, sizeof(char*));
  186. for(s = 0; s < S; ++s)
  187. for(i = 1; i < conf.ntag[s] + 1; ++i, ++pos)
  188. {
  189. strncpy(str + pos, tags[s][i].name, strlen(tags[s][i].name));
  190. pos += strlen(tags[s][i].name);
  191. str[pos] = '\0';
  192. }
  193. XChangeProperty(dpy, ROOT, net_atom[net_desktop_names], net_atom[utf8_string], 8,
  194. PropModeReplace, (uchar*)str, pos);
  195. for(i = 0; i < pos; ++i)
  196. if(str[i] == '\0' && i < pos - 1)
  197. str[i] = ' ';
  198. XChangeProperty(dpy, ROOT, net_atom[wmfs_tag_list], net_atom[utf8_string], 8,
  199. PropModeReplace, (uchar*)str, pos);
  200. free(str);
  201. return;
  202. }
  203. /** Manage _NET_DESKTOP_GEOMETRY
  204. */
  205. void
  206. ewmh_set_desktop_geometry(void)
  207. {
  208. long data[2] = { MAXW, MAXH };
  209. XChangeProperty(dpy, ROOT, net_atom[net_desktop_geometry], XA_CARDINAL, 32,
  210. PropModeReplace, (uchar*)&data, 2);
  211. return;
  212. }
  213. /** Manage _NET_WORKAREA
  214. */
  215. void
  216. ewmh_set_workarea(void)
  217. {
  218. long *data;
  219. int i, s, j, tag_c = 0, pos = 0;
  220. s = screen_count();
  221. for(i = 0; i < s; ++i)
  222. tag_c += conf.ntag[i];
  223. data = emalloc(tag_c * 4, sizeof(long));
  224. for(i = 0; i < s; ++i)
  225. for(j = 0; j < conf.ntag[i]; ++j)
  226. {
  227. data[pos++] = spgeo[i].x;
  228. data[pos++] = spgeo[i].y;
  229. data[pos++] = spgeo[i].width;
  230. data[pos++] = spgeo[i].height;
  231. }
  232. XChangeProperty(dpy, ROOT, net_atom[net_workarea], XA_CARDINAL, 32,
  233. PropModeReplace, (uchar*)data, 4 * tag_c);
  234. free(data);
  235. return;
  236. }
  237. /** Manage _NET_WM_STATE_* ewmh
  238. */
  239. void
  240. ewmh_manage_net_wm_state(long data_l[], Client *c)
  241. {
  242. /* Manage _NET_WM_STATE_FULLSCREEN */
  243. if(data_l[1] == net_atom[net_wm_state_fullscreen])
  244. {
  245. if(data_l[0] == _NET_WM_STATE_ADD && !(c->flags & FSSFlag))
  246. {
  247. c->screen = screen_get_with_geo(c->geo.x, c->geo.y);
  248. client_unmap(c);
  249. c->flags &= ~UnmapFlag;
  250. XMapWindow(dpy, c->win);
  251. XReparentWindow(dpy, c->win, ROOT, spgeo[c->screen].x, spgeo[c->screen].y);
  252. XResizeWindow(dpy, c->win,
  253. spgeo[c->screen].width,
  254. spgeo[c->screen].height);
  255. c->tmp_geo = c->geo;
  256. if(c->flags & FreeFlag)
  257. c->ogeo = c->geo;
  258. c->flags |= (FSSFlag | MaxFlag);
  259. client_raise(c);
  260. client_focus(c);
  261. }
  262. else if(data_l[0] == _NET_WM_STATE_REMOVE && (c->flags & FSSFlag))
  263. {
  264. c->flags &= ~(FSSFlag | MaxFlag);
  265. client_map(c);
  266. XReparentWindow(dpy, c->win, c->frame, BORDH, TBARH);
  267. client_moveresize(c, c->tmp_geo, False);
  268. }
  269. }
  270. /* Manage _NET_WM_STATE_DEMANDS_ATTENTION */
  271. else if(data_l[1] == net_atom[net_wm_state_demands_attention])
  272. {
  273. if(data_l[0] == _NET_WM_STATE_ADD)
  274. client_focus(c);
  275. if(data_l[0] == _NET_WM_STATE_REMOVE)
  276. if(c == sel)
  277. client_focus(NULL);
  278. }
  279. return;
  280. }
  281. /** Manage the client hints
  282. *\param c Client pointer
  283. */
  284. void
  285. ewmh_manage_window_type(Client *c)
  286. {
  287. Atom *atom, rf;
  288. int i, f;
  289. ulong n, il;
  290. uchar *data = NULL;
  291. if(XGetWindowProperty(dpy, c->win, net_atom[net_wm_window_type], 0L, 0x7FFFFFFFL,
  292. False, XA_ATOM, &rf, &f, &n, &il, &data) == Success && n)
  293. {
  294. atom = (Atom*)data;
  295. for(i = 0; i < n; ++i)
  296. {
  297. /* Manage _NET_WM_WINDOW_TYPE_DOCK & _NET_WM_WINDOW_TYPE_SPLASH */
  298. if(atom[i] == net_atom[net_wm_window_type_dock]
  299. || atom[i] == net_atom[net_wm_window_type_splash])
  300. {
  301. /* Unmap frame, decoration.. */
  302. client_unmap(c);
  303. /* Map only window */
  304. XMapWindow(dpy, c->win);
  305. /* Reparent it to ROOT win */
  306. XReparentWindow(dpy, c->win, ROOT, c->geo.x, c->geo.y);
  307. XRaiseWindow(dpy, c->win);
  308. /* This window will not be managed anymore,
  309. * so let's detach it. */
  310. client_detach(c);
  311. }
  312. /* MANAGE _NET_WM_WINDOW_TYPE_DIALOG */
  313. else if(atom[i] == net_atom[net_wm_window_type_dialog])
  314. {
  315. c->flags |= FreeFlag;
  316. c->flags &= ~(TileFlag | MaxFlag | LMaxFlag);
  317. client_moveresize(sel, sel->ogeo, True);
  318. client_focus(c);
  319. tags[selscreen][seltag[selscreen]].layout.func(selscreen);
  320. }
  321. }
  322. XFree(data);
  323. }
  324. return;
  325. }