Mirror of Awesome WM window manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ewmh.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * ewmh.c - EWMH support functions
  3. *
  4. * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #include "ewmh.h"
  22. #include "objects/client.h"
  23. #include "objects/tag.h"
  24. #include "common/atoms.h"
  25. #include "xwindow.h"
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <xcb/xcb.h>
  29. #include <xcb/xcb_atom.h>
  30. #define _NET_WM_STATE_REMOVE 0
  31. #define _NET_WM_STATE_ADD 1
  32. #define _NET_WM_STATE_TOGGLE 2
  33. #define ALL_DESKTOPS 0xffffffff
  34. /** Update client EWMH hints.
  35. * \param L The Lua VM state.
  36. */
  37. static int
  38. ewmh_client_update_hints(lua_State *L)
  39. {
  40. client_t *c = luaA_checkudata(L, 1, &client_class);
  41. xcb_atom_t state[10]; /* number of defined state atoms */
  42. int i = 0;
  43. if(c->modal)
  44. state[i++] = _NET_WM_STATE_MODAL;
  45. if(c->fullscreen)
  46. state[i++] = _NET_WM_STATE_FULLSCREEN;
  47. if(c->maximized_vertical || c->maximized)
  48. state[i++] = _NET_WM_STATE_MAXIMIZED_VERT;
  49. if(c->maximized_horizontal || c->maximized)
  50. state[i++] = _NET_WM_STATE_MAXIMIZED_HORZ;
  51. if(c->sticky)
  52. state[i++] = _NET_WM_STATE_STICKY;
  53. if(c->skip_taskbar)
  54. state[i++] = _NET_WM_STATE_SKIP_TASKBAR;
  55. if(c->above)
  56. state[i++] = _NET_WM_STATE_ABOVE;
  57. if(c->below)
  58. state[i++] = _NET_WM_STATE_BELOW;
  59. if(c->minimized)
  60. state[i++] = _NET_WM_STATE_HIDDEN;
  61. if(c->urgent)
  62. state[i++] = _NET_WM_STATE_DEMANDS_ATTENTION;
  63. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  64. c->window, _NET_WM_STATE, XCB_ATOM_ATOM, 32, i, state);
  65. return 0;
  66. }
  67. static int
  68. ewmh_update_net_active_window(lua_State *L)
  69. {
  70. xcb_window_t win;
  71. if(globalconf.focus.client)
  72. win = globalconf.focus.client->window;
  73. else
  74. win = XCB_NONE;
  75. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  76. globalconf.screen->root,
  77. _NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 32, 1, &win);
  78. return 0;
  79. }
  80. static int
  81. ewmh_update_net_client_list(lua_State *L)
  82. {
  83. xcb_window_t *wins = p_alloca(xcb_window_t, globalconf.clients.len);
  84. int n = 0;
  85. foreach(client, globalconf.clients)
  86. wins[n++] = (*client)->window;
  87. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  88. globalconf.screen->root,
  89. _NET_CLIENT_LIST, XCB_ATOM_WINDOW, 32, n, wins);
  90. return 0;
  91. }
  92. static int
  93. ewmh_client_update_frame_extents(lua_State *L)
  94. {
  95. client_t *c = luaA_checkudata(L, 1, &client_class);;
  96. uint32_t extents[4];
  97. extents[0] = c->border_width + c->titlebar[CLIENT_TITLEBAR_LEFT].size;
  98. extents[1] = c->border_width + c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
  99. extents[2] = c->border_width + c->titlebar[CLIENT_TITLEBAR_TOP].size;
  100. extents[3] = c->border_width + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
  101. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  102. c->window, _NET_FRAME_EXTENTS, XCB_ATOM_CARDINAL, 32, 4, extents);
  103. return 0;
  104. }
  105. void
  106. ewmh_init(void)
  107. {
  108. xcb_window_t father;
  109. xcb_screen_t *xscreen = globalconf.screen;
  110. xcb_atom_t atom[] =
  111. {
  112. _NET_SUPPORTED,
  113. _NET_SUPPORTING_WM_CHECK,
  114. _NET_STARTUP_ID,
  115. _NET_CLIENT_LIST,
  116. _NET_CLIENT_LIST_STACKING,
  117. _NET_NUMBER_OF_DESKTOPS,
  118. _NET_CURRENT_DESKTOP,
  119. _NET_DESKTOP_NAMES,
  120. _NET_ACTIVE_WINDOW,
  121. _NET_CLOSE_WINDOW,
  122. _NET_FRAME_EXTENTS,
  123. _NET_WM_NAME,
  124. _NET_WM_STRUT_PARTIAL,
  125. _NET_WM_ICON_NAME,
  126. _NET_WM_VISIBLE_ICON_NAME,
  127. _NET_WM_DESKTOP,
  128. _NET_WM_WINDOW_TYPE,
  129. _NET_WM_WINDOW_TYPE_DESKTOP,
  130. _NET_WM_WINDOW_TYPE_DOCK,
  131. _NET_WM_WINDOW_TYPE_TOOLBAR,
  132. _NET_WM_WINDOW_TYPE_MENU,
  133. _NET_WM_WINDOW_TYPE_UTILITY,
  134. _NET_WM_WINDOW_TYPE_SPLASH,
  135. _NET_WM_WINDOW_TYPE_DIALOG,
  136. _NET_WM_WINDOW_TYPE_DROPDOWN_MENU,
  137. _NET_WM_WINDOW_TYPE_POPUP_MENU,
  138. _NET_WM_WINDOW_TYPE_TOOLTIP,
  139. _NET_WM_WINDOW_TYPE_NOTIFICATION,
  140. _NET_WM_WINDOW_TYPE_COMBO,
  141. _NET_WM_WINDOW_TYPE_DND,
  142. _NET_WM_WINDOW_TYPE_NORMAL,
  143. _NET_WM_ICON,
  144. _NET_WM_PID,
  145. _NET_WM_STATE,
  146. _NET_WM_STATE_STICKY,
  147. _NET_WM_STATE_SKIP_TASKBAR,
  148. _NET_WM_STATE_FULLSCREEN,
  149. _NET_WM_STATE_MAXIMIZED_HORZ,
  150. _NET_WM_STATE_MAXIMIZED_VERT,
  151. _NET_WM_STATE_ABOVE,
  152. _NET_WM_STATE_BELOW,
  153. _NET_WM_STATE_MODAL,
  154. _NET_WM_STATE_HIDDEN,
  155. _NET_WM_STATE_DEMANDS_ATTENTION
  156. };
  157. int i;
  158. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  159. xscreen->root, _NET_SUPPORTED, XCB_ATOM_ATOM, 32,
  160. countof(atom), atom);
  161. /* create our own window */
  162. father = xcb_generate_id(globalconf.connection);
  163. xcb_create_window(globalconf.connection, xscreen->root_depth,
  164. father, xscreen->root, -1, -1, 1, 1, 0,
  165. XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
  166. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  167. xscreen->root, _NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32,
  168. 1, &father);
  169. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  170. father, _NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32,
  171. 1, &father);
  172. /* set the window manager name */
  173. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  174. father, _NET_WM_NAME, UTF8_STRING, 8, 7, "awesome");
  175. /* Set an instance, just because we can */
  176. xwindow_set_class_instance(father);
  177. /* set the window manager PID */
  178. i = getpid();
  179. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  180. father, _NET_WM_PID, XCB_ATOM_CARDINAL, 32, 1, &i);
  181. }
  182. static void
  183. ewmh_update_maximize(bool h, bool status, bool toggle)
  184. {
  185. lua_State *L = globalconf_get_lua_State();
  186. if (h)
  187. lua_pushstring(L, "client_maximize_horizontal");
  188. else
  189. lua_pushstring(L, "client_maximize_vertical");
  190. /* Create table argument with raise=true. */
  191. lua_newtable(L);
  192. lua_pushstring(L, "toggle");
  193. lua_pushboolean(L, toggle);
  194. lua_settable(L, -3);
  195. lua_pushstring(L, "status");
  196. lua_pushboolean(L, status);
  197. lua_settable(L, -3);
  198. luaA_object_emit_signal(L, -3, "request::geometry", 2);
  199. }
  200. void
  201. ewmh_init_lua(void)
  202. {
  203. lua_State *L = globalconf_get_lua_State();
  204. luaA_class_connect_signal(L, &client_class, "focus", ewmh_update_net_active_window);
  205. luaA_class_connect_signal(L, &client_class, "unfocus", ewmh_update_net_active_window);
  206. luaA_class_connect_signal(L, &client_class, "manage", ewmh_update_net_client_list);
  207. luaA_class_connect_signal(L, &client_class, "unmanage", ewmh_update_net_client_list);
  208. luaA_class_connect_signal(L, &client_class, "property::modal" , ewmh_client_update_hints);
  209. luaA_class_connect_signal(L, &client_class, "property::fullscreen" , ewmh_client_update_hints);
  210. luaA_class_connect_signal(L, &client_class, "property::maximized_horizontal" , ewmh_client_update_hints);
  211. luaA_class_connect_signal(L, &client_class, "property::maximized_vertical" , ewmh_client_update_hints);
  212. luaA_class_connect_signal(L, &client_class, "property::maximized" , ewmh_client_update_hints);
  213. luaA_class_connect_signal(L, &client_class, "property::sticky" , ewmh_client_update_hints);
  214. luaA_class_connect_signal(L, &client_class, "property::skip_taskbar" , ewmh_client_update_hints);
  215. luaA_class_connect_signal(L, &client_class, "property::above" , ewmh_client_update_hints);
  216. luaA_class_connect_signal(L, &client_class, "property::below" , ewmh_client_update_hints);
  217. luaA_class_connect_signal(L, &client_class, "property::minimized" , ewmh_client_update_hints);
  218. luaA_class_connect_signal(L, &client_class, "property::urgent" , ewmh_client_update_hints);
  219. luaA_class_connect_signal(L, &client_class, "property::titlebar_top" , ewmh_client_update_frame_extents);
  220. luaA_class_connect_signal(L, &client_class, "property::titlebar_bottom" , ewmh_client_update_frame_extents);
  221. luaA_class_connect_signal(L, &client_class, "property::titlebar_right" , ewmh_client_update_frame_extents);
  222. luaA_class_connect_signal(L, &client_class, "property::titlebar_left" , ewmh_client_update_frame_extents);
  223. luaA_class_connect_signal(L, &client_class, "property::border_width" , ewmh_client_update_frame_extents);
  224. luaA_class_connect_signal(L, &client_class, "manage", ewmh_client_update_frame_extents);
  225. /* NET_CURRENT_DESKTOP handling */
  226. luaA_class_connect_signal(L, &client_class, "focus", ewmh_update_net_current_desktop);
  227. luaA_class_connect_signal(L, &client_class, "unfocus", ewmh_update_net_current_desktop);
  228. luaA_class_connect_signal(L, &client_class, "tagged", ewmh_update_net_current_desktop);
  229. luaA_class_connect_signal(L, &client_class, "untagged", ewmh_update_net_current_desktop);
  230. luaA_class_connect_signal(L, &tag_class, "property::selected", ewmh_update_net_current_desktop);
  231. }
  232. /** Set the client list in stacking order, bottom to top.
  233. */
  234. void
  235. ewmh_update_net_client_list_stacking(void)
  236. {
  237. int n = 0;
  238. xcb_window_t *wins = p_alloca(xcb_window_t, globalconf.stack.len);
  239. foreach(client, globalconf.stack)
  240. wins[n++] = (*client)->window;
  241. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  242. globalconf.screen->root,
  243. _NET_CLIENT_LIST_STACKING, XCB_ATOM_WINDOW, 32, n, wins);
  244. }
  245. void
  246. ewmh_update_net_numbers_of_desktop(void)
  247. {
  248. uint32_t count = globalconf.tags.len;
  249. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  250. globalconf.screen->root,
  251. _NET_NUMBER_OF_DESKTOPS, XCB_ATOM_CARDINAL, 32, 1, &count);
  252. }
  253. int
  254. ewmh_update_net_current_desktop(lua_State *L)
  255. {
  256. uint32_t idx = tags_get_current_or_first_selected_index();
  257. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  258. globalconf.screen->root,
  259. _NET_CURRENT_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &idx);
  260. return 0;
  261. }
  262. void
  263. ewmh_update_net_desktop_names(void)
  264. {
  265. buffer_t buf;
  266. buffer_inita(&buf, BUFSIZ);
  267. foreach(tag, globalconf.tags)
  268. {
  269. buffer_adds(&buf, tag_get_name(*tag));
  270. buffer_addc(&buf, '\0');
  271. }
  272. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  273. globalconf.screen->root,
  274. _NET_DESKTOP_NAMES, UTF8_STRING, 8, buf.len, buf.s);
  275. buffer_wipe(&buf);
  276. }
  277. static void
  278. ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set)
  279. {
  280. lua_State *L = globalconf_get_lua_State();
  281. luaA_object_push(L, c);
  282. if(state == _NET_WM_STATE_STICKY)
  283. {
  284. if(set == _NET_WM_STATE_REMOVE)
  285. client_set_sticky(L, -1, false);
  286. else if(set == _NET_WM_STATE_ADD)
  287. client_set_sticky(L, -1, true);
  288. else if(set == _NET_WM_STATE_TOGGLE)
  289. client_set_sticky(L, -1, !c->sticky);
  290. }
  291. else if(state == _NET_WM_STATE_SKIP_TASKBAR)
  292. {
  293. if(set == _NET_WM_STATE_REMOVE)
  294. client_set_skip_taskbar(L, -1, false);
  295. else if(set == _NET_WM_STATE_ADD)
  296. client_set_skip_taskbar(L, -1, true);
  297. else if(set == _NET_WM_STATE_TOGGLE)
  298. client_set_skip_taskbar(L, -1, !c->skip_taskbar);
  299. }
  300. else if(state == _NET_WM_STATE_FULLSCREEN)
  301. {
  302. if(set == _NET_WM_STATE_REMOVE)
  303. client_set_fullscreen(L, -1, false);
  304. else if(set == _NET_WM_STATE_ADD)
  305. client_set_fullscreen(L, -1, true);
  306. else if(set == _NET_WM_STATE_TOGGLE)
  307. client_set_fullscreen(L, -1, !c->fullscreen);
  308. }
  309. else if(state == _NET_WM_STATE_MAXIMIZED_HORZ)
  310. {
  311. if(set == _NET_WM_STATE_REMOVE)
  312. ewmh_update_maximize(true, false, false);
  313. else if(set == _NET_WM_STATE_ADD)
  314. ewmh_update_maximize(true, true, false);
  315. else if(set == _NET_WM_STATE_TOGGLE)
  316. ewmh_update_maximize(true, false, true);
  317. }
  318. else if(state == _NET_WM_STATE_MAXIMIZED_VERT)
  319. {
  320. if(set == _NET_WM_STATE_REMOVE)
  321. ewmh_update_maximize(false, false, false);
  322. else if(set == _NET_WM_STATE_ADD)
  323. ewmh_update_maximize(false, true, false);
  324. else if(set == _NET_WM_STATE_TOGGLE)
  325. ewmh_update_maximize(false, false, true);
  326. }
  327. else if(state == _NET_WM_STATE_ABOVE)
  328. {
  329. if(set == _NET_WM_STATE_REMOVE)
  330. client_set_above(L, -1, false);
  331. else if(set == _NET_WM_STATE_ADD)
  332. client_set_above(L, -1, true);
  333. else if(set == _NET_WM_STATE_TOGGLE)
  334. client_set_above(L, -1, !c->above);
  335. }
  336. else if(state == _NET_WM_STATE_BELOW)
  337. {
  338. if(set == _NET_WM_STATE_REMOVE)
  339. client_set_below(L, -1, false);
  340. else if(set == _NET_WM_STATE_ADD)
  341. client_set_below(L, -1, true);
  342. else if(set == _NET_WM_STATE_TOGGLE)
  343. client_set_below(L, -1, !c->below);
  344. }
  345. else if(state == _NET_WM_STATE_MODAL)
  346. {
  347. if(set == _NET_WM_STATE_REMOVE)
  348. client_set_modal(L, -1, false);
  349. else if(set == _NET_WM_STATE_ADD)
  350. client_set_modal(L, -1, true);
  351. else if(set == _NET_WM_STATE_TOGGLE)
  352. client_set_modal(L, -1, !c->modal);
  353. }
  354. else if(state == _NET_WM_STATE_HIDDEN)
  355. {
  356. if(set == _NET_WM_STATE_REMOVE)
  357. client_set_minimized(L, -1, false);
  358. else if(set == _NET_WM_STATE_ADD)
  359. client_set_minimized(L, -1, true);
  360. else if(set == _NET_WM_STATE_TOGGLE)
  361. client_set_minimized(L, -1, !c->minimized);
  362. }
  363. else if(state == _NET_WM_STATE_DEMANDS_ATTENTION)
  364. {
  365. if(set == _NET_WM_STATE_REMOVE) {
  366. lua_pushboolean(L, false);
  367. luaA_object_emit_signal(L, -2, "request::urgent", 1);
  368. }
  369. else if(set == _NET_WM_STATE_ADD) {
  370. lua_pushboolean(L, true);
  371. luaA_object_emit_signal(L, -2, "request::urgent", 1);
  372. }
  373. else if(set == _NET_WM_STATE_TOGGLE) {
  374. lua_pushboolean(L, !c->urgent);
  375. luaA_object_emit_signal(L, -2, "request::urgent", 1);
  376. }
  377. }
  378. lua_pop(L, 1);
  379. }
  380. static void
  381. ewmh_process_desktop(client_t *c, uint32_t desktop)
  382. {
  383. lua_State *L = globalconf_get_lua_State();
  384. int idx = desktop;
  385. if(desktop == ALL_DESKTOPS)
  386. {
  387. luaA_object_push(L, c);
  388. lua_pushboolean(L, true);
  389. luaA_object_emit_signal(L, -2, "request::tag", 1);
  390. /* Pop the client, arguments are already popped */
  391. lua_pop(L, 1);
  392. }
  393. else if (idx >= 0 && idx < globalconf.tags.len)
  394. {
  395. luaA_object_push(L, c);
  396. luaA_object_push(L, globalconf.tags.tab[idx]);
  397. luaA_object_emit_signal(L, -2, "request::tag", 1);
  398. /* Pop the client, arguments are already popped */
  399. lua_pop(L, 1);
  400. }
  401. }
  402. int
  403. ewmh_process_client_message(xcb_client_message_event_t *ev)
  404. {
  405. client_t *c;
  406. if(ev->type == _NET_CURRENT_DESKTOP)
  407. {
  408. int idx = ev->data.data32[0];
  409. if (idx >= 0 && idx < globalconf.tags.len)
  410. {
  411. lua_State *L = globalconf_get_lua_State();
  412. luaA_object_push(L, globalconf.tags.tab[idx]);
  413. luaA_object_emit_signal(L, -1, "request::select", 0);
  414. lua_pop(L, 1);
  415. }
  416. }
  417. else if(ev->type == _NET_CLOSE_WINDOW)
  418. {
  419. if((c = client_getbywin(ev->window)))
  420. client_kill(c);
  421. }
  422. else if(ev->type == _NET_WM_DESKTOP)
  423. {
  424. if((c = client_getbywin(ev->window)))
  425. {
  426. ewmh_process_desktop(c, ev->data.data32[0]);
  427. }
  428. }
  429. else if(ev->type == _NET_WM_STATE)
  430. {
  431. if((c = client_getbywin(ev->window)))
  432. {
  433. ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[1], ev->data.data32[0]);
  434. if(ev->data.data32[2])
  435. ewmh_process_state_atom(c, (xcb_atom_t) ev->data.data32[2],
  436. ev->data.data32[0]);
  437. }
  438. }
  439. else if(ev->type == _NET_ACTIVE_WINDOW)
  440. {
  441. if((c = client_getbywin(ev->window))) {
  442. lua_State *L = globalconf_get_lua_State();
  443. luaA_object_push(L, c);
  444. lua_pushstring(L, "ewmh");
  445. /* Create table argument with raise=true. */
  446. lua_newtable(L);
  447. lua_pushstring(L, "raise");
  448. lua_pushboolean(L, true);
  449. lua_settable(L, -3);
  450. luaA_object_emit_signal(L, -3, "request::activate", 2);
  451. lua_pop(L, 1);
  452. }
  453. }
  454. return 0;
  455. }
  456. /** Update the client active desktop.
  457. * This is "wrong" since it can be on several tags, but EWMH has a strict view
  458. * of desktop system so just take the first tag.
  459. * \param c The client.
  460. */
  461. void
  462. ewmh_client_update_desktop(client_t *c)
  463. {
  464. int i;
  465. if(c->sticky)
  466. {
  467. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  468. c->window, _NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1,
  469. (uint32_t[]) { ALL_DESKTOPS });
  470. return;
  471. }
  472. for(i = 0; i < globalconf.tags.len; i++)
  473. if(is_client_tagged(c, globalconf.tags.tab[i]))
  474. {
  475. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  476. c->window, _NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &i);
  477. return;
  478. }
  479. /* It doesn't have any tags, remove the property */
  480. xcb_delete_property(globalconf.connection, c->window, _NET_WM_DESKTOP);
  481. }
  482. /** Update the client struts.
  483. * \param window The window to update the struts for.
  484. * \param strut The strut type to update the window with.
  485. */
  486. void
  487. ewmh_update_strut(xcb_window_t window, strut_t *strut)
  488. {
  489. if(window)
  490. {
  491. const uint32_t state[] =
  492. {
  493. strut->left,
  494. strut->right,
  495. strut->top,
  496. strut->bottom,
  497. strut->left_start_y,
  498. strut->left_end_y,
  499. strut->right_start_y,
  500. strut->right_end_y,
  501. strut->top_start_x,
  502. strut->top_end_x,
  503. strut->bottom_start_x,
  504. strut->bottom_end_x
  505. };
  506. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  507. window, _NET_WM_STRUT_PARTIAL, XCB_ATOM_CARDINAL, 32, countof(state), state);
  508. }
  509. }
  510. /** Update the window type.
  511. * \param window The window to update.
  512. * \param type The new type to set.
  513. */
  514. void
  515. ewmh_update_window_type(xcb_window_t window, uint32_t type)
  516. {
  517. xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
  518. window, _NET_WM_WINDOW_TYPE, XCB_ATOM_ATOM, 32, 1, &type);
  519. }
  520. void
  521. ewmh_client_check_hints(client_t *c)
  522. {
  523. xcb_atom_t *state;
  524. void *data = NULL;
  525. xcb_get_property_cookie_t c0, c1, c2;
  526. xcb_get_property_reply_t *reply;
  527. bool is_h_max = false;
  528. bool is_v_max = false;
  529. /* Send the GetProperty requests which will be processed later */
  530. c0 = xcb_get_property_unchecked(globalconf.connection, false, c->window,
  531. _NET_WM_DESKTOP, XCB_GET_PROPERTY_TYPE_ANY, 0, 1);
  532. c1 = xcb_get_property_unchecked(globalconf.connection, false, c->window,
  533. _NET_WM_STATE, XCB_ATOM_ATOM, 0, UINT32_MAX);
  534. c2 = xcb_get_property_unchecked(globalconf.connection, false, c->window,
  535. _NET_WM_WINDOW_TYPE, XCB_ATOM_ATOM, 0, UINT32_MAX);
  536. reply = xcb_get_property_reply(globalconf.connection, c0, NULL);
  537. if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
  538. {
  539. ewmh_process_desktop(c, *(uint32_t *) data);
  540. }
  541. p_delete(&reply);
  542. reply = xcb_get_property_reply(globalconf.connection, c1, NULL);
  543. if(reply && (data = xcb_get_property_value(reply)))
  544. {
  545. state = (xcb_atom_t *) data;
  546. for(int i = 0; i < xcb_get_property_value_length(reply) / ssizeof(xcb_atom_t); i++)
  547. if (state[i] == _NET_WM_STATE_MAXIMIZED_HORZ)
  548. is_h_max = true;
  549. else if (state[i] == _NET_WM_STATE_MAXIMIZED_VERT)
  550. is_v_max = true;
  551. else
  552. ewmh_process_state_atom(c, state[i], _NET_WM_STATE_ADD);
  553. }
  554. /* Check maximization manually */
  555. if (is_h_max && is_v_max) {
  556. lua_State *L = globalconf_get_lua_State();
  557. luaA_object_push(L, c);
  558. client_set_maximized(L, -1, true);
  559. lua_pop(L, 1);
  560. }
  561. else if(is_h_max)
  562. {
  563. lua_State *L = globalconf_get_lua_State();
  564. luaA_object_push(L, c);
  565. client_set_maximized_horizontal(L, -1, true);
  566. lua_pop(L, 1);
  567. }
  568. else if(is_v_max)
  569. {
  570. lua_State *L = globalconf_get_lua_State();
  571. luaA_object_push(L, c);
  572. client_set_maximized_vertical(L, -1, true);
  573. lua_pop(L, 1);
  574. }
  575. p_delete(&reply);
  576. reply = xcb_get_property_reply(globalconf.connection, c2, NULL);
  577. if(reply && (data = xcb_get_property_value(reply)))
  578. {
  579. c->has_NET_WM_WINDOW_TYPE = true;
  580. state = (xcb_atom_t *) data;
  581. for(int i = 0; i < xcb_get_property_value_length(reply) / ssizeof(xcb_atom_t); i++)
  582. if(state[i] == _NET_WM_WINDOW_TYPE_DESKTOP)
  583. c->type = MAX(c->type, WINDOW_TYPE_DESKTOP);
  584. else if(state[i] == _NET_WM_WINDOW_TYPE_DIALOG)
  585. c->type = MAX(c->type, WINDOW_TYPE_DIALOG);
  586. else if(state[i] == _NET_WM_WINDOW_TYPE_SPLASH)
  587. c->type = MAX(c->type, WINDOW_TYPE_SPLASH);
  588. else if(state[i] == _NET_WM_WINDOW_TYPE_DOCK)
  589. c->type = MAX(c->type, WINDOW_TYPE_DOCK);
  590. else if(state[i] == _NET_WM_WINDOW_TYPE_MENU)
  591. c->type = MAX(c->type, WINDOW_TYPE_MENU);
  592. else if(state[i] == _NET_WM_WINDOW_TYPE_TOOLBAR)
  593. c->type = MAX(c->type, WINDOW_TYPE_TOOLBAR);
  594. else if(state[i] == _NET_WM_WINDOW_TYPE_UTILITY)
  595. c->type = MAX(c->type, WINDOW_TYPE_UTILITY);
  596. } else
  597. c->has_NET_WM_WINDOW_TYPE = false;
  598. p_delete(&reply);
  599. }
  600. /** Process the WM strut of a client.
  601. * \param c The client.
  602. */
  603. void
  604. ewmh_process_client_strut(client_t *c)
  605. {
  606. void *data;
  607. xcb_get_property_reply_t *strut_r;
  608. xcb_get_property_cookie_t strut_q = xcb_get_property_unchecked(globalconf.connection, false, c->window,
  609. _NET_WM_STRUT_PARTIAL, XCB_ATOM_CARDINAL, 0, 12);
  610. strut_r = xcb_get_property_reply(globalconf.connection, strut_q, NULL);
  611. if(strut_r
  612. && strut_r->value_len
  613. && (data = xcb_get_property_value(strut_r)))
  614. {
  615. uint32_t *strut = data;
  616. if(c->strut.left != strut[0]
  617. || c->strut.right != strut[1]
  618. || c->strut.top != strut[2]
  619. || c->strut.bottom != strut[3]
  620. || c->strut.left_start_y != strut[4]
  621. || c->strut.left_end_y != strut[5]
  622. || c->strut.right_start_y != strut[6]
  623. || c->strut.right_end_y != strut[7]
  624. || c->strut.top_start_x != strut[8]
  625. || c->strut.top_end_x != strut[9]
  626. || c->strut.bottom_start_x != strut[10]
  627. || c->strut.bottom_end_x != strut[11])
  628. {
  629. c->strut.left = strut[0];
  630. c->strut.right = strut[1];
  631. c->strut.top = strut[2];
  632. c->strut.bottom = strut[3];
  633. c->strut.left_start_y = strut[4];
  634. c->strut.left_end_y = strut[5];
  635. c->strut.right_start_y = strut[6];
  636. c->strut.right_end_y = strut[7];
  637. c->strut.top_start_x = strut[8];
  638. c->strut.top_end_x = strut[9];
  639. c->strut.bottom_start_x = strut[10];
  640. c->strut.bottom_end_x = strut[11];
  641. lua_State *L = globalconf_get_lua_State();
  642. luaA_object_push(L, c);
  643. luaA_object_emit_signal(L, -1, "property::struts", 0);
  644. lua_pop(L, 1);
  645. }
  646. }
  647. p_delete(&strut_r);
  648. }
  649. /** Send request to get NET_WM_ICON (EWMH)
  650. * \param w The window.
  651. * \return The cookie associated with the request.
  652. */
  653. xcb_get_property_cookie_t
  654. ewmh_window_icon_get_unchecked(xcb_window_t w)
  655. {
  656. return xcb_get_property_unchecked(globalconf.connection, false, w,
  657. _NET_WM_ICON, XCB_ATOM_CARDINAL, 0, UINT32_MAX);
  658. }
  659. static cairo_surface_t *
  660. ewmh_window_icon_from_reply_next(uint32_t **data, uint32_t *data_end)
  661. {
  662. uint32_t width, height;
  663. uint64_t data_len;
  664. uint32_t *icon_data;
  665. if(data_end - *data <= 2)
  666. return NULL;
  667. width = (*data)[0];
  668. height = (*data)[1];
  669. /* Check that we have enough data, handling overflow */
  670. data_len = width * (uint64_t) height;
  671. if (width < 1 || height < 1 || data_len > (uint64_t) (data_end - *data) - 2)
  672. return NULL;
  673. icon_data = *data + 2;
  674. *data += 2 + data_len;
  675. return draw_surface_from_data(width, height, icon_data);
  676. }
  677. static cairo_surface_array_t
  678. ewmh_window_icon_from_reply(xcb_get_property_reply_t *r)
  679. {
  680. uint32_t *data, *data_end;
  681. cairo_surface_array_t result;
  682. cairo_surface_t *s;
  683. cairo_surface_array_init(&result);
  684. if(!r || r->type != XCB_ATOM_CARDINAL || r->format != 32)
  685. return result;
  686. data = (uint32_t*) xcb_get_property_value(r);
  687. data_end = &data[r->length];
  688. if(!data)
  689. return result;
  690. while ((s = ewmh_window_icon_from_reply_next(&data, data_end)) != NULL) {
  691. cairo_surface_array_push(&result, s);
  692. }
  693. return result;
  694. }
  695. /** Get NET_WM_ICON.
  696. * \param cookie The cookie.
  697. * \return An array of icons.
  698. */
  699. cairo_surface_array_t
  700. ewmh_window_icon_get_reply(xcb_get_property_cookie_t cookie)
  701. {
  702. xcb_get_property_reply_t *r = xcb_get_property_reply(globalconf.connection, cookie, NULL);
  703. cairo_surface_array_t result = ewmh_window_icon_from_reply(r);
  704. p_delete(&r);
  705. return result;
  706. }
  707. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80