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.

371 lines
8.1 KiB

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