cwm/client.c

865 lines
19 KiB
C
Raw Normal View History

2007-04-27 21:58:48 +04:00
/*
* calmwm - the calm window manager
*
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2007-04-27 21:58:48 +04:00
*
2011-05-11 17:53:51 +04:00
* $OpenBSD$
2007-04-27 21:58:48 +04:00
*/
#include <sys/param.h>
#include <sys/queue.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
2007-04-27 21:58:48 +04:00
#include "calmwm.h"
static struct client_ctx *client_mrunext(struct client_ctx *);
static struct client_ctx *client_mruprev(struct client_ctx *);
static void client_none(struct screen_ctx *);
static void client_placecalc(struct client_ctx *);
static void client_update(struct client_ctx *);
static void client_gethints(struct client_ctx *);
static void client_freehints(struct client_ctx *);
static int client_inbound(struct client_ctx *, int, int);
2007-04-27 21:58:48 +04:00
2008-04-16 00:24:41 +04:00
static char emptystring[] = "";
struct client_ctx *_curcc = NULL;
2007-04-27 21:58:48 +04:00
struct client_ctx *
client_find(Window win)
{
struct client_ctx *cc;
2007-04-27 21:58:48 +04:00
TAILQ_FOREACH(cc, &Clientq, entry)
if (cc->win == win)
2007-04-27 21:58:48 +04:00
return (cc);
return (NULL);
}
struct client_ctx *
client_new(Window win, struct screen_ctx *sc, int mapped)
{
struct client_ctx *cc;
XWindowAttributes wattr;
XWMHints *wmhints;
int state;
2007-04-27 21:58:48 +04:00
if (win == None)
return (NULL);
cc = xcalloc(1, sizeof(*cc));
2007-04-27 21:58:48 +04:00
XGrabServer(X_Dpy);
2007-04-27 21:58:48 +04:00
cc->state = mapped ? NormalState : IconicState;
cc->sc = sc;
cc->win = win;
2008-04-16 00:24:41 +04:00
cc->size = XAllocSizeHints();
client_getsizehints(cc);
2007-04-27 21:58:48 +04:00
TAILQ_INIT(&cc->nameq);
client_setname(cc);
conf_client(cc);
2007-04-27 21:58:48 +04:00
/* Saved pointer position */
cc->ptr.x = -1;
cc->ptr.y = -1;
XGetWindowAttributes(X_Dpy, cc->win, &wattr);
2007-04-27 21:58:48 +04:00
cc->geom.x = wattr.x;
cc->geom.y = wattr.y;
cc->geom.width = wattr.width;
cc->geom.height = wattr.height;
cc->cmap = wattr.colormap;
if (wattr.map_state != IsViewable) {
client_placecalc(cc);
if ((wmhints = XGetWMHints(X_Dpy, cc->win)) != NULL) {
2007-04-27 21:58:48 +04:00
if (wmhints->flags & StateHint)
xu_setstate(cc, wmhints->initial_state);
2007-04-27 21:58:48 +04:00
XFree(wmhints);
}
client_move(cc);
2007-04-27 21:58:48 +04:00
}
client_draw_border(cc);
2007-04-27 21:58:48 +04:00
if (xu_getstate(cc, &state) < 0)
state = NormalState;
2008-05-16 01:56:21 +04:00
XSelectInput(X_Dpy, cc->win, ColormapChangeMask | EnterWindowMask |
PropertyChangeMask | KeyReleaseMask);
2007-04-27 21:58:48 +04:00
XAddToSaveSet(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
/* Notify client of its configuration. */
xu_configure(cc);
2007-04-27 21:58:48 +04:00
(state == IconicState) ? client_hide(cc) : client_unhide(cc);
2007-04-27 21:58:48 +04:00
xu_setstate(cc, cc->state);
XSync(X_Dpy, False);
XUngrabServer(X_Dpy);
2007-04-27 21:58:48 +04:00
TAILQ_INSERT_TAIL(&sc->mruq, cc, mru_entry);
TAILQ_INSERT_TAIL(&Clientq, cc, entry);
/* append to the client list */
XChangeProperty(X_Dpy, sc->rootwin, _NET_CLIENT_LIST, XA_WINDOW, 32,
PropModeAppend, (unsigned char *)&cc->win, 1);
2007-04-27 21:58:48 +04:00
client_gethints(cc);
client_update(cc);
2008-04-16 00:24:41 +04:00
2008-05-16 01:56:21 +04:00
if (mapped)
group_autogroup(cc);
2007-04-27 21:58:48 +04:00
return (cc);
}
int
client_delete(struct client_ctx *cc)
2007-04-27 21:58:48 +04:00
{
struct screen_ctx *sc = cc->sc;
struct client_ctx *tcc;
struct winname *wn;
Window *winlist;
int i, j;
2007-04-27 21:58:48 +04:00
group_client_delete(cc);
XGrabServer(X_Dpy);
2007-04-27 21:58:48 +04:00
xu_setstate(cc, WithdrawnState);
XRemoveFromSaveSet(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
XSync(X_Dpy, False);
XUngrabServer(X_Dpy);
2007-04-27 21:58:48 +04:00
TAILQ_REMOVE(&sc->mruq, cc, mru_entry);
TAILQ_REMOVE(&Clientq, cc, entry);
/*
* Sadly we can't remove just one entry from a property, so we must
* redo the whole thing from scratch. this is the stupid way, the other
* way incurs many roundtrips to the server.
*/
i = j = 0;
TAILQ_FOREACH(tcc, &Clientq, entry)
i++;
if (i > 0) {
winlist = xmalloc(i * sizeof(*winlist));
TAILQ_FOREACH(tcc, &Clientq, entry)
winlist[j++] = tcc->win;
XChangeProperty(X_Dpy, sc->rootwin, _NET_CLIENT_LIST,
XA_WINDOW, 32, PropModeReplace,
(unsigned char *)winlist, i);
xfree(winlist);
}
2007-04-27 21:58:48 +04:00
if (_curcc == cc)
client_none(sc);
2007-04-27 21:58:48 +04:00
XFree(cc->size);
while ((wn = TAILQ_FIRST(&cc->nameq)) != NULL) {
TAILQ_REMOVE(&cc->nameq, wn, entry);
if (wn->name != emptystring)
xfree(wn->name);
2007-04-27 21:58:48 +04:00
xfree(wn);
}
client_freehints(cc);
xfree(cc);
return (0);
}
void
client_leave(struct client_ctx *cc)
{
struct screen_ctx *sc;
2007-04-27 21:58:48 +04:00
if (cc == NULL)
cc = _curcc;
if (cc == NULL)
return;
sc = cc->sc;
2007-04-27 21:58:48 +04:00
xu_btn_ungrab(sc->rootwin, AnyModifier, Button1);
}
void
client_setactive(struct client_ctx *cc, int fg)
{
struct screen_ctx *sc;
2007-04-27 21:58:48 +04:00
if (cc == NULL)
cc = _curcc;
if (cc == NULL)
return;
sc = cc->sc;
2007-04-27 21:58:48 +04:00
if (fg) {
XInstallColormap(X_Dpy, cc->cmap);
XSetInputFocus(X_Dpy, cc->win,
2007-04-27 21:58:48 +04:00
RevertToPointerRoot, CurrentTime);
conf_grab_mouse(cc);
2007-04-27 21:58:48 +04:00
/*
* If we're in the middle of alt-tabbing, don't change
* the order please.
*/
if (!sc->altpersist)
client_mtf(cc);
} else
client_leave(cc);
if (fg && _curcc != cc) {
client_setactive(NULL, 0);
_curcc = cc;
XChangeProperty(X_Dpy, sc->rootwin, _NET_ACTIVE_WINDOW,
XA_WINDOW, 32, PropModeReplace,
(unsigned char *)&cc->win, 1);
2007-04-27 21:58:48 +04:00
}
cc->active = fg;
client_draw_border(cc);
}
/*
* set when there is no active client
*/
static void
client_none(struct screen_ctx *sc)
{
Window none = None;
XChangeProperty(X_Dpy, sc->rootwin, _NET_ACTIVE_WINDOW,
XA_WINDOW, 32, PropModeReplace, (unsigned char *)&none, 1);
_curcc = NULL;
}
2007-04-27 21:58:48 +04:00
struct client_ctx *
client_current(void)
{
return (_curcc);
}
void
client_freeze(struct client_ctx *cc)
{
if (cc->flags & CLIENT_FREEZE)
cc->flags &= ~CLIENT_FREEZE;
else
cc->flags |= CLIENT_FREEZE;
}
2007-04-27 21:58:48 +04:00
void
client_maximize(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
int xmax = sc->xmax, ymax = sc->ymax;
int x_org = 0, y_org = 0;
if (cc->flags & CLIENT_FREEZE)
return;
2007-04-27 21:58:48 +04:00
if (cc->flags & CLIENT_MAXIMIZED) {
cc->geom = cc->savegeom;
} else {
if (!(cc->flags & (CLIENT_VMAXIMIZED | CLIENT_HMAXIMIZED)))
cc->savegeom = cc->geom;
if (HasXinerama) {
XineramaScreenInfo *xine;
/*
* pick screen that the middle of the window is on.
* that's probably more fair than if just the origin of
* a window is poking over a boundary
*/
2009-08-25 05:26:09 +04:00
xine = screen_find_xinerama(sc,
cc->geom.x + cc->geom.width / 2,
cc->geom.y + cc->geom.height / 2);
if (xine == NULL)
goto calc;
x_org = xine->x_org;
y_org = xine->y_org;
xmax = xine->width;
ymax = xine->height;
}
calc:
cc->geom.x = x_org + sc->gap.left;
cc->geom.y = y_org + sc->gap.top;
cc->geom.height = ymax - (sc->gap.top + sc->gap.bottom);
cc->geom.width = xmax - (sc->gap.left + sc->gap.right);
cc->flags |= CLIENT_DOMAXIMIZE;
2007-04-27 21:58:48 +04:00
}
client_resize(cc);
}
void
client_vertmaximize(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
int y_org = 0, ymax = sc->ymax;
if (cc->flags & CLIENT_FREEZE)
return;
if (cc->flags & CLIENT_VMAXIMIZED) {
cc->geom = cc->savegeom;
} else {
if (!(cc->flags & (CLIENT_MAXIMIZED | CLIENT_HMAXIMIZED)))
cc->savegeom = cc->geom;
if (HasXinerama) {
XineramaScreenInfo *xine;
2009-08-25 05:26:09 +04:00
xine = screen_find_xinerama(sc,
cc->geom.x + cc->geom.width / 2,
cc->geom.y + cc->geom.height / 2);
if (xine == NULL)
goto calc;
y_org = xine->y_org;
ymax = xine->height;
}
calc:
cc->geom.y = y_org + sc->gap.top;
cc->geom.height = ymax - (cc->bwidth * 2) - (sc->gap.top +
sc->gap.bottom);
cc->flags |= CLIENT_DOVMAXIMIZE;
}
client_resize(cc);
}
void
client_horizmaximize(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
int x_org = 0, xmax = sc->xmax;
if (cc->flags & CLIENT_FREEZE)
return;
if (cc->flags & CLIENT_HMAXIMIZED) {
cc->geom = cc->savegeom;
} else {
if (!(cc->flags & (CLIENT_MAXIMIZED | CLIENT_VMAXIMIZED)))
cc->savegeom = cc->geom;
if (HasXinerama) {
XineramaScreenInfo *xine;
2009-08-25 05:26:09 +04:00
xine = screen_find_xinerama(sc,
cc->geom.x + cc->geom.width / 2,
cc->geom.y + cc->geom.height / 2);
if (xine == NULL)
goto calc;
x_org = xine->x_org;
xmax = xine->width;
}
calc:
cc->geom.x = x_org + sc->gap.left;
cc->geom.width = xmax - (cc->bwidth * 2) - (sc->gap.left +
sc->gap.right);
cc->flags |= CLIENT_DOHMAXIMIZE;
}
client_resize(cc);
}
2007-04-27 21:58:48 +04:00
void
client_resize(struct client_ctx *cc)
{
cc->flags &= ~(CLIENT_MAXIMIZED | CLIENT_VMAXIMIZED |
CLIENT_HMAXIMIZED);
if (cc->flags & CLIENT_DOMAXIMIZE) {
cc->bwidth = 0;
cc->flags &= ~CLIENT_DOMAXIMIZE;
cc->flags |= CLIENT_MAXIMIZED;
} else if (cc->flags & CLIENT_DOVMAXIMIZE) {
cc->flags &= ~CLIENT_DOVMAXIMIZE;
cc->flags |= CLIENT_VMAXIMIZED;
} else if (cc->flags & CLIENT_DOHMAXIMIZE) {
cc->flags &= ~CLIENT_DOHMAXIMIZE;
cc->flags |= CLIENT_HMAXIMIZED;
} else {
cc->bwidth = Conf.bwidth;
}
client_draw_border(cc);
XMoveResizeWindow(X_Dpy, cc->win, cc->geom.x,
cc->geom.y, cc->geom.width, cc->geom.height);
xu_configure(cc);
2007-04-27 21:58:48 +04:00
}
void
client_move(struct client_ctx *cc)
{
if (cc->flags & CLIENT_VMAXIMIZED)
cc->savegeom.x = cc->geom.x;
if (cc->flags & CLIENT_HMAXIMIZED)
cc->savegeom.y = cc->geom.y;
XMoveWindow(X_Dpy, cc->win, cc->geom.x, cc->geom.y);
xu_configure(cc);
2007-04-27 21:58:48 +04:00
}
void
client_lower(struct client_ctx *cc)
{
XLowerWindow(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
}
void
client_raise(struct client_ctx *cc)
{
XRaiseWindow(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
}
void
client_ptrwarp(struct client_ctx *cc)
{
int x = cc->ptr.x, y = cc->ptr.y;
2007-04-27 21:58:48 +04:00
if (x == -1 || y == -1) {
x = cc->geom.width / 2;
y = cc->geom.height / 2;
}
(cc->state == IconicState) ? client_unhide(cc) : client_raise(cc);
xu_ptr_setpos(cc->win, x, y);
2007-04-27 21:58:48 +04:00
}
void
client_ptrsave(struct client_ctx *cc)
{
int x, y;
2007-04-27 21:58:48 +04:00
xu_ptr_getpos(cc->win, &x, &y);
if (client_inbound(cc, x, y)) {
2007-04-27 21:58:48 +04:00
cc->ptr.x = x;
cc->ptr.y = y;
} else {
cc->ptr.x = -1;
cc->ptr.y = -1;
2008-04-16 00:24:41 +04:00
}
2007-04-27 21:58:48 +04:00
}
void
client_hide(struct client_ctx *cc)
{
/* XXX - add wm_state stuff */
XUnmapWindow(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
cc->active = 0;
cc->flags |= CLIENT_HIDDEN;
xu_setstate(cc, IconicState);
if (cc == _curcc)
client_none(cc->sc);
2007-04-27 21:58:48 +04:00
}
void
client_unhide(struct client_ctx *cc)
{
XMapRaised(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
cc->highlight = 0;
2007-04-27 21:58:48 +04:00
cc->flags &= ~CLIENT_HIDDEN;
xu_setstate(cc, NormalState);
client_draw_border(cc);
2007-04-27 21:58:48 +04:00
}
void
client_draw_border(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
unsigned long pixel;
2007-04-27 21:58:48 +04:00
if (cc->active)
switch (cc->highlight) {
case CLIENT_HIGHLIGHT_GROUP:
pixel = sc->color[CWM_COLOR_BORDER_GROUP].pixel;
break;
case CLIENT_HIGHLIGHT_UNGROUP:
pixel = sc->color[CWM_COLOR_BORDER_UNGROUP].pixel;
break;
default:
pixel = sc->color[CWM_COLOR_BORDOR_ACTIVE].pixel;
break;
}
else
pixel = sc->color[CWM_COLOR_BORDER_INACTIVE].pixel;
XSetWindowBorderWidth(X_Dpy, cc->win, cc->bwidth);
XSetWindowBorder(X_Dpy, cc->win, pixel);
2007-04-27 21:58:48 +04:00
}
static void
2007-04-27 21:58:48 +04:00
client_update(struct client_ctx *cc)
{
2009-11-28 20:52:12 +03:00
Atom *p;
int i;
long n;
2007-04-27 21:58:48 +04:00
if ((n = xu_getprop(cc->win, WM_PROTOCOLS,
2007-04-27 21:58:48 +04:00
XA_ATOM, 20L, (u_char **)&p)) <= 0)
return;
for (i = 0; i < n; i++)
if (p[i] == WM_DELETE_WINDOW)
2007-04-27 21:58:48 +04:00
cc->xproto |= CLIENT_PROTO_DELETE;
else if (p[i] == WM_TAKE_FOCUS)
2007-04-27 21:58:48 +04:00
cc->xproto |= CLIENT_PROTO_TAKEFOCUS;
XFree(p);
}
void
client_send_delete(struct client_ctx *cc)
{
if (cc->xproto & CLIENT_PROTO_DELETE)
xu_sendmsg(cc->win, WM_PROTOCOLS, WM_DELETE_WINDOW);
2007-04-27 21:58:48 +04:00
else
XKillClient(X_Dpy, cc->win);
2007-04-27 21:58:48 +04:00
}
void
client_setname(struct client_ctx *cc)
{
struct winname *wn;
char *newname;
2007-04-27 21:58:48 +04:00
if (!xu_getstrprop(cc->win, _NET_WM_NAME, &newname))
xu_getstrprop(cc->win, XA_WM_NAME, &newname);
2007-04-27 21:58:48 +04:00
if (newname == NULL)
newname = emptystring;
TAILQ_FOREACH(wn, &cc->nameq, entry)
if (strcmp(wn->name, newname) == 0) {
/* Move to the last since we got a hit. */
TAILQ_REMOVE(&cc->nameq, wn, entry);
TAILQ_INSERT_TAIL(&cc->nameq, wn, entry);
goto match;
}
wn = xmalloc(sizeof(*wn));
2007-04-27 21:58:48 +04:00
wn->name = newname;
TAILQ_INSERT_TAIL(&cc->nameq, wn, entry);
cc->nameqlen++;
match:
cc->name = wn->name;
/* Now, do some garbage collection. */
if (cc->nameqlen > CLIENT_MAXNAMEQLEN) {
wn = TAILQ_FIRST(&cc->nameq);
assert(wn != NULL);
TAILQ_REMOVE(&cc->nameq, wn, entry);
if (wn->name != emptystring)
xfree(wn->name);
2007-04-27 21:58:48 +04:00
xfree(wn);
cc->nameqlen--;
}
2008-04-16 00:24:41 +04:00
return;
2007-04-27 21:58:48 +04:00
}
struct client_ctx *
client_cycle(struct screen_ctx *sc, int reverse)
2007-04-27 21:58:48 +04:00
{
struct client_ctx *oldcc, *newcc;
int again = 1;
2007-04-27 21:58:48 +04:00
oldcc = client_current();
/* If no windows then you cant cycle */
if (TAILQ_EMPTY(&sc->mruq))
return (NULL);
if (oldcc == NULL)
oldcc = (reverse ? TAILQ_LAST(&sc->mruq, cycle_entry_q) :
TAILQ_FIRST(&sc->mruq));
newcc = oldcc;
while (again) {
again = 0;
2007-04-27 21:58:48 +04:00
newcc = (reverse ? client_mruprev(newcc) :
client_mrunext(newcc));
2007-04-27 21:58:48 +04:00
/* Only cycle visible and non-ignored windows. */
if (newcc->flags & (CLIENT_HIDDEN|CLIENT_IGNORE))
again = 1;
2007-04-27 21:58:48 +04:00
/* Is oldcc the only non-hidden window? */
if (newcc == oldcc) {
if (again)
return (NULL); /* No windows visible. */
2007-04-27 21:58:48 +04:00
break;
}
}
/* reset when alt is released. XXX I hate this hack */
sc->altpersist = 1;
client_ptrsave(oldcc);
client_ptrwarp(newcc);
2007-04-27 21:58:48 +04:00
return (newcc);
2007-04-27 21:58:48 +04:00
}
static struct client_ctx *
2007-04-27 21:58:48 +04:00
client_mrunext(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
struct client_ctx *ccc;
2007-04-27 21:58:48 +04:00
return ((ccc = TAILQ_NEXT(cc, mru_entry)) != NULL ?
ccc : TAILQ_FIRST(&sc->mruq));
}
static struct client_ctx *
2007-04-27 21:58:48 +04:00
client_mruprev(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
struct client_ctx *ccc;
2007-04-27 21:58:48 +04:00
return ((ccc = TAILQ_PREV(cc, cycle_entry_q, mru_entry)) != NULL ?
ccc : TAILQ_LAST(&sc->mruq, cycle_entry_q));
}
static void
2007-04-27 21:58:48 +04:00
client_placecalc(struct client_ctx *cc)
{
struct screen_ctx *sc = cc->sc;
int xslack, yslack;
2007-04-27 21:58:48 +04:00
if (cc->size->flags & (USPosition|PPosition)) {
/*
* Ignore XINERAMA screens, just make sure it's somewhere
* in the virtual desktop. else it stops people putting xterms
* at startup in the screen the mouse doesn't start in *sigh*.
* XRandR bits mean that {x,y}max shouldn't be outside what's
* currently there.
*/
xslack = sc->xmax - cc->geom.width - cc->bwidth * 2;
yslack = sc->ymax - cc->geom.height - cc->bwidth * 2;
if (cc->size->x > 0)
cc->geom.x = MIN(cc->size->x, xslack);
if (cc->size->y > 0)
cc->geom.y = MIN(cc->size->y, yslack);
2007-04-27 21:58:48 +04:00
} else {
XineramaScreenInfo *info;
int xmouse, ymouse, xorig, yorig;
int xmax, ymax;
xu_ptr_getpos(sc->rootwin, &xmouse, &ymouse);
if (HasXinerama) {
info = screen_find_xinerama(sc, xmouse, ymouse);
if (info == NULL)
goto noxine;
xorig = info->x_org;
yorig = info->y_org;
xmax = xorig + info->width;
ymax = yorig + info->height;
} else {
noxine:
xorig = yorig = 0;
xmax = sc->xmax;
ymax = sc->ymax;
}
xmouse = MAX(xmouse, xorig) - cc->geom.width / 2;
ymouse = MAX(ymouse, yorig) - cc->geom.height / 2;
xmouse = MAX(xmouse, xorig);
ymouse = MAX(ymouse, yorig);
xslack = xmax - cc->geom.width - cc->bwidth * 2;
yslack = ymax - cc->geom.height - cc->bwidth * 2;
if (xslack >= xorig) {
cc->geom.x = MAX(MIN(xmouse, xslack),
xorig + sc->gap.left);
if (cc->geom.x > (xslack - sc->gap.right))
cc->geom.x -= sc->gap.right;
2007-04-27 21:58:48 +04:00
} else {
cc->geom.x = xorig + sc->gap.left;
cc->geom.width = xmax - sc->gap.left;
2007-04-27 21:58:48 +04:00
}
if (yslack >= yorig) {
cc->geom.y = MAX(MIN(ymouse, yslack),
yorig + sc->gap.top);
if (cc->geom.y > (yslack - sc->gap.bottom))
cc->geom.y -= sc->gap.bottom;
2007-04-27 21:58:48 +04:00
} else {
cc->geom.y = yorig + sc->gap.top;
cc->geom.height = ymax - sc->gap.top;
2007-04-27 21:58:48 +04:00
}
}
}
void
client_mtf(struct client_ctx *cc)
{
struct screen_ctx *sc;
2007-04-27 21:58:48 +04:00
if (cc == NULL)
cc = _curcc;
if (cc == NULL)
return;
sc = cc->sc;
2007-04-27 21:58:48 +04:00
/* Move to front. */
TAILQ_REMOVE(&sc->mruq, cc, mru_entry);
TAILQ_INSERT_HEAD(&sc->mruq, cc, mru_entry);
}
void
client_getsizehints(struct client_ctx *cc)
{
long tmp;
if (!XGetWMNormalHints(X_Dpy, cc->win, cc->size, &tmp))
cc->size->flags = PSize;
if (cc->size->flags & PBaseSize) {
cc->geom.basew = cc->size->base_width;
cc->geom.baseh = cc->size->base_height;
} else if (cc->size->flags & PMinSize) {
cc->geom.basew = cc->size->min_width;
cc->geom.baseh = cc->size->min_height;
}
if (cc->size->flags & PMinSize) {
cc->geom.minw = cc->size->min_width;
cc->geom.minh = cc->size->min_height;
} else if (cc->size->flags & PBaseSize) {
cc->geom.minw = cc->size->base_width;
cc->geom.minh = cc->size->base_height;
}
if (cc->size->flags & PMaxSize) {
cc->geom.maxw = cc->size->max_width;
cc->geom.maxh = cc->size->max_height;
}
if (cc->size->flags & PResizeInc) {
cc->geom.incw = cc->size->width_inc;
cc->geom.inch = cc->size->height_inc;
}
cc->geom.incw = MAX(1, cc->geom.incw);
cc->geom.inch = MAX(1, cc->geom.inch);
if (cc->size->flags & PAspect) {
2009-11-28 20:52:12 +03:00
if (cc->size->min_aspect.x > 0)
cc->geom.mina = (float)cc->size->min_aspect.y /
cc->size->min_aspect.x;
2009-11-28 20:52:12 +03:00
if (cc->size->max_aspect.y > 0)
cc->geom.maxa = (float)cc->size->max_aspect.x /
cc->size->max_aspect.y;
}
}
void
client_applysizehints(struct client_ctx *cc)
{
Bool baseismin;
baseismin = (cc->geom.basew == cc->geom.minw) &&
(cc->geom.baseh == cc->geom.minh);
/* temporarily remove base dimensions, ICCCM 4.1.2.3 */
if (!baseismin) {
cc->geom.width -= cc->geom.basew;
cc->geom.height -= cc->geom.baseh;
}
/* adjust for aspect limits */
if (cc->geom.mina > 0 && cc->geom.maxa > 0) {
if (cc->geom.maxa <
(float)cc->geom.width / cc->geom.height)
cc->geom.width = cc->geom.height * cc->geom.maxa;
else if (cc->geom.mina <
(float)cc->geom.height / cc->geom.width)
cc->geom.height = cc->geom.width * cc->geom.mina;
}
/* remove base dimensions for increment */
if (baseismin) {
cc->geom.width -= cc->geom.basew;
cc->geom.height -= cc->geom.baseh;
}
/* adjust for increment value */
cc->geom.width -= cc->geom.width % cc->geom.incw;
cc->geom.height -= cc->geom.height % cc->geom.inch;
/* restore base dimensions */
cc->geom.width += cc->geom.basew;
cc->geom.height += cc->geom.baseh;
/* adjust for min width/height */
cc->geom.width = MAX(cc->geom.width, cc->geom.minw);
cc->geom.height = MAX(cc->geom.height, cc->geom.minh);
/* adjust for max width/height */
if (cc->geom.maxw)
cc->geom.width = MIN(cc->geom.width, cc->geom.maxw);
if (cc->geom.maxh)
cc->geom.height = MIN(cc->geom.height, cc->geom.maxh);
}
static void
2007-04-27 21:58:48 +04:00
client_gethints(struct client_ctx *cc)
{
XClassHint xch;
struct mwm_hints *mwmh;
2007-04-27 21:58:48 +04:00
if (XGetClassHint(X_Dpy, cc->win, &xch)) {
2007-04-27 21:58:48 +04:00
if (xch.res_name != NULL)
cc->app_name = xch.res_name;
if (xch.res_class != NULL)
cc->app_class = xch.res_class;
}
if (xu_getprop(cc->win, _MOTIF_WM_HINTS, _MOTIF_WM_HINTS,
PROP_MWM_HINTS_ELEMENTS, (u_char **)&mwmh) == MWM_NUMHINTS)
2007-04-27 21:58:48 +04:00
if (mwmh->flags & MWM_HINTS_DECORATIONS &&
!(mwmh->decorations & MWM_DECOR_ALL) &&
!(mwmh->decorations & MWM_DECOR_BORDER))
cc->bwidth = 0;
}
static void
2007-04-27 21:58:48 +04:00
client_freehints(struct client_ctx *cc)
{
if (cc->app_name != NULL)
XFree(cc->app_name);
if (cc->app_class != NULL)
XFree(cc->app_class);
}
static int
client_inbound(struct client_ctx *cc, int x, int y)
2007-04-27 21:58:48 +04:00
{
return (x < cc->geom.width && x >= 0 &&
y < cc->geom.height && y >= 0);
}