Configuration file for DWM on MacBook Air
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.

423 lines
7.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
  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. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  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->tx + 2 * sw, c->ty);
  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)))
  58. c = getnext(clients);
  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. Client *c, **l;
  171. XSetWindowAttributes twa;
  172. Window trans;
  173. c = emallocz(sizeof(Client));
  174. c->win = w;
  175. c->tx = c->x = wa->x;
  176. c->ty = c->y = wa->y;
  177. if(c->y < bh)
  178. c->ty = c->y += bh;
  179. c->tw = c->w = wa->width;
  180. c->h = wa->height;
  181. c->th = bh;
  182. c->border = 1;
  183. c->proto = getproto(c->win);
  184. setsize(c);
  185. XSelectInput(dpy, c->win,
  186. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  187. XGetTransientForHint(dpy, c->win, &trans);
  188. twa.override_redirect = 1;
  189. twa.background_pixmap = ParentRelative;
  190. twa.event_mask = ExposureMask;
  191. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  192. 0, DefaultDepth(dpy, screen), CopyFromParent,
  193. DefaultVisual(dpy, screen),
  194. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  195. settitle(c);
  196. settags(c);
  197. for(l = &clients; *l; l = &(*l)->next);
  198. c->next = *l; /* *l == nil */
  199. *l = c;
  200. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  201. GrabModeAsync, GrabModeSync, None, None);
  202. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  203. GrabModeAsync, GrabModeSync, None, None);
  204. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  205. GrabModeAsync, GrabModeSync, None, None);
  206. if(!c->dofloat)
  207. c->dofloat = trans
  208. || ((c->maxw == c->minw) && (c->maxh == c->minh));
  209. arrange(NULL);
  210. /* mapping the window now prevents flicker */
  211. if(c->tags[tsel]) {
  212. XMapRaised(dpy, c->win);
  213. XMapRaised(dpy, c->title);
  214. focus(c);
  215. }
  216. else {
  217. ban(c);
  218. XMapRaised(dpy, c->win);
  219. XMapRaised(dpy, c->title);
  220. }
  221. }
  222. void
  223. maximize(Arg *arg)
  224. {
  225. if(!sel)
  226. return;
  227. sel->x = sx;
  228. sel->y = sy + bh;
  229. sel->w = sw - 2 * sel->border;
  230. sel->h = sh - 2 * sel->border - bh;
  231. higher(sel);
  232. resize(sel, False);
  233. }
  234. void
  235. resize(Client *c, Bool inc)
  236. {
  237. XConfigureEvent e;
  238. if(inc) {
  239. if(c->incw)
  240. c->w -= (c->w - c->basew) % c->incw;
  241. if(c->inch)
  242. c->h -= (c->h - c->baseh) % c->inch;
  243. }
  244. if(c->x > sw) /* might happen on restart */
  245. c->x = sw - c->w;
  246. if(c->y > sh)
  247. c->ty = c->y = sh - c->h;
  248. if(c->minw && c->w < c->minw)
  249. c->w = c->minw;
  250. if(c->minh && c->h < c->minh)
  251. c->h = c->minh;
  252. if(c->maxw && c->w > c->maxw)
  253. c->w = c->maxw;
  254. if(c->maxh && c->h > c->maxh)
  255. c->h = c->maxh;
  256. resizetitle(c);
  257. XSetWindowBorderWidth(dpy, c->win, 1);
  258. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  259. e.type = ConfigureNotify;
  260. e.event = c->win;
  261. e.window = c->win;
  262. e.x = c->x;
  263. e.y = c->y;
  264. e.width = c->w;
  265. e.height = c->h;
  266. e.border_width = c->border;
  267. e.above = None;
  268. e.override_redirect = False;
  269. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  270. XSync(dpy, False);
  271. }
  272. void
  273. setsize(Client *c)
  274. {
  275. XSizeHints size;
  276. long msize;
  277. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  278. size.flags = PSize;
  279. c->flags = size.flags;
  280. if(c->flags & PBaseSize) {
  281. c->basew = size.base_width;
  282. c->baseh = size.base_height;
  283. }
  284. else
  285. c->basew = c->baseh = 0;
  286. if(c->flags & PResizeInc) {
  287. c->incw = size.width_inc;
  288. c->inch = size.height_inc;
  289. }
  290. else
  291. c->incw = c->inch = 0;
  292. if(c->flags & PMaxSize) {
  293. c->maxw = size.max_width;
  294. c->maxh = size.max_height;
  295. }
  296. else
  297. c->maxw = c->maxh = 0;
  298. if(c->flags & PMinSize) {
  299. c->minw = size.min_width;
  300. c->minh = size.min_height;
  301. }
  302. else
  303. c->minw = c->minh = 0;
  304. if(c->flags & PWinGravity)
  305. c->grav = size.win_gravity;
  306. else
  307. c->grav = NorthWestGravity;
  308. }
  309. void
  310. settitle(Client *c)
  311. {
  312. XTextProperty name;
  313. int n;
  314. char **list = NULL;
  315. name.nitems = 0;
  316. c->name[0] = 0;
  317. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  318. if(!name.nitems)
  319. XGetWMName(dpy, c->win, &name);
  320. if(!name.nitems)
  321. return;
  322. if(name.encoding == XA_STRING)
  323. strncpy(c->name, (char *)name.value, sizeof(c->name));
  324. else {
  325. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  326. && n > 0 && *list)
  327. {
  328. strncpy(c->name, *list, sizeof(c->name));
  329. XFreeStringList(list);
  330. }
  331. }
  332. XFree(name.value);
  333. resizetitle(c);
  334. }
  335. void
  336. unmanage(Client *c)
  337. {
  338. Client **l;
  339. XGrabServer(dpy);
  340. XSetErrorHandler(xerrordummy);
  341. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  342. XDestroyWindow(dpy, c->title);
  343. for(l = &clients; *l && *l != c; l = &(*l)->next);
  344. *l = c->next;
  345. for(l = &clients; *l; l = &(*l)->next)
  346. if((*l)->revert == c)
  347. (*l)->revert = NULL;
  348. if(sel == c)
  349. sel = sel->revert ? sel->revert : clients;
  350. free(c);
  351. XSync(dpy, False);
  352. XSetErrorHandler(xerror);
  353. XUngrabServer(dpy);
  354. arrange(NULL);
  355. if(sel)
  356. focus(sel);
  357. }
  358. void
  359. zoom(Arg *arg)
  360. {
  361. Client **l, *c;
  362. if(!sel)
  363. return;
  364. if(sel == getnext(clients) && sel->next) {
  365. if((c = getnext(sel->next)))
  366. sel = c;
  367. }
  368. for(l = &clients; *l && *l != sel; l = &(*l)->next);
  369. *l = sel->next;
  370. sel->next = clients; /* pop */
  371. clients = sel;
  372. arrange(NULL);
  373. focus(sel);
  374. }