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.

property.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * property.c - property handlers
  3. *
  4. * Copyright © 2008-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 "property.h"
  22. #include "common/atoms.h"
  23. #include "common/xutil.h"
  24. #include "ewmh.h"
  25. #include "objects/client.h"
  26. #include "objects/drawin.h"
  27. #include "xwindow.h"
  28. #include <xcb/xcb_atom.h>
  29. #define HANDLE_TEXT_PROPERTY(funcname, atom, setfunc) \
  30. xcb_get_property_cookie_t \
  31. property_get_##funcname(client_t *c) \
  32. { \
  33. return xcb_get_property(globalconf.connection, \
  34. false, \
  35. c->window, \
  36. atom, \
  37. XCB_GET_PROPERTY_TYPE_ANY, \
  38. 0, \
  39. UINT_MAX); \
  40. } \
  41. void \
  42. property_update_##funcname(client_t *c, xcb_get_property_cookie_t cookie) \
  43. { \
  44. lua_State *L = globalconf_get_lua_State(); \
  45. xcb_get_property_reply_t * reply = \
  46. xcb_get_property_reply(globalconf.connection, cookie, NULL); \
  47. luaA_object_push(L, c); \
  48. setfunc(L, -1, xutil_get_text_property_from_reply(reply)); \
  49. lua_pop(L, 1); \
  50. p_delete(&reply); \
  51. } \
  52. static int \
  53. property_handle_##funcname(uint8_t state, \
  54. xcb_window_t window) \
  55. { \
  56. client_t *c = client_getbywin(window); \
  57. if(c) \
  58. property_update_##funcname(c, property_get_##funcname(c));\
  59. return 0; \
  60. }
  61. HANDLE_TEXT_PROPERTY(wm_name, XCB_ATOM_WM_NAME, client_set_alt_name)
  62. HANDLE_TEXT_PROPERTY(net_wm_name, _NET_WM_NAME, client_set_name)
  63. HANDLE_TEXT_PROPERTY(wm_icon_name, XCB_ATOM_WM_ICON_NAME, client_set_alt_icon_name)
  64. HANDLE_TEXT_PROPERTY(net_wm_icon_name, _NET_WM_ICON_NAME, client_set_icon_name)
  65. HANDLE_TEXT_PROPERTY(wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, client_set_machine)
  66. HANDLE_TEXT_PROPERTY(wm_window_role, WM_WINDOW_ROLE, client_set_role)
  67. #undef HANDLE_TEXT_PROPERTY
  68. #define HANDLE_PROPERTY(name) \
  69. static int \
  70. property_handle_##name(uint8_t state, \
  71. xcb_window_t window) \
  72. { \
  73. client_t *c = client_getbywin(window); \
  74. if(c) \
  75. property_update_##name(c, property_get_##name(c));\
  76. return 0; \
  77. }
  78. HANDLE_PROPERTY(wm_protocols)
  79. HANDLE_PROPERTY(wm_transient_for)
  80. HANDLE_PROPERTY(wm_client_leader)
  81. HANDLE_PROPERTY(wm_normal_hints)
  82. HANDLE_PROPERTY(wm_hints)
  83. HANDLE_PROPERTY(wm_class)
  84. HANDLE_PROPERTY(net_wm_icon)
  85. HANDLE_PROPERTY(net_wm_pid)
  86. #undef HANDLE_PROPERTY
  87. xcb_get_property_cookie_t
  88. property_get_wm_transient_for(client_t *c)
  89. {
  90. return xcb_icccm_get_wm_transient_for_unchecked(globalconf.connection, c->window);
  91. }
  92. void
  93. property_update_wm_transient_for(client_t *c, xcb_get_property_cookie_t cookie)
  94. {
  95. lua_State *L = globalconf_get_lua_State();
  96. xcb_window_t trans;
  97. int counter;
  98. client_t *tc, *tmp;
  99. if(!xcb_icccm_get_wm_transient_for_reply(globalconf.connection,
  100. cookie,
  101. &trans, NULL))
  102. return;
  103. tmp = tc = client_getbywin(trans);
  104. luaA_object_push(L, c);
  105. client_set_type(L, -1, WINDOW_TYPE_DIALOG);
  106. client_set_above(L, -1, false);
  107. /* Verify that there are no loops in the transient_for relation after we are done */
  108. for(counter = 0; tmp != NULL && counter <= globalconf.stack.len; counter++)
  109. {
  110. if (tmp == c)
  111. /* We arrived back at the client we started from, so there is a loop */
  112. counter = globalconf.stack.len+1;
  113. tmp = tmp->transient_for;
  114. }
  115. if (counter <= globalconf.stack.len)
  116. client_set_transient_for(L, -1, tc);
  117. lua_pop(L, 1);
  118. }
  119. xcb_get_property_cookie_t
  120. property_get_wm_client_leader(client_t *c)
  121. {
  122. return xcb_get_property_unchecked(globalconf.connection, false, c->window,
  123. WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32);
  124. }
  125. /** Update leader hint of a client.
  126. * \param c The client.
  127. * \param cookie Cookie returned by property_get_wm_client_leader.
  128. */
  129. void
  130. property_update_wm_client_leader(client_t *c, xcb_get_property_cookie_t cookie)
  131. {
  132. xcb_get_property_reply_t *reply;
  133. void *data;
  134. reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
  135. if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
  136. c->leader_window = *(xcb_window_t *) data;
  137. p_delete(&reply);
  138. }
  139. xcb_get_property_cookie_t
  140. property_get_wm_normal_hints(client_t *c)
  141. {
  142. return xcb_icccm_get_wm_normal_hints_unchecked(globalconf.connection, c->window);
  143. }
  144. /** Update the size hints of a client.
  145. * \param c The client.
  146. * \param cookie Cookie returned by property_get_wm_normal_hints.
  147. */
  148. void
  149. property_update_wm_normal_hints(client_t *c, xcb_get_property_cookie_t cookie)
  150. {
  151. xcb_icccm_get_wm_normal_hints_reply(globalconf.connection,
  152. cookie,
  153. &c->size_hints, NULL);
  154. }
  155. xcb_get_property_cookie_t
  156. property_get_wm_hints(client_t *c)
  157. {
  158. return xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
  159. }
  160. /** Update the WM hints of a client.
  161. * \param c The client.
  162. * \param cookie Cookie returned by property_get_wm_hints.
  163. */
  164. void
  165. property_update_wm_hints(client_t *c, xcb_get_property_cookie_t cookie)
  166. {
  167. lua_State *L = globalconf_get_lua_State();
  168. xcb_icccm_wm_hints_t wmh;
  169. if(!xcb_icccm_get_wm_hints_reply(globalconf.connection,
  170. cookie,
  171. &wmh, NULL))
  172. return;
  173. luaA_object_push(L, c);
  174. client_set_urgent(L, -1, xcb_icccm_wm_hints_get_urgency(&wmh));
  175. if(wmh.flags & XCB_ICCCM_WM_HINT_INPUT)
  176. c->nofocus = !wmh.input;
  177. if(wmh.flags & XCB_ICCCM_WM_HINT_WINDOW_GROUP)
  178. client_set_group_window(L, -1, wmh.window_group);
  179. if(!c->have_ewmh_icon)
  180. {
  181. if(wmh.flags & XCB_ICCCM_WM_HINT_ICON_PIXMAP)
  182. {
  183. if(wmh.flags & XCB_ICCCM_WM_HINT_ICON_MASK)
  184. client_set_icon_from_pixmaps(c, wmh.icon_pixmap, wmh.icon_mask);
  185. else
  186. client_set_icon_from_pixmaps(c, wmh.icon_pixmap, XCB_NONE);
  187. }
  188. else
  189. client_set_icon(c, NULL);
  190. }
  191. lua_pop(L, 1);
  192. }
  193. xcb_get_property_cookie_t
  194. property_get_wm_class(client_t *c)
  195. {
  196. return xcb_icccm_get_wm_class_unchecked(globalconf.connection, c->window);
  197. }
  198. /** Update WM_CLASS of a client.
  199. * \param c The client.
  200. * \param cookie Cookie returned by property_get_wm_class.
  201. */
  202. void
  203. property_update_wm_class(client_t *c, xcb_get_property_cookie_t cookie)
  204. {
  205. lua_State *L = globalconf_get_lua_State();
  206. xcb_icccm_get_wm_class_reply_t hint;
  207. if(!xcb_icccm_get_wm_class_reply(globalconf.connection,
  208. cookie,
  209. &hint, NULL))
  210. return;
  211. luaA_object_push(L, c);
  212. client_set_class_instance(L, -1, hint.class_name, hint.instance_name);
  213. lua_pop(L, 1);
  214. xcb_icccm_get_wm_class_reply_wipe(&hint);
  215. }
  216. static int
  217. property_handle_net_wm_strut_partial(uint8_t state,
  218. xcb_window_t window)
  219. {
  220. client_t *c = client_getbywin(window);
  221. if(c)
  222. ewmh_process_client_strut(c);
  223. return 0;
  224. }
  225. xcb_get_property_cookie_t
  226. property_get_net_wm_icon(client_t *c)
  227. {
  228. return ewmh_window_icon_get_unchecked(c->window);
  229. }
  230. void
  231. property_update_net_wm_icon(client_t *c, xcb_get_property_cookie_t cookie)
  232. {
  233. cairo_surface_t *surface = ewmh_window_icon_get_reply(cookie);
  234. if(!surface)
  235. return;
  236. c->have_ewmh_icon = true;
  237. client_set_icon(c, surface);
  238. cairo_surface_destroy(surface);
  239. }
  240. xcb_get_property_cookie_t
  241. property_get_net_wm_pid(client_t *c)
  242. {
  243. return xcb_get_property_unchecked(globalconf.connection, false, c->window, _NET_WM_PID, XCB_ATOM_CARDINAL, 0L, 1L);
  244. }
  245. void
  246. property_update_net_wm_pid(client_t *c, xcb_get_property_cookie_t cookie)
  247. {
  248. xcb_get_property_reply_t *reply;
  249. reply = xcb_get_property_reply(globalconf.connection, cookie, NULL);
  250. if(reply && reply->value_len)
  251. {
  252. uint32_t *rdata = xcb_get_property_value(reply);
  253. if(rdata)
  254. {
  255. lua_State *L = globalconf_get_lua_State();
  256. luaA_object_push(L, c);
  257. client_set_pid(L, -1, *rdata);
  258. lua_pop(L, 1);
  259. }
  260. }
  261. p_delete(&reply);
  262. }
  263. xcb_get_property_cookie_t
  264. property_get_wm_protocols(client_t *c)
  265. {
  266. return xcb_icccm_get_wm_protocols_unchecked(globalconf.connection,
  267. c->window, WM_PROTOCOLS);
  268. }
  269. /** Update the list of supported protocols for a client.
  270. * \param c The client.
  271. * \param cookie Cookie from property_get_wm_protocols.
  272. */
  273. void
  274. property_update_wm_protocols(client_t *c, xcb_get_property_cookie_t cookie)
  275. {
  276. xcb_icccm_get_wm_protocols_reply_t protocols;
  277. /* If this fails for any reason, we still got the old value */
  278. if(!xcb_icccm_get_wm_protocols_reply(globalconf.connection,
  279. cookie,
  280. &protocols, NULL))
  281. return;
  282. xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
  283. memcpy(&c->protocols, &protocols, sizeof(protocols));
  284. }
  285. /** The property notify event handler.
  286. * \param state currently unused
  287. * \param window The window to obtain update the property with.
  288. */
  289. static int
  290. property_handle_xembed_info(uint8_t state,
  291. xcb_window_t window)
  292. {
  293. xembed_window_t *emwin = xembed_getbywin(&globalconf.embedded, window);
  294. if(emwin)
  295. {
  296. xcb_get_property_cookie_t cookie =
  297. xcb_get_property(globalconf.connection, 0, window, _XEMBED_INFO,
  298. XCB_GET_PROPERTY_TYPE_ANY, 0, 3);
  299. xcb_get_property_reply_t *propr =
  300. xcb_get_property_reply(globalconf.connection, cookie, 0);
  301. xembed_property_update(globalconf.connection, emwin, propr);
  302. p_delete(&propr);
  303. }
  304. return 0;
  305. }
  306. static int
  307. property_handle_net_wm_opacity(uint8_t state,
  308. xcb_window_t window)
  309. {
  310. lua_State *L = globalconf_get_lua_State();
  311. drawin_t *drawin = drawin_getbywin(window);
  312. if(drawin)
  313. {
  314. luaA_object_push(L, drawin);
  315. window_set_opacity(L, -1, xwindow_get_opacity(drawin->window));
  316. lua_pop(L, -1);
  317. }
  318. else
  319. {
  320. client_t *c = client_getbywin(window);
  321. if(c)
  322. {
  323. luaA_object_push(L, c);
  324. window_set_opacity(L, -1, xwindow_get_opacity(c->window));
  325. lua_pop(L, 1);
  326. }
  327. }
  328. return 0;
  329. }
  330. static int
  331. property_handle_xrootpmap_id(uint8_t state,
  332. xcb_window_t window)
  333. {
  334. lua_State *L = globalconf_get_lua_State();
  335. signal_object_emit(L, &global_signals, "wallpaper_changed", 0);
  336. return 0;
  337. }
  338. /** The property notify event handler handling xproperties.
  339. * \param ev The event.
  340. */
  341. static void
  342. property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev)
  343. {
  344. lua_State *L = globalconf_get_lua_State();
  345. xproperty_t *prop;
  346. xproperty_t lookup = { .atom = ev->atom };
  347. buffer_t buf;
  348. void *obj;
  349. prop = xproperty_array_lookup(&globalconf.xproperties, &lookup);
  350. if(!prop)
  351. /* Property is not registered */
  352. return;
  353. if (ev->window != globalconf.screen->root)
  354. {
  355. obj = client_getbywin(ev->window);
  356. if(!obj)
  357. obj = drawin_getbywin(ev->window);
  358. if(!obj)
  359. return;
  360. } else
  361. obj = NULL;
  362. /* Get us the name of the property */
  363. buffer_inita(&buf, a_strlen(prop->name) + a_strlen("xproperty::") + 1);
  364. buffer_addf(&buf, "xproperty::%s", prop->name);
  365. /* And emit the right signal */
  366. if (obj)
  367. {
  368. luaA_object_push(L, obj);
  369. luaA_object_emit_signal(L, -1, buf.s, 0);
  370. lua_pop(L, 1);
  371. } else
  372. signal_object_emit(L, &global_signals, buf.s, 0);
  373. buffer_wipe(&buf);
  374. }
  375. /** The property notify event handler.
  376. * \param ev The event.
  377. */
  378. void
  379. property_handle_propertynotify(xcb_property_notify_event_t *ev)
  380. {
  381. int (*handler)(uint8_t state,
  382. xcb_window_t window) = NULL;
  383. globalconf.timestamp = ev->time;
  384. property_handle_propertynotify_xproperty(ev);
  385. /* Find the correct event handler */
  386. #define HANDLE(atom_, cb) \
  387. if (ev->atom == atom_) \
  388. { \
  389. handler = cb; \
  390. } else
  391. #define END return
  392. /* Xembed stuff */
  393. HANDLE(_XEMBED_INFO, property_handle_xembed_info)
  394. /* ICCCM stuff */
  395. HANDLE(XCB_ATOM_WM_TRANSIENT_FOR, property_handle_wm_transient_for)
  396. HANDLE(WM_CLIENT_LEADER, property_handle_wm_client_leader)
  397. HANDLE(XCB_ATOM_WM_NORMAL_HINTS, property_handle_wm_normal_hints)
  398. HANDLE(XCB_ATOM_WM_HINTS, property_handle_wm_hints)
  399. HANDLE(XCB_ATOM_WM_NAME, property_handle_wm_name)
  400. HANDLE(XCB_ATOM_WM_ICON_NAME, property_handle_wm_icon_name)
  401. HANDLE(XCB_ATOM_WM_CLASS, property_handle_wm_class)
  402. HANDLE(WM_PROTOCOLS, property_handle_wm_protocols)
  403. HANDLE(XCB_ATOM_WM_CLIENT_MACHINE, property_handle_wm_client_machine)
  404. HANDLE(WM_WINDOW_ROLE, property_handle_wm_window_role)
  405. /* EWMH stuff */
  406. HANDLE(_NET_WM_NAME, property_handle_net_wm_name)
  407. HANDLE(_NET_WM_ICON_NAME, property_handle_net_wm_icon_name)
  408. HANDLE(_NET_WM_STRUT_PARTIAL, property_handle_net_wm_strut_partial)
  409. HANDLE(_NET_WM_ICON, property_handle_net_wm_icon)
  410. HANDLE(_NET_WM_PID, property_handle_net_wm_pid)
  411. HANDLE(_NET_WM_WINDOW_OPACITY, property_handle_net_wm_opacity)
  412. /* background change */
  413. HANDLE(_XROOTPMAP_ID, property_handle_xrootpmap_id)
  414. /* If nothing was found, return */
  415. END;
  416. #undef HANDLE
  417. #undef END
  418. (*handler)(ev->state, ev->window);
  419. }
  420. /** Register a new xproperty.
  421. * \param L The Lua VM state.
  422. * \return The number of elements pushed on stack.
  423. * \luastack
  424. * \lparam The name of the X11 property
  425. * \lparam One of "string", "number" or "boolean"
  426. */
  427. int
  428. luaA_register_xproperty(lua_State *L)
  429. {
  430. const char *name;
  431. struct xproperty property;
  432. struct xproperty *found;
  433. const char *const args[] = { "string", "number", "boolean" };
  434. xcb_intern_atom_reply_t *atom_r;
  435. int type;
  436. name = luaL_checkstring(L, 1);
  437. type = luaL_checkoption(L, 2, NULL, args);
  438. if (type == 0)
  439. property.type = PROP_STRING;
  440. else if (type == 1)
  441. property.type = PROP_NUMBER;
  442. else
  443. property.type = PROP_BOOLEAN;
  444. atom_r = xcb_intern_atom_reply(globalconf.connection,
  445. xcb_intern_atom_unchecked(globalconf.connection, false,
  446. a_strlen(name), name),
  447. NULL);
  448. if(!atom_r)
  449. return 0;
  450. property.atom = atom_r->atom;
  451. p_delete(&atom_r);
  452. found = xproperty_array_lookup(&globalconf.xproperties, &property);
  453. if(found)
  454. {
  455. /* Property already registered */
  456. if(found->type != property.type)
  457. return luaL_error(L, "xproperty '%s' already registered with different type", name);
  458. }
  459. else
  460. {
  461. buffer_t buf;
  462. buffer_inita(&buf, a_strlen(name) + a_strlen("xproperty::") + 1);
  463. buffer_addf(&buf, "xproperty::%s", name);
  464. property.name = a_strdup(name);
  465. xproperty_array_insert(&globalconf.xproperties, property);
  466. signal_add(&window_class.signals, buf.s);
  467. signal_add(&global_signals, buf.s);
  468. buffer_wipe(&buf);
  469. }
  470. return 0;
  471. }
  472. /** Set an xproperty.
  473. * \param L The Lua VM state.
  474. * \return The number of elements pushed on stack.
  475. */
  476. int
  477. luaA_set_xproperty(lua_State *L)
  478. {
  479. return window_set_xproperty(L, globalconf.screen->root, 1, 2);
  480. }
  481. /** Get an xproperty.
  482. * \param L The Lua VM state.
  483. * \return The number of elements pushed on stack.
  484. */
  485. int
  486. luaA_get_xproperty(lua_State *L)
  487. {
  488. return window_get_xproperty(L, globalconf.screen->root, 1);
  489. }
  490. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80