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.

368 lines
7.9 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
  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. #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
  10. #define MouseMask (ButtonMask | PointerMotionMask)
  11. /********** CUSTOMIZE **********/
  12. const char *term[] = {
  13. "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
  14. "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
  15. };
  16. const char *browse[] = { "firefox", NULL };
  17. const char *xlock[] = { "xlock", NULL };
  18. Key key[] = {
  19. /* modifier key function arguments */
  20. { Mod1Mask, XK_Return, zoom, { 0 } },
  21. { Mod1Mask, XK_k, focusprev, { 0 } },
  22. { Mod1Mask, XK_j, focusnext, { 0 } },
  23. { Mod1Mask, XK_m, maximize, { 0 } },
  24. { Mod1Mask, XK_0, view, { .i = Tscratch } },
  25. { Mod1Mask, XK_1, view, { .i = Tdev } },
  26. { Mod1Mask, XK_2, view, { .i = Twww } },
  27. { Mod1Mask, XK_3, view, { .i = Twork } },
  28. { Mod1Mask, XK_space, dotile, { 0 } },
  29. { Mod1Mask|ShiftMask, XK_space, dofloat, { 0 } },
  30. { Mod1Mask|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
  31. { Mod1Mask|ShiftMask, XK_1, replacetag, { .i = Tdev } },
  32. { Mod1Mask|ShiftMask, XK_2, replacetag, { .i = Twww } },
  33. { Mod1Mask|ShiftMask, XK_3, replacetag, { .i = Twork } },
  34. { Mod1Mask|ShiftMask, XK_c, killclient, { 0 } },
  35. { Mod1Mask|ShiftMask, XK_q, quit, { 0 } },
  36. { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } },
  37. { Mod1Mask|ShiftMask, XK_w, spawn, { .argv = browse } },
  38. { Mod1Mask|ShiftMask, XK_l, spawn, { .argv = xlock } },
  39. { ControlMask, XK_0, appendtag, { .i = Tscratch } },
  40. { ControlMask, XK_1, appendtag, { .i = Tdev } },
  41. { ControlMask, XK_2, appendtag, { .i = Twww } },
  42. { ControlMask, XK_3, appendtag, { .i = Twork } },
  43. };
  44. /********** CUSTOMIZE **********/
  45. /* static functions */
  46. static void
  47. movemouse(Client *c)
  48. {
  49. XEvent ev;
  50. int x1, y1, ocx, ocy, di;
  51. unsigned int dui;
  52. Window dummy;
  53. ocx = c->x;
  54. ocy = c->y;
  55. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  56. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  57. return;
  58. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  59. for(;;) {
  60. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  61. switch (ev.type) {
  62. default: break;
  63. case Expose:
  64. handler[Expose](&ev);
  65. break;
  66. case MotionNotify:
  67. XSync(dpy, False);
  68. c->x = ocx + (ev.xmotion.x - x1);
  69. c->y = ocy + (ev.xmotion.y - y1);
  70. resize(c, False);
  71. break;
  72. case ButtonRelease:
  73. XUngrabPointer(dpy, CurrentTime);
  74. return;
  75. }
  76. }
  77. }
  78. static void
  79. resizemouse(Client *c)
  80. {
  81. XEvent ev;
  82. int ocx, ocy;
  83. ocx = c->x;
  84. ocy = c->y;
  85. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  86. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  87. return;
  88. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  89. for(;;) {
  90. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  91. switch(ev.type) {
  92. default: break;
  93. case Expose:
  94. handler[Expose](&ev);
  95. break;
  96. case MotionNotify:
  97. XSync(dpy, False);
  98. c->w = abs(ocx - ev.xmotion.x);
  99. c->h = abs(ocy - ev.xmotion.y);
  100. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  101. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  102. resize(c, True);
  103. break;
  104. case ButtonRelease:
  105. XUngrabPointer(dpy, CurrentTime);
  106. return;
  107. }
  108. }
  109. }
  110. static void
  111. buttonpress(XEvent *e)
  112. {
  113. int x;
  114. Arg a;
  115. XButtonPressedEvent *ev = &e->xbutton;
  116. Client *c;
  117. if(barwin == ev->window) {
  118. switch(ev->button) {
  119. default:
  120. x = 0;
  121. for(a.i = 0; a.i < TLast; a.i++) {
  122. x += textw(tags[a.i]);
  123. if(ev->x < x) {
  124. view(&a);
  125. break;
  126. }
  127. }
  128. break;
  129. case Button4:
  130. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  131. view(&a);
  132. break;
  133. case Button5:
  134. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  135. view(&a);
  136. break;
  137. }
  138. }
  139. else if((c = getclient(ev->window))) {
  140. if(arrange == dotile && !c->isfloat) {
  141. if((ev->state & ControlMask) && (ev->button == Button1))
  142. zoom(NULL);
  143. return;
  144. }
  145. /* floating windows */
  146. higher(c);
  147. switch(ev->button) {
  148. default:
  149. break;
  150. case Button1:
  151. movemouse(c);
  152. break;
  153. case Button2:
  154. lower(c);
  155. break;
  156. case Button3:
  157. resizemouse(c);
  158. break;
  159. }
  160. }
  161. }
  162. static void
  163. configurerequest(XEvent *e)
  164. {
  165. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  166. XWindowChanges wc;
  167. Client *c;
  168. ev->value_mask &= ~CWSibling;
  169. if((c = getclient(ev->window))) {
  170. gravitate(c, True);
  171. if(ev->value_mask & CWX)
  172. c->x = ev->x;
  173. if(ev->value_mask & CWY)
  174. c->y = ev->y;
  175. if(ev->value_mask & CWWidth)
  176. c->w = ev->width;
  177. if(ev->value_mask & CWHeight)
  178. c->h = ev->height;
  179. if(ev->value_mask & CWBorderWidth)
  180. c->border = 1;
  181. gravitate(c, False);
  182. resize(c, True);
  183. }
  184. wc.x = ev->x;
  185. wc.y = ev->y;
  186. wc.width = ev->width;
  187. wc.height = ev->height;
  188. wc.border_width = 1;
  189. wc.sibling = None;
  190. wc.stack_mode = Above;
  191. ev->value_mask &= ~CWStackMode;
  192. ev->value_mask |= CWBorderWidth;
  193. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  194. XSync(dpy, False);
  195. }
  196. static void
  197. destroynotify(XEvent *e)
  198. {
  199. Client *c;
  200. XDestroyWindowEvent *ev = &e->xdestroywindow;
  201. if((c = getclient(ev->window)))
  202. unmanage(c);
  203. }
  204. static void
  205. enternotify(XEvent *e)
  206. {
  207. XCrossingEvent *ev = &e->xcrossing;
  208. Client *c;
  209. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  210. return;
  211. if((c = getclient(ev->window)))
  212. focus(c);
  213. else if(ev->window == root)
  214. issel = True;
  215. }
  216. static void
  217. expose(XEvent *e)
  218. {
  219. XExposeEvent *ev = &e->xexpose;
  220. Client *c;
  221. if(ev->count == 0) {
  222. if(barwin == ev->window)
  223. drawstatus();
  224. else if((c = getctitle(ev->window)))
  225. drawtitle(c);
  226. }
  227. }
  228. static void
  229. keypress(XEvent *e)
  230. {
  231. XKeyEvent *ev = &e->xkey;
  232. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  233. unsigned int i;
  234. KeySym keysym;
  235. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  236. for(i = 0; i < len; i++)
  237. if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
  238. if(key[i].func)
  239. key[i].func(&key[i].arg);
  240. return;
  241. }
  242. }
  243. static void
  244. leavenotify(XEvent *e)
  245. {
  246. XCrossingEvent *ev = &e->xcrossing;
  247. if((ev->window == root) && !ev->same_screen)
  248. issel = True;
  249. }
  250. static void
  251. maprequest(XEvent *e)
  252. {
  253. XMapRequestEvent *ev = &e->xmaprequest;
  254. static XWindowAttributes wa;
  255. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  256. return;
  257. if(wa.override_redirect) {
  258. XSelectInput(dpy, ev->window,
  259. (StructureNotifyMask | PropertyChangeMask));
  260. return;
  261. }
  262. if(!getclient(ev->window))
  263. manage(ev->window, &wa);
  264. }
  265. static void
  266. propertynotify(XEvent *e)
  267. {
  268. XPropertyEvent *ev = &e->xproperty;
  269. Window trans;
  270. Client *c;
  271. if(ev->state == PropertyDelete)
  272. return; /* ignore */
  273. if((c = getclient(ev->window))) {
  274. if(ev->atom == wmatom[WMProtocols]) {
  275. c->proto = getproto(c->win);
  276. return;
  277. }
  278. switch (ev->atom) {
  279. default: break;
  280. case XA_WM_TRANSIENT_FOR:
  281. XGetTransientForHint(dpy, c->win, &trans);
  282. if(!c->isfloat && (c->isfloat = (trans != 0)))
  283. arrange(NULL);
  284. break;
  285. case XA_WM_NORMAL_HINTS:
  286. setsize(c);
  287. break;
  288. }
  289. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  290. settitle(c);
  291. drawtitle(c);
  292. }
  293. }
  294. }
  295. static void
  296. unmapnotify(XEvent *e)
  297. {
  298. Client *c;
  299. XUnmapEvent *ev = &e->xunmap;
  300. if((c = getclient(ev->window)))
  301. unmanage(c);
  302. }
  303. /* extern functions */
  304. void (*handler[LASTEvent]) (XEvent *) = {
  305. [ButtonPress] = buttonpress,
  306. [ConfigureRequest] = configurerequest,
  307. [DestroyNotify] = destroynotify,
  308. [EnterNotify] = enternotify,
  309. [LeaveNotify] = leavenotify,
  310. [Expose] = expose,
  311. [KeyPress] = keypress,
  312. [MapRequest] = maprequest,
  313. [PropertyNotify] = propertynotify,
  314. [UnmapNotify] = unmapnotify
  315. };
  316. void
  317. grabkeys()
  318. {
  319. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  320. unsigned int i;
  321. KeyCode code;
  322. for(i = 0; i < len; i++) {
  323. code = XKeysymToKeycode(dpy, key[i].keysym);
  324. XUngrabKey(dpy, code, key[i].mod, root);
  325. XGrabKey(dpy, code, key[i].mod, root, True,
  326. GrabModeAsync, GrabModeAsync);
  327. }
  328. }