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.

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