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.

352 lines
7.5 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
  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", "-fn",
  14. "-*-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. XFlush(dpy);
  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. XFlush(dpy);
  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. x = 0;
  119. for(a.i = 0; a.i < TLast; a.i++) {
  120. x += textw(tags[a.i]);
  121. if(ev->x < x) {
  122. view(&a);
  123. break;
  124. }
  125. }
  126. }
  127. else if((c = getclient(ev->window))) {
  128. if(arrange == dotile && !c->dofloat)
  129. return;
  130. higher(c);
  131. switch(ev->button) {
  132. default:
  133. break;
  134. case Button1:
  135. movemouse(c);
  136. break;
  137. case Button2:
  138. lower(c);
  139. break;
  140. case Button3:
  141. resizemouse(c);
  142. break;
  143. }
  144. }
  145. }
  146. static void
  147. configurerequest(XEvent *e)
  148. {
  149. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  150. XWindowChanges wc;
  151. Client *c;
  152. ev->value_mask &= ~CWSibling;
  153. if((c = getclient(ev->window))) {
  154. gravitate(c, True);
  155. if(ev->value_mask & CWX)
  156. c->x = ev->x;
  157. if(ev->value_mask & CWY)
  158. c->y = ev->y;
  159. if(ev->value_mask & CWWidth)
  160. c->w = ev->width;
  161. if(ev->value_mask & CWHeight)
  162. c->h = ev->height;
  163. if(ev->value_mask & CWBorderWidth)
  164. c->border = 1;
  165. gravitate(c, False);
  166. resize(c, True);
  167. }
  168. wc.x = ev->x;
  169. wc.y = ev->y;
  170. wc.width = ev->width;
  171. wc.height = ev->height;
  172. wc.border_width = 1;
  173. wc.sibling = None;
  174. wc.stack_mode = Above;
  175. ev->value_mask &= ~CWStackMode;
  176. ev->value_mask |= CWBorderWidth;
  177. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  178. XFlush(dpy);
  179. }
  180. static void
  181. destroynotify(XEvent *e)
  182. {
  183. Client *c;
  184. XDestroyWindowEvent *ev = &e->xdestroywindow;
  185. if((c = getclient(ev->window)))
  186. unmanage(c);
  187. }
  188. static void
  189. enternotify(XEvent *e)
  190. {
  191. XCrossingEvent *ev = &e->xcrossing;
  192. Client *c;
  193. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  194. return;
  195. if((c = getclient(ev->window)))
  196. focus(c);
  197. else if(ev->window == root)
  198. issel = True;
  199. }
  200. static void
  201. expose(XEvent *e)
  202. {
  203. XExposeEvent *ev = &e->xexpose;
  204. Client *c;
  205. if(ev->count == 0) {
  206. if(barwin == ev->window)
  207. drawstatus();
  208. else if((c = getctitle(ev->window)))
  209. drawtitle(c);
  210. }
  211. }
  212. static void
  213. keypress(XEvent *e)
  214. {
  215. XKeyEvent *ev = &e->xkey;
  216. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  217. unsigned int i;
  218. KeySym keysym;
  219. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  220. for(i = 0; i < len; i++)
  221. if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
  222. if(key[i].func)
  223. key[i].func(&key[i].arg);
  224. return;
  225. }
  226. }
  227. static void
  228. leavenotify(XEvent *e)
  229. {
  230. XCrossingEvent *ev = &e->xcrossing;
  231. if((ev->window == root) && !ev->same_screen)
  232. issel = True;
  233. }
  234. static void
  235. maprequest(XEvent *e)
  236. {
  237. XMapRequestEvent *ev = &e->xmaprequest;
  238. static XWindowAttributes wa;
  239. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  240. return;
  241. if(wa.override_redirect) {
  242. XSelectInput(dpy, ev->window,
  243. (StructureNotifyMask | PropertyChangeMask));
  244. return;
  245. }
  246. if(!getclient(ev->window))
  247. manage(ev->window, &wa);
  248. }
  249. static void
  250. propertynotify(XEvent *e)
  251. {
  252. XPropertyEvent *ev = &e->xproperty;
  253. Window trans;
  254. Client *c;
  255. if(ev->state == PropertyDelete)
  256. return; /* ignore */
  257. if((c = getclient(ev->window))) {
  258. if(ev->atom == wmatom[WMProtocols]) {
  259. c->proto = getproto(c->win);
  260. return;
  261. }
  262. switch (ev->atom) {
  263. default: break;
  264. case XA_WM_TRANSIENT_FOR:
  265. XGetTransientForHint(dpy, c->win, &trans);
  266. if(!c->dofloat && (c->dofloat = (trans != 0)))
  267. arrange(NULL);
  268. break;
  269. case XA_WM_NORMAL_HINTS:
  270. setsize(c);
  271. break;
  272. }
  273. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  274. settitle(c);
  275. drawtitle(c);
  276. }
  277. }
  278. }
  279. static void
  280. unmapnotify(XEvent *e)
  281. {
  282. Client *c;
  283. XUnmapEvent *ev = &e->xunmap;
  284. if((c = getclient(ev->window)))
  285. unmanage(c);
  286. }
  287. /* extern functions */
  288. void (*handler[LASTEvent]) (XEvent *) = {
  289. [ButtonPress] = buttonpress,
  290. [ConfigureRequest] = configurerequest,
  291. [DestroyNotify] = destroynotify,
  292. [EnterNotify] = enternotify,
  293. [LeaveNotify] = leavenotify,
  294. [Expose] = expose,
  295. [KeyPress] = keypress,
  296. [MapRequest] = maprequest,
  297. [PropertyNotify] = propertynotify,
  298. [UnmapNotify] = unmapnotify
  299. };
  300. void
  301. grabkeys()
  302. {
  303. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  304. unsigned int i;
  305. KeyCode code;
  306. for(i = 0; i < len; i++) {
  307. code = XKeysymToKeycode(dpy, key[i].keysym);
  308. XUngrabKey(dpy, code, key[i].mod, root);
  309. XGrabKey(dpy, code, key[i].mod, root, True,
  310. GrabModeAsync, GrabModeAsync);
  311. }
  312. }