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.

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