systray.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. bool
  8. systray_acquire(void)
  9. {
  10. XSetWindowAttributes wattr =
  11. {
  12. .event_mask = ButtonPressMask | ExposureMask,
  13. .override_redirect = true,
  14. .background_pixmap = ParentRelative,
  15. .background_pixel = 0
  16. };
  17. if(!(W->flags & WMFS_SYSTRAY) || W->systray.win)
  18. return false;
  19. if(XGetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s]) != None)
  20. {
  21. warnx("Can't initialize system tray: owned by another process.");
  22. return False;
  23. }
  24. /* Init traywin window */
  25. XChangeWindowAttributes(W->dpy, W->systray.win, CWEventMask | CWOverrideRedirect | CWBackPixel, &wattr);
  26. XSelectInput(W->dpy, W->systray.win, KeyPressMask | ButtonPressMask);
  27. XMapRaised(W->dpy, W->systray.win);
  28. XSetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s], W->systray.win, CurrentTime);
  29. if(XGetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s]) != W->systray.win)
  30. {
  31. systray_freeicons();
  32. warnx("System tray: can't get systray manager");
  33. return false;
  34. }
  35. ewmh_send_message(W->root, W->root, "MANAGER", CurrentTime,
  36. W->net_atom[net_system_tray_s], W->systray.win, 0, 0);
  37. XSync(dpy, false);
  38. return true;
  39. }
  40. void
  41. systray_add(Window win)
  42. {
  43. struct _systray *s;
  44. if(!(W->flags & WMFS_SYSTRAY))
  45. return;
  46. s = xcalloc(1, sizeof(struct _systray));
  47. s->win = win;
  48. s->geo.h = W->systray.barwin->geo.h;
  49. s->geo.w = W->systray.barwin->geo.w + 2;
  50. ewmh_set_win_state(s->win, WithdrawnState);
  51. XSelectInput(W->dpy, s->win, StructureNotifyMask | PropertyChangeMask| EnterWindowMask | FocusChangeMask);
  52. XReparentWindow(W->dpy, s->win, W->systray.win, 0, 0);
  53. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime,
  54. XEMBED_EMBEDDED_NOTIFY, 0, W->systray,win, 0);
  55. SLIST_INSERT_HEAD(&W->systray.head, s, next);
  56. }
  57. void
  58. systray_del(struct _systray *s)
  59. {
  60. if(!(W->flags & WMFS_SYSTRAY))
  61. return;
  62. SLIST_REMOVE(&W->systray.head, s, _systray, next);
  63. free(s);
  64. }
  65. void
  66. systray_state(struct _systray *s)
  67. {
  68. long flags;
  69. int code = 0;
  70. if(!(W->flags & WMFS_SYSTRAY) || !(flags = ewmh_get_xembed_state(s->win)))
  71. return;
  72. if(flags & XEMBED_MAPPED)
  73. {
  74. code = XEMBED_WINDOW_ACTIVATE;
  75. XMapRaised(W->dpy, s->win);
  76. ewmh_set_win_state(s->win, NormalState);
  77. }
  78. else
  79. {
  80. code = XEMBED_WINDOW_DEACTIVATE;
  81. XUnmapWindow(W->dpy, s->win);
  82. ewmh_set_win_state(s->win, WithdrawnState);
  83. }
  84. ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime, code, 0, 0, 0);
  85. }
  86. void
  87. systray_freeicons(void)
  88. {
  89. struct _systray *i;
  90. if(!(W->flags & WMFS_SYSTRAY))
  91. return;
  92. while(!SLIST_EMPTY(&W->systray.head))
  93. {
  94. i = SLIST_FIRST(&W->systray.head);
  95. SLIST_REMOVE_HEAD(&W->systray.head, next);
  96. XUnmapWindow(dpy, i->win);
  97. XReparentWindow(dpy, i->win, W->root, 0, 0);
  98. free(i);
  99. }
  100. XSetSelectionOwner(W->dpy, W->net_atom[net_system_tray_s], None, CurrentTime);
  101. XDestroyWindow(W->dpy, W->systray.win);
  102. XSync(W->dpy, false);
  103. }
  104. struct _systray*
  105. systray_find(Window win)
  106. {
  107. struct _systray *i;
  108. if(!(W->flags & WMFS_SYSTRAY))
  109. return NULL;
  110. SLIST_FOREACH(i, &W->systray.head, next)
  111. if(i->win == win)
  112. return i;
  113. return NULL;
  114. }