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.

469 lines
8.8 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
  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. }
  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)))
  63. c = getnext(clients);
  64. if(c) {
  65. focus(c);
  66. restack();
  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. focus(c);
  83. restack();
  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. killclient(Arg *arg)
  157. {
  158. if(!sel)
  159. return;
  160. if(sel->proto & PROTODELWIN)
  161. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  162. else
  163. XKillClient(dpy, sel->win);
  164. }
  165. void
  166. manage(Window w, XWindowAttributes *wa)
  167. {
  168. Client *c;
  169. Window trans;
  170. XSetWindowAttributes twa;
  171. c = emallocz(sizeof(Client));
  172. c->tags = emallocz(ntags * sizeof(Bool));
  173. c->win = w;
  174. c->x = c->tx = wa->x;
  175. c->y = c->ty = wa->y;
  176. c->w = c->tw = wa->width;
  177. c->h = wa->height;
  178. c->th = bh;
  179. c->border = 0;
  180. setsize(c);
  181. if(c->h != sh && c->y < bh)
  182. c->y = c->ty = bh;
  183. c->proto = getproto(c->win);
  184. XSelectInput(dpy, c->win,
  185. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  186. XGetTransientForHint(dpy, c->win, &trans);
  187. twa.override_redirect = 1;
  188. twa.background_pixmap = ParentRelative;
  189. twa.event_mask = ExposureMask | EnterWindowMask;
  190. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  191. 0, DefaultDepth(dpy, screen), CopyFromParent,
  192. DefaultVisual(dpy, screen),
  193. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  194. if(clients)
  195. clients->prev = c;
  196. c->next = clients;
  197. clients = c;
  198. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  199. GrabModeAsync, GrabModeSync, None, None);
  200. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  201. GrabModeAsync, GrabModeSync, None, None);
  202. XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
  203. GrabModeAsync, GrabModeSync, None, None);
  204. XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
  205. GrabModeAsync, GrabModeSync, None, None);
  206. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  207. GrabModeAsync, GrabModeSync, None, None);
  208. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  209. GrabModeAsync, GrabModeSync, None, None);
  210. XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
  211. GrabModeAsync, GrabModeSync, None, None);
  212. XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
  213. GrabModeAsync, GrabModeSync, None, None);
  214. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  215. GrabModeAsync, GrabModeSync, None, None);
  216. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  217. GrabModeAsync, GrabModeSync, None, None);
  218. XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
  219. GrabModeAsync, GrabModeSync, None, None);
  220. XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
  221. GrabModeAsync, GrabModeSync, None, None);
  222. settags(c);
  223. if(!c->isfloat)
  224. c->isfloat = trans
  225. || (c->maxw && c->minw &&
  226. c->maxw == c->minw && c->maxh == c->minh);
  227. settitle(c);
  228. XMapWindow(dpy, c->win);
  229. XMapWindow(dpy, c->title);
  230. if(isvisible(c))
  231. focus(c);
  232. arrange(NULL);
  233. }
  234. void
  235. resize(Client *c, Bool sizehints, Corner sticky)
  236. {
  237. int bottom = c->y + c->h;
  238. int right = c->x + c->w;
  239. /*XConfigureEvent e;*/
  240. XWindowChanges wc;
  241. if(sizehints) {
  242. if(c->incw)
  243. c->w -= (c->w - c->basew) % c->incw;
  244. if(c->inch)
  245. c->h -= (c->h - c->baseh) % c->inch;
  246. if(c->minw && c->w < c->minw)
  247. c->w = c->minw;
  248. if(c->minh && c->h < c->minh)
  249. c->h = c->minh;
  250. if(c->maxw && c->w > c->maxw)
  251. c->w = c->maxw;
  252. if(c->maxh && c->h > c->maxh)
  253. c->h = c->maxh;
  254. }
  255. if(c->x > right) /* might happen on restart */
  256. c->x = right - c->w;
  257. if(c->y > bottom)
  258. c->y = bottom - c->h;
  259. if(sticky == TopRight || sticky == BotRight)
  260. c->x = right - c->w;
  261. if(sticky == BotLeft || sticky == BotRight)
  262. c->y = bottom - c->h;
  263. resizetitle(c);
  264. wc.x = c->x;
  265. wc.y = c->y;
  266. wc.width = c->w;
  267. wc.height = c->h;
  268. if(c->w == sw && c->h == sh)
  269. wc.border_width = 0;
  270. else
  271. wc.border_width = 1;
  272. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  273. XSync(dpy, False);
  274. }
  275. void
  276. setsize(Client *c)
  277. {
  278. long msize;
  279. XSizeHints size;
  280. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  281. size.flags = PSize;
  282. c->flags = size.flags;
  283. if(c->flags & PBaseSize) {
  284. c->basew = size.base_width;
  285. c->baseh = size.base_height;
  286. }
  287. else
  288. c->basew = c->baseh = 0;
  289. if(c->flags & PResizeInc) {
  290. c->incw = size.width_inc;
  291. c->inch = size.height_inc;
  292. }
  293. else
  294. c->incw = c->inch = 0;
  295. if(c->flags & PMaxSize) {
  296. c->maxw = size.max_width;
  297. c->maxh = size.max_height;
  298. }
  299. else
  300. c->maxw = c->maxh = 0;
  301. if(c->flags & PMinSize) {
  302. c->minw = size.min_width;
  303. c->minh = size.min_height;
  304. }
  305. else
  306. c->minw = c->minh = 0;
  307. if(c->flags & PWinGravity)
  308. c->grav = size.win_gravity;
  309. else
  310. c->grav = NorthWestGravity;
  311. }
  312. void
  313. settitle(Client *c)
  314. {
  315. char **list = NULL;
  316. int n;
  317. XTextProperty name;
  318. name.nitems = 0;
  319. c->name[0] = 0;
  320. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  321. if(!name.nitems)
  322. XGetWMName(dpy, c->win, &name);
  323. if(!name.nitems)
  324. return;
  325. if(name.encoding == XA_STRING)
  326. strncpy(c->name, (char *)name.value, sizeof(c->name));
  327. else {
  328. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  329. && n > 0 && *list)
  330. {
  331. strncpy(c->name, *list, sizeof(c->name));
  332. XFreeStringList(list);
  333. }
  334. }
  335. XFree(name.value);
  336. resizetitle(c);
  337. }
  338. void
  339. togglemax(Arg *arg)
  340. {
  341. int ox, oy, ow, oh;
  342. XEvent ev;
  343. if(!sel)
  344. return;
  345. if((sel->ismax = !sel->ismax)) {
  346. ox = sel->x;
  347. oy = sel->y;
  348. ow = sel->w;
  349. oh = sel->h;
  350. sel->x = sx;
  351. sel->y = sy + bh;
  352. sel->w = sw - 2;
  353. sel->h = sh - 2 - bh;
  354. restack();
  355. resize(sel, arrange == dofloat, TopLeft);
  356. sel->x = ox;
  357. sel->y = oy;
  358. sel->w = ow;
  359. sel->h = oh;
  360. }
  361. else
  362. resize(sel, False, TopLeft);
  363. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  364. }
  365. void
  366. unmanage(Client *c)
  367. {
  368. XGrabServer(dpy);
  369. XSetErrorHandler(xerrordummy);
  370. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  371. XDestroyWindow(dpy, c->title);
  372. if(c->prev)
  373. c->prev->next = c->next;
  374. if(c->next)
  375. c->next->prev = c->prev;
  376. if(c == clients)
  377. clients = c->next;
  378. if(sel == c)
  379. sel = getnext(clients);
  380. free(c->tags);
  381. free(c);
  382. XSync(dpy, False);
  383. XSetErrorHandler(xerror);
  384. XUngrabServer(dpy);
  385. if(sel)
  386. focus(sel);
  387. arrange(NULL);
  388. }
  389. void
  390. zoom(Arg *arg)
  391. {
  392. Client *c;
  393. if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
  394. return;
  395. if(sel == getnext(clients)) {
  396. if((c = getnext(sel->next)))
  397. sel = c;
  398. else
  399. return;
  400. }
  401. /* pop */
  402. sel->prev->next = sel->next;
  403. if(sel->next)
  404. sel->next->prev = sel->prev;
  405. sel->prev = NULL;
  406. clients->prev = sel;
  407. sel->next = clients;
  408. clients = sel;
  409. focus(sel);
  410. arrange(NULL);
  411. }