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.

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