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.

222 lines
4.1 KiB

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