Configuration file for DWM on MacBook Air
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.

418 lines
8.5 KiB

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