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.

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