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.

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