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.

294 lines
5.7 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
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 <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <X11/keysym.h>
  11. #include <X11/Xatom.h>
  12. #include "dwm.h"
  13. #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
  14. #define MouseMask (ButtonMask | PointerMotionMask)
  15. /* local functions */
  16. static void buttonpress(XEvent *e);
  17. static void configurerequest(XEvent *e);
  18. static void destroynotify(XEvent *e);
  19. static void enternotify(XEvent *e);
  20. static void leavenotify(XEvent *e);
  21. static void expose(XEvent *e);
  22. static void maprequest(XEvent *e);
  23. static void propertynotify(XEvent *e);
  24. static void unmapnotify(XEvent *e);
  25. void (*handler[LASTEvent]) (XEvent *) = {
  26. [ButtonPress] = buttonpress,
  27. [ConfigureRequest] = configurerequest,
  28. [DestroyNotify] = destroynotify,
  29. [EnterNotify] = enternotify,
  30. [LeaveNotify] = leavenotify,
  31. [Expose] = expose,
  32. [KeyPress] = keypress,
  33. [MapRequest] = maprequest,
  34. [PropertyNotify] = propertynotify,
  35. [UnmapNotify] = unmapnotify
  36. };
  37. static void
  38. mresize(Client *c)
  39. {
  40. XEvent ev;
  41. int ocx, ocy;
  42. ocx = c->x;
  43. ocy = c->y;
  44. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  45. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  46. return;
  47. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  48. for(;;) {
  49. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  50. switch(ev.type) {
  51. default: break;
  52. case Expose:
  53. handler[Expose](&ev);
  54. break;
  55. case MotionNotify:
  56. XFlush(dpy);
  57. c->w = abs(ocx - ev.xmotion.x);
  58. c->h = abs(ocy - ev.xmotion.y);
  59. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  60. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  61. resize(c, True);
  62. break;
  63. case ButtonRelease:
  64. XUngrabPointer(dpy, CurrentTime);
  65. return;
  66. }
  67. }
  68. }
  69. static void
  70. mmove(Client *c)
  71. {
  72. XEvent ev;
  73. int x1, y1, ocx, ocy, di;
  74. unsigned int dui;
  75. Window dummy;
  76. ocx = c->x;
  77. ocy = c->y;
  78. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  79. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  80. return;
  81. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  82. for(;;) {
  83. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  84. switch (ev.type) {
  85. default: break;
  86. case Expose:
  87. handler[Expose](&ev);
  88. break;
  89. case MotionNotify:
  90. XFlush(dpy);
  91. c->x = ocx + (ev.xmotion.x - x1);
  92. c->y = ocy + (ev.xmotion.y - y1);
  93. resize(c, False);
  94. break;
  95. case ButtonRelease:
  96. XUngrabPointer(dpy, CurrentTime);
  97. return;
  98. }
  99. }
  100. }
  101. static void
  102. buttonpress(XEvent *e)
  103. {
  104. int x;
  105. Arg a;
  106. XButtonPressedEvent *ev = &e->xbutton;
  107. Client *c;
  108. if(barwin == ev->window) {
  109. x = (arrange == floating) ? textw("~") : 0;
  110. for(a.i = 0; a.i < TLast; a.i++) {
  111. x += textw(tags[a.i]);
  112. if(ev->x < x) {
  113. view(&a);
  114. break;
  115. }
  116. }
  117. }
  118. else if((c = getclient(ev->window))) {
  119. if(arrange == tiling && !c->floating)
  120. return;
  121. craise(c);
  122. switch(ev->button) {
  123. default:
  124. break;
  125. case Button1:
  126. mmove(c);
  127. break;
  128. case Button2:
  129. lower(c);
  130. break;
  131. case Button3:
  132. mresize(c);
  133. break;
  134. }
  135. }
  136. }
  137. static void
  138. configurerequest(XEvent *e)
  139. {
  140. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  141. XWindowChanges wc;
  142. Client *c;
  143. ev->value_mask &= ~CWSibling;
  144. if((c = getclient(ev->window))) {
  145. gravitate(c, True);
  146. if(ev->value_mask & CWX)
  147. c->x = ev->x;
  148. if(ev->value_mask & CWY)
  149. c->y = ev->y;
  150. if(ev->value_mask & CWWidth)
  151. c->w = ev->width;
  152. if(ev->value_mask & CWHeight)
  153. c->h = ev->height;
  154. if(ev->value_mask & CWBorderWidth)
  155. c->border = 1;
  156. gravitate(c, False);
  157. resize(c, True);
  158. }
  159. wc.x = ev->x;
  160. wc.y = ev->y;
  161. wc.width = ev->width;
  162. wc.height = ev->height;
  163. wc.border_width = 1;
  164. wc.sibling = None;
  165. wc.stack_mode = Above;
  166. ev->value_mask &= ~CWStackMode;
  167. ev->value_mask |= CWBorderWidth;
  168. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  169. XFlush(dpy);
  170. }
  171. static void
  172. destroynotify(XEvent *e)
  173. {
  174. Client *c;
  175. XDestroyWindowEvent *ev = &e->xdestroywindow;
  176. if((c = getclient(ev->window)))
  177. unmanage(c);
  178. }
  179. static void
  180. enternotify(XEvent *e)
  181. {
  182. XCrossingEvent *ev = &e->xcrossing;
  183. Client *c;
  184. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  185. return;
  186. if((c = getclient(ev->window)))
  187. focus(c);
  188. else if(ev->window == root)
  189. issel = True;
  190. }
  191. static void
  192. leavenotify(XEvent *e)
  193. {
  194. XCrossingEvent *ev = &e->xcrossing;
  195. if((ev->window == root) && !ev->same_screen)
  196. issel = True;
  197. }
  198. static void
  199. expose(XEvent *e)
  200. {
  201. XExposeEvent *ev = &e->xexpose;
  202. Client *c;
  203. if(ev->count == 0) {
  204. if(barwin == ev->window)
  205. draw_bar();
  206. else if((c = gettitle(ev->window)))
  207. draw_client(c);
  208. }
  209. }
  210. static void
  211. maprequest(XEvent *e)
  212. {
  213. XMapRequestEvent *ev = &e->xmaprequest;
  214. static XWindowAttributes wa;
  215. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  216. return;
  217. if(wa.override_redirect) {
  218. XSelectInput(dpy, ev->window,
  219. (StructureNotifyMask | PropertyChangeMask));
  220. return;
  221. }
  222. if(!getclient(ev->window))
  223. manage(ev->window, &wa);
  224. }
  225. static void
  226. propertynotify(XEvent *e)
  227. {
  228. XPropertyEvent *ev = &e->xproperty;
  229. Window trans;
  230. Client *c;
  231. if(ev->state == PropertyDelete)
  232. return; /* ignore */
  233. if((c = getclient(ev->window))) {
  234. if(ev->atom == wm_atom[WMProtocols]) {
  235. c->proto = win_proto(c->win);
  236. return;
  237. }
  238. switch (ev->atom) {
  239. default: break;
  240. case XA_WM_TRANSIENT_FOR:
  241. XGetTransientForHint(dpy, c->win, &trans);
  242. if(!c->floating && (c->floating = (trans != 0)))
  243. arrange(NULL);
  244. break;
  245. case XA_WM_NORMAL_HINTS:
  246. update_size(c);
  247. break;
  248. }
  249. if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) {
  250. update_name(c);
  251. draw_client(c);
  252. }
  253. }
  254. }
  255. static void
  256. unmapnotify(XEvent *e)
  257. {
  258. Client *c;
  259. XUnmapEvent *ev = &e->xunmap;
  260. if((c = getclient(ev->window)))
  261. unmanage(c);
  262. }