polytreewm/src/dwm.c

1615 lines
33 KiB
C
Raw Normal View History

2021-11-21 01:29:09 +00:00
#include "dwm.h"
2021-11-20 13:52:00 +00:00
#include "helpers.h"
2021-12-04 18:42:03 +00:00
#include "logger.h"
2021-11-21 01:49:30 +00:00
#include "main.h"
2021-11-20 13:52:00 +00:00
#include "settings.h"
#include "spawn.h"
2021-11-20 19:34:34 +00:00
#include "state.h"
2021-11-20 13:52:00 +00:00
#include "unit.h"
#include "util.h"
2021-11-22 04:07:13 +00:00
#include "xbase.h"
2021-11-20 13:52:00 +00:00
2007-09-15 20:25:27 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-11-20 13:53:00 +00:00
#include <unistd.h>
2007-09-15 20:25:27 +00:00
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
2007-10-17 09:19:14 +00:00
#include <X11/Xlib.h>
2007-09-15 20:25:27 +00:00
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
2021-11-20 13:50:06 +00:00
#ifdef ENABLE_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif // ENABLE_XINERAMA
2021-11-20 13:52:00 +00:00
// TODO: Include necessary headers in this header.
2013-04-17 19:21:47 +00:00
#include "drw.h"
2012-11-17 18:01:22 +00:00
2021-11-20 13:50:06 +00:00
/**********
* macros *
**********/
2021-11-20 13:53:31 +00:00
#define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
#define ISVISIBLE(C) (true)
#define LENGTH(X) (sizeof(X) / sizeof(X[0]))
#define MOUSEMASK (BUTTONMASK | PointerMotionMask)
2021-11-20 20:25:56 +00:00
#define CLEANMASK(mask) ( \
(mask) & \
~(numlockmask | LockMask) & \
(ShiftMask | ControlMask | Mod1Mask | Mod2Mask | \
Mod3Mask | Mod4Mask | Mod5Mask) \
)
2021-12-04 17:26:32 +00:00
#define INTERSECT(x,y,w,h,basic_geom) ( \
MAX( \
0, \
MIN( \
(x) + (w), \
basic_geom.position.x \
+ \
basic_geom.sizes.w \
) \
- \
MAX( \
(x), \
basic_geom.position.x \
) \
) \
* \
MAX( \
0, \
MIN( \
(y) + (h), \
basic_geom.position.y \
+ \
basic_geom.sizes.h \
) \
- \
MAX( \
(y), \
basic_geom.position.y \
) \
) \
)
2021-11-16 15:09:23 +00:00
/*********
* types *
*********/
2022-09-09 20:47:55 +00:00
#include "dwm/types.h"
2021-11-16 15:09:23 +00:00
/*************************
* function declarations *
*************************/
2021-12-04 17:44:06 +00:00
static int applysizehints(Client *c, WinGeom win_geom, int interact);
static void arrange(Monitor *m);
static void arrangemon(Monitor *m);
2008-06-11 08:12:06 +00:00
static void attach(Client *c);
static void attachstack(Client *c);
static void configure(Client *c);
static void detach(Client *c);
static void detachstack(Client *c);
static Monitor *dirtomon(int dir);
2008-06-11 08:12:06 +00:00
static void focus(Client *c);
static Atom getatomprop(Client *c, Atom prop);
2015-11-08 21:48:43 +00:00
static int getrootptr(int *x, int *y);
2008-06-11 08:12:06 +00:00
static long getstate(Window w);
2015-11-08 21:48:43 +00:00
static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
static void grabbuttons(Client *c, int focused);
2021-11-20 16:06:20 +00:00
static void grabkeys();
static int isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info);
2008-06-11 08:12:06 +00:00
static void manage(Window w, XWindowAttributes *wa);
2021-12-05 13:27:19 +00:00
static void managepolybar(Window w, XWindowAttributes *wa);
2021-11-21 22:51:41 +00:00
static Monitor *monitor_create();
static void monitor_destroy(Monitor *mon);
2009-06-23 16:20:33 +00:00
static Client *nexttiled(Client *c);
2011-04-15 08:13:06 +00:00
static void pop(Client *);
2011-11-06 19:31:29 +00:00
static Monitor *recttomon(int x, int y, int w, int h);
2021-12-04 17:44:06 +00:00
static void resize(Client *c, struct WinGeom win_geom, int interact);
static void resizeclient(Client *c, struct WinGeom win_geom);
static void restack(Monitor *m);
2021-11-20 16:06:20 +00:00
static void run();
static void scan();
2021-12-04 16:57:26 +00:00
static void scheme_create();
2021-11-21 22:48:55 +00:00
static void scheme_destroy();
static int sendevent(Client *c, Atom proto);
static void sendmon(Client *c, Monitor *m);
2008-06-11 08:12:06 +00:00
static void setclientstate(Client *c, long state);
static void setfocus(Client *c);
2015-11-08 21:48:43 +00:00
static void setfullscreen(Client *c, int fullscreen);
2021-11-20 20:01:49 +00:00
static void seturgent(Client *c, bool is_urgent);
static void showhide(Client *c);
2015-11-08 21:48:43 +00:00
static void unfocus(Client *c, int setfocus);
static void unmanage(Client *c, int destroyed);
2021-11-20 16:06:20 +00:00
static void updateclientlist();
static int updategeom();
static void updatenumlockmask();
2008-06-11 08:12:06 +00:00
static void updatesizehints(Client *c);
static void updatetitle(Client *c);
static void updatewindowtype(Client *c);
2008-06-11 08:12:06 +00:00
static void updatewmhints(Client *c);
2009-06-30 18:39:59 +00:00
static Client *wintoclient(Window w);
static Monitor *wintomon(Window w);
2022-09-09 20:24:58 +00:00
2022-09-09 20:44:59 +00:00
#include "dwm/spaghetti/bar.h"
#include "dwm/spaghetti/handlers.h"
#include "dwm/spaghetti/interaction.h"
#include "dwm/spaghetti/wmcheckwin.h"
#include "dwm/spaghetti/xerror.h"
2021-11-16 14:46:19 +00:00
2021-11-16 15:09:23 +00:00
/*************
* variables *
*************/
2021-11-22 04:07:13 +00:00
static Xbase xbase = NULL;
2021-11-21 22:57:22 +00:00
2021-11-17 03:21:32 +00:00
static Unit global_unit = NULL;
2021-11-20 22:01:13 +00:00
2009-07-12 21:49:06 +00:00
static const char broken[] = "broken";
2008-07-16 17:17:42 +00:00
static unsigned int numlockmask = 0;
2021-11-21 01:24:15 +00:00
static int running = 1;
static Cur *cursor[CurLast];
static Clr **scheme;
static Drw *drw;
static Monitor *mons, *selmon;
static void (*handler[LASTEvent])(XEvent*) = {
2021-11-16 14:46:19 +00:00
[ButtonPress] = on_button_press,
[ClientMessage] = on_client_message,
[ConfigureRequest] = on_configure_request,
[ConfigureNotify] = on_configure_notify,
[DestroyNotify] = on_destroy_notify,
[FocusIn] = on_focus_in,
[KeyPress] = on_key_press,
[MappingNotify] = on_mapping_notify,
[MapRequest] = on_map_request,
[PropertyNotify] = on_property_notify,
[UnmapNotify] = on_unmap_notify
};
2009-07-02 19:56:23 +00:00
2021-11-16 15:09:23 +00:00
/***************************************************************
* configuration, allows nested code to access above variables *
***************************************************************/
2021-12-04 19:17:10 +00:00
static const char col_gray1[] = "#222222";
static const char col_gray2[] = "#444444";
static const char col_gray3[] = "#bbbbbb";
static const char col_gray4[] = "#eeeeee";
static const char col_cyan[] = "#005577";
static const char *colors[][3] = {
/* fg bg border */
[SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
[SchemeSel] = { col_gray4, col_cyan, "#d9b01c" },
};
2021-11-21 02:03:19 +00:00
/************************************
* Private function implementations *
************************************/
2022-09-09 20:44:59 +00:00
#include "dwm/spaghetti/bar.c"
#include "dwm/spaghetti/handlers.c"
#include "dwm/spaghetti/interaction.c"
#include "dwm/spaghetti/layouts.c"
#include "dwm/spaghetti/wmcheckwin.c"
#include "dwm/spaghetti/xerror.c"
2021-11-21 02:03:19 +00:00
/***********************************
* Public function implementations *
***********************************/
2021-11-21 22:57:22 +00:00
int dwm_main(const char *const new_program_title)
2021-11-16 15:09:23 +00:00
{
xbase = xbase_new(new_program_title, xerror);
if (!(global_unit = unit_new(UNIT_GLOBAL, NULL))) {
2021-12-04 16:47:37 +00:00
fatal("cannot create global unit");
}
2021-12-04 16:57:26 +00:00
// Setup start
XSetWindowAttributes wa;
drw = drw_create(
xbase->x_display,
xbase->x_screen,
xbase->x_root,
xbase->screen_sizes.w,
xbase->screen_sizes.h
2021-12-04 16:57:26 +00:00
);
updategeom();
/* init cursors */
cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
cursor[CurResize] = drw_cur_create(drw, XC_sizing);
cursor[CurMove] = drw_cur_create(drw, XC_fleur);
/* init appearance */
scheme_create();
/* supporting window for NetWMCheck */
wmcheckwin_create();
/* EWMH support per view */
XChangeProperty(
xbase->x_display,
xbase->x_root,
xbase->atoms->netatom[NetSupported],
XA_ATOM,
32,
PropModeReplace,
(unsigned char*)xbase->atoms->netatom,
NetLast
);
XDeleteProperty(
xbase->x_display,
xbase->x_root,
xbase->atoms->netatom[NetClientList]
);
/* select events */
wa.cursor = cursor[CurNormal]->cursor;
wa.event_mask =
SubstructureRedirectMask | SubstructureNotifyMask | ButtonPressMask |
PointerMotionMask | EnterWindowMask | LeaveWindowMask |
StructureNotifyMask | PropertyChangeMask;
XChangeWindowAttributes(
xbase->x_display,
xbase->x_root,
CWEventMask | CWCursor,
&wa
);
XSelectInput(xbase->x_display, xbase->x_root, wa.event_mask);
grabkeys();
focus(NULL);
// Setup end
2021-11-16 15:09:23 +00:00
scan();
run();
2021-12-04 16:57:26 +00:00
// Cleanup start
2022-09-09 20:37:35 +00:00
Layout foo = { NULL };
2021-12-04 16:57:26 +00:00
selmon->lt[selmon->sellt] = &foo;
for (Monitor *m = mons; m; m = m->next) {
while (m->stack) {
unmanage(m->stack, 0);
}
}
XUngrabKey(xbase->x_display, AnyKey, AnyModifier, xbase->x_root);
while (mons) {
monitor_destroy(mons);
}
for (size_t i = 0; i < CurLast; i++) {
drw_cur_free(drw, cursor[i]);
}
scheme_destroy();
wmcheckwin_destroy();
drw_free(drw);
XSync(xbase->x_display, False);
XSetInputFocus(
xbase->x_display,
PointerRoot,
RevertToPointerRoot,
CurrentTime
);
XDeleteProperty(
xbase->x_display,
xbase->x_root,
xbase->atoms->netatom[NetActiveWindow]
);
// Cleanup end
UNIT_DELETE(global_unit);
2021-11-22 04:07:13 +00:00
XBASE_DELETE(xbase);
2021-11-16 15:09:23 +00:00
return EXIT_SUCCESS;
}
2021-11-21 02:03:19 +00:00
/************************************
* Private function implementations *
************************************/
2021-11-20 16:06:20 +00:00
int applysizehints(
Client *c,
2021-12-04 17:44:06 +00:00
WinGeom win_geom,
2021-11-20 16:06:20 +00:00
int interact
) {
2021-12-04 17:44:06 +00:00
int *const x = &win_geom->basic.position.x;
int *const y = &win_geom->basic.position.y;
int *const w = &win_geom->basic.sizes.w;
int *const h = &win_geom->basic.sizes.h;
int const bw = win_geom->border_width;
2021-11-21 00:25:52 +00:00
2009-06-30 19:15:31 +00:00
Monitor *m = c->mon;
/* set minimum possible */
*w = MAX(1, *w);
*h = MAX(1, *h);
2021-11-20 17:28:07 +00:00
2015-11-08 22:11:48 +00:00
if (interact) {
2021-11-22 04:35:20 +00:00
if (*x > xbase->screen_sizes.w) {
2021-11-20 22:01:13 +00:00
*x =
2021-11-22 04:35:20 +00:00
xbase->screen_sizes.w
2021-11-20 22:01:13 +00:00
-
2021-12-04 17:44:06 +00:00
win_geom_total_width(&c->state.geom);
2021-11-20 17:28:07 +00:00
}
2021-11-22 04:35:20 +00:00
if (*y > xbase->screen_sizes.h) {
2021-11-20 22:01:13 +00:00
*y =
2021-11-22 04:35:20 +00:00
xbase->screen_sizes.h
2021-11-20 22:01:13 +00:00
-
2021-12-04 17:44:06 +00:00
win_geom_total_height(&c->state.geom);
2021-11-20 17:28:07 +00:00
}
if (*x + *w + 2 * bw < 0) {
*x = 0;
2021-11-20 17:28:07 +00:00
}
if (*y + *h + 2 * bw < 0) {
*y = 0;
2021-11-20 17:28:07 +00:00
}
2015-11-08 22:11:48 +00:00
} else {
2021-11-20 21:44:01 +00:00
if (
*x
>=
2021-12-04 17:26:32 +00:00
m->window_area_geom.position.x + m->window_area_geom.sizes.w
2021-11-20 21:44:01 +00:00
) {
2021-11-20 19:11:06 +00:00
*x =
2021-12-04 17:26:32 +00:00
m->window_area_geom.position.x
2021-11-20 19:11:06 +00:00
+
2021-12-04 17:26:32 +00:00
m->window_area_geom.sizes.w
2021-11-20 19:11:06 +00:00
-
2021-12-04 17:44:06 +00:00
win_geom_total_width(&c->state.geom);
2021-11-20 17:28:07 +00:00
}
2021-11-20 21:44:01 +00:00
if (
*y
>=
2021-12-04 17:26:32 +00:00
m->window_area_geom.position.y + m->window_area_geom.sizes.h
2021-11-20 21:44:01 +00:00
) {
2021-11-20 19:11:06 +00:00
*y =
2021-12-04 17:26:32 +00:00
m->window_area_geom.position.y
2021-11-20 19:11:06 +00:00
+
2021-12-04 17:26:32 +00:00
m->window_area_geom.sizes.h
2021-11-20 19:11:06 +00:00
-
2021-12-04 17:44:06 +00:00
win_geom_total_height(&c->state.geom);
2021-11-20 17:28:07 +00:00
}
2021-12-04 17:26:32 +00:00
if (*x + *w + 2 * bw <= m->window_area_geom.position.x) {
*x = m->window_area_geom.position.x;
2021-11-20 17:28:07 +00:00
}
2021-12-04 17:26:32 +00:00
if (*y + *h + 2 * bw <= m->window_area_geom.position.y) {
*y = m->window_area_geom.position.y;
2021-11-20 17:28:07 +00:00
}
}
2021-11-20 17:28:07 +00:00
if (
2021-11-20 19:53:53 +00:00
c->state.is_floating
||
(
settings_get_respect_resize_hints_in_floating_layout()
&&
!c->mon->lt[c->mon->sellt]->arrange
)
) {
/* see last two sentences in ICCCM 4.1.2.3 */
2021-11-20 18:18:41 +00:00
const int baseismin =
c->size_hints.basew == c->size_hints.minw
&&
c->size_hints.baseh == c->size_hints.minh;
2021-11-20 17:28:07 +00:00
/* temporarily remove base dimensions */
if (!baseismin) {
2021-11-20 18:18:41 +00:00
*w -= c->size_hints.basew;
*h -= c->size_hints.baseh;
}
2021-11-20 17:28:07 +00:00
/* adjust for aspect limits */
2021-11-20 18:18:41 +00:00
if (c->size_hints.mina > 0 && c->size_hints.maxa > 0) {
if (c->size_hints.maxa < (float)*w / *h) {
*w = *h * c->size_hints.maxa + 0.5;
} else if (c->size_hints.mina < (float)*h / *w) {
*h = *w * c->size_hints.mina + 0.5;
2021-11-20 17:28:07 +00:00
}
}
2021-11-20 17:28:07 +00:00
/* increment calculation requires this */
if (baseismin) {
2021-11-20 18:18:41 +00:00
*w -= c->size_hints.basew;
*h -= c->size_hints.baseh;
}
2021-11-20 17:28:07 +00:00
/* adjust for increment value */
2021-11-20 18:18:41 +00:00
if (c->size_hints.incw) {
*w -= *w % c->size_hints.incw;
2021-11-20 17:28:07 +00:00
}
2021-11-20 18:18:41 +00:00
if (c->size_hints.inch) {
*h -= *h % c->size_hints.inch;
2021-11-20 17:28:07 +00:00
}
/* restore base dimensions */
2021-11-20 18:18:41 +00:00
*w = MAX(*w + c->size_hints.basew, c->size_hints.minw);
*h = MAX(*h + c->size_hints.baseh, c->size_hints.minh);
2021-11-20 17:28:07 +00:00
2021-11-20 18:18:41 +00:00
if (c->size_hints.maxw) {
*w = MIN(*w, c->size_hints.maxw);
2021-11-20 17:28:07 +00:00
}
2021-11-20 18:18:41 +00:00
if (c->size_hints.maxh) {
*h = MIN(*h, c->size_hints.maxh);
2021-11-20 17:28:07 +00:00
}
}
2021-11-20 17:28:07 +00:00
2021-11-20 17:57:26 +00:00
return (
2021-12-04 17:26:32 +00:00
*x != c->state.geom.basic.position.x
2021-11-20 17:57:26 +00:00
||
2021-12-04 17:26:32 +00:00
*y != c->state.geom.basic.position.y
2021-11-20 17:57:26 +00:00
||
2021-12-04 17:26:32 +00:00
*w != c->state.geom.basic.sizes.w
2021-11-20 17:57:26 +00:00
||
2021-12-04 17:26:32 +00:00
*h != c->state.geom.basic.sizes.h
2021-11-20 17:57:26 +00:00
||
2021-12-04 17:26:32 +00:00
bw != c->state.geom.border_width
2021-11-20 17:57:26 +00:00
);
}
2021-11-20 16:06:20 +00:00
void arrange(Monitor *m)
2015-11-08 22:11:48 +00:00
{
if (m) {
2021-11-14 06:17:15 +00:00
showhide(m->stack);
} else {
for (m = mons; m; m = m->next) {
showhide(m->stack);
}
}
2015-11-08 22:11:48 +00:00
if (m) {
arrangemon(m);
restack(m);
} else {
for (m = mons; m; m = m->next) {
arrangemon(m);
}
}
}
2021-11-20 16:06:20 +00:00
void arrangemon(Monitor *m)
2015-11-08 22:11:48 +00:00
{
2021-11-14 22:55:59 +00:00
unsigned int visible_clients = 0;
2021-11-14 22:55:59 +00:00
for (const Client *client = m->clients; client; client = client->next) {
if (ISVISIBLE(client)) {
++visible_clients;
}
2021-11-14 22:55:59 +00:00
}
if (m->lt[m->sellt]->arrange) {
m->lt[m->sellt]->arrange(m);
} else {
floating(selmon);
2021-11-15 04:05:57 +00:00
}
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void attach(Client *c)
2015-11-08 22:11:48 +00:00
{
c->next = c->mon->clients;
c->mon->clients = c;
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void attachstack(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-14 06:17:15 +00:00
c->snext = c->mon->stack;
c->mon->stack = c;
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void configure(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-20 16:56:33 +00:00
XConfigureEvent ce = {
.type = ConfigureNotify,
.serial = 0,
.send_event = False,
2021-11-22 04:31:28 +00:00
.display = xbase->x_display,
.event = c->x_window,
.window = c->x_window,
2021-12-04 17:26:32 +00:00
.x = c->state.geom.basic.position.x,
.y = c->state.geom.basic.position.y,
.width = c->state.geom.basic.sizes.w,
.height = c->state.geom.basic.sizes.h,
.border_width = c->state.geom.border_width,
2021-11-20 16:56:33 +00:00
.above = None,
.override_redirect = False,
};
2021-11-22 04:31:28 +00:00
XSendEvent(
xbase->x_display,
c->x_window,
False,
StructureNotifyMask,
(XEvent*)&ce
);
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
void detach(Client *c)
2015-11-08 22:11:48 +00:00
{
2008-07-03 09:58:35 +00:00
Client **tc;
2008-06-11 08:12:06 +00:00
2015-11-08 22:11:48 +00:00
for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
2008-07-03 09:58:35 +00:00
*tc = c->next;
2007-09-16 09:53:14 +00:00
}
2007-09-15 20:25:27 +00:00
2021-11-20 16:06:20 +00:00
void detachstack(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-14 06:17:15 +00:00
Client **tc, *t;
for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
*tc = c->snext;
2009-06-30 18:39:59 +00:00
2015-11-08 22:11:48 +00:00
if (c == c->mon->sel) {
2021-11-14 06:17:15 +00:00
for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
c->mon->sel = t;
2009-06-30 18:39:59 +00:00
}
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
Monitor *dirtomon(int dir)
2015-11-08 22:11:48 +00:00
{
Monitor *m = NULL;
2015-11-08 22:11:48 +00:00
if (dir > 0) {
if (!(m = selmon->next))
m = mons;
2015-11-08 22:11:48 +00:00
} else if (selmon == mons)
for (m = mons; m->next; m = m->next);
2011-07-27 17:59:10 +00:00
else
2015-11-08 22:11:48 +00:00
for (m = mons; m->next != selmon; m = m->next);
return m;
}
2021-11-20 16:06:20 +00:00
void focus(Client *c)
2015-11-08 22:11:48 +00:00
{
if (!c || !ISVISIBLE(c))
2021-11-14 06:17:15 +00:00
for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
2015-11-08 22:11:48 +00:00
if (selmon->sel && selmon->sel != c)
2015-11-08 21:48:43 +00:00
unfocus(selmon->sel, 0);
2015-11-08 22:11:48 +00:00
if (c) {
if (c->mon != selmon)
2009-06-24 14:37:32 +00:00
selmon = c->mon;
2021-11-20 19:53:53 +00:00
if (c->state.is_urgent)
2021-11-20 20:01:49 +00:00
seturgent(c, false);
2007-09-16 09:53:14 +00:00
detachstack(c);
attachstack(c);
2015-11-08 21:48:43 +00:00
grabbuttons(c, 1);
2021-11-22 04:31:28 +00:00
XSetWindowBorder(
xbase->x_display,
c->x_window,
scheme[SchemeSel][ColBorder].pixel
);
setfocus(c);
2015-11-08 22:11:48 +00:00
} else {
2021-11-22 04:31:28 +00:00
XSetInputFocus(
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-11-22 04:31:28 +00:00
RevertToPointerRoot,
CurrentTime
);
XDeleteProperty(
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetActiveWindow]
2021-11-22 04:31:28 +00:00
);
}
selmon->sel = c;
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
Atom getatomprop(Client *c, Atom prop)
2015-11-08 22:11:48 +00:00
{
2021-11-20 17:04:23 +00:00
Atom atom = None;
Atom da = None;
int di = 0;
unsigned long dl = 0;
2011-11-06 19:30:06 +00:00
unsigned char *p = NULL;
2021-11-20 17:04:23 +00:00
if (
XGetWindowProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
c->x_window,
2021-11-20 17:04:23 +00:00
prop,
0L,
sizeof(atom),
False,
XA_ATOM,
&da,
&di,
&dl,
&dl,
&p
) == Success && p
) {
atom = *(Atom*)p;
2011-11-06 19:30:06 +00:00
XFree(p);
}
2021-11-20 17:04:23 +00:00
2011-11-06 19:30:06 +00:00
return atom;
}
2021-11-20 16:06:20 +00:00
int getrootptr(int *x, int *y)
2015-11-08 22:11:48 +00:00
{
int di;
unsigned int dui;
Window dummy;
2009-07-02 17:40:04 +00:00
2021-11-22 04:31:28 +00:00
return XQueryPointer(
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-11-22 04:31:28 +00:00
&dummy,
&dummy,
x,
y,
&di,
&di,
&dui
);
}
2021-11-20 16:06:20 +00:00
long getstate(Window w)
2015-11-08 22:11:48 +00:00
{
int format;
2007-09-16 09:53:14 +00:00
long result = -1;
unsigned char *p = NULL;
2008-07-16 17:17:42 +00:00
unsigned long n, extra;
2007-09-16 09:53:14 +00:00
Atom real;
2007-09-15 20:25:27 +00:00
2021-11-21 06:03:58 +00:00
if (
XGetWindowProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
w,
2021-12-04 16:49:53 +00:00
xbase->atoms->wmatom[WMState],
2021-11-21 06:03:58 +00:00
0L,
2L,
False,
2021-12-04 16:49:53 +00:00
xbase->atoms->wmatom[WMState],
2021-11-21 06:03:58 +00:00
&real,
&format,
&n,
&extra,
(unsigned char **)&p
) != Success
) {
2007-09-16 09:53:14 +00:00
return -1;
2021-11-21 06:03:58 +00:00
}
if (n != 0) {
2007-09-16 09:53:14 +00:00
result = *p;
2021-11-21 06:03:58 +00:00
}
2007-09-16 09:53:14 +00:00
XFree(p);
return result;
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
int gettextprop(Window w, Atom atom, char *text, unsigned int size)
2015-11-08 22:11:48 +00:00
{
2007-09-16 09:53:14 +00:00
char **list = NULL;
int n;
XTextProperty name;
2007-09-15 20:25:27 +00:00
2021-11-21 06:03:58 +00:00
if (!text || size == 0) {
2015-11-08 21:48:43 +00:00
return 0;
2021-11-21 06:03:58 +00:00
}
2007-09-16 09:53:14 +00:00
text[0] = '\0';
2021-11-21 06:03:58 +00:00
2021-11-22 04:31:28 +00:00
if (!XGetTextProperty(xbase->x_display, w, &name, atom) || !name.nitems) {
2015-11-08 21:48:43 +00:00
return 0;
2021-11-21 06:03:58 +00:00
}
if (name.encoding == XA_STRING) {
2007-09-16 09:53:14 +00:00
strncpy(text, (char *)name.value, size - 1);
2021-11-21 06:03:58 +00:00
} else {
if (
2021-11-22 04:31:28 +00:00
(
XmbTextPropertyToTextList(xbase->x_display, &name, &list, &n)
>=
Success
)
2021-11-21 06:03:58 +00:00
&&
n > 0
&&
*list
) {
2007-09-16 09:53:14 +00:00
strncpy(text, *list, size - 1);
XFreeStringList(list);
2007-09-15 20:25:27 +00:00
}
2007-09-16 09:53:14 +00:00
}
text[size - 1] = '\0';
XFree(name.value);
2015-11-08 21:48:43 +00:00
return 1;
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void grabbuttons(Client *c, int focused)
2015-11-08 22:11:48 +00:00
{
updatenumlockmask();
{
unsigned int j;
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
2021-11-22 04:31:28 +00:00
XUngrabButton(xbase->x_display, AnyButton, AnyModifier, c->x_window);
2021-11-21 06:03:58 +00:00
if (!focused) {
XGrabButton(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
AnyButton,
AnyModifier,
c->x_window,
False,
BUTTONMASK,
GrabModeSync,
GrabModeSync,
None,
None
);
}
for (size_t i = 0; i < buttons_count(); ++i) {
2021-11-21 06:03:58 +00:00
if (buttons[i].click == ClkClientWin) {
for (j = 0; j < LENGTH(modifiers); j++) {
XGrabButton(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
buttons[i].button,
buttons[i].mask | modifiers[j],
2021-11-21 06:03:58 +00:00
c->x_window,
False,
BUTTONMASK,
GrabModeAsync,
GrabModeSync,
None,
None
);
}
}
}
}
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
void grabkeys()
2015-11-08 22:11:48 +00:00
{
updatenumlockmask();
2009-07-02 17:40:04 +00:00
{
unsigned int j;
2008-08-23 08:31:28 +00:00
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
KeyCode code;
2021-11-22 04:38:11 +00:00
XUngrabKey(xbase->x_display, AnyKey, AnyModifier, xbase->x_root);
2021-11-21 06:03:58 +00:00
for (size_t i = 0; i < keys_count(); ++i) {
2021-11-22 04:31:28 +00:00
if ((code = XKeysymToKeycode(xbase->x_display, keys[i].keysym))) {
2021-11-21 06:03:58 +00:00
for (j = 0; j < LENGTH(modifiers); j++) {
XGrabKey(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
code,
keys[i].mod | modifiers[j],
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-11-21 06:03:58 +00:00
True,
GrabModeAsync,
GrabModeAsync
);
}
}
}
}
}
2021-11-16 22:02:06 +00:00
#ifdef ENABLE_XINERAMA
2021-11-20 16:06:20 +00:00
int isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
2015-11-08 22:11:48 +00:00
{
while (n--)
if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
&& unique[n].width == info->width && unique[n].height == info->height)
2015-11-08 21:48:43 +00:00
return 0;
return 1;
}
2021-11-16 22:02:06 +00:00
#endif /* ENABLE_XINERAMA */
2021-11-20 16:06:20 +00:00
void manage(Window w, XWindowAttributes *wa)
2015-11-08 22:11:48 +00:00
{
2021-11-13 19:48:52 +00:00
Client *const c = ecalloc(1, sizeof(Client));
2007-09-15 20:25:27 +00:00
2021-11-20 20:16:53 +00:00
client_state_init(&c->state);
c->x_window = w;
2021-12-04 17:26:32 +00:00
c->state.geom.basic.position.x = wa->x;
c->state.geom.basic.position.y = wa->y;
c->state.geom.basic.sizes.w = wa->width;
c->state.geom.basic.sizes.h = wa->height;
2021-11-20 20:01:49 +00:00
c->state.is_floating = false;
2009-07-12 21:34:29 +00:00
updatetitle(c);
2021-11-13 19:48:52 +00:00
Window trans = None;
{
Client *t = NULL;
2021-11-22 04:31:28 +00:00
if (
XGetTransientForHint(xbase->x_display, w, &trans)
&&
(t = wintoclient(trans))
) {
2021-11-13 19:48:52 +00:00
c->mon = t->mon;
} else {
c->mon = selmon;
}
}
2011-11-06 19:30:06 +00:00
2021-12-04 17:44:06 +00:00
win_geom_adjust_to_boundary(
2021-12-04 17:26:32 +00:00
&c->state.geom,
&c->mon->screen_geom
2021-11-20 21:44:01 +00:00
);
2021-11-13 19:48:52 +00:00
2021-12-04 17:26:32 +00:00
c->state.geom.border_width = settings_get_border_width();
2011-11-06 19:30:06 +00:00
2021-11-13 19:48:52 +00:00
{
XWindowChanges wc;
2021-12-04 17:26:32 +00:00
wc.border_width = c->state.geom.border_width;
2021-11-22 04:31:28 +00:00
XConfigureWindow(xbase->x_display, w, CWBorderWidth, &wc);
2021-11-13 19:48:52 +00:00
}
2021-11-22 04:31:28 +00:00
XSetWindowBorder(xbase->x_display, w, scheme[SchemeNorm][ColBorder].pixel);
2021-11-13 19:48:52 +00:00
2007-09-16 09:53:14 +00:00
configure(c); /* propagates border_width, if size doesn't change */
2011-11-02 12:01:28 +00:00
updatewindowtype(c);
2007-09-16 09:53:14 +00:00
updatesizehints(c);
updatewmhints(c);
2021-11-13 19:48:52 +00:00
2021-11-20 21:00:39 +00:00
{
2021-12-04 17:44:06 +00:00
const int total_width = win_geom_total_width(&c->state.geom);
2021-11-20 21:00:39 +00:00
const int total_height =
2021-12-04 17:44:06 +00:00
win_geom_total_height(&c->state.geom);
2021-11-20 21:00:39 +00:00
2021-12-04 17:26:32 +00:00
c->state.geom.basic.position.x =
c->mon->screen_geom.position.x
2021-11-20 21:00:39 +00:00
+
2021-12-04 17:26:32 +00:00
(c->mon->screen_geom.sizes.w - total_width) / 2;
c->state.geom.basic.position.y =
c->mon->screen_geom.position.y
2021-11-20 21:00:39 +00:00
+
2021-12-04 17:26:32 +00:00
(c->mon->screen_geom.sizes.h - total_height) / 2;
2021-11-20 21:00:39 +00:00
}
2021-11-13 19:48:52 +00:00
XSelectInput(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-13 19:48:52 +00:00
w,
EnterWindowMask |
FocusChangeMask |
PropertyChangeMask |
StructureNotifyMask
);
2015-11-08 21:48:43 +00:00
grabbuttons(c, 0);
2021-11-13 19:48:52 +00:00
2021-11-20 19:53:53 +00:00
if (!c->state.is_floating) {
c->state.is_floating = trans != None || c->state.is_fixed;
2021-11-13 19:48:52 +00:00
}
2021-11-20 19:53:53 +00:00
if (c->state.is_floating) {
2021-11-22 04:31:28 +00:00
XRaiseWindow(xbase->x_display, c->x_window);
2021-11-13 19:48:52 +00:00
}
attach(c);
2007-09-16 09:53:14 +00:00
attachstack(c);
2021-11-13 19:48:52 +00:00
XChangeProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetClientList],
2021-11-13 19:48:52 +00:00
XA_WINDOW,
32,
PropModeAppend,
(unsigned char*)&(c->x_window),
2021-11-13 19:48:52 +00:00
1
);
/* some windows require this */
2021-11-20 17:57:26 +00:00
XMoveResizeWindow(
2021-11-22 04:31:28 +00:00
xbase->x_display,
c->x_window,
2021-12-04 17:26:32 +00:00
c->state.geom.basic.position.x + 2 * xbase->screen_sizes.w,
c->state.geom.basic.position.y,
c->state.geom.basic.sizes.w,
c->state.geom.basic.sizes.h
2021-11-20 17:57:26 +00:00
);
2021-11-13 19:48:52 +00:00
2007-09-16 09:53:14 +00:00
setclientstate(c, NormalState);
2021-11-13 19:48:52 +00:00
if (c->mon == selmon) {
2015-11-08 21:48:43 +00:00
unfocus(selmon->sel, 0);
2021-11-13 19:48:52 +00:00
}
c->mon->sel = c;
2021-11-13 19:48:52 +00:00
arrange(c->mon);
2021-11-22 04:31:28 +00:00
XMapWindow(xbase->x_display, c->x_window);
focus(NULL);
2007-09-15 20:25:27 +00:00
}
2021-12-05 13:27:19 +00:00
void managepolybar(Window w, XWindowAttributes *wa)
{
Monitor *m = recttomon(wa->x, wa->y, wa->width, wa->height);
if (!m) return;
XSelectInput(
xbase->x_display,
w,
EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask
);
XMoveResizeWindow(
xbase->x_display,
w,
wa->x,
wa->y,
wa->width,
wa->height
);
XMapWindow(xbase->x_display, w);
XChangeProperty(
xbase->x_display,
xbase->x_root,
xbase->atoms->netatom[NetClientList],
XA_WINDOW,
32,
PropModeAppend,
(unsigned char*)&w,
1
);
}
2021-11-21 22:51:41 +00:00
Monitor *monitor_create()
{
Monitor *const m = ecalloc(1, sizeof(Monitor));
2021-12-04 17:26:32 +00:00
m->screen_geom = basic_geom_create();
m->window_area_geom = basic_geom_create();
2021-11-21 22:51:41 +00:00
if (!m) goto fail_without_mon;
if (!(m->unit = unit_new(UNIT_MONITOR, global_unit))) {
goto fail_without_unit;
}
m->nmaster = settings_get_default_clients_in_master();
m->lt[0] = &layouts[0];
m->lt[1] = &layouts[1 % layouts_count()];
2021-11-21 22:51:41 +00:00
return m;
fail_without_unit:
free(m);
fail_without_mon:
return NULL;
}
void monitor_destroy(Monitor *mon)
{
if (mon == mons) {
mons = mons->next;
} else {
Monitor *m;
for (m = mons; m && m->next != mon; m = m->next);
m->next = mon->next;
}
UNIT_DELETE(mon->unit);
free(mon);
}
2021-11-20 16:06:20 +00:00
Client *nexttiled(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-20 19:53:53 +00:00
for (; c && (c->state.is_floating || !ISVISIBLE(c)); c = c->next);
2007-09-16 09:53:14 +00:00
return c;
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void pop(Client *c)
2015-11-08 22:11:48 +00:00
{
detach(c);
attach(c);
focus(c);
arrange(c->mon);
2009-06-30 18:39:59 +00:00
}
2021-11-20 16:06:20 +00:00
Monitor *recttomon(int x, int y, int w, int h)
2015-11-08 22:11:48 +00:00
{
2011-11-06 19:31:29 +00:00
Monitor *m, *r = selmon;
int a, area = 0;
2015-11-08 22:11:48 +00:00
for (m = mons; m; m = m->next)
2021-12-04 17:26:32 +00:00
if ((a = INTERSECT(x, y, w, h, m->window_area_geom)) > area) {
2011-11-06 19:31:29 +00:00
area = a;
r = m;
}
return r;
}
2021-12-04 17:44:06 +00:00
void resize(Client *c, struct WinGeom win_geom, int interact)
2015-11-08 22:11:48 +00:00
{
2021-12-04 17:44:06 +00:00
if (applysizehints(c, &win_geom, interact)) {
resizeclient(c, win_geom);
2021-11-20 22:49:19 +00:00
}
}
2021-12-04 17:44:06 +00:00
void resizeclient(Client *c, const struct WinGeom win_geom)
2015-11-08 22:11:48 +00:00
{
2021-12-04 17:44:06 +00:00
c->state.geom = win_geom;
2008-02-20 08:13:41 +00:00
2021-11-20 22:49:19 +00:00
XWindowChanges wc = { 0 };
2021-12-04 17:44:06 +00:00
win_geom_convert_to_x_window_changes(&win_geom, &wc);
2021-11-20 17:57:26 +00:00
XConfigureWindow(
2021-11-22 04:31:28 +00:00
xbase->x_display,
c->x_window,
CWX | CWY | CWWidth | CWHeight | CWBorderWidth,
&wc
);
configure(c);
2021-11-22 04:31:28 +00:00
XSync(xbase->x_display, False);
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void restack(Monitor *m)
2015-11-08 22:11:48 +00:00
{
2007-09-16 09:53:14 +00:00
Client *c;
XEvent ev;
XWindowChanges wc;
2007-09-15 20:25:27 +00:00
2021-11-21 06:03:58 +00:00
if (!m->sel) return;
if (m->sel->state.is_floating || !m->lt[m->sellt]->arrange) {
2021-11-22 04:31:28 +00:00
XRaiseWindow(xbase->x_display, m->sel->x_window);
2021-11-21 06:03:58 +00:00
}
2015-11-08 22:11:48 +00:00
if (m->lt[m->sellt]->arrange) {
2007-09-16 09:53:14 +00:00
wc.stack_mode = Below;
// TODO: Learn what is sibling and what
// is the following line responsible for.
// wc.sibling = m->bar->barwin;
2021-11-21 06:03:58 +00:00
for (c = m->stack; c; c = c->snext) {
2021-11-20 19:53:53 +00:00
if (!c->state.is_floating && ISVISIBLE(c)) {
2021-11-21 06:03:58 +00:00
XConfigureWindow(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
c->x_window,
CWSibling | CWStackMode,
&wc
);
wc.sibling = c->x_window;
2008-04-27 17:22:52 +00:00
}
2021-11-21 06:03:58 +00:00
}
2007-09-16 09:53:14 +00:00
}
2021-11-22 04:31:28 +00:00
XSync(xbase->x_display, False);
while (XCheckMaskEvent(xbase->x_display, EnterWindowMask, &ev));
2007-09-15 20:25:27 +00:00
}
2021-11-20 16:06:20 +00:00
void run()
2015-11-08 22:11:48 +00:00
{
2007-09-16 10:34:08 +00:00
XEvent ev;
/* main event loop */
2021-11-22 04:31:28 +00:00
XSync(xbase->x_display, False);
while (running && !XNextEvent(xbase->x_display, &ev)) {
2021-11-21 06:03:58 +00:00
if (handler[ev.type]) {
2009-07-14 15:01:14 +00:00
handler[ev.type](&ev); /* call handler */
2021-11-21 06:03:58 +00:00
}
}
2007-09-16 10:34:08 +00:00
}
2021-11-20 16:06:20 +00:00
void scan()
2015-11-08 22:11:48 +00:00
{
2008-07-16 17:17:42 +00:00
unsigned int i, num;
Window d1, d2, *wins = NULL;
2007-09-15 20:25:27 +00:00
XWindowAttributes wa;
2021-11-22 04:38:11 +00:00
if (XQueryTree(xbase->x_display, xbase->x_root, &d1, &d2, &wins, &num)) {
2015-11-08 22:11:48 +00:00
for (i = 0; i < num; i++) {
2021-11-21 06:03:58 +00:00
if (
2021-11-22 04:31:28 +00:00
!XGetWindowAttributes(xbase->x_display, wins[i], &wa)
2021-11-21 06:03:58 +00:00
||
wa.override_redirect
||
2021-11-22 04:31:28 +00:00
XGetTransientForHint(xbase->x_display, wins[i], &d1)
2021-11-21 06:03:58 +00:00
) {
continue;
2021-11-21 06:03:58 +00:00
}
2021-12-05 13:05:23 +00:00
if (winpolybar(wins[i])) {
2021-12-05 13:27:19 +00:00
managepolybar(wins[i], &wa);
2021-12-05 13:05:23 +00:00
} else if (
2021-11-21 06:03:58 +00:00
wa.map_state == IsViewable
||
getstate(wins[i]) == IconicState
) {
manage(wins[i], &wa);
2021-11-21 06:03:58 +00:00
}
}
2015-11-08 22:11:48 +00:00
for (i = 0; i < num; i++) { /* now the transients */
2021-11-22 04:31:28 +00:00
if (!XGetWindowAttributes(xbase->x_display, wins[i], &wa)) {
continue;
2021-11-21 06:03:58 +00:00
}
2021-12-05 13:14:54 +00:00
if (winpolybar(wins[i])) {
// do nothing
} else if (
2021-11-22 04:31:28 +00:00
XGetTransientForHint(xbase->x_display, wins[i], &d1)
2021-11-21 06:03:58 +00:00
&&
(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
) {
manage(wins[i], &wa);
2021-11-21 06:03:58 +00:00
}
2007-09-15 20:25:27 +00:00
}
2015-11-08 22:11:48 +00:00
if (wins)
XFree(wins);
2007-09-15 20:25:27 +00:00
}
}
2021-11-21 22:48:55 +00:00
void scheme_create()
{
scheme = ecalloc(LENGTH(colors), sizeof(Clr*));
for (unsigned int i = 0; i < LENGTH(colors); ++i) {
scheme[i] = drw_scm_create(drw, colors[i], 3);
}
}
void scheme_destroy()
{
for (size_t i = 0; i < LENGTH(colors); ++i) {
free(scheme[i]);
}
}
2021-11-20 16:06:20 +00:00
void sendmon(Client *c, Monitor *m)
2015-11-08 22:11:48 +00:00
{
if (c->mon == m)
return;
2015-11-08 21:48:43 +00:00
unfocus(c, 1);
detach(c);
detachstack(c);
c->mon = m;
attach(c);
attachstack(c);
focus(NULL);
arrange(NULL);
}
2021-11-20 16:06:20 +00:00
void setclientstate(Client *c, long state)
2015-11-08 22:11:48 +00:00
{
2009-07-02 19:56:23 +00:00
long data[] = { state, None };
2007-09-16 09:53:14 +00:00
XChangeProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
c->x_window,
2021-12-04 16:49:53 +00:00
xbase->atoms->wmatom[WMState],
xbase->atoms->wmatom[WMState],
32,
PropModeReplace,
(unsigned char*)data,
2
);
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
int sendevent(Client *c, Atom proto)
2015-11-08 22:11:48 +00:00
{
int n;
Atom *protocols;
2015-11-08 21:48:43 +00:00
int exists = 0;
XEvent ev;
2021-11-22 04:31:28 +00:00
if (XGetWMProtocols(xbase->x_display, c->x_window, &protocols, &n)) {
2021-11-21 06:03:58 +00:00
while (!exists && n--) {
exists = protocols[n] == proto;
2021-11-21 06:03:58 +00:00
}
XFree(protocols);
}
2021-11-21 06:03:58 +00:00
2015-11-08 22:11:48 +00:00
if (exists) {
ev.type = ClientMessage;
ev.xclient.window = c->x_window;
2021-12-04 16:49:53 +00:00
ev.xclient.message_type = xbase->atoms->wmatom[WMProtocols];
ev.xclient.format = 32;
ev.xclient.data.l[0] = proto;
ev.xclient.data.l[1] = CurrentTime;
2021-11-22 04:31:28 +00:00
XSendEvent(xbase->x_display, c->x_window, False, NoEventMask, &ev);
}
2021-11-21 06:03:58 +00:00
return exists;
}
2021-11-20 16:06:20 +00:00
void setfocus(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-20 19:53:53 +00:00
if (!c->state.never_focus) {
2021-11-22 04:31:28 +00:00
XSetInputFocus(
xbase->x_display,
c->x_window,
RevertToPointerRoot,
CurrentTime
);
XChangeProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetActiveWindow],
XA_WINDOW,
32,
PropModeReplace,
(unsigned char*)&(c->x_window),
1
);
}
2021-12-04 16:49:53 +00:00
sendevent(c, xbase->atoms->wmatom[WMTakeFocus]);
}
2021-11-20 16:06:20 +00:00
void setfullscreen(Client *c, int fullscreen)
2015-11-08 22:11:48 +00:00
{
2021-11-20 19:53:53 +00:00
if (fullscreen && !c->state.is_fullscreen) {
2021-11-21 06:03:58 +00:00
XChangeProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
c->x_window,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetWMState],
2021-11-21 06:03:58 +00:00
XA_ATOM,
32,
PropModeReplace,
2021-12-04 16:49:53 +00:00
(unsigned char*)&xbase->atoms->netatom[NetWMFullscreen],
2021-11-21 06:03:58 +00:00
1
);
2021-11-20 20:01:49 +00:00
c->state.is_fullscreen = true;
// We have to rearrange because borders and gaps may have changed.
arrange(c->mon);
2021-11-20 19:53:53 +00:00
} else if (!fullscreen && c->state.is_fullscreen){
2021-11-21 06:03:58 +00:00
XChangeProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-21 06:03:58 +00:00
c->x_window,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetWMState],
2021-11-21 06:03:58 +00:00
XA_ATOM,
32,
PropModeReplace,
(unsigned char*)0,
0
);
2021-11-20 20:01:49 +00:00
c->state.is_fullscreen = false;
// We have to rearrange because borders and gaps may have changed.
arrange(c->mon);
2011-11-06 19:30:06 +00:00
}
}
2021-11-20 20:01:49 +00:00
void seturgent(Client *c, bool is_urgent)
{
XWMHints *wmh;
2021-11-20 20:01:49 +00:00
c->state.is_urgent = is_urgent;
2021-11-22 04:31:28 +00:00
if (!(wmh = XGetWMHints(xbase->x_display, c->x_window))) return;
2021-11-21 06:03:58 +00:00
2021-11-20 20:01:49 +00:00
wmh->flags =
is_urgent ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
2021-11-22 04:31:28 +00:00
XSetWMHints(xbase->x_display, c->x_window, wmh);
XFree(wmh);
}
2021-11-20 16:06:20 +00:00
void showhide(Client *c)
2015-11-08 22:11:48 +00:00
{
if (!c)
2008-09-06 08:34:49 +00:00
return;
2015-11-08 22:11:48 +00:00
if (ISVISIBLE(c)) {
/* show clients top down */
XMoveWindow(
2021-11-22 04:31:28 +00:00
xbase->x_display,
c->x_window,
2021-12-04 17:26:32 +00:00
c->state.geom.basic.position.x,
c->state.geom.basic.position.y
);
2021-11-20 19:53:53 +00:00
if (!c->mon->lt[c->mon->sellt]->arrange || c->state.is_floating) {
2021-12-04 17:26:32 +00:00
resize(c, c->state.geom, 0);
2021-11-20 17:57:26 +00:00
}
2021-11-14 06:17:15 +00:00
showhide(c->snext);
2015-11-08 22:11:48 +00:00
} else {
/* hide clients bottom up */
2021-11-14 06:17:15 +00:00
showhide(c->snext);
2021-11-20 21:00:39 +00:00
XMoveWindow(
2021-11-22 04:31:28 +00:00
xbase->x_display,
c->x_window,
2021-12-04 17:44:06 +00:00
win_geom_total_width(&c->state.geom) * -2,
2021-12-04 17:26:32 +00:00
c->state.geom.basic.position.y
2021-11-20 21:00:39 +00:00
);
2008-09-06 08:34:49 +00:00
}
}
2021-11-20 16:06:20 +00:00
void unfocus(Client *c, int setfocus)
2015-11-08 22:11:48 +00:00
{
if (!c)
return;
2015-11-08 21:48:43 +00:00
grabbuttons(c, 0);
2021-11-22 04:31:28 +00:00
XSetWindowBorder(
xbase->x_display,
c->x_window,
scheme[SchemeNorm][ColBorder].pixel
);
2015-11-08 22:11:48 +00:00
if (setfocus) {
2021-11-22 04:31:28 +00:00
XSetInputFocus(
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-11-22 04:31:28 +00:00
RevertToPointerRoot,
CurrentTime
);
XDeleteProperty(
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetActiveWindow]
2021-11-22 04:31:28 +00:00
);
}
}
2021-11-20 16:06:20 +00:00
void unmanage(Client *c, int destroyed)
2015-11-08 22:11:48 +00:00
{
Monitor *m = c->mon;
2007-09-16 09:53:14 +00:00
XWindowChanges wc;
detach(c);
detachstack(c);
2015-11-08 22:11:48 +00:00
if (!destroyed) {
2021-11-20 18:34:59 +00:00
wc.border_width = 0;
2021-11-22 04:31:28 +00:00
XGrabServer(xbase->x_display); /* avoid race conditions */
2009-09-08 12:13:03 +00:00
XSetErrorHandler(xerrordummy);
2021-11-22 04:31:28 +00:00
XConfigureWindow(xbase->x_display, c->x_window, CWBorderWidth, &wc); /* remove border */
XUngrabButton(xbase->x_display, AnyButton, AnyModifier, c->x_window);
2009-09-08 12:13:03 +00:00
setclientstate(c, WithdrawnState);
2021-11-22 04:31:28 +00:00
XSync(xbase->x_display, False);
2009-09-08 12:13:03 +00:00
XSetErrorHandler(xerror);
2021-11-22 04:31:28 +00:00
XUngrabServer(xbase->x_display);
2009-09-08 12:13:03 +00:00
}
2007-09-16 09:53:14 +00:00
free(c);
2021-11-20 16:42:59 +00:00
focus(NULL);
updateclientlist();
arrange(m);
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
void updateclientlist()
2015-11-08 22:11:48 +00:00
{
2021-11-22 04:38:11 +00:00
XDeleteProperty(
xbase->x_display,
xbase->x_root,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetClientList]
2021-11-22 04:38:11 +00:00
);
for (Monitor *m = mons; m; m = m->next) {
for (Client *c = m->clients; c; c = c->next) {
XChangeProperty(
2021-11-22 04:31:28 +00:00
xbase->x_display,
2021-11-22 04:38:11 +00:00
xbase->x_root,
2021-12-04 16:49:53 +00:00
xbase->atoms->netatom[NetClientList],
XA_WINDOW,
32,
PropModeAppend,
(unsigned char*)&(c->x_window),
1
);
}
}
}
2021-11-20 16:06:20 +00:00
int updategeom()
2015-11-08 22:11:48 +00:00
{
2015-11-08 21:48:43 +00:00
int dirty = 0;
2021-11-16 22:02:06 +00:00
#ifdef ENABLE_XINERAMA
2021-11-22 04:31:28 +00:00
if (XineramaIsActive(xbase->x_display)) {
int i, j, n, nn;
Client *c;
Monitor *m;
2021-11-22 04:31:28 +00:00
XineramaScreenInfo *info = XineramaQueryScreens(xbase->x_display, &nn);
XineramaScreenInfo *unique = NULL;
2015-11-08 22:11:48 +00:00
for (n = 0, m = mons; m; m = m->next, n++);
/* only consider unique geometries as separate screens */
unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2015-11-08 22:11:48 +00:00
for (i = 0, j = 0; i < nn; i++)
if (isuniquegeom(unique, j, &info[i]))
memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
XFree(info);
nn = j;
if (n <= nn) { /* new monitors available */
for (i = 0; i < (nn - n); i++) {
2015-11-08 22:11:48 +00:00
for (m = mons; m && m->next; m = m->next);
if (m)
2021-11-21 22:51:41 +00:00
m->next = monitor_create();
else
2021-11-21 22:51:41 +00:00
mons = monitor_create();
}
2015-11-08 22:11:48 +00:00
for (i = 0, m = mons; i < nn && m; m = m->next, i++)
if (
i >= n
||
2021-12-04 17:26:32 +00:00
unique[i].x_org != m->screen_geom.position.x
||
2021-12-04 17:26:32 +00:00
unique[i].y_org != m->screen_geom.position.y
||
2021-12-04 17:26:32 +00:00
unique[i].width != m->screen_geom.sizes.w
||
2021-12-04 17:26:32 +00:00
unique[i].height != m->screen_geom.sizes.h
) {
2015-11-08 21:48:43 +00:00
dirty = 1;
m->num = i;
2021-11-21 00:53:59 +00:00
2021-12-04 17:26:32 +00:00
m->screen_geom = m->window_area_geom =
basic_geom_create_from_args(
2021-11-21 00:53:59 +00:00
unique[i].x_org,
unique[i].y_org,
unique[i].width,
unique[i].height
);
}
} else { /* less monitors available nn < n */
2015-11-08 22:11:48 +00:00
for (i = nn; i < n; i++) {
for (m = mons; m && m->next; m = m->next);
while ((c = m->clients)) {
2015-11-08 21:48:43 +00:00
dirty = 1;
m->clients = c->next;
detachstack(c);
c->mon = mons;
attach(c);
attachstack(c);
}
2015-11-08 22:11:48 +00:00
if (m == selmon)
selmon = mons;
2021-11-21 22:51:41 +00:00
monitor_destroy(m);
}
}
free(unique);
2015-11-08 22:11:48 +00:00
} else
2021-11-16 22:02:06 +00:00
#endif /* ENABLE_XINERAMA */
{ /* default monitor setup */
2015-11-08 22:11:48 +00:00
if (!mons)
2021-11-21 22:51:41 +00:00
mons = monitor_create();
2021-11-21 00:53:59 +00:00
2021-11-20 21:56:08 +00:00
if (
2021-12-04 17:26:32 +00:00
mons->screen_geom.sizes.w != xbase->screen_sizes.w
2021-11-20 21:56:08 +00:00
||
2021-12-04 17:26:32 +00:00
mons->screen_geom.sizes.h != xbase->screen_sizes.h
2021-11-20 21:56:08 +00:00
) {
2015-11-08 21:48:43 +00:00
dirty = 1;
2021-12-04 17:26:32 +00:00
mons->screen_geom.sizes =
mons->window_area_geom.sizes =
2021-11-22 04:35:20 +00:00
xbase->screen_sizes;
}
2009-06-22 13:58:08 +00:00
}
2015-11-08 22:11:48 +00:00
if (dirty) {
selmon = mons;
2021-11-22 04:38:11 +00:00
selmon = wintomon(xbase->x_root);
}
return dirty;
}
2021-11-20 16:06:20 +00:00
void updatenumlockmask()
2015-11-08 22:11:48 +00:00
{
2021-11-18 18:50:43 +00:00
unsigned int i;
XModifierKeymap *modmap;
numlockmask = 0;
2021-11-22 04:31:28 +00:00
modmap = XGetModifierMapping(xbase->x_display);
2021-11-21 06:03:58 +00:00
for (i = 0; i < 8; i++) {
for (int j = 0; j < modmap->max_keypermod; j++) {
if (
modmap->modifiermap[i * modmap->max_keypermod + j]
==
2021-11-22 04:31:28 +00:00
XKeysymToKeycode(xbase->x_display, XK_Num_Lock)
2021-11-21 06:03:58 +00:00
) {
numlockmask = (1 << i);
2021-11-21 06:03:58 +00:00
}
}
}
XFreeModifiermap(modmap);
}
2021-11-20 16:06:20 +00:00
void updatesizehints(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-20 18:13:49 +00:00
XSizeHints size = { 0 };
long msize = 0;
2007-09-16 09:53:14 +00:00
2021-11-22 04:31:28 +00:00
if (!XGetWMNormalHints(xbase->x_display, c->x_window, &size, &msize)) {
2008-09-07 08:53:59 +00:00
/* size is uninitialized, ensure that size.flags aren't used */
size.flags = PSize;
2021-11-20 18:13:49 +00:00
}
2021-11-20 19:48:37 +00:00
client_size_hints_update(&c->size_hints, &size);
2021-11-20 18:13:49 +00:00
2021-11-20 19:53:53 +00:00
c->state.is_fixed = (
2021-11-20 18:18:41 +00:00
c->size_hints.maxw
&&
c->size_hints.maxh
&&
c->size_hints.maxw == c->size_hints.minw
&&
c->size_hints.maxh == c->size_hints.minh
);
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
void updatetitle(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-12-04 16:49:53 +00:00
if (!gettextprop(c->x_window,
xbase->atoms->netatom[NetWMName],
c->state.name,
sizeof(c->state.name)))
{
gettextprop(c->x_window, XA_WM_NAME, c->state.name, sizeof(c->state.name));
2021-12-04 16:49:53 +00:00
}
// hack to mark broken clients
if (c->state.name[0] == '\0') {
2021-11-20 19:22:11 +00:00
strcpy(c->state.name, broken);
2021-12-04 16:49:53 +00:00
}
2007-09-16 09:53:14 +00:00
}
2021-11-20 16:06:20 +00:00
void updatewindowtype(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-12-04 16:49:53 +00:00
Atom state = getatomprop(c, xbase->atoms->netatom[NetWMState]);
Atom wtype = getatomprop(c, xbase->atoms->netatom[NetWMWindowType]);
2011-11-02 12:01:28 +00:00
2021-12-04 16:49:53 +00:00
if (state == xbase->atoms->netatom[NetWMFullscreen])
2015-11-08 21:48:43 +00:00
setfullscreen(c, 1);
2021-12-04 16:49:53 +00:00
if (wtype == xbase->atoms->netatom[NetWMWindowTypeDialog])
2021-11-20 20:01:49 +00:00
c->state.is_floating = true;
2011-11-02 12:01:28 +00:00
}
2021-11-20 16:06:20 +00:00
void updatewmhints(Client *c)
2015-11-08 22:11:48 +00:00
{
2021-11-22 04:31:28 +00:00
XWMHints *wmh = XGetWMHints(xbase->x_display, c->x_window);
2021-11-20 17:17:29 +00:00
if (!wmh) return;
2021-11-20 17:17:29 +00:00
if (c == selmon->sel && wmh->flags & XUrgencyHint) {
wmh->flags &= ~XUrgencyHint;
2021-11-22 04:31:28 +00:00
XSetWMHints(xbase->x_display, c->x_window, wmh);
2021-11-20 17:17:29 +00:00
} else {
2021-11-20 20:01:49 +00:00
c->state.is_urgent = (wmh->flags & XUrgencyHint) ? true : false;
}
2021-11-20 17:17:29 +00:00
if (wmh->flags & InputHint) {
2021-11-20 19:53:53 +00:00
c->state.never_focus = !wmh->input;
2021-11-20 17:17:29 +00:00
} else {
2021-11-20 20:01:49 +00:00
c->state.never_focus = false;
2021-11-20 17:17:29 +00:00
}
XFree(wmh);
}
2021-11-20 16:06:20 +00:00
Client *wintoclient(Window w)
2015-11-08 22:11:48 +00:00
{
2021-11-20 17:15:54 +00:00
for (Monitor *m = mons; m; m = m->next) {
for (Client *c = m->clients; c; c = c->next) {
if (c->x_window == w) {
2009-06-30 18:39:59 +00:00
return c;
2021-11-20 17:15:54 +00:00
}
}
}
2009-06-30 18:39:59 +00:00
return NULL;
}
2021-11-20 16:06:20 +00:00
Monitor *wintomon(Window w)
2015-11-08 22:11:48 +00:00
{
2021-11-20 17:14:57 +00:00
{
int x = 0, y = 0;
2021-11-22 04:38:11 +00:00
if (w == xbase->x_root && getrootptr(&x, &y)) {
2021-11-20 17:14:57 +00:00
return recttomon(x, y, 1, 1);
}
}
{
Client *c = wintoclient(w);
if (c) return c->mon;
}
2009-06-30 18:39:59 +00:00
2009-07-02 13:42:06 +00:00
return selmon;
2009-06-30 18:39:59 +00:00
}