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.

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