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.

479 lines
9.0 KiB

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