systray.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include "wmfs.h"
  6. #include "systray.h"
  7. #include "ewmh.h"
  8. #include "infobar.h"
  9. #define SYSTRAY_SPACING (2)
  10. void
  11. systray_acquire(void)
  12. {
  13. Window w = 0;
  14. XSetWindowAttributes wattr =
  15. {
  16. .event_mask = ButtonPressMask | ExposureMask,
  17. .override_redirect = true,
  18. .background_pixmap = ParentRelative,
  19. .background_pixel = W->systray.infobar->theme->bars.bg,
  20. };
  21. if(!(W->flags & WMFS_SYSTRAY) || W->systray.win)
  22. return;
  23. if(XGetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s]) != None)
  24. {
  25. warnx("Can't initialize system tray: owned by another process.");
  26. return;
  27. }
  28. SLIST_INIT(&W->systray.head);
  29. /* Init systray window */
  30. w = XCreateSimpleWindow(W->dpy, W->systray.barwin->win, 0, 0,
  31. W->systray.barwin->geo.h, W->systray.barwin->geo.h, 0, 0, 0);
  32. XChangeWindowAttributes(W->dpy, w, CWEventMask | CWOverrideRedirect | CWBackPixel, &wattr);
  33. XSelectInput(W->dpy, w, KeyPressMask | ButtonPressMask);
  34. XMapRaised(W->dpy, w);
  35. XSetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s], w, CurrentTime);
  36. if(XGetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s]) != w)
  37. {
  38. warnl("System tray: can't get systray manager");
  39. systray_freeicons();
  40. return;
  41. }
  42. ewmh_send_message(W->root, W->root, "MANAGER", CurrentTime,
  43. W->net_atom[net_system_tray_s], w, 0, 0);
  44. XSync(W->dpy, false);
  45. W->systray.win = w;
  46. }
  47. void
  48. systray_add(Window win)
  49. {
  50. struct _systray *s;
  51. if(!(W->flags & WMFS_SYSTRAY))
  52. return;
  53. s = xcalloc(1, sizeof(struct _systray));
  54. s->win = win;
  55. s->geo.h = W->systray.barwin->geo.h;
  56. s->geo.w = W->systray.barwin->geo.h + SYSTRAY_SPACING;
  57. ewmh_set_wm_state(s->win, NormalState);
  58. XSelectInput(W->dpy, s->win, StructureNotifyMask | PropertyChangeMask| EnterWindowMask | FocusChangeMask);
  59. XReparentWindow(W->dpy, s->win, W->systray.win, 0, 0);
  60. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime,
  61. XEMBED_EMBEDDED_NOTIFY, 0, W->systray.win, 0);
  62. SLIST_INSERT_HEAD(&W->systray.head, s, next);
  63. W->systray.redim = true;
  64. }
  65. void
  66. systray_del(struct _systray *s)
  67. {
  68. if(!(W->flags & WMFS_SYSTRAY))
  69. return;
  70. SLIST_REMOVE(&W->systray.head, s, _systray, next);
  71. free(s);
  72. W->systray.redim = true;
  73. }
  74. void
  75. systray_state(struct _systray *s)
  76. {
  77. long flags;
  78. int code = 0;
  79. if(!(W->flags & WMFS_SYSTRAY) || !(flags = ewmh_get_xembed_state(s->win)))
  80. return;
  81. if(flags & XEMBED_MAPPED)
  82. {
  83. code = XEMBED_WINDOW_ACTIVATE;
  84. XMapRaised(W->dpy, s->win);
  85. ewmh_set_wm_state(s->win, NormalState);
  86. }
  87. else
  88. {
  89. code = XEMBED_WINDOW_DEACTIVATE;
  90. XUnmapWindow(W->dpy, s->win);
  91. ewmh_set_wm_state(s->win, WithdrawnState);
  92. }
  93. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime, code, 0, 0, 0);
  94. }
  95. void
  96. systray_freeicons(void)
  97. {
  98. struct _systray *i;
  99. if(!(W->flags & WMFS_SYSTRAY))
  100. return;
  101. while(!SLIST_EMPTY(&W->systray.head))
  102. {
  103. i = SLIST_FIRST(&W->systray.head);
  104. SLIST_REMOVE_HEAD(&W->systray.head, next);
  105. XUnmapWindow(W->dpy, i->win);
  106. XReparentWindow(W->dpy, i->win, W->root, 0, 0);
  107. free(i);
  108. }
  109. XSetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s], None, CurrentTime);
  110. W->systray.barwin->geo.w = 0;
  111. infobar_elem_reinit(W->systray.infobar);
  112. XSync(W->dpy, false);
  113. }
  114. struct _systray*
  115. systray_find(Window win)
  116. {
  117. struct _systray *i;
  118. if(!(W->flags & WMFS_SYSTRAY))
  119. return NULL;
  120. SLIST_FOREACH(i, &W->systray.head, next)
  121. if(i->win == win)
  122. return i;
  123. return NULL;
  124. }
  125. int
  126. systray_get_width(void)
  127. {
  128. int w = 1;
  129. struct _systray *i;
  130. SLIST_FOREACH(i, &W->systray.head, next)
  131. w += i->geo.w + SYSTRAY_SPACING;
  132. return w;
  133. }
  134. void
  135. systray_update(void)
  136. {
  137. int x = 1;
  138. struct _systray *i;
  139. if(!(W->flags & WMFS_SYSTRAY))
  140. return;
  141. if(W->systray.redim)
  142. {
  143. W->systray.redim = false;
  144. infobar_elem_reinit(W->systray.infobar);
  145. }
  146. SLIST_FOREACH(i, &W->systray.head, next)
  147. {
  148. XMapWindow(W->dpy, i->win);
  149. XMoveResizeWindow(W->dpy, i->win, (i->geo.x = x), 0, i->geo.w, i->geo.h);
  150. x += i->geo.w + SYSTRAY_SPACING;
  151. }
  152. }