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.

355 lines
7.6 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 movemouse(Client *c);
  47. static void resizemouse(Client *c);
  48. static void
  49. buttonpress(XEvent *e)
  50. {
  51. int x;
  52. Arg a;
  53. XButtonPressedEvent *ev = &e->xbutton;
  54. Client *c;
  55. if(barwin == ev->window) {
  56. x = (arrange == dofloat) ? textw("~") : 0;
  57. for(a.i = 0; a.i < TLast; a.i++) {
  58. x += textw(tags[a.i]);
  59. if(ev->x < x) {
  60. view(&a);
  61. break;
  62. }
  63. }
  64. }
  65. else if((c = getclient(ev->window))) {
  66. if(arrange == dotile && !c->dofloat)
  67. return;
  68. higher(c);
  69. switch(ev->button) {
  70. default:
  71. break;
  72. case Button1:
  73. movemouse(c);
  74. break;
  75. case Button2:
  76. lower(c);
  77. break;
  78. case Button3:
  79. resizemouse(c);
  80. break;
  81. }
  82. }
  83. }
  84. static void
  85. configurerequest(XEvent *e)
  86. {
  87. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  88. XWindowChanges wc;
  89. Client *c;
  90. ev->value_mask &= ~CWSibling;
  91. if((c = getclient(ev->window))) {
  92. gravitate(c, True);
  93. if(ev->value_mask & CWX)
  94. c->x = ev->x;
  95. if(ev->value_mask & CWY)
  96. c->y = ev->y;
  97. if(ev->value_mask & CWWidth)
  98. c->w = ev->width;
  99. if(ev->value_mask & CWHeight)
  100. c->h = ev->height;
  101. if(ev->value_mask & CWBorderWidth)
  102. c->border = 1;
  103. gravitate(c, False);
  104. resize(c, True);
  105. }
  106. wc.x = ev->x;
  107. wc.y = ev->y;
  108. wc.width = ev->width;
  109. wc.height = ev->height;
  110. wc.border_width = 1;
  111. wc.sibling = None;
  112. wc.stack_mode = Above;
  113. ev->value_mask &= ~CWStackMode;
  114. ev->value_mask |= CWBorderWidth;
  115. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  116. XFlush(dpy);
  117. }
  118. static void
  119. destroynotify(XEvent *e)
  120. {
  121. Client *c;
  122. XDestroyWindowEvent *ev = &e->xdestroywindow;
  123. if((c = getclient(ev->window)))
  124. unmanage(c);
  125. }
  126. static void
  127. enternotify(XEvent *e)
  128. {
  129. XCrossingEvent *ev = &e->xcrossing;
  130. Client *c;
  131. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  132. return;
  133. if((c = getclient(ev->window)))
  134. focus(c);
  135. else if(ev->window == root)
  136. issel = True;
  137. }
  138. static void
  139. expose(XEvent *e)
  140. {
  141. XExposeEvent *ev = &e->xexpose;
  142. Client *c;
  143. if(ev->count == 0) {
  144. if(barwin == ev->window)
  145. drawstatus();
  146. else if((c = getctitle(ev->window)))
  147. drawtitle(c);
  148. }
  149. }
  150. static void
  151. keypress(XEvent *e)
  152. {
  153. XKeyEvent *ev = &e->xkey;
  154. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  155. unsigned int i;
  156. KeySym keysym;
  157. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  158. for(i = 0; i < len; i++)
  159. if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
  160. if(key[i].func)
  161. key[i].func(&key[i].arg);
  162. return;
  163. }
  164. }
  165. static void
  166. leavenotify(XEvent *e)
  167. {
  168. XCrossingEvent *ev = &e->xcrossing;
  169. if((ev->window == root) && !ev->same_screen)
  170. issel = True;
  171. }
  172. static void
  173. maprequest(XEvent *e)
  174. {
  175. XMapRequestEvent *ev = &e->xmaprequest;
  176. static XWindowAttributes wa;
  177. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  178. return;
  179. if(wa.override_redirect) {
  180. XSelectInput(dpy, ev->window,
  181. (StructureNotifyMask | PropertyChangeMask));
  182. return;
  183. }
  184. if(!getclient(ev->window))
  185. manage(ev->window, &wa);
  186. }
  187. static void
  188. movemouse(Client *c)
  189. {
  190. XEvent ev;
  191. int x1, y1, ocx, ocy, di;
  192. unsigned int dui;
  193. Window dummy;
  194. ocx = c->x;
  195. ocy = c->y;
  196. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  197. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  198. return;
  199. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  200. for(;;) {
  201. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  202. switch (ev.type) {
  203. default: break;
  204. case Expose:
  205. handler[Expose](&ev);
  206. break;
  207. case MotionNotify:
  208. XFlush(dpy);
  209. c->x = ocx + (ev.xmotion.x - x1);
  210. c->y = ocy + (ev.xmotion.y - y1);
  211. resize(c, False);
  212. break;
  213. case ButtonRelease:
  214. XUngrabPointer(dpy, CurrentTime);
  215. return;
  216. }
  217. }
  218. }
  219. static void
  220. propertynotify(XEvent *e)
  221. {
  222. XPropertyEvent *ev = &e->xproperty;
  223. Window trans;
  224. Client *c;
  225. if(ev->state == PropertyDelete)
  226. return; /* ignore */
  227. if((c = getclient(ev->window))) {
  228. if(ev->atom == wm_atom[WMProtocols]) {
  229. c->proto = getproto(c->win);
  230. return;
  231. }
  232. switch (ev->atom) {
  233. default: break;
  234. case XA_WM_TRANSIENT_FOR:
  235. XGetTransientForHint(dpy, c->win, &trans);
  236. if(!c->dofloat && (c->dofloat = (trans != 0)))
  237. arrange(NULL);
  238. break;
  239. case XA_WM_NORMAL_HINTS:
  240. setsize(c);
  241. break;
  242. }
  243. if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) {
  244. settitle(c);
  245. drawtitle(c);
  246. }
  247. }
  248. }
  249. static void
  250. resizemouse(Client *c)
  251. {
  252. XEvent ev;
  253. int ocx, ocy;
  254. ocx = c->x;
  255. ocy = c->y;
  256. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  257. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  258. return;
  259. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  260. for(;;) {
  261. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  262. switch(ev.type) {
  263. default: break;
  264. case Expose:
  265. handler[Expose](&ev);
  266. break;
  267. case MotionNotify:
  268. XFlush(dpy);
  269. c->w = abs(ocx - ev.xmotion.x);
  270. c->h = abs(ocy - ev.xmotion.y);
  271. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  272. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  273. resize(c, True);
  274. break;
  275. case ButtonRelease:
  276. XUngrabPointer(dpy, CurrentTime);
  277. return;
  278. }
  279. }
  280. }
  281. static void
  282. unmapnotify(XEvent *e)
  283. {
  284. Client *c;
  285. XUnmapEvent *ev = &e->xunmap;
  286. if((c = getclient(ev->window)))
  287. unmanage(c);
  288. }
  289. /* extern functions */
  290. void (*handler[LASTEvent]) (XEvent *) = {
  291. [ButtonPress] = buttonpress,
  292. [ConfigureRequest] = configurerequest,
  293. [DestroyNotify] = destroynotify,
  294. [EnterNotify] = enternotify,
  295. [LeaveNotify] = leavenotify,
  296. [Expose] = expose,
  297. [KeyPress] = keypress,
  298. [MapRequest] = maprequest,
  299. [PropertyNotify] = propertynotify,
  300. [UnmapNotify] = unmapnotify
  301. };
  302. void
  303. grabkeys()
  304. {
  305. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  306. unsigned int i;
  307. KeyCode code;
  308. for(i = 0; i < len; i++) {
  309. code = XKeysymToKeycode(dpy, key[i].keysym);
  310. XUngrabKey(dpy, code, key[i].mod, root);
  311. XGrabKey(dpy, code, key[i].mod, root, True,
  312. GrabModeAsync, GrabModeAsync);
  313. }
  314. }