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.

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