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 17KB

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