change info in copyright
[awesome.git] / client.c
blobd572eeeabdbae4ba834d6531b3305baf62106dfe
1 /*
2 * client.c - client management
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/shape.h>
26 #include "screen.h"
27 #include "awesome.h"
28 #include "layout.h"
29 #include "tag.h"
30 #include "rules.h"
31 #include "util.h"
32 #include "xutil.h"
33 #include "statusbar.h"
34 #include "window.h"
35 #include "focus.h"
36 #include "layouts/floating.h"
39 extern AwesomeConf globalconf;
41 /** Load windows properties, restoring client's tag
42 * and floating state before awesome was restarted if any
43 * \todo this may bug if number of tags is != than before
44 * \param c Client ref
45 * \param screen Screen ID
47 static Bool
48 client_loadprops(Client * c, int screen)
50 int i, ntags = 0;
51 Tag *tag;
52 char *prop;
53 Bool result = False;
55 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
56 ntags++;
58 prop = p_new(char, ntags + 2);
60 if(xgettextprop(c->display, c->win, AWESOMEPROPS_ATOM(c->display), prop, ntags + 2))
62 for(i = 0, tag = globalconf.screens[screen].tags; tag && i < ntags && prop[i]; i++, tag = tag->next)
63 if(prop[i] == '1')
65 tag_client(c, tag, screen);
66 result = True;
68 else
69 untag_client(c, tag, screen);
71 if(i <= ntags && prop[i])
72 c->isfloating = prop[i] == '1';
75 p_delete(&prop);
77 return result;
80 /** Check if client supports protocol WM_DELETE_WINDOW
81 * \param disp the display
82 * \param win the Window
83 * \return True if client has WM_DELETE_WINDOW
85 static Bool
86 isprotodel(Display *disp, Window win)
88 int i, n;
89 Atom *protocols;
90 Bool ret = False;
92 if(XGetWMProtocols(disp, win, &protocols, &n))
94 for(i = 0; !ret && i < n; i++)
95 if(protocols[i] == XInternAtom(disp, "WM_DELETE_WINDOW", False))
96 ret = True;
97 XFree(protocols);
99 return ret;
102 /** Swap two client in the linked list clients
103 * \param head pointer ito the client list head
104 * \param c1 first client
105 * \param c2 second client
107 static void
108 client_swap(Client **head, Client *c1, Client *c2)
110 Client *tmp;
112 tmp = c1->next;
113 c1->next = c2->next;
114 c2->next = (tmp == c2 ? c1 : tmp);
116 tmp = c2->prev;
117 c2->prev = c1->prev;
118 c1->prev = (tmp == c1 ? c2 : tmp );
120 if(c1->next)
121 c1->next->prev = c1;
123 if(c1->prev)
124 c1->prev->next = c1;
126 if(c2->next)
127 c2->next->prev = c2;
129 if(c2->prev)
130 c2->prev->next = c2;
132 if(*head == c1)
133 *head = c2;
136 /** Get a Client by its window
137 * \param list Client list to look info
138 * \param w Client window to find
139 * \return client
141 Client *
142 get_client_bywin(Client *list, Window w)
144 Client *c;
146 for(c = list; c && c->win != w; c = c->next);
147 return c;
150 void
151 client_updatetitle(Client *c)
153 if(!xgettextprop(c->display, c->win, XInternAtom(c->display, "_NET_WM_NAME", False), c->name, sizeof(c->name)))
154 xgettextprop(c->display, c->win, XInternAtom(c->display, "WM_NAME", False), c->name, sizeof(c->name));
157 /** Ban client and unmap it
158 * \param c the client
160 void
161 client_ban(Client * c)
163 XUnmapWindow(c->display, c->win);
164 window_setstate(c->display, c->win, IconicState);
167 /** Attach client to the beginning of the clients stack
168 * \param c the client
170 void
171 client_attach(Client *c)
173 if(globalconf.clients)
174 (globalconf.clients)->prev = c;
175 c->next = globalconf.clients;
176 globalconf.clients = c;
179 /** Detach client from clients list
180 * \param c client to detach
182 void
183 client_detach(Client *c)
185 if(c->prev)
186 c->prev->next = c->next;
187 if(c->next)
188 c->next->prev = c->prev;
189 if(c == globalconf.clients)
190 globalconf.clients = c->next;
191 c->next = c->prev = NULL;
194 /** Give focus to client, or to first client if c is NULL
195 * \param c client
196 * \param selscreen True if current screen is selected
197 * \param screen Screen ID
199 void
200 focus(Client *c, Bool selscreen, int screen)
202 /* unfocus current selected client */
203 if(globalconf.focus->client)
205 window_grabbuttons(globalconf.focus->client->display, globalconf.focus->client->phys_screen,
206 globalconf.focus->client->win, False, True);
207 XSetWindowBorder(globalconf.focus->client->display, globalconf.focus->client->win,
208 globalconf.screens[screen].colors_normal[ColBorder].pixel);
209 window_settrans(globalconf.focus->client->display, globalconf.focus->client->win,
210 globalconf.screens[screen].opacity_unfocused);
213 /* if c is NULL or invisible, take next client in the stack */
214 if((!c && selscreen) || (c && !client_isvisible(c, screen)))
215 for(c = globalconf.clients; c && !client_isvisible(c, screen); c = c->next);
217 if(c)
219 XSetWindowBorder(globalconf.display, c->win, globalconf.screens[screen].colors_selected[ColBorder].pixel);
220 window_grabbuttons(c->display, c->phys_screen, c->win,
221 True, True);
224 if(!selscreen)
225 return;
227 /* save old sel in focus history */
228 focus_add_client(c);
230 statusbar_draw(screen);
232 if(globalconf.focus->client)
234 XSetInputFocus(globalconf.focus->client->display,
235 globalconf.focus->client->win, RevertToPointerRoot, CurrentTime);
236 for(c = globalconf.clients; c; c = c->next)
237 if(c != globalconf.focus->client)
238 window_settrans(globalconf.display, globalconf.focus->client->win,
239 globalconf.screens[screen].opacity_unfocused);
240 window_settrans(globalconf.display, globalconf.focus->client->win, -1);
242 else
243 XSetInputFocus(globalconf.display,
244 RootWindow(globalconf.display, get_phys_screen(screen)),
245 RevertToPointerRoot, CurrentTime);
248 /** Manage a new client
249 * \param w The window
250 * \param wa Window attributes
251 * \param screen Screen ID
253 void
254 client_manage(Window w, XWindowAttributes *wa, int screen)
256 Client *c, *t = NULL;
257 Window trans;
258 Status rettrans;
259 XWindowChanges wc;
260 ScreenInfo *screen_info;
261 Tag *tag;
263 c = p_new(Client, 1);
265 c->win = w;
266 c->x = c->rx = wa->x;
267 c->y = c->ry = wa->y;
268 c->w = c->rw = wa->width;
269 c->h = c->rh = wa->height;
270 c->oldborder = wa->border_width;
272 c->display = globalconf.display;
273 c->screen = get_screen_bycoord(c->x, c->y);
274 c->phys_screen = get_phys_screen(c->screen);
276 move_client_to_screen(c, screen, True);
278 /* update window title */
279 client_updatetitle(c);
281 /* loadprops or apply rules if no props */
282 if(!client_loadprops(c, screen))
283 tag_client_with_rules(c);
285 screen_info = get_screen_info(screen, NULL, NULL);
287 /* if window request fullscreen mode */
288 if(c->w == screen_info[screen].width && c->h == screen_info[screen].height)
290 c->x = screen_info[screen].x_org;
291 c->y = screen_info[screen].y_org;
293 c->border = wa->border_width;
295 else
297 ScreenInfo *display_info = get_display_info(c->phys_screen,
298 globalconf.screens[screen].statusbar,
299 &globalconf.screens[screen].padding);
301 if(c->x + c->w + 2 * c->border > display_info->x_org + display_info->width)
302 c->x = c->rx = display_info->x_org + display_info->width - c->w - 2 * c->border;
303 if(c->y + c->h + 2 * c->border > display_info->y_org + display_info->height)
304 c->y = c->ry = display_info->y_org + display_info->height - c->h - 2 * c->border;
305 if(c->x < display_info->x_org)
306 c->x = c->rx = display_info->x_org;
307 if(c->y < display_info->y_org)
308 c->y = c->ry = display_info->y_org;
310 c->border = globalconf.screens[screen].borderpx;
312 p_delete(&display_info);
314 p_delete(&screen_info);
316 /* set borders */
317 wc.border_width = c->border;
318 XConfigureWindow(c->display, w, CWBorderWidth, &wc);
319 XSetWindowBorder(c->display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
321 /* propagates border_width, if size doesn't change */
322 window_configure(c->display, c->win, c->x, c->y, c->w, c->h, c->border);
324 /* update sizehint */
325 client_updatesizehints(c);
327 XSelectInput(c->display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
329 /* handle xshape */
330 if(globalconf.have_shape)
332 XShapeSelectInput(c->display, w, ShapeNotifyMask);
333 window_setshape(c->display, c->phys_screen, c->win);
336 /* grab buttons */
337 window_grabbuttons(c->display, c->phys_screen, c->win, False, True);
339 /* check for transient and set tags like its parent */
340 if((rettrans = XGetTransientForHint(c->display, w, &trans) == Success)
341 && (t = get_client_bywin(globalconf.clients, trans)))
342 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
343 if(is_client_tagged(t, tag, c->screen))
344 tag_client(c, tag, c->screen);
346 /* should be floating if transsient or fixed) */
347 if(!c->isfloating)
348 c->isfloating = (rettrans == Success) || c->isfixed;
350 /* save new props */
351 client_saveprops(c, c->screen);
353 /* attach to the stack */
354 client_attach(c);
356 /* some windows require this */
357 XMoveResizeWindow(c->display, c->win, c->x, c->y, c->w, c->h);
359 focus(c, True, screen);
361 /* rearrange to display new window */
362 arrange(screen);
365 void
366 client_resize(Client *c, int x, int y, int w, int h,
367 Bool sizehints, Bool volatile_coords)
369 double dx, dy, max, min, ratio;
370 XWindowChanges wc;
371 ScreenInfo *si;
373 if(sizehints)
375 if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0)
377 dx = (double) (w - c->basew);
378 dy = (double) (h - c->baseh);
379 min = (double) (c->minax) / (double) (c->minay);
380 max = (double) (c->maxax) / (double) (c->maxay);
381 ratio = dx / dy;
382 if(max > 0 && min > 0 && ratio > 0)
384 if(ratio < min)
386 dy = (dx * min + dy) / (min * min + 1);
387 dx = dy * min;
388 w = (int) dx + c->basew;
389 h = (int) dy + c->baseh;
391 else if(ratio > max)
393 dy = (dx * min + dy) / (max * max + 1);
394 dx = dy * min;
395 w = (int) dx + c->basew;
396 h = (int) dy + c->baseh;
400 if(c->minw && w < c->minw)
401 w = c->minw;
402 if(c->minh && h < c->minh)
403 h = c->minh;
404 if(c->maxw && w > c->maxw)
405 w = c->maxw;
406 if(c->maxh && h > c->maxh)
407 h = c->maxh;
408 if(c->incw)
409 w -= (w - c->basew) % c->incw;
410 if(c->inch)
411 h -= (h - c->baseh) % c->inch;
413 if(w <= 0 || h <= 0)
414 return;
415 /* offscreen appearance fixes */
416 si = get_display_info(c->phys_screen,
417 NULL,
418 &globalconf.screens[c->screen].padding);
419 if(x > si->width)
420 x = si->width - w - 2 * c->border;
421 if(y > si->height)
422 y = si->height - h - 2 * c->border;
423 p_delete(&si);
424 if(x + w + 2 * c->border < 0)
425 x = 0;
426 if(y + h + 2 * c->border < 0)
427 y = 0;
428 if(c->x != x || c->y != y || c->w != w || c->h != h)
430 c->x = wc.x = x;
431 c->y = wc.y = y;
432 c->w = wc.width = w;
433 c->h = wc.height = h;
434 if(!volatile_coords
435 && (c->isfloating
436 || get_current_layout(c->screen)->arrange == layout_floating))
438 c->rx = c->x;
439 c->ry = c->y;
440 c->rw = c->w;
441 c->rh = c->h;
443 wc.border_width = c->border;
444 XConfigureWindow(c->display, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
445 window_configure(c->display, c->win, c->x, c->y, c->w, c->h, c->border);
446 XSync(c->display, False);
447 if((c->x >= 0 || c->y >= 0) && XineramaIsActive(c->display))
449 int new_screen = get_screen_bycoord(c->x, c->y);
450 if(c->screen != new_screen)
451 move_client_to_screen(c, new_screen, False);
456 void
457 client_saveprops(Client * c, int screen)
459 int i = 0, ntags = 0;
460 char *prop;
461 Tag *tag;
463 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
464 ntags++;
466 prop = p_new(char, ntags + 2);
468 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next, i++)
469 prop[i] = is_client_tagged(c, tag, screen) ? '1' : '0';
471 if(i <= ntags)
472 prop[i] = c->isfloating ? '1' : '0';
474 prop[++i] = '\0';
476 XChangeProperty(c->display, c->win, AWESOMEPROPS_ATOM(c->display), XA_STRING, 8,
477 PropModeReplace, (unsigned char *) prop, i);
479 p_delete(&prop);
482 void
483 client_unban(Client *c)
485 XMapWindow(c->display, c->win);
486 window_setstate(c->display, c->win, NormalState);
489 void
490 client_unmanage(Client *c, long state)
492 XWindowChanges wc;
493 Tag *tag;
495 wc.border_width = c->oldborder;
496 /* The server grab construct avoids race conditions. */
497 XGrabServer(c->display);
498 XConfigureWindow(c->display, c->win, CWBorderWidth, &wc); /* restore border */
499 client_detach(c);
500 if(globalconf.focus->client == c)
501 focus(NULL, True, c->screen);
502 focus_delete_client(c);
503 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
504 untag_client(c, tag, c->screen);
505 XUngrabButton(c->display, AnyButton, AnyModifier, c->win);
506 window_setstate(c->display, c->win, state);
507 XSync(c->display, False);
508 XSetErrorHandler(xerror);
509 XUngrabServer(c->display);
510 if(state != NormalState)
511 arrange(c->screen);
512 p_delete(&c);
515 void
516 client_updatesizehints(Client *c)
518 long msize;
519 XSizeHints size;
521 if(!XGetWMNormalHints(c->display, c->win, &size, &msize) || !size.flags)
522 size.flags = PSize;
523 c->flags = size.flags;
524 if(c->flags & PBaseSize)
526 c->basew = size.base_width;
527 c->baseh = size.base_height;
529 else if(c->flags & PMinSize)
531 c->basew = size.min_width;
532 c->baseh = size.min_height;
534 else
535 c->basew = c->baseh = 0;
536 if(c->flags & PResizeInc)
538 c->incw = size.width_inc;
539 c->inch = size.height_inc;
541 else
542 c->incw = c->inch = 0;
544 if(c->flags & PMaxSize)
546 c->maxw = size.max_width;
547 c->maxh = size.max_height;
549 else
550 c->maxw = c->maxh = 0;
552 if(c->flags & PMinSize)
554 c->minw = size.min_width;
555 c->minh = size.min_height;
557 else if(c->flags & PBaseSize)
559 c->minw = size.base_width;
560 c->minh = size.base_height;
562 else
563 c->minw = c->minh = 0;
565 if(c->flags & PAspect)
567 c->minax = size.min_aspect.x;
568 c->maxax = size.max_aspect.x;
569 c->minay = size.min_aspect.y;
570 c->maxay = size.max_aspect.y;
572 else
573 c->minax = c->maxax = c->minay = c->maxay = 0;
575 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
576 && c->maxw == c->minw && c->maxh == c->minh);
579 /** Returns True if a client is tagged
580 * with one of the tags
581 * \return True or False
583 Bool
584 client_isvisible(Client *c, int screen)
586 Tag *tag;
588 if(c->screen != screen)
589 return False;
591 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
592 if(tag->selected && is_client_tagged(c, tag, screen))
593 return True;
594 return False;
597 /** Set selected client transparency
598 * \param screen Screen ID
599 * \param arg unused arg
600 * \ingroup ui_callback
602 void
603 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
605 double delta = 100.0, current_opacity = 100.0;
606 unsigned char *data;
607 Atom actual;
608 int format;
609 unsigned long n, left;
610 unsigned int current_opacity_raw = 0;
611 int set_prop = 0;
612 Client *sel = globalconf.focus->client;
614 if(!sel)
615 return;
617 XGetWindowProperty(globalconf.display, sel->win,
618 XInternAtom(sel->display, "_NET_WM_WINDOW_OPACITY", False),
619 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
620 (unsigned char **) &data);
621 if(data)
623 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
624 XFree(data);
625 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
627 else
628 set_prop = 1;
630 delta = compute_new_value_from_arg(arg, current_opacity);
632 if(delta <= 0.0)
633 delta = 0.0;
634 else if(delta > 100.0)
636 delta = 100.0;
637 set_prop = 1;
640 if(delta == 100.0 && !set_prop)
641 window_settrans(sel->display, sel->win, -1);
642 else
643 window_settrans(sel->display, sel->win, delta);
647 /** Set border size
648 * \param screen Screen ID
649 * \param arg X, +X or -X
650 * \ingroup ui_callback
652 void
653 uicb_setborder(int screen, char *arg)
655 if(!arg)
656 return;
658 if((globalconf.screens[screen].borderpx = (int) compute_new_value_from_arg(arg, (double) globalconf.screens[screen].borderpx)) < 0)
659 globalconf.screens[screen].borderpx = 0;
662 /** Swap current with next client
663 * \param screen Screen ID
664 * \param arg nothing
665 * \ingroup ui_callback
667 void
668 uicb_client_swapnext(int screen, char *arg __attribute__ ((unused)))
670 Client *next, *sel = globalconf.focus->client;
672 if(!sel)
673 return;
675 for(next = sel->next; next && !client_isvisible(next, screen); next = next->next);
676 if(next)
678 client_swap(&globalconf.clients, sel, next);
679 arrange(screen);
680 /* restore focus */
681 focus(sel, True, screen);
685 /** Swap current with previous client
686 * \param screen Screen ID
687 * \param arg nothing
688 * \ingroup ui_callback
690 void
691 uicb_client_swapprev(int screen, char *arg __attribute__ ((unused)))
693 Client *prev, *sel = globalconf.focus->client;
695 if(!sel)
696 return;
698 for(prev = sel->prev; prev && !client_isvisible(prev, screen); prev = prev->prev);
699 if(prev)
701 client_swap(&globalconf.clients, prev, sel);
702 arrange(screen);
703 /* restore focus */
704 focus(sel, True, screen);
708 /** Move and resize client
709 * \param screen Screen ID
710 * \param arg x y w h
711 * \ingroup ui_callback
713 void
714 uicb_client_moveresize(int screen, char *arg)
716 int nx, ny, nw, nh, ox, oy, ow, oh;
717 char x[8], y[8], w[8], h[8];
718 int mx, my, dx, dy, nmx, nmy;
719 unsigned int dui;
720 Window dummy;
721 Client *sel = globalconf.focus->client;
723 if(get_current_layout(screen)->arrange != layout_floating)
724 if(!sel || !sel->isfloating || sel->isfixed || !arg)
725 return;
726 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
727 return;
728 nx = (int) compute_new_value_from_arg(x, sel->x);
729 ny = (int) compute_new_value_from_arg(y, sel->y);
730 nw = (int) compute_new_value_from_arg(w, sel->w);
731 nh = (int) compute_new_value_from_arg(h, sel->h);
733 ox = sel->x;
734 oy = sel->y;
735 ow = sel->w;
736 oh = sel->h;
738 Bool xqp = XQueryPointer(globalconf.display,
739 RootWindow(globalconf.display,
740 get_phys_screen(screen)),
741 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
742 client_resize(sel, nx, ny, nw, nh, True, False);
743 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
745 nmx = mx - ox + sel->w - ow - 1 < 0 ? 0 : mx - ox + sel->w - ow - 1;
746 nmy = my - oy + sel->h - oh - 1 < 0 ? 0 : my - oy + sel->h - oh - 1;
747 XWarpPointer(globalconf.display,
748 None, sel->win,
749 0, 0, 0, 0, nmx, nmy);
753 /** Kill selected client
754 * \param screen Screen ID
755 * \param arg unused
756 * \ingroup ui_callback
758 void
759 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
761 XEvent ev;
762 Client *sel = globalconf.focus->client;
764 if(!sel)
765 return;
766 if(isprotodel(sel->display, sel->win))
768 ev.type = ClientMessage;
769 ev.xclient.window = sel->win;
770 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
771 ev.xclient.format = 32;
772 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
773 ev.xclient.data.l[1] = CurrentTime;
774 XSendEvent(globalconf.display, sel->win, False, NoEventMask, &ev);
776 else
777 XKillClient(globalconf.display, sel->win);
779 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80