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.

382 lines
8.3 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
  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. typedef struct {
  13. unsigned long mod;
  14. KeySym keysym;
  15. void (*func)(Arg *arg);
  16. Arg arg;
  17. } Key;
  18. const char *browse[] = { "firefox", NULL };
  19. const char *gimp[] = { "gimp", NULL };
  20. const char *term[] = {
  21. "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
  22. "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
  23. };
  24. const char *xlock[] = { "xlock", NULL };
  25. static Key key[] = {
  26. /* modifier key function arguments */
  27. { ControlMask, XK_0, appendtag, { .i = Tscratch } },
  28. { ControlMask, XK_1, appendtag, { .i = Tdev } },
  29. { ControlMask, XK_2, appendtag, { .i = Twww } },
  30. { ControlMask, XK_3, appendtag, { .i = Twork } },
  31. { MODKEY, XK_0, view, { .i = Tscratch } },
  32. { MODKEY, XK_1, view, { .i = Tdev } },
  33. { MODKEY, XK_2, view, { .i = Twww } },
  34. { MODKEY, XK_3, view, { .i = Twork } },
  35. { MODKEY, XK_j, focusnext, { 0 } },
  36. { MODKEY, XK_k, focusprev, { 0 } },
  37. { MODKEY, XK_m, togglemax, { 0 } },
  38. { MODKEY, XK_space, togglemode, { 0 } },
  39. { MODKEY, XK_Return, zoom, { 0 } },
  40. { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
  41. { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
  42. { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
  43. { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
  44. { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
  45. { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
  46. { MODKEY|ShiftMask, XK_2, replacetag, { .i = Twww } },
  47. { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
  48. { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
  49. { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
  50. { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
  51. { MODKEY|ShiftMask, XK_q, quit, { 0 } },
  52. { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
  53. { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
  54. };
  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. switch(ev->button) {
  156. default:
  157. break;
  158. case Button1:
  159. if(arrange == dofloat || c->isfloat) {
  160. higher(c);
  161. movemouse(c);
  162. }
  163. break;
  164. case Button2:
  165. lower(c);
  166. break;
  167. case Button3:
  168. if(arrange == dofloat || c->isfloat) {
  169. higher(c);
  170. resizemouse(c);
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. static void
  177. configurerequest(XEvent *e)
  178. {
  179. Client *c;
  180. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  181. XWindowChanges wc;
  182. ev->value_mask &= ~CWSibling;
  183. if((c = getclient(ev->window))) {
  184. gravitate(c, True);
  185. if(ev->value_mask & CWX)
  186. c->x = ev->x;
  187. if(ev->value_mask & CWY)
  188. c->y = ev->y;
  189. if(ev->value_mask & CWWidth)
  190. c->w = ev->width;
  191. if(ev->value_mask & CWHeight)
  192. c->h = ev->height;
  193. if(ev->value_mask & CWBorderWidth)
  194. c->border = 1;
  195. gravitate(c, False);
  196. resize(c, True, TopLeft);
  197. }
  198. wc.x = ev->x;
  199. wc.y = ev->y;
  200. wc.width = ev->width;
  201. wc.height = ev->height;
  202. wc.border_width = 1;
  203. wc.sibling = None;
  204. wc.stack_mode = Above;
  205. ev->value_mask &= ~CWStackMode;
  206. ev->value_mask |= CWBorderWidth;
  207. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  208. XSync(dpy, False);
  209. }
  210. static void
  211. destroynotify(XEvent *e)
  212. {
  213. Client *c;
  214. XDestroyWindowEvent *ev = &e->xdestroywindow;
  215. if((c = getclient(ev->window)))
  216. unmanage(c);
  217. }
  218. static void
  219. enternotify(XEvent *e)
  220. {
  221. Client *c;
  222. XCrossingEvent *ev = &e->xcrossing;
  223. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  224. return;
  225. if((c = getclient(ev->window)))
  226. focus(c);
  227. else if(ev->window == root)
  228. issel = True;
  229. }
  230. static void
  231. expose(XEvent *e)
  232. {
  233. Client *c;
  234. XExposeEvent *ev = &e->xexpose;
  235. if(ev->count == 0) {
  236. if(barwin == ev->window)
  237. drawstatus();
  238. else if((c = getctitle(ev->window)))
  239. drawtitle(c);
  240. }
  241. }
  242. static void
  243. keypress(XEvent *e)
  244. {
  245. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  246. unsigned int i;
  247. KeySym keysym;
  248. XKeyEvent *ev = &e->xkey;
  249. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  250. for(i = 0; i < len; i++)
  251. if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
  252. if(key[i].func)
  253. key[i].func(&key[i].arg);
  254. return;
  255. }
  256. }
  257. static void
  258. leavenotify(XEvent *e)
  259. {
  260. XCrossingEvent *ev = &e->xcrossing;
  261. if((ev->window == root) && !ev->same_screen)
  262. issel = True;
  263. }
  264. static void
  265. maprequest(XEvent *e)
  266. {
  267. static XWindowAttributes wa;
  268. XMapRequestEvent *ev = &e->xmaprequest;
  269. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  270. return;
  271. if(wa.override_redirect) {
  272. XSelectInput(dpy, ev->window,
  273. (StructureNotifyMask | PropertyChangeMask));
  274. return;
  275. }
  276. if(!getclient(ev->window))
  277. manage(ev->window, &wa);
  278. }
  279. static void
  280. propertynotify(XEvent *e)
  281. {
  282. Client *c;
  283. Window trans;
  284. XPropertyEvent *ev = &e->xproperty;
  285. if(ev->state == PropertyDelete)
  286. return; /* ignore */
  287. if((c = getclient(ev->window))) {
  288. if(ev->atom == wmatom[WMProtocols]) {
  289. c->proto = getproto(c->win);
  290. return;
  291. }
  292. switch (ev->atom) {
  293. default: break;
  294. case XA_WM_TRANSIENT_FOR:
  295. XGetTransientForHint(dpy, c->win, &trans);
  296. if(!c->isfloat && (c->isfloat = (trans != 0)))
  297. arrange(NULL);
  298. break;
  299. case XA_WM_NORMAL_HINTS:
  300. setsize(c);
  301. break;
  302. }
  303. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  304. settitle(c);
  305. drawtitle(c);
  306. }
  307. }
  308. }
  309. static void
  310. unmapnotify(XEvent *e)
  311. {
  312. Client *c;
  313. XUnmapEvent *ev = &e->xunmap;
  314. if((c = getclient(ev->window)))
  315. unmanage(c);
  316. }
  317. /* extern */
  318. void (*handler[LASTEvent]) (XEvent *) = {
  319. [ButtonPress] = buttonpress,
  320. [ConfigureRequest] = configurerequest,
  321. [DestroyNotify] = destroynotify,
  322. [EnterNotify] = enternotify,
  323. [LeaveNotify] = leavenotify,
  324. [Expose] = expose,
  325. [KeyPress] = keypress,
  326. [MapRequest] = maprequest,
  327. [PropertyNotify] = propertynotify,
  328. [UnmapNotify] = unmapnotify
  329. };
  330. void
  331. grabkeys()
  332. {
  333. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  334. unsigned int i;
  335. KeyCode code;
  336. for(i = 0; i < len; i++) {
  337. code = XKeysymToKeycode(dpy, key[i].keysym);
  338. XUngrabKey(dpy, code, key[i].mod, root);
  339. XGrabKey(dpy, code, key[i].mod, root, True,
  340. GrabModeAsync, GrabModeAsync);
  341. }
  342. }