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.

381 lines
8.1 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_j, focusnext, { 0 } },
  31. { MODKEY, XK_k, focusprev, { 0 } },
  32. { MODKEY, XK_m, togglemax, { 0 } },
  33. { MODKEY, XK_space, togglemode, { 0 } },
  34. { MODKEY, XK_Return, zoom, { 0 } },
  35. { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
  36. { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
  37. { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
  38. { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
  39. { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
  40. { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
  41. { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
  42. { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
  43. { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
  44. { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
  45. { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
  46. { MODKEY|ShiftMask, XK_q, quit, { 0 } },
  47. { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
  48. { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
  49. { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
  50. { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
  51. };
  52. /* END CUSTOMIZE */
  53. /* static */
  54. static void
  55. movemouse(Client *c)
  56. {
  57. int x1, y1, ocx, ocy, di;
  58. unsigned int dui;
  59. Window dummy;
  60. XEvent ev;
  61. ocx = c->x;
  62. ocy = c->y;
  63. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  64. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  65. return;
  66. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  67. for(;;) {
  68. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  69. switch (ev.type) {
  70. default: break;
  71. case Expose:
  72. handler[Expose](&ev);
  73. break;
  74. case MotionNotify:
  75. XSync(dpy, False);
  76. c->x = ocx + (ev.xmotion.x - x1);
  77. c->y = ocy + (ev.xmotion.y - y1);
  78. resize(c, False, TopLeft);
  79. break;
  80. case ButtonRelease:
  81. XUngrabPointer(dpy, CurrentTime);
  82. return;
  83. }
  84. }
  85. }
  86. static void
  87. resizemouse(Client *c)
  88. {
  89. int ocx, ocy;
  90. Corner sticky;
  91. XEvent ev;
  92. ocx = c->x;
  93. ocy = c->y;
  94. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  95. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  96. return;
  97. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  98. for(;;) {
  99. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  100. switch(ev.type) {
  101. default: break;
  102. case Expose:
  103. handler[Expose](&ev);
  104. break;
  105. case MotionNotify:
  106. XSync(dpy, False);
  107. c->w = abs(ocx - ev.xmotion.x);
  108. c->h = abs(ocy - ev.xmotion.y);
  109. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  110. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  111. if(ocx <= ev.xmotion.x)
  112. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  113. else
  114. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  115. resize(c, True, sticky);
  116. break;
  117. case ButtonRelease:
  118. XUngrabPointer(dpy, CurrentTime);
  119. return;
  120. }
  121. }
  122. }
  123. static void
  124. buttonpress(XEvent *e)
  125. {
  126. int x;
  127. Arg a;
  128. Client *c;
  129. XButtonPressedEvent *ev = &e->xbutton;
  130. if(barwin == ev->window) {
  131. switch(ev->button) {
  132. default:
  133. x = 0;
  134. for(a.i = 0; a.i < TLast; a.i++) {
  135. x += textw(tags[a.i]);
  136. if(ev->x < x) {
  137. view(&a);
  138. break;
  139. }
  140. }
  141. break;
  142. case Button4:
  143. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  144. view(&a);
  145. break;
  146. case Button5:
  147. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  148. view(&a);
  149. break;
  150. }
  151. }
  152. else if((c = getclient(ev->window))) {
  153. focus(c);
  154. switch(ev->button) {
  155. default:
  156. break;
  157. case Button1:
  158. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  159. higher(c);
  160. movemouse(c);
  161. }
  162. break;
  163. case Button2:
  164. lower(c);
  165. break;
  166. case Button3:
  167. if(!c->ismax && (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. Client *c;
  179. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  180. XWindowChanges wc;
  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. Client *c;
  221. XCrossingEvent *ev = &e->xcrossing;
  222. if(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. Client *c;
  233. XExposeEvent *ev = &e->xexpose;
  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. static unsigned int len = sizeof(key) / sizeof(key[0]);
  245. unsigned int i;
  246. KeySym keysym;
  247. XKeyEvent *ev = &e->xkey;
  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. static XWindowAttributes wa;
  267. XMapRequestEvent *ev = &e->xmaprequest;
  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. Client *c;
  282. Window trans;
  283. XPropertyEvent *ev = &e->xproperty;
  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 = sizeof(key) / sizeof(key[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. }