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.

384 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. /* END CUSTOMIZE */
  56. /* static */
  57. static void
  58. movemouse(Client *c)
  59. {
  60. int x1, y1, ocx, ocy, di;
  61. unsigned int dui;
  62. Window dummy;
  63. XEvent ev;
  64. ocx = c->x;
  65. ocy = c->y;
  66. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  67. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  68. return;
  69. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  70. for(;;) {
  71. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  72. switch (ev.type) {
  73. default: break;
  74. case Expose:
  75. handler[Expose](&ev);
  76. break;
  77. case MotionNotify:
  78. XSync(dpy, False);
  79. c->x = ocx + (ev.xmotion.x - x1);
  80. c->y = ocy + (ev.xmotion.y - y1);
  81. resize(c, False, TopLeft);
  82. break;
  83. case ButtonRelease:
  84. XUngrabPointer(dpy, CurrentTime);
  85. return;
  86. }
  87. }
  88. }
  89. static void
  90. resizemouse(Client *c)
  91. {
  92. int ocx, ocy;
  93. Corner sticky;
  94. XEvent ev;
  95. ocx = c->x;
  96. ocy = c->y;
  97. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  98. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  99. return;
  100. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  101. for(;;) {
  102. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  103. switch(ev.type) {
  104. default: break;
  105. case Expose:
  106. handler[Expose](&ev);
  107. break;
  108. case MotionNotify:
  109. XSync(dpy, False);
  110. c->w = abs(ocx - ev.xmotion.x);
  111. c->h = abs(ocy - ev.xmotion.y);
  112. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  113. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  114. if(ocx <= ev.xmotion.x)
  115. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  116. else
  117. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  118. resize(c, True, sticky);
  119. break;
  120. case ButtonRelease:
  121. XUngrabPointer(dpy, CurrentTime);
  122. return;
  123. }
  124. }
  125. }
  126. static void
  127. buttonpress(XEvent *e)
  128. {
  129. int x;
  130. Arg a;
  131. Client *c;
  132. XButtonPressedEvent *ev = &e->xbutton;
  133. if(barwin == ev->window) {
  134. switch(ev->button) {
  135. default:
  136. x = 0;
  137. for(a.i = 0; a.i < TLast; a.i++) {
  138. x += textw(tags[a.i]);
  139. if(ev->x < x) {
  140. view(&a);
  141. break;
  142. }
  143. }
  144. break;
  145. case Button4:
  146. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  147. view(&a);
  148. break;
  149. case Button5:
  150. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  151. view(&a);
  152. break;
  153. }
  154. }
  155. else if((c = getclient(ev->window))) {
  156. switch(ev->button) {
  157. default:
  158. break;
  159. case Button1:
  160. if(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(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->mode != NotifyNormal || 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 = key ? sizeof(key) / sizeof(key[0]) : 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 = key ? sizeof(key) / sizeof(key[0]) : 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. }