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.

systray.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * systray.c - systray handling
  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 "systray.h"
  22. #include "common/atoms.h"
  23. #include "common/xutil.h"
  24. #include "objects/drawin.h"
  25. #include "xwindow.h"
  26. #include "globalconf.h"
  27. #include <xcb/xcb.h>
  28. #include <xcb/xcb_icccm.h>
  29. #include <xcb/xcb_atom.h>
  30. #define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
  31. /** Initialize systray information in X.
  32. */
  33. void
  34. systray_init(void)
  35. {
  36. xcb_intern_atom_cookie_t atom_systray_q;
  37. xcb_intern_atom_reply_t *atom_systray_r;
  38. char *atom_name;
  39. xcb_screen_t *xscreen = globalconf.screen;
  40. globalconf.systray.window = xcb_generate_id(globalconf.connection);
  41. globalconf.systray.background_pixel = xscreen->black_pixel;
  42. xcb_create_window(globalconf.connection, xscreen->root_depth,
  43. globalconf.systray.window,
  44. xscreen->root,
  45. -1, -1, 1, 1, 0,
  46. XCB_COPY_FROM_PARENT, xscreen->root_visual,
  47. XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, (const uint32_t [])
  48. { xscreen->black_pixel, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT });
  49. xwindow_set_class_instance(globalconf.systray.window);
  50. xwindow_set_name_static(globalconf.systray.window, "Awesome systray window");
  51. atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf.default_screen);
  52. if(!atom_name)
  53. fatal("error getting systray atom name");
  54. atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
  55. a_strlen(atom_name), atom_name);
  56. p_delete(&atom_name);
  57. atom_systray_r = xcb_intern_atom_reply(globalconf.connection, atom_systray_q, NULL);
  58. if(!atom_systray_r)
  59. fatal("error getting systray atom");
  60. globalconf.systray.atom = atom_systray_r->atom;
  61. p_delete(&atom_systray_r);
  62. }
  63. /** Register systray in X.
  64. */
  65. static void
  66. systray_register(void)
  67. {
  68. xcb_client_message_event_t ev;
  69. xcb_screen_t *xscreen = globalconf.screen;
  70. if(globalconf.systray.registered)
  71. return;
  72. globalconf.systray.registered = true;
  73. /* Fill event */
  74. p_clear(&ev, 1);
  75. ev.response_type = XCB_CLIENT_MESSAGE;
  76. ev.window = xscreen->root;
  77. ev.format = 32;
  78. ev.type = MANAGER;
  79. ev.data.data32[0] = globalconf.timestamp;
  80. ev.data.data32[1] = globalconf.systray.atom;
  81. ev.data.data32[2] = globalconf.systray.window;
  82. ev.data.data32[3] = ev.data.data32[4] = 0;
  83. xcb_set_selection_owner(globalconf.connection,
  84. globalconf.systray.window,
  85. globalconf.systray.atom,
  86. globalconf.timestamp);
  87. xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
  88. }
  89. /** Remove systray information in X.
  90. */
  91. void
  92. systray_cleanup(void)
  93. {
  94. if(!globalconf.systray.registered)
  95. return;
  96. globalconf.systray.registered = false;
  97. xcb_set_selection_owner(globalconf.connection,
  98. XCB_NONE,
  99. globalconf.systray.atom,
  100. globalconf.timestamp);
  101. xcb_unmap_window(globalconf.connection,
  102. globalconf.systray.window);
  103. }
  104. /** Handle a systray request.
  105. * \param embed_win The window to embed.
  106. * \return 0 on no error.
  107. */
  108. int
  109. systray_request_handle(xcb_window_t embed_win)
  110. {
  111. xembed_window_t em;
  112. xcb_get_property_cookie_t em_cookie;
  113. const uint32_t select_input_val[] =
  114. {
  115. XCB_EVENT_MASK_STRUCTURE_NOTIFY
  116. | XCB_EVENT_MASK_PROPERTY_CHANGE
  117. | XCB_EVENT_MASK_ENTER_WINDOW
  118. };
  119. /* check if not already trayed */
  120. if(xembed_getbywin(&globalconf.embedded, embed_win))
  121. return -1;
  122. p_clear(&em_cookie, 1);
  123. em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
  124. xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
  125. select_input_val);
  126. /* we grab the window, but also make sure it's automatically reparented back
  127. * to the root window if we should die.
  128. */
  129. xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, embed_win);
  130. xcb_reparent_window(globalconf.connection, embed_win,
  131. globalconf.systray.window,
  132. 0, 0);
  133. em.win = embed_win;
  134. if (!xembed_info_get_reply(globalconf.connection, em_cookie, &em.info)) {
  135. /* Set some sane defaults */
  136. em.info.version = XEMBED_VERSION;
  137. em.info.flags = XEMBED_MAPPED;
  138. }
  139. xembed_embedded_notify(globalconf.connection, em.win,
  140. globalconf.timestamp, globalconf.systray.window,
  141. MIN(XEMBED_VERSION, em.info.version));
  142. xembed_window_array_append(&globalconf.embedded, em);
  143. luaA_systray_invalidate();
  144. return 0;
  145. }
  146. /** Handle systray message.
  147. * \param ev The event.
  148. * \return 0 on no error.
  149. */
  150. int
  151. systray_process_client_message(xcb_client_message_event_t *ev)
  152. {
  153. int ret = 0;
  154. xcb_get_geometry_cookie_t geom_c;
  155. xcb_get_geometry_reply_t *geom_r;
  156. switch(ev->data.data32[1])
  157. {
  158. case SYSTEM_TRAY_REQUEST_DOCK:
  159. geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
  160. if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
  161. return -1;
  162. if(globalconf.screen->root == geom_r->root)
  163. ret = systray_request_handle(ev->data.data32[2]);
  164. p_delete(&geom_r);
  165. break;
  166. }
  167. return ret;
  168. }
  169. /** Check if a window is a KDE tray.
  170. * \param w The window to check.
  171. * \return True if it is, false otherwise.
  172. */
  173. bool
  174. systray_iskdedockapp(xcb_window_t w)
  175. {
  176. xcb_get_property_cookie_t kde_check_q;
  177. xcb_get_property_reply_t *kde_check;
  178. bool ret;
  179. /* Check if that is a KDE tray because it does not respect fdo standards,
  180. * thanks KDE. */
  181. kde_check_q = xcb_get_property_unchecked(globalconf.connection, false, w,
  182. _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
  183. XCB_ATOM_WINDOW, 0, 1);
  184. kde_check = xcb_get_property_reply(globalconf.connection, kde_check_q, NULL);
  185. /* it's a KDE systray ?*/
  186. ret = (kde_check && kde_check->value_len);
  187. p_delete(&kde_check);
  188. return ret;
  189. }
  190. /** Handle xembed client message.
  191. * \param ev The event.
  192. * \return 0 on no error.
  193. */
  194. int
  195. xembed_process_client_message(xcb_client_message_event_t *ev)
  196. {
  197. switch(ev->data.data32[1])
  198. {
  199. case XEMBED_REQUEST_FOCUS:
  200. xembed_focus_in(globalconf.connection, ev->window,
  201. globalconf.timestamp, XEMBED_FOCUS_CURRENT);
  202. break;
  203. }
  204. return 0;
  205. }
  206. static int
  207. systray_num_visible_entries(void)
  208. {
  209. int result = 0;
  210. foreach(em, globalconf.embedded)
  211. if (em->info.flags & XEMBED_MAPPED)
  212. result++;
  213. return result;
  214. }
  215. /** Inform lua that the systray needs to be updated.
  216. */
  217. void
  218. luaA_systray_invalidate(void)
  219. {
  220. lua_State *L = globalconf_get_lua_State();
  221. signal_object_emit(L, &global_signals, "systray::update", 0);
  222. /* Unmap now if the systray became empty */
  223. if(systray_num_visible_entries() == 0)
  224. xcb_unmap_window(globalconf.connection, globalconf.systray.window);
  225. }
  226. static void
  227. systray_update(int base_size, bool horizontal, bool reverse, int spacing, bool force_redraw)
  228. {
  229. if(base_size <= 0)
  230. return;
  231. /* Give the systray window the correct size */
  232. int num_entries = systray_num_visible_entries();
  233. uint32_t config_vals[4] = { base_size, base_size, 0, 0 };
  234. if(horizontal)
  235. config_vals[0] = base_size * num_entries + spacing * (num_entries - 1);
  236. else
  237. config_vals[1] = base_size * num_entries + spacing * (num_entries - 1);
  238. xcb_configure_window(globalconf.connection,
  239. globalconf.systray.window,
  240. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  241. config_vals);
  242. /* Now resize each embedded window */
  243. config_vals[0] = config_vals[1] = 0;
  244. config_vals[2] = config_vals[3] = base_size;
  245. for(int i = 0; i < globalconf.embedded.len; i++)
  246. {
  247. xembed_window_t *em;
  248. if(reverse)
  249. em = &globalconf.embedded.tab[(globalconf.embedded.len - i - 1)];
  250. else
  251. em = &globalconf.embedded.tab[i];
  252. if (!(em->info.flags & XEMBED_MAPPED))
  253. {
  254. xcb_unmap_window(globalconf.connection, em->win);
  255. continue;
  256. }
  257. xcb_configure_window(globalconf.connection, em->win,
  258. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  259. config_vals);
  260. xcb_map_window(globalconf.connection, em->win);
  261. if (force_redraw)
  262. xcb_clear_area(globalconf.connection, 1, em->win, 0, 0, 0, 0);
  263. if(horizontal)
  264. config_vals[0] += base_size + spacing;
  265. else
  266. config_vals[1] += base_size + spacing;
  267. }
  268. }
  269. /** Update the systray
  270. * \param L The Lua VM state.
  271. * \return The number of elements pushed on stack.
  272. * \luastack
  273. * \lparam The drawin to display the systray in.
  274. * \lparam x X position for the systray.
  275. * \lparam y Y position for the systray.
  276. * \lparam base_size The size (width and height) each systray item gets.
  277. * \lparam horiz If true, the systray is horizontal, else vertical.
  278. * \lparam bg Color of the systray background.
  279. * \lparam revers If true, the systray icon order will be reversed, else default.
  280. * \lparam spacing The size of the spacing between icons.
  281. */
  282. int
  283. luaA_systray(lua_State *L)
  284. {
  285. systray_register();
  286. if(lua_gettop(L) == 1)
  287. luaA_drawin_systray_kickout(L);
  288. if(lua_gettop(L) > 1)
  289. {
  290. size_t bg_len;
  291. drawin_t *w = luaA_checkudata(L, 1, &drawin_class);
  292. int x = round(luaA_checknumber_range(L, 2, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
  293. int y = round(luaA_checknumber_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
  294. int base_size = ceil(luaA_checknumber_range(L, 4, MIN_X11_SIZE, MAX_X11_SIZE));
  295. bool horiz = lua_toboolean(L, 5);
  296. const char *bg = luaL_checklstring(L, 6, &bg_len);
  297. bool revers = lua_toboolean(L, 7);
  298. int spacing = ceil(luaA_checknumber_range(L, 8, 0, MAX_X11_COORDINATE));
  299. color_t bg_color;
  300. bool force_redraw = false;
  301. if(color_init_reply(color_init_unchecked(&bg_color, bg, bg_len, globalconf.default_visual))
  302. && globalconf.systray.background_pixel != bg_color.pixel)
  303. {
  304. uint32_t config_back[] = { bg_color.pixel };
  305. globalconf.systray.background_pixel = bg_color.pixel;
  306. xcb_change_window_attributes(globalconf.connection,
  307. globalconf.systray.window,
  308. XCB_CW_BACK_PIXEL, config_back);
  309. xcb_clear_area(globalconf.connection, 1, globalconf.systray.window, 0, 0, 0, 0);
  310. force_redraw = true;
  311. }
  312. if(globalconf.systray.parent != w)
  313. xcb_reparent_window(globalconf.connection,
  314. globalconf.systray.window,
  315. w->window,
  316. x, y);
  317. else
  318. {
  319. uint32_t config_vals[2] = { x, y };
  320. xcb_configure_window(globalconf.connection,
  321. globalconf.systray.window,
  322. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
  323. config_vals);
  324. }
  325. globalconf.systray.parent = w;
  326. if(systray_num_visible_entries() != 0)
  327. {
  328. systray_update(base_size, horiz, revers, spacing, force_redraw);
  329. xcb_map_window(globalconf.connection,
  330. globalconf.systray.window);
  331. }
  332. }
  333. lua_pushinteger(L, systray_num_visible_entries());
  334. luaA_object_push(L, globalconf.systray.parent);
  335. return 2;
  336. }
  337. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80