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.

218 lines
3.9 KiB

  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <X11/keysym.h>
  9. #include <X11/Xatom.h>
  10. #include "wm.h"
  11. /* local functions */
  12. static void buttonpress(XEvent *e);
  13. static void configurerequest(XEvent *e);
  14. static void destroynotify(XEvent *e);
  15. static void enternotify(XEvent *e);
  16. static void leavenotify(XEvent *e);
  17. static void expose(XEvent *e);
  18. static void keymapnotify(XEvent *e);
  19. static void maprequest(XEvent *e);
  20. static void propertynotify(XEvent *e);
  21. static void unmapnotify(XEvent *e);
  22. void (*handler[LASTEvent]) (XEvent *) = {
  23. [ButtonPress] = buttonpress,
  24. [ConfigureRequest] = configurerequest,
  25. [DestroyNotify] = destroynotify,
  26. [EnterNotify] = enternotify,
  27. [LeaveNotify] = leavenotify,
  28. [Expose] = expose,
  29. [KeyPress] = keypress,
  30. [KeymapNotify] = keymapnotify,
  31. [MapRequest] = maprequest,
  32. [PropertyNotify] = propertynotify,
  33. [UnmapNotify] = unmapnotify
  34. };
  35. void
  36. discard_events(long even_mask)
  37. {
  38. XEvent ev;
  39. while(XCheckMaskEvent(dpy, even_mask, &ev));
  40. }
  41. static void
  42. buttonpress(XEvent *e)
  43. {
  44. XButtonPressedEvent *ev = &e->xbutton;
  45. Client *c;
  46. if((c = getclient(ev->window))) {
  47. raise(c);
  48. switch(ev->button) {
  49. default:
  50. break;
  51. case Button1:
  52. mmove(c);
  53. break;
  54. case Button2:
  55. lower(c);
  56. break;
  57. case Button3:
  58. mresize(c);
  59. break;
  60. }
  61. }
  62. }
  63. static void
  64. configurerequest(XEvent *e)
  65. {
  66. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  67. XWindowChanges wc;
  68. Client *c;
  69. ev->value_mask &= ~CWSibling;
  70. if((c = getclient(ev->window))) {
  71. if(ev->value_mask & CWX)
  72. c->x = ev->x;
  73. if(ev->value_mask & CWY)
  74. c->y = ev->y;
  75. if(ev->value_mask & CWWidth)
  76. c->w = ev->width;
  77. if(ev->value_mask & CWHeight)
  78. c->h = ev->height;
  79. }
  80. wc.x = ev->x;
  81. wc.y = ev->y;
  82. wc.width = ev->width;
  83. wc.height = ev->height;
  84. wc.border_width = 1;
  85. wc.sibling = None;
  86. wc.stack_mode = Above;
  87. ev->value_mask &= ~CWStackMode;
  88. ev->value_mask |= CWBorderWidth;
  89. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  90. XFlush(dpy);
  91. }
  92. static void
  93. destroynotify(XEvent *e)
  94. {
  95. Client *c;
  96. XDestroyWindowEvent *ev = &e->xdestroywindow;
  97. if((c = getclient(ev->window)))
  98. unmanage(c);
  99. }
  100. static void
  101. enternotify(XEvent *e)
  102. {
  103. XCrossingEvent *ev = &e->xcrossing;
  104. Client *c;
  105. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  106. return;
  107. if((c = getclient(ev->window)))
  108. focus(c);
  109. else if(ev->window == root)
  110. sel_screen = True;
  111. }
  112. static void
  113. leavenotify(XEvent *e)
  114. {
  115. XCrossingEvent *ev = &e->xcrossing;
  116. if((ev->window == root) && !ev->same_screen)
  117. sel_screen = True;
  118. }
  119. static void
  120. expose(XEvent *e)
  121. {
  122. XExposeEvent *ev = &e->xexpose;
  123. Client *c;
  124. if(ev->count == 0) {
  125. if((c = gettitle(ev->window)))
  126. draw_client(c);
  127. else if(ev->window == barwin)
  128. draw_bar();
  129. }
  130. }
  131. static void
  132. keymapnotify(XEvent *e)
  133. {
  134. update_keys();
  135. }
  136. static void
  137. maprequest(XEvent *e)
  138. {
  139. XMapRequestEvent *ev = &e->xmaprequest;
  140. static XWindowAttributes wa;
  141. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  142. return;
  143. if(wa.override_redirect) {
  144. XSelectInput(dpy, ev->window,
  145. (StructureNotifyMask | PropertyChangeMask));
  146. return;
  147. }
  148. if(!getclient(ev->window))
  149. manage(ev->window, &wa);
  150. }
  151. static void
  152. propertynotify(XEvent *e)
  153. {
  154. XPropertyEvent *ev = &e->xproperty;
  155. Client *c;
  156. if(ev->state == PropertyDelete)
  157. return; /* ignore */
  158. if(ev->atom == wm_atom[WMProtocols]) {
  159. c->proto = win_proto(c->win);
  160. return;
  161. }
  162. if((c = getclient(ev->window))) {
  163. switch (ev->atom) {
  164. default: break;
  165. case XA_WM_TRANSIENT_FOR:
  166. XGetTransientForHint(dpy, c->win, &c->trans);
  167. break;
  168. update_size(c);
  169. case XA_WM_NORMAL_HINTS:
  170. update_size(c);
  171. break;
  172. }
  173. if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) {
  174. update_name(c);
  175. if(c == stack)
  176. draw_bar();
  177. else
  178. draw_client(c);
  179. }
  180. }
  181. }
  182. static void
  183. unmapnotify(XEvent *e)
  184. {
  185. Client *c;
  186. XUnmapEvent *ev = &e->xunmap;
  187. if((c = getclient(ev->window)))
  188. unmanage(c);
  189. }