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.

417 lines
8.3 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
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
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 "dwm.h"
  6. #include <stdlib.h>
  7. #include <X11/keysym.h>
  8. #include <X11/Xatom.h>
  9. /* static */
  10. typedef struct {
  11. unsigned long mod;
  12. KeySym keysym;
  13. void (*func)(Arg *arg);
  14. Arg arg;
  15. } Key;
  16. KEYS
  17. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  18. static void
  19. movemouse(Client *c)
  20. {
  21. int x1, y1, ocx, ocy, di;
  22. unsigned int dui;
  23. Window dummy;
  24. XEvent ev;
  25. ocx = c->x;
  26. ocy = c->y;
  27. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  28. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  29. return;
  30. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  31. for(;;) {
  32. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  33. switch (ev.type) {
  34. default: break;
  35. case Expose:
  36. handler[Expose](&ev);
  37. break;
  38. case MotionNotify:
  39. XSync(dpy, False);
  40. c->x = ocx + (ev.xmotion.x - x1);
  41. c->y = ocy + (ev.xmotion.y - y1);
  42. resize(c, False, TopLeft);
  43. break;
  44. case ButtonRelease:
  45. XUngrabPointer(dpy, CurrentTime);
  46. return;
  47. }
  48. }
  49. }
  50. static void
  51. resizemouse(Client *c)
  52. {
  53. int ocx, ocy;
  54. int nw, nh;
  55. Corner sticky;
  56. XEvent ev;
  57. ocx = c->x;
  58. ocy = c->y;
  59. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  60. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  61. return;
  62. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  63. for(;;) {
  64. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  65. switch(ev.type) {
  66. default: break;
  67. case Expose:
  68. handler[Expose](&ev);
  69. break;
  70. case MotionNotify:
  71. XSync(dpy, False);
  72. if((nw = abs(ocx - ev.xmotion.x)))
  73. c->w = abs(ocx - ev.xmotion.x);
  74. if((nh = abs(ocy - ev.xmotion.y)))
  75. c->h = abs(ocy - ev.xmotion.y);
  76. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  77. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  78. if(ocx <= ev.xmotion.x)
  79. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  80. else
  81. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  82. resize(c, True, sticky);
  83. break;
  84. case ButtonRelease:
  85. XUngrabPointer(dpy, CurrentTime);
  86. return;
  87. }
  88. }
  89. }
  90. static void
  91. buttonpress(XEvent *e)
  92. {
  93. int x;
  94. Arg a;
  95. Client *c;
  96. XButtonPressedEvent *ev = &e->xbutton;
  97. if(barwin == ev->window) {
  98. x = 0;
  99. for(a.i = 0; a.i < ntags; a.i++) {
  100. x += textw(tags[a.i]);
  101. if(ev->x < x) {
  102. if(ev->button == Button1)
  103. view(&a);
  104. else if(ev->button == Button3)
  105. toggleview(&a);
  106. return;
  107. }
  108. }
  109. if(ev->x < x + bmw) {
  110. if(ev->button == Button1)
  111. togglemode(NULL);
  112. }
  113. }
  114. else if((c = getclient(ev->window))) {
  115. focus(c);
  116. if(CLEANMASK(ev->state) != MODKEY)
  117. return;
  118. switch(ev->button) {
  119. default:
  120. break;
  121. case Button1:
  122. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  123. restack(c);
  124. movemouse(c);
  125. }
  126. break;
  127. case Button2:
  128. zoom(NULL);
  129. break;
  130. case Button3:
  131. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  132. restack(c);
  133. resizemouse(c);
  134. }
  135. break;
  136. }
  137. }
  138. }
  139. static void
  140. configurerequest(XEvent *e)
  141. {
  142. int ox, oy, ow, oh;
  143. unsigned long newmask;
  144. Client *c;
  145. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  146. XEvent synev;
  147. XWindowChanges wc;
  148. if((c = getclient(ev->window))) {
  149. ox = c->x;
  150. oy = c->y;
  151. ow = c->w;
  152. oh = c->h;
  153. gravitate(c, True);
  154. if(ev->value_mask & CWX)
  155. c->x = ev->x;
  156. if(ev->value_mask & CWY)
  157. c->y = ev->y;
  158. if(ev->value_mask & CWWidth)
  159. c->w = ev->width;
  160. if(ev->value_mask & CWHeight)
  161. c->h = ev->height;
  162. if(ev->value_mask & CWBorderWidth)
  163. c->border = ev->border_width;
  164. gravitate(c, False);
  165. wc.x = c->x;
  166. wc.y = c->y;
  167. wc.width = c->w;
  168. wc.height = c->h;
  169. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  170. if(newmask)
  171. XConfigureWindow(dpy, c->win, newmask, &wc);
  172. else {
  173. synev.type = ConfigureNotify;
  174. synev.xconfigure.display = dpy;
  175. synev.xconfigure.event = c->win;
  176. synev.xconfigure.window = c->win;
  177. synev.xconfigure.x = c->x;
  178. synev.xconfigure.y = c->y;
  179. synev.xconfigure.width = c->w;
  180. synev.xconfigure.height = c->h;
  181. synev.xconfigure.border_width = c->border;
  182. synev.xconfigure.above = None;
  183. /* Send synthetic ConfigureNotify */
  184. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  185. }
  186. XSync(dpy, False);
  187. if(c->isfloat)
  188. resize(c, False, TopLeft);
  189. else if(c->ismax) {
  190. resize(c, False, TopLeft);
  191. c->x = ox;
  192. c->y = oy;
  193. c->w = ow;
  194. c->h = oh;
  195. }
  196. else
  197. arrange(NULL);
  198. }
  199. else {
  200. wc.x = ev->x;
  201. wc.y = ev->y;
  202. wc.width = ev->width;
  203. wc.height = ev->height;
  204. wc.border_width = ev->border_width;
  205. wc.sibling = ev->above;
  206. wc.stack_mode = ev->detail;
  207. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  208. XSync(dpy, False);
  209. }
  210. }
  211. static void
  212. destroynotify(XEvent *e)
  213. {
  214. Client *c;
  215. XDestroyWindowEvent *ev = &e->xdestroywindow;
  216. if((c = getclient(ev->window)))
  217. unmanage(c);
  218. }
  219. static void
  220. enternotify(XEvent *e)
  221. {
  222. Client *c;
  223. XCrossingEvent *ev = &e->xcrossing;
  224. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  225. return;
  226. if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
  227. focus(c);
  228. else if(ev->window == root) {
  229. issel = True;
  230. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  231. drawall();
  232. }
  233. }
  234. static void
  235. expose(XEvent *e)
  236. {
  237. Client *c;
  238. XExposeEvent *ev = &e->xexpose;
  239. if(ev->count == 0) {
  240. if(barwin == ev->window)
  241. drawstatus();
  242. else if((c = getctitle(ev->window)))
  243. drawtitle(c);
  244. }
  245. }
  246. static void
  247. keypress(XEvent *e)
  248. {
  249. static unsigned int len = sizeof(key) / sizeof(key[0]);
  250. unsigned int i;
  251. KeySym keysym;
  252. XKeyEvent *ev = &e->xkey;
  253. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  254. for(i = 0; i < len; i++) {
  255. if(keysym == key[i].keysym &&
  256. CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  257. {
  258. if(key[i].func)
  259. key[i].func(&key[i].arg);
  260. return;
  261. }
  262. }
  263. }
  264. static void
  265. leavenotify(XEvent *e)
  266. {
  267. XCrossingEvent *ev = &e->xcrossing;
  268. if((ev->window == root) && !ev->same_screen) {
  269. issel = False;
  270. drawall();
  271. }
  272. }
  273. static void
  274. mappingnotify(XEvent *e)
  275. {
  276. XMappingEvent *ev = &e->xmapping;
  277. XRefreshKeyboardMapping(ev);
  278. if(ev->request == MappingKeyboard)
  279. grabkeys();
  280. }
  281. static void
  282. maprequest(XEvent *e)
  283. {
  284. static XWindowAttributes wa;
  285. XMapRequestEvent *ev = &e->xmaprequest;
  286. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  287. return;
  288. if(wa.override_redirect) {
  289. XSelectInput(dpy, ev->window,
  290. (StructureNotifyMask | PropertyChangeMask));
  291. return;
  292. }
  293. if(!getclient(ev->window))
  294. manage(ev->window, &wa);
  295. }
  296. static void
  297. propertynotify(XEvent *e)
  298. {
  299. Client *c;
  300. Window trans;
  301. XPropertyEvent *ev = &e->xproperty;
  302. if(ev->state == PropertyDelete)
  303. return; /* ignore */
  304. if((c = getclient(ev->window))) {
  305. if(ev->atom == wmatom[WMProtocols]) {
  306. c->proto = getproto(c->win);
  307. return;
  308. }
  309. switch (ev->atom) {
  310. default: break;
  311. case XA_WM_TRANSIENT_FOR:
  312. XGetTransientForHint(dpy, c->win, &trans);
  313. if(!c->isfloat && (c->isfloat = (trans != 0)))
  314. arrange(NULL);
  315. break;
  316. case XA_WM_NORMAL_HINTS:
  317. setsize(c);
  318. break;
  319. }
  320. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  321. settitle(c);
  322. drawtitle(c);
  323. }
  324. }
  325. }
  326. static void
  327. unmapnotify(XEvent *e)
  328. {
  329. Client *c;
  330. XUnmapEvent *ev = &e->xunmap;
  331. if((c = getclient(ev->window)))
  332. unmanage(c);
  333. }
  334. /* extern */
  335. void (*handler[LASTEvent]) (XEvent *) = {
  336. [ButtonPress] = buttonpress,
  337. [ConfigureRequest] = configurerequest,
  338. [DestroyNotify] = destroynotify,
  339. [EnterNotify] = enternotify,
  340. [LeaveNotify] = leavenotify,
  341. [Expose] = expose,
  342. [KeyPress] = keypress,
  343. [MappingNotify] = mappingnotify,
  344. [MapRequest] = maprequest,
  345. [PropertyNotify] = propertynotify,
  346. [UnmapNotify] = unmapnotify
  347. };
  348. void
  349. grabkeys()
  350. {
  351. static unsigned int len = sizeof(key) / sizeof(key[0]);
  352. unsigned int i;
  353. KeyCode code;
  354. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  355. for(i = 0; i < len; i++) {
  356. code = XKeysymToKeycode(dpy, key[i].keysym);
  357. XGrabKey(dpy, code, key[i].mod, root, True,
  358. GrabModeAsync, GrabModeAsync);
  359. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  360. GrabModeAsync, GrabModeAsync);
  361. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  362. GrabModeAsync, GrabModeAsync);
  363. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  364. GrabModeAsync, GrabModeAsync);
  365. }
  366. }
  367. void
  368. procevent()
  369. {
  370. XEvent ev;
  371. while(XPending(dpy)) {
  372. XNextEvent(dpy, &ev);
  373. if(handler[ev.type])
  374. (handler[ev.type])(&ev); /* call handler */
  375. }
  376. }