ewmh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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, j, showing_desk = 0;
  43. char root_name[] = WMFS_VERSION;
  44. char class[] = "wmfs", st[64];
  45. long pid = (long)getpid();
  46. net_atom = emalloc(net_last + screen_count(), sizeof(Atom));
  47. /* EWMH hints */
  48. net_atom[net_supported] = ATOM("_NET_SUPPORTED");
  49. net_atom[net_client_list] = ATOM("_NET_CLIENT_LIST");
  50. net_atom[net_frame_extents] = ATOM("_NET_FRAME_EXTENTS");
  51. net_atom[net_number_of_desktops] = ATOM("_NET_NUMBER_OF_DESKTOPS");
  52. net_atom[net_current_desktop] = ATOM("_NET_CURRENT_DESKTOP");
  53. net_atom[net_desktop_names] = ATOM("_NET_DESKTOP_NAMES");
  54. net_atom[net_desktop_geometry] = ATOM("_NET_DESKTOP_GEOMETRY");
  55. net_atom[net_workarea] = ATOM("_NET_WORKAREA");
  56. net_atom[net_active_window] = ATOM("_NET_ACTIVE_WINDOW");
  57. net_atom[net_close_window] = ATOM("_NET_CLOSE_WINDOW");
  58. net_atom[net_wm_name] = ATOM("_NET_WM_NAME");
  59. net_atom[net_wm_pid] = ATOM("_NET_WM_PID");
  60. net_atom[net_wm_desktop] = ATOM("_NET_WM_DESKTOP");
  61. net_atom[net_showing_desktop] = ATOM("_NET_SHOWING_DESKTOP");
  62. net_atom[net_wm_icon_name] = ATOM("_NET_WM_ICON_NAME");
  63. net_atom[net_wm_window_type] = ATOM("_NET_WM_WINDOW_TYPE");
  64. net_atom[net_supporting_wm_check] = ATOM("_NET_SUPPORTING_WM_CHECK");
  65. net_atom[net_wm_window_type_normal] = ATOM("_NET_WM_WINDOW_TYPE_NORMAL");
  66. net_atom[net_wm_window_type_dock] = ATOM("_NET_WM_WINDOW_TYPE_DOCK");
  67. net_atom[net_wm_window_type_splash] = ATOM("_NET_WM_WINDOW_TYPE_SPLASH");
  68. net_atom[net_wm_window_type_dialog] = ATOM("_NET_WM_WINDOW_TYPE_DIALOG");
  69. net_atom[net_wm_icon] = ATOM("_NET_WM_ICON");
  70. net_atom[net_wm_state] = ATOM("_NET_WM_STATE");
  71. net_atom[net_wm_state_fullscreen] = ATOM("_NET_WM_STATE_FULLSCREEN");
  72. net_atom[net_wm_state_demands_attention] = ATOM("_NET_WM_STATE_DEMANDS_ATTENTION");
  73. net_atom[utf8_string] = ATOM("UTF8_STRING");
  74. /* WMFS hints */
  75. net_atom[wmfs_running] = ATOM("_WMFS_RUNNING");
  76. net_atom[wmfs_update_hints] = ATOM("_WMFS_UPDATE_HINTS");
  77. net_atom[wmfs_update_status] = ATOM("_WMFS_UPDATE_STATUS");
  78. net_atom[wmfs_set_screen] = ATOM("_WMFS_SET_SCREEN");
  79. net_atom[wmfs_screen_count] = ATOM("_WMFS_SCREEN_COUNT");
  80. net_atom[wmfs_current_tag] = ATOM("_WMFS_CURRENT_TAG");
  81. net_atom[wmfs_tag_list] = ATOM("_WMFS_TAG_LIST");
  82. net_atom[wmfs_current_screen] = ATOM("_WMFS_CURRENT_SCREEN");
  83. net_atom[wmfs_current_layout] = ATOM("_WMFS_CURRENT_LAYOUT");
  84. net_atom[wmfs_mwfact] = ATOM("_WMFS_MWFACT");
  85. net_atom[wmfs_nmaster] = ATOM("_WMFS_NMASTER");
  86. net_atom[wmfs_function] = ATOM("_WMFS_FUNCTION");
  87. net_atom[wmfs_cmd] = ATOM("_WMFS_CMD");
  88. /* Multi atom _WMFS_STATUSTEXT_<screennum> */
  89. for(j = 0; j < screen_count(); ++j)
  90. {
  91. sprintf(st, "_WMFS_STATUSTEXT_%d", j);
  92. net_atom[wmfs_statustext + j] = ATOM(st);
  93. }
  94. XChangeProperty(dpy, ROOT, net_atom[net_supported], XA_ATOM, 32,
  95. PropModeReplace, (uchar*)net_atom, net_last + screen_count());
  96. XChangeProperty(dpy, ROOT, net_atom[wmfs_running], XA_CARDINAL, 32,
  97. PropModeReplace, (uchar*)&i, 1);
  98. /* Set _NET_SUPPORTING_WM_CHECK */
  99. XChangeProperty(dpy, ROOT, net_atom[net_supporting_wm_check], XA_WINDOW, 32,
  100. PropModeReplace, (uchar*)&ROOT, 1);
  101. XChangeProperty(dpy, ROOT, net_atom[net_wm_name], net_atom[utf8_string], 8,
  102. PropModeReplace, (uchar*)&root_name, strlen(root_name));
  103. XChangeProperty(dpy, ROOT, ATOM("WM_CLASS"), XA_STRING, 8,
  104. PropModeReplace, (uchar*)&class, strlen(class));
  105. /* Set _NET_WM_PID */
  106. XChangeProperty(dpy, ROOT, net_atom[net_wm_pid], XA_CARDINAL, 32,
  107. PropModeReplace, (uchar*)&pid, 1);
  108. /* Set _NET_SHOWING_DESKTOP */
  109. XChangeProperty(dpy, ROOT, net_atom[net_showing_desktop], XA_CARDINAL, 32,
  110. PropModeReplace, (uchar*)&showing_desk, 1);
  111. return;
  112. }
  113. /** Get the number of desktop (tag)
  114. */
  115. void
  116. ewmh_get_number_of_desktop(void)
  117. {
  118. int c = 0, i;
  119. for(i = 0; i < screen_count(); ++i)
  120. c += conf.ntag[i];
  121. XChangeProperty(dpy, ROOT, net_atom[net_number_of_desktops], XA_CARDINAL, 32,
  122. PropModeReplace, (uchar*)&c, 1);
  123. return;
  124. }
  125. /** Get the current desktop
  126. */
  127. void
  128. ewmh_update_current_tag_prop(void)
  129. {
  130. int t;
  131. char *s = NULL;
  132. screen_get_sel();
  133. t = seltag[selscreen] - 1;
  134. s = emalloc(8, sizeof(char));
  135. /* Get current desktop (tag) */
  136. XChangeProperty(dpy, ROOT, net_atom[net_current_desktop], XA_CARDINAL, 32,
  137. PropModeReplace, (uchar*)&t, 1);
  138. /* Current tag name */
  139. XChangeProperty(dpy, ROOT, net_atom[wmfs_current_tag], net_atom[utf8_string], 8,
  140. PropModeReplace, (uchar*)tags[selscreen][seltag[selscreen]].name,
  141. strlen(tags[selscreen][seltag[selscreen]].name));
  142. sprintf(s, "%.3f", tags[selscreen][t + 1].mwfact);
  143. /* Current tag mwfact */
  144. XChangeProperty(dpy, ROOT, net_atom[wmfs_mwfact], XA_STRING, 8,
  145. PropModeReplace, (uchar*)s, strlen(s));
  146. /* Current nmaster */
  147. XChangeProperty(dpy, ROOT, net_atom[wmfs_nmaster], XA_CARDINAL, 32,
  148. PropModeReplace, (uchar*)&tags[selscreen][t + 1].nmaster, 1);
  149. /* Current layout */
  150. XChangeProperty(dpy, ROOT, net_atom[wmfs_current_layout], net_atom[utf8_string], 8,
  151. PropModeReplace, (uchar*)tags[selscreen][seltag[selscreen]].layout.symbol,
  152. strlen(tags[selscreen][seltag[selscreen]].layout.symbol));
  153. free(s);
  154. return;
  155. }
  156. /** Get _NET_CLIENT_LIST
  157. */
  158. void
  159. ewmh_get_client_list(void)
  160. {
  161. Window *list;
  162. Client *c;
  163. int win_n;
  164. for(win_n = 0, c = clients; c; c = c->next, ++win_n);
  165. list = emalloc(win_n, sizeof(Window));
  166. for(win_n = 0, c = clients; c; c = c->next, ++win_n)
  167. list[win_n] = c->win;
  168. XChangeProperty(dpy, ROOT, net_atom[net_client_list], XA_WINDOW, 32,
  169. PropModeReplace, (uchar *)list, win_n);
  170. free(list);
  171. return;
  172. }
  173. /** The desktop names
  174. */
  175. void
  176. ewmh_get_desktop_names(void)
  177. {
  178. char *str = NULL;
  179. int s, i, len = 0, pos = 0;
  180. for(s = 0 ; s < screen_count(); ++s)
  181. for(i = 1; i < conf.ntag[s] + 1; ++i)
  182. len += strlen(tags[s][i].name);
  183. str = emalloc(len + i + 1, sizeof(char*));
  184. for(s = 0; s < screen_count(); ++s)
  185. for(i = 1; i < conf.ntag[s] + 1; ++i, ++pos)
  186. {
  187. strncpy(str + pos, tags[s][i].name, strlen(tags[s][i].name));
  188. pos += strlen(tags[s][i].name);
  189. str[pos] = '\0';
  190. }
  191. XChangeProperty(dpy, ROOT, net_atom[net_desktop_names], net_atom[utf8_string], 8,
  192. PropModeReplace, (uchar*)str, pos);
  193. for(i = 0; i < pos; ++i)
  194. if(str[i] == '\0' && i < pos - 1)
  195. str[i] = ' ';
  196. XChangeProperty(dpy, ROOT, net_atom[wmfs_tag_list], net_atom[utf8_string], 8,
  197. PropModeReplace, (uchar*)str, pos);
  198. free(str);
  199. return;
  200. }
  201. /** Manage _NET_DESKTOP_GEOMETRY
  202. */
  203. void
  204. ewmh_set_desktop_geometry(void)
  205. {
  206. long data[2] = { MAXW, MAXH };
  207. XChangeProperty(dpy, ROOT, net_atom[net_desktop_geometry], XA_CARDINAL, 32,
  208. PropModeReplace, (uchar*)&data, 2);
  209. return;
  210. }
  211. /** Manage _NET_WORKAREA
  212. */
  213. void
  214. ewmh_set_workarea(void)
  215. {
  216. long *data;
  217. int i, j, tag_c = 0, pos = 0;
  218. for(i = 0; i < screen_count(); ++i)
  219. tag_c += conf.ntag[i];
  220. data = emalloc(tag_c * 4, sizeof(long));
  221. for(i = 0; i < screen_count(); ++i)
  222. for(j = 0; j < conf.ntag[i]; ++j)
  223. {
  224. data[pos++] = spgeo[i].x;
  225. data[pos++] = spgeo[i].y;
  226. data[pos++] = spgeo[i].width;
  227. data[pos++] = spgeo[i].height;
  228. }
  229. XChangeProperty(dpy, ROOT, net_atom[net_workarea], XA_CARDINAL, 32,
  230. PropModeReplace, (uchar*)data, 4 * tag_c);
  231. free(data);
  232. return;
  233. }
  234. /** Manage _NET_WM_STATE_* ewmh
  235. */
  236. void
  237. ewmh_manage_net_wm_state(long data_l[], Client *c)
  238. {
  239. /* Manage _NET_WM_STATE_FULLSCREEN */
  240. if(data_l[1] == net_atom[net_wm_state_fullscreen])
  241. {
  242. if(data_l[0] == _NET_WM_STATE_ADD && !(c->flags & FSSFlag))
  243. {
  244. c->screen = screen_get_with_geo(c->geo.x, c->geo.y);
  245. client_unmap(c);
  246. c->flags &= ~UnmapFlag;
  247. XMapWindow(dpy, c->win);
  248. XReparentWindow(dpy, c->win, ROOT, spgeo[c->screen].x, spgeo[c->screen].y);
  249. XResizeWindow(dpy, c->win,
  250. spgeo[c->screen].width,
  251. spgeo[c->screen].height);
  252. c->tmp_geo = c->geo;
  253. if(c->flags & FreeFlag)
  254. c->ogeo = c->geo;
  255. c->flags |= (FSSFlag | MaxFlag);
  256. client_raise(c);
  257. client_focus(c);
  258. }
  259. else if(data_l[0] == _NET_WM_STATE_REMOVE && (c->flags & FSSFlag))
  260. {
  261. c->flags &= ~(FSSFlag | MaxFlag);
  262. client_map(c);
  263. XReparentWindow(dpy, c->win, c->frame, BORDH, TBARH);
  264. client_moveresize(c, c->tmp_geo, False);
  265. }
  266. }
  267. /* Manage _NET_WM_STATE_DEMANDS_ATTENTION */
  268. else if(data_l[1] == net_atom[net_wm_state_demands_attention])
  269. {
  270. if(data_l[0] == _NET_WM_STATE_ADD)
  271. client_focus(c);
  272. if(data_l[0] == _NET_WM_STATE_REMOVE)
  273. if(c == sel)
  274. client_focus(NULL);
  275. }
  276. return;
  277. }
  278. /** Manage the client hints
  279. *\param c Client pointer
  280. */
  281. void
  282. ewmh_manage_window_type(Client *c)
  283. {
  284. Atom *atom, rf;
  285. int i, f;
  286. ulong n, il;
  287. uchar *data = NULL;
  288. if(XGetWindowProperty(dpy, c->win, net_atom[net_wm_window_type], 0L, 0x7FFFFFFFL,
  289. False, XA_ATOM, &rf, &f, &n, &il, &data) == Success && n)
  290. {
  291. atom = (Atom*)data;
  292. for(i = 0; i < n; ++i)
  293. {
  294. /* Manage _NET_WM_WINDOW_TYPE_DOCK & _NET_WM_WINDOW_TYPE_SPLASH */
  295. if(atom[i] == net_atom[net_wm_window_type_dock]
  296. || atom[i] == net_atom[net_wm_window_type_splash])
  297. {
  298. /* Unmap frame, decoration.. */
  299. client_unmap(c);
  300. /* Map only window */
  301. XMapWindow(dpy, c->win);
  302. /* Reparent it to ROOT win */
  303. XReparentWindow(dpy, c->win, ROOT, c->geo.x, c->geo.y);
  304. XRaiseWindow(dpy, c->win);
  305. /* This window will not be managed anymore,
  306. * so let's detach it. */
  307. client_detach(c);
  308. }
  309. /* MANAGE _NET_WM_WINDOW_TYPE_DIALOG */
  310. else if(atom[i] == net_atom[net_wm_window_type_dialog])
  311. {
  312. c->flags |= FreeFlag;
  313. c->flags &= ~(TileFlag | MaxFlag | LMaxFlag);
  314. client_moveresize(sel, sel->ogeo, True);
  315. client_focus(c);
  316. tags[selscreen][seltag[selscreen]].layout.func(selscreen);
  317. }
  318. }
  319. XFree(data);
  320. }
  321. return;
  322. }