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.

226 lines
4.3 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. unsigned int
  36. discard_events(long even_mask)
  37. {
  38. XEvent ev;
  39. unsigned int n = 0;
  40. while(XCheckMaskEvent(dpy, even_mask, &ev)) n++;
  41. return n;
  42. }
  43. static void
  44. buttonpress(XEvent *e)
  45. {
  46. XButtonPressedEvent *ev = &e->xbutton;
  47. Client *c;
  48. if((c = getclient(ev->window))) {
  49. switch(ev->button) {
  50. default:
  51. break;
  52. case Button1:
  53. mmove(c);
  54. break;
  55. case Button2:
  56. XLowerWindow(dpy, c->win);
  57. break;
  58. case Button3:
  59. mresize(c);
  60. break;
  61. }
  62. }
  63. }
  64. static void
  65. configurerequest(XEvent *e)
  66. {
  67. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  68. XWindowChanges wc;
  69. Client *c;
  70. ev->value_mask &= ~CWSibling;
  71. if((c = getclient(ev->window))) {
  72. if(ev->value_mask & CWX)
  73. c->r[RFloat].x = ev->x;
  74. if(ev->value_mask & CWY)
  75. c->r[RFloat].y = ev->y;
  76. if(ev->value_mask & CWWidth)
  77. c->r[RFloat].width = ev->width;
  78. if(ev->value_mask & CWHeight)
  79. c->r[RFloat].height = ev->height;
  80. if(ev->value_mask & CWBorderWidth)
  81. c->border = ev->border_width;
  82. }
  83. wc.x = ev->x;
  84. wc.y = ev->y;
  85. wc.width = ev->width;
  86. wc.height = ev->height;
  87. wc.border_width = 0;
  88. wc.sibling = None;
  89. wc.stack_mode = Above;
  90. ev->value_mask &= ~CWStackMode;
  91. ev->value_mask |= CWBorderWidth;
  92. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  93. XFlush(dpy);
  94. }
  95. static void
  96. destroynotify(XEvent *e)
  97. {
  98. Client *c;
  99. XDestroyWindowEvent *ev = &e->xdestroywindow;
  100. if((c = getclient(ev->window)))
  101. unmanage(c);
  102. }
  103. static void
  104. enternotify(XEvent *e)
  105. {
  106. XCrossingEvent *ev = &e->xcrossing;
  107. Client *c;
  108. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  109. return;
  110. if((c = getclient(ev->window)))
  111. focus(c);
  112. else if(ev->window == root) {
  113. sel_screen = True;
  114. /*draw_frames();*/
  115. }
  116. }
  117. static void
  118. leavenotify(XEvent *e)
  119. {
  120. XCrossingEvent *ev = &e->xcrossing;
  121. if((ev->window == root) && !ev->same_screen) {
  122. sel_screen = True;
  123. /*draw_frames();*/
  124. }
  125. }
  126. static void
  127. expose(XEvent *e)
  128. {
  129. XExposeEvent *ev = &e->xexpose;
  130. if(ev->count == 0) {
  131. if(ev->window == barwin)
  132. draw_bar();
  133. }
  134. }
  135. static void
  136. keymapnotify(XEvent *e)
  137. {
  138. update_keys();
  139. }
  140. static void
  141. maprequest(XEvent *e)
  142. {
  143. XMapRequestEvent *ev = &e->xmaprequest;
  144. static XWindowAttributes wa;
  145. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  146. return;
  147. if(wa.override_redirect) {
  148. XSelectInput(dpy, ev->window,
  149. (StructureNotifyMask | PropertyChangeMask));
  150. return;
  151. }
  152. if(!getclient(ev->window))
  153. manage(ev->window, &wa);
  154. }
  155. static void
  156. propertynotify(XEvent *e)
  157. {
  158. XPropertyEvent *ev = &e->xproperty;
  159. long msize;
  160. Client *c;
  161. if(ev->state == PropertyDelete)
  162. return; /* ignore */
  163. if(ev->atom == wm_atom[WMProtocols]) {
  164. c->proto = win_proto(c->win);
  165. return;
  166. }
  167. if((c = getclient(ev->window))) {
  168. switch (ev->atom) {
  169. default: break;
  170. case XA_WM_TRANSIENT_FOR:
  171. XGetTransientForHint(dpy, c->win, &c->trans);
  172. break;
  173. case XA_WM_NORMAL_HINTS:
  174. if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize)
  175. || !c->size.flags)
  176. c->size.flags = PSize;
  177. if(c->size.flags & PMinSize && c->size.flags & PMaxSize
  178. && c->size.min_width == c->size.max_width
  179. && c->size.min_height == c->size.max_height)
  180. c->fixedsize = True;
  181. else
  182. c->fixedsize = False;
  183. break;
  184. }
  185. if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) {
  186. update_name(c);
  187. }
  188. }
  189. }
  190. static void
  191. unmapnotify(XEvent *e)
  192. {
  193. Client *c;
  194. XUnmapEvent *ev = &e->xunmap;
  195. if((c = getclient(ev->window)))
  196. unmanage(c);
  197. }