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.

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