Configuration of dwm for Mac Computers
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.

496 lines
9.4 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
  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 <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. /* static functions */
  11. static void
  12. resizetitle(Client *c)
  13. {
  14. int i;
  15. c->tw = 0;
  16. for(i = 0; i < ntags; i++)
  17. if(c->tags[i])
  18. c->tw += textw(tags[i]);
  19. c->tw += textw(c->name);
  20. if(c->tw > c->w)
  21. c->tw = c->w + 2;
  22. c->tx = c->x + c->w - c->tw + 2;
  23. c->ty = c->y;
  24. if(c->tags[tsel])
  25. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  26. else
  27. XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
  28. }
  29. static int
  30. xerrordummy(Display *dsply, XErrorEvent *ee)
  31. {
  32. return 0;
  33. }
  34. /* extern functions */
  35. void
  36. ban(Client *c)
  37. {
  38. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  39. XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
  40. }
  41. void
  42. focus(Client *c)
  43. {
  44. Client *old = sel;
  45. XEvent ev;
  46. sel = c;
  47. if(old && old != c)
  48. drawtitle(old);
  49. drawtitle(c);
  50. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  51. XSync(dpy, False);
  52. XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW, 32,
  53. PropModeReplace, (unsigned char *)&c->win, 1);
  54. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  55. }
  56. void
  57. focusnext(Arg *arg)
  58. {
  59. Client *c;
  60. if(!sel)
  61. return;
  62. if(sel->ismax)
  63. togglemax(NULL);
  64. if(!(c = getnext(sel->next)))
  65. c = getnext(clients);
  66. if(c) {
  67. higher(c);
  68. focus(c);
  69. }
  70. }
  71. void
  72. focusprev(Arg *arg)
  73. {
  74. Client *c;
  75. if(!sel)
  76. return;
  77. if(sel->ismax)
  78. togglemax(NULL);
  79. if(!(c = getprev(sel->prev))) {
  80. for(c = clients; c && c->next; c = c->next);
  81. c = getprev(c);
  82. }
  83. if(c) {
  84. higher(c);
  85. focus(c);
  86. }
  87. }
  88. Client *
  89. getclient(Window w)
  90. {
  91. Client *c;
  92. for(c = clients; c; c = c->next)
  93. if(c->win == w)
  94. return c;
  95. return NULL;
  96. }
  97. Client *
  98. getctitle(Window w)
  99. {
  100. Client *c;
  101. for(c = clients; c; c = c->next)
  102. if(c->title == w)
  103. return c;
  104. return NULL;
  105. }
  106. void
  107. gravitate(Client *c, Bool invert)
  108. {
  109. int dx = 0, dy = 0;
  110. switch(c->grav) {
  111. default:
  112. break;
  113. case StaticGravity:
  114. case NorthWestGravity:
  115. case NorthGravity:
  116. case NorthEastGravity:
  117. dy = c->border;
  118. break;
  119. case EastGravity:
  120. case CenterGravity:
  121. case WestGravity:
  122. dy = -(c->h / 2) + c->border;
  123. break;
  124. case SouthEastGravity:
  125. case SouthGravity:
  126. case SouthWestGravity:
  127. dy = -(c->h);
  128. break;
  129. }
  130. switch (c->grav) {
  131. default:
  132. break;
  133. case StaticGravity:
  134. case NorthWestGravity:
  135. case WestGravity:
  136. case SouthWestGravity:
  137. dx = c->border;
  138. break;
  139. case NorthGravity:
  140. case CenterGravity:
  141. case SouthGravity:
  142. dx = -(c->w / 2) + c->border;
  143. break;
  144. case NorthEastGravity:
  145. case EastGravity:
  146. case SouthEastGravity:
  147. dx = -(c->w + c->border);
  148. break;
  149. }
  150. if(invert) {
  151. dx = -dx;
  152. dy = -dy;
  153. }
  154. c->x += dx;
  155. c->y += dy;
  156. }
  157. void
  158. higher(Client *c)
  159. {
  160. XRaiseWindow(dpy, c->win);
  161. XRaiseWindow(dpy, c->title);
  162. }
  163. void
  164. killclient(Arg *arg)
  165. {
  166. if(!sel)
  167. return;
  168. if(sel->proto & PROTODELWIN)
  169. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  170. else
  171. XKillClient(dpy, sel->win);
  172. }
  173. void
  174. lower(Client *c)
  175. {
  176. XLowerWindow(dpy, c->title);
  177. XLowerWindow(dpy, c->win);
  178. }
  179. void
  180. manage(Window w, XWindowAttributes *wa)
  181. {
  182. Client *c;
  183. Window trans;
  184. XSetWindowAttributes twa;
  185. c = emallocz(sizeof(Client));
  186. c->tags = emallocz(ntags * sizeof(Bool));
  187. c->win = w;
  188. c->x = c->tx = wa->x;
  189. c->y = c->ty = wa->y;
  190. c->w = c->tw = wa->width;
  191. c->h = wa->height;
  192. c->th = bh;
  193. c->border = 0;
  194. setsize(c);
  195. if(c->h != sh && c->y < bh)
  196. c->y = c->ty = bh;
  197. c->proto = getproto(c->win);
  198. XSelectInput(dpy, c->win,
  199. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  200. XGetTransientForHint(dpy, c->win, &trans);
  201. twa.override_redirect = 1;
  202. twa.background_pixmap = ParentRelative;
  203. twa.event_mask = ExposureMask | EnterWindowMask;
  204. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  205. 0, DefaultDepth(dpy, screen), CopyFromParent,
  206. DefaultVisual(dpy, screen),
  207. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  208. if(clients)
  209. clients->prev = c;
  210. c->next = clients;
  211. clients = c;
  212. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  213. GrabModeAsync, GrabModeSync, None, None);
  214. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  215. GrabModeAsync, GrabModeSync, None, None);
  216. XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
  217. GrabModeAsync, GrabModeSync, None, None);
  218. XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
  219. GrabModeAsync, GrabModeSync, None, None);
  220. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  221. GrabModeAsync, GrabModeSync, None, None);
  222. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  223. GrabModeAsync, GrabModeSync, None, None);
  224. XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
  225. GrabModeAsync, GrabModeSync, None, None);
  226. XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
  227. GrabModeAsync, GrabModeSync, None, None);
  228. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  229. GrabModeAsync, GrabModeSync, None, None);
  230. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  231. GrabModeAsync, GrabModeSync, None, None);
  232. XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
  233. GrabModeAsync, GrabModeSync, None, None);
  234. XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
  235. GrabModeAsync, GrabModeSync, None, None);
  236. settags(c);
  237. if(!c->isfloat)
  238. c->isfloat = trans
  239. || (c->maxw && c->minw &&
  240. c->maxw == c->minw && c->maxh == c->minh);
  241. settitle(c);
  242. arrange(NULL);
  243. /* mapping the window now prevents flicker */
  244. XMapRaised(dpy, c->win);
  245. XMapRaised(dpy, c->title);
  246. if(c->tags[tsel])
  247. focus(c);
  248. }
  249. void
  250. resize(Client *c, Bool sizehints, Corner sticky)
  251. {
  252. int bottom = c->y + c->h;
  253. int right = c->x + c->w;
  254. /*XConfigureEvent e;*/
  255. XWindowChanges wc;
  256. if(sizehints) {
  257. if(c->incw)
  258. c->w -= (c->w - c->basew) % c->incw;
  259. if(c->inch)
  260. c->h -= (c->h - c->baseh) % c->inch;
  261. if(c->minw && c->w < c->minw)
  262. c->w = c->minw;
  263. if(c->minh && c->h < c->minh)
  264. c->h = c->minh;
  265. if(c->maxw && c->w > c->maxw)
  266. c->w = c->maxw;
  267. if(c->maxh && c->h > c->maxh)
  268. c->h = c->maxh;
  269. }
  270. if(c->x > right) /* might happen on restart */
  271. c->x = right - c->w;
  272. if(c->y > bottom)
  273. c->y = bottom - c->h;
  274. if(sticky == TopRight || sticky == BotRight)
  275. c->x = right - c->w;
  276. if(sticky == BotLeft || sticky == BotRight)
  277. c->y = bottom - c->h;
  278. resizetitle(c);
  279. wc.x = c->x;
  280. wc.y = c->y;
  281. wc.width = c->w;
  282. wc.height = c->h;
  283. if(c->w == sw && c->h == sh)
  284. wc.border_width = 0;
  285. else
  286. wc.border_width = 1;
  287. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  288. XSync(dpy, False);
  289. }
  290. void
  291. setsize(Client *c)
  292. {
  293. long msize;
  294. XSizeHints size;
  295. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  296. size.flags = PSize;
  297. c->flags = size.flags;
  298. if(c->flags & PBaseSize) {
  299. c->basew = size.base_width;
  300. c->baseh = size.base_height;
  301. }
  302. else
  303. c->basew = c->baseh = 0;
  304. if(c->flags & PResizeInc) {
  305. c->incw = size.width_inc;
  306. c->inch = size.height_inc;
  307. }
  308. else
  309. c->incw = c->inch = 0;
  310. if(c->flags & PMaxSize) {
  311. c->maxw = size.max_width;
  312. c->maxh = size.max_height;
  313. }
  314. else
  315. c->maxw = c->maxh = 0;
  316. if(c->flags & PMinSize) {
  317. c->minw = size.min_width;
  318. c->minh = size.min_height;
  319. }
  320. else
  321. c->minw = c->minh = 0;
  322. if(c->flags & PWinGravity)
  323. c->grav = size.win_gravity;
  324. else
  325. c->grav = NorthWestGravity;
  326. }
  327. void
  328. settitle(Client *c)
  329. {
  330. char **list = NULL;
  331. int n;
  332. XTextProperty name;
  333. name.nitems = 0;
  334. c->name[0] = 0;
  335. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  336. if(!name.nitems)
  337. XGetWMName(dpy, c->win, &name);
  338. if(!name.nitems)
  339. return;
  340. if(name.encoding == XA_STRING)
  341. strncpy(c->name, (char *)name.value, sizeof(c->name));
  342. else {
  343. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  344. && n > 0 && *list)
  345. {
  346. strncpy(c->name, *list, sizeof(c->name));
  347. XFreeStringList(list);
  348. }
  349. }
  350. XFree(name.value);
  351. resizetitle(c);
  352. }
  353. void
  354. togglemax(Arg *arg)
  355. {
  356. int ox, oy, ow, oh;
  357. XEvent ev;
  358. if(!sel)
  359. return;
  360. if((sel->ismax = !sel->ismax)) {
  361. ox = sel->x;
  362. oy = sel->y;
  363. ow = sel->w;
  364. oh = sel->h;
  365. sel->x = sx;
  366. sel->y = sy + bh;
  367. sel->w = sw - 2;
  368. sel->h = sh - 2 - bh;
  369. higher(sel);
  370. resize(sel, False, TopLeft);
  371. sel->x = ox;
  372. sel->y = oy;
  373. sel->w = ow;
  374. sel->h = oh;
  375. }
  376. else
  377. resize(sel, False, TopLeft);
  378. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  379. }
  380. void
  381. unmanage(Client *c)
  382. {
  383. XGrabServer(dpy);
  384. XSetErrorHandler(xerrordummy);
  385. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  386. XDestroyWindow(dpy, c->title);
  387. if(c->prev)
  388. c->prev->next = c->next;
  389. if(c->next)
  390. c->next->prev = c->prev;
  391. if(c == clients)
  392. clients = c->next;
  393. if(sel == c) {
  394. sel = getnext(c->next);
  395. if(!sel)
  396. sel = getprev(c->prev);
  397. if(!sel)
  398. sel = clients;
  399. }
  400. free(c->tags);
  401. free(c);
  402. XSync(dpy, False);
  403. XSetErrorHandler(xerror);
  404. XUngrabServer(dpy);
  405. arrange(NULL);
  406. if(sel)
  407. focus(sel);
  408. else
  409. XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW, 32,
  410. PropModeReplace, (unsigned char *)NULL, 1);
  411. }
  412. void
  413. zoom(Arg *arg)
  414. {
  415. Client *c;
  416. if(!sel || (arrange != dotile) || sel->isfloat)
  417. return;
  418. if(sel == getnext(clients)) {
  419. if((c = getnext(sel->next)))
  420. sel = c;
  421. else
  422. return;
  423. }
  424. /* pop */
  425. if(sel->prev)
  426. sel->prev->next = sel->next;
  427. if(sel->next)
  428. sel->next->prev = sel->prev;
  429. sel->prev = NULL;
  430. if(clients)
  431. clients->prev = sel;
  432. sel->next = clients;
  433. clients = sel;
  434. arrange(NULL);
  435. focus(sel);
  436. }