wm/wm.c
2022-09-19 14:02:58 +02:00

691 lines
19 KiB
C

/*
The GPLv3 License (GPLv3)
Copyright (c) 2022 Akos Horvath
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wm.h"
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xinerama.h>
#include <asm-generic/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "handler.h"
#include "client.h"
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info)
{
Monitor m = {
.display = d,
.info = info
};
return m;
}
void wm_monitors_open_all(Wm *wm, Display *d)
{
DEBUG_PRINT("wm_monitors_open_all\n");
int count;
XineramaScreenInfo *xsi = XineramaQueryScreens(d, &count);
if (!xsi) {
printf("wm: Could not open display. Exiting.\n");
exit(1);
}
wm->monitors = malloc(count * sizeof(Monitor));
for (int i = 0; i < count; i++) {
wm->monitors[i] = wm_monitor_open(d, xsi[i]);
//TODO: cfg
wm->monitors[i].wscount = 9;
wm->monitors[i].wss = malloc(wm->monitors[i].wscount*sizeof(int));
memset(wm->monitors[i].wss, 0, wm->monitors[i].wscount);
wm->monitors[i].selws = wm->monitors[i].wss[0];
for (int j = 0; j < wm->monitors[i].wscount; j++)
wm->monitors[i].wss[i] = j;
//wm->monitors[i].clients = &wm->root;
wm->monitors[i].clients = NULL;
}
wm->smon = &wm->monitors[0];
// wm->smon->clients = &wm->root;
DEBUG_PRINT("smon width: %d\n", wm->smon->info.width);
}
Display* wm_connect_display()
{
Display *d;
if (!(d = XOpenDisplay(NULL)))
{
printf("wm: Could not open display. Exiting.\n");
exit(1);
}
return d;
}
bool wm_window_is_dock(Wm *wm, Window w)
{
// TODO: get atom new function
unsigned char *prop_ret;
Atom atom_ret;
int format_ret;
unsigned long nitems_ret;
unsigned long bytes_remain_ret;
int ret;
ret = XGetWindowProperty(wm->display, w,
XInternAtom(wm->display, "_NET_WM_WINDOW_TYPE", False), 0, (~0L), False,
AnyPropertyType, &atom_ret,
&format_ret, &nitems_ret, &bytes_remain_ret, &prop_ret);
if (ret != Success || nitems_ret == 0) {
fprintf(stderr, "wm: XGetWindowProperty failed\n");
return false;
}
DEBUG_PRINT("XGetWindowProperty return values, window %d:\n", (int)w);
// DEBUG_PRINT("actual atom return: %s\n", XGetAtomName(wm->display, atom_ret));
DEBUG_PRINT("actual format return: %d\n", format_ret)
DEBUG_PRINT("actual number of items return: %ld\n", nitems_ret)
DEBUG_PRINT("bytes remaining: %ld\n", nitems_ret)
//DEBUG_PRINT("property return dec: %d\n", prop_ret)
for (int i = 0; i < nitems_ret; i++) {
printf("property return str: %s\n",
XGetAtomName(wm->display, ((Atom*)prop_ret)[i]));
if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_DOCK") == 0) {
DEBUG_PRINT("%s", __func__)
DEBUG_PRINT("returning true\n")
return true;
}
}
return false;
}
void wm_switch_ws(Wm *wm, int ws)
{
if (ws > wm->smon->wscount || ws < 0)
return;
int wscc = 0;
int xasws;
Client *c;
for (c = wm->smon->clients; c; c = c->next) {
if (c->ws == wm->smon->selws) {
DEBUG_PRINT("wm_switch_ws hide client selws: %d\n", c->ws)
wm_client_hide(wm, c);
}
}
wm->smon->selws = ws;
wm_client_set_atom(wm, &wm-> root, "_NET_CURRENT_DESKTOP",
(unsigned char*) &(ws), XA_CARDINAL, 1);
// XChangeProperty(wm->display, root.window,
// XInternAtom(wm->display, "_NET_CURRENT_DESKTOP", False), XA_CARDINAL,
// 32, PropModeReplace, (unsigned char *) &(xasws), 1);
for (c = wm->smon->clients; c; c = c->next) {
if (c->ws == wm->smon->selws) {
wscc++;
wm_client_show(wm, c);
}
}
DEBUG_PRINT("wm_switch_ws focused ws client count: %d\n", wscc)
}
// TODO maybe x,y is not 0, because of bar
void wm_mstack(Wm *wm, Monitor *m)
{
DEBUG_PRINT("%s\n", __func__)
RETURN_IF_NULL(m);
Client *c;
XWindowChanges ch;
int cc_sws = 0;
int n = 0;
int sx = 0;
int sy = 0;
XTextProperty xt;
XEvent e;
unsigned int value_mask = CWX | CWY | CWWidth | CWHeight;
// TODO: function
// FIXME: &root probably wrong
if (wm->dock != -1) {
XWindowAttributes x;
XGetWindowAttributes(wm->display, wm->dock, &x);
sx = x.x;
sy = x.y+x.height;
DEBUG_PRINT("%s dock sx x: %d y: %d\n", __func__, sx, sy)
}
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c != &wm->root && !c->is_floating) {
cc_sws++;
}
}
//DEBUG_PRINT("mstack cc_sws: %d\n", cc_sws)
// dynamic
if (cc_sws == 1) {
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c != &wm->root)
break;
}
c->x = 0;
c->y = sy;
c->w = wm->smon->info.width-wm->cfg_border_width*2;
c->h = (wm->smon->info.height-wm->cfg_border_width*2)-sy;
ch = wm_client_to_xwchanges(*c);
DEBUG_PRINT("mstack client: 0x%x\n", c);
// XGetWMName(wm->display, c->window, &xt);
DEBUG_PRINT("mstack window id: %d\n", c->window);
// DEBUG_PRINT("mstack wm_name: %s\n", xt.value)
// DEBUG_PRINT("mstack client width: %d\n", ch.width)
// DEBUG_PRINT("mstack client height: %d\n", ch.height)
XConfigureWindow(wm->display, c->window, value_mask, &ch);
wm_client_show(wm, c);
//wm_client_focus(c);
}
else {
// FIXME not working with dock
for (c = m->clients; c; c = c->next) {
if (c->ws == m->selws && c->window != wm->root.window && !c->is_floating) {
if (n == 0) {
c->x = 0;
c->y = sy;
c->w = m->info.width*wm->cfg_ms_p-wm->cfg_border_width*2;
c->h = (m->info.height-wm->cfg_border_width*2)-sy;
} else {
c->x = (m->info.width*wm->cfg_ms_p);
c->y = ((n-1)*m->info.height/(cc_sws-1))+sy;
c->w = (m->info.width - m->info.width*wm->cfg_ms_p) -
wm->cfg_border_width*2;
c->h = ((m->info.height)/(cc_sws-1)-wm->cfg_border_width*2);
}
n++;
ch = wm_client_to_xwchanges(*c);
// TODO store wm name when client is created
// FIXME vv RUNTIME MALLOC ABORT vv
//XGetWMName(wm->display, c->window, &xt);
DEBUG_PRINT("mstack client: 0x%x\n", c);
DEBUG_PRINT("mstack window id: %d\n", c->window);
// DEBUG_PRINT("mstack wm_name: %s\n", xt.value)
// DEBUG_PRINT("mstack client x: %d ", ch.x)
// DEBUG_PRINT("y: %d\n", ch.y)
// DEBUG_PRINT("mstack client width: %d ", ch.width)
// DEBUG_PRINT("height: %d\n", ch.height)
XConfigureWindow(wm->display, c->window, value_mask, &ch);
wm_client_show(wm, c);
DEBUG_PRINT("%s %d. client next: 0x%x\n", __func__, n, c->next)
if (c->next == NULL)
{
wm_client_focus(wm, c);
}
}
}
}
DEBUG_PRINT("%s end\n", __func__)
XSync(wm->display, False);
while(XCheckMaskEvent(wm->display, EnterWindowMask, &e));
wm_monitor_clients_border(wm, m);
}
void wm_set_layout(Wm *wm, void(*f)(Wm *wm, Monitor*))
{
wm->active_layout = f;
}
// TODO: layout floating window, selected layout func p
void wm_layout(Wm *wm, Monitor *m)
{
RETURN_IF_NULL(m);
wm_mstack(wm, m);
}
Client *wm_get_last_client(Wm *wm, Monitor m)
{
Client *c = m.clients;
if (c == NULL)
return NULL;
while (c->next != NULL)
c = c->next;
return c;
}
void wm_mainloop(Wm *wm)
{
// int s = DefaultScreen(wm->display);
// Window w = XCreateSimpleWindow(wm->display, root.window, 10, 10, 300, 300,
// 2, WhitePixel(wm->display, s), BlackPixel(wm->display, s));
// XSelectInput(wm->display, w, KeyPressMask | KeyReleaseMask);
// XMapWindow(wm->display, w);
struct sockaddr s;
socklen_t t = 108;
getsockname(wm->socket_fd, &s, &t);
int accepted_socket;
char *buffer;
int len;
while (1) {
// accepted_socket = accept(wm->socket_fd, &s, &t);
// if (accepted_socket == -1) {
// if (errno == EAGAIN || errno == EWOULDBLOCK) {
// // fprintf(stderr,
// // "wm: accept failed, no waiting connections are present\n");
// } else {
// fprintf(stderr, "wm: accept failed\n");
// }
// }
// else {
// DEBUG_PRINT("accept success\n")
// len = recv(accepted_socket, buffer, 100, 0);
// if (len != -1) {
// DEBUG_PRINT("got %d bytes:", len)
// for (int i = 0; i < len; i++)
// DEBUG_PRINT("%c", buffer[i])
// DEBUG_PRINT("\n")
// } else {
// fprintf(stderr, "wm: recv failed\n");
// fprintf(stderr, "errno: %d\n", errno);
// }
// }
while (XPending(wm->display)) {
XEvent e;
XNextEvent(wm->display, &e);
switch (e.type) {
case CreateNotify:
wm_create_handler(wm, e.xcreatewindow);
break;
case DestroyNotify:
wm_destroy_handler(wm, e.xdestroywindow);
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
case ConfigureRequest:
wm_configure_handler(wm, e.xconfigurerequest);
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(wm, e.xkey);
break;
case MotionNotify:
wm_motion_handler(wm, e.xmotion);
break;
case FocusIn:
break;
case FocusOut:
break;
case EnterNotify:
DEBUG_PRINT("pointer entered window: %d\n",
(int)e.xcrossing.window);
if (wm->cfg_focus_on_motion)
wm_client_focus(wm, wm_client_find(wm, e.xcrossing.window));
break;
case LeaveNotify:
DEBUG_PRINT("pointer left window: %d\n",
(int)e.xcrossing.window);
break;
case ConfigureNotify:
DEBUG_PRINT("ConfigureNotify: %d\n", (int)e.xconfigure.window)
break;
case MapRequest:
DEBUG_PRINT("MapRequest: %d\n", (int)e.xmaprequest.window)
wm_maprequest_handler(wm, e.xmaprequest);
break;
}
}
}
}
void wm_init(Wm *wm)
{
XSetWindowAttributes xa;
Display *d;
signal(SIGINT, wm_sigint_handler);
DEBUG_PRINT("wm_init\n")
DEBUG_PRINT("pid: %ld\n", ((long)getpid()))
d = wm_connect_display();
wm->display = d;
wm_monitors_open_all(wm, d);
// TODO: only testing
// wm_socket_init();
wm->root.window = XRootWindow(wm->display, DefaultScreen(wm->display));
wm->root.ws = 999;
DEBUG_PRINT("root window: %ld\n", wm->root.window);
XSync(d, false);
xa.event_mask = StructureNotifyMask | StructureNotifyMask |
SubstructureNotifyMask | SubstructureRedirectMask |
ButtonPressMask | PropertyChangeMask;
XChangeWindowAttributes(wm->display, wm->root.window, CWEventMask, &xa);
XSelectInput(wm->display, wm->root.window, xa.event_mask);
// DEBUG_PRINT("root window id: %d\n", (root.window))
// XUngrabKey(wm->display, AnyKey, AnyModifier, root.window);
// TODO: read config
wm_init_cfg_def(wm);
wm_grab_keys(wm);
//XSetErrorHandler(&wm_other_wm_handler);
// if (wm->other_wm) {
// printf("wm: Detected other window manager. Exiting.\n");
// exit(1);
// }
//TODO: new function!
XChangeProperty(wm->display, wm->root.window,
XInternAtom(wm->display, "_NET_NUMBER_OF_DESKTOPS", False), XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &wm->smon->wscount, 1);
char *desktop_names[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9"};
wm_client_set_atom(wm, &wm->root, "_NET_DESKTOP_NAMES",
(unsigned char*) desktop_names, XA_STRING, 9);
Atom wm_supp[] = {
XInternAtom(wm->display, "_NET_NUMBER_OF_DESKTOPS", False),
XInternAtom(wm->display, "_NET_DESKTOP_GEOMETRY", False),
XInternAtom(wm->display, "_NET_DESKTOP_VIEWPORT", False),
XInternAtom(wm->display, "_NET_CURRENT_DESKTOP", False),
XInternAtom(wm->display, "_NET_ACTIVE_WINDOW", False),
XInternAtom(wm->display, "_NET_DESKTOP_NAMES", False),
};
XChangeProperty(wm->display, wm->root.window,
XInternAtom(wm->display, "_NET_SUPPORTED", False), XA_ATOM, 32,
PropModeReplace, (unsigned char *) &wm_supp, sizeof(wm_supp)/sizeof(long));
//XSetErrorHandler(&wm_xerror_handler);
wm->dock = -1;
wm_mainloop(wm);
}
void wm_init_cfg_def(Wm *wm)
{
wm->cfg_ms_p = 0.5;
wm->cfg_border_col = "#222222";
wm->cfg_focused_border_col = "#444444";
wm->cfg_border_width = 2;
wm->cfg_focus_on_motion = true;
wm_keybinds_init_def(wm);
}
void wm_grab_keys(Wm *wm)
{
RETURN_IF_NULL(wm->cfg_keybinds)
Keybind k;
for (int i = 0; i < wm->cfg_kb_count; i++) {
k = wm->cfg_keybinds[i];
XGrabKey(wm->display, XKeysymToKeycode(wm->display, k.keysym), k.mask,
wm->root.window, True, GrabModeAsync, GrabModeAsync);
}
}
void wm_kb_spawn(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
RETURN_IF_NULL(args->sl)
DEBUG_PRINT("args sl 1: %s\n", args->sl[args->count-1])
DEBUG_PRINT("args sl 0: %s\n", args->sl[0])
DEBUG_PRINT("args count: %d\n", args->count)
if (args->sl[args->count-1] != NULL) {
fprintf(stderr, "%s recieved non null-terminated args. Returning\n",
__func__);
}
if (fork() == 0) {
if (wm->display)
close(ConnectionNumber(wm->display));
setsid();
execvp(args->sl[0], &(args->sl[1]));
exit(0);
}
}
void wm_kb_kill(Wm *wm, Arg *args)
{
Client *c;
c = wm_client_get_focused(wm);
RETURN_IF_NULL(c)
wm_client_kill(wm, c);
}
void wm_kb_switch_ws(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)
wm_switch_ws(wm, args->i);
}
void wm_kb_focus_dir(Wm *wm, Arg *args)
{
Client *c;
RETURN_IF_NULL(args)
c = wm_client_get_focused(wm);
wm_client_focus_dir(wm, c, args->i);
}
void wm_keybinds_init_def(Wm *wm)
{
char *st[] = {"st", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
int size;
Keybind k[] = {
(Keybind) {Mod4Mask, XK_Return, *wm_kb_spawn,
(Arg) {.sl = sth, .count = 2}},
(Keybind) {Mod4Mask, XK_c, *wm_kb_kill,
(Arg) {.c = NULL}},
(Keybind) {Mod4Mask, XK_1, *wm_kb_switch_ws,
(Arg) {.i = 0}},
(Keybind) {Mod4Mask, XK_2, *wm_kb_switch_ws,
(Arg) {.i = 1}},
(Keybind) {Mod4Mask, XK_3, *wm_kb_switch_ws,
(Arg) {.i = 2}},
(Keybind) {Mod4Mask, XK_4, *wm_kb_switch_ws,
(Arg) {.i = 3}},
(Keybind) {Mod4Mask, XK_5, *wm_kb_switch_ws,
(Arg) {.i = 4}},
(Keybind) {Mod4Mask, XK_6, *wm_kb_switch_ws,
(Arg) {.i = 5}},
(Keybind) {Mod4Mask, XK_7, *wm_kb_switch_ws,
(Arg) {.i = 6}},
(Keybind) {Mod4Mask, XK_8, *wm_kb_switch_ws,
(Arg) {.i = 7}},
(Keybind) {Mod4Mask, XK_9, *wm_kb_switch_ws,
(Arg) {.i = 8}},
(Keybind) {Mod4Mask, XK_h, *wm_kb_focus_dir,
(Arg) {.i = LEFT}},
(Keybind) {Mod4Mask, XK_j, *wm_kb_focus_dir,
(Arg) {.i = DOWN}},
(Keybind) {Mod4Mask, XK_k, *wm_kb_focus_dir,
(Arg) {.i = UP}},
(Keybind) {Mod4Mask, XK_l, *wm_kb_focus_dir,
(Arg) {.i = RIGHT}},
};
size = sizeof(k);
wm->cfg_kb_count = size / sizeof(Keybind);
DEBUG_PRINT("sizeof k: %d\n", size);
wm->cfg_keybinds = malloc(size);
memcpy(wm->cfg_keybinds, k, size);
}
struct sockaddr wm_socket_init(Wm *wm)
{
struct sockaddr_un sock_addr;
int ret;
ret = wm->socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (ret == -1) {
fprintf(stderr, "wm: could not create socket. Exiting.\n");
exit(EXIT_FAILURE);
}
sock_addr.sun_family = AF_UNIX;
snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path),
"/tmp/wm_socket_%c", DisplayString(wm->display)[1]);
ret = bind(wm->socket_fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr));
if (ret == -1) {
fprintf(stderr, "wm: could not bind to socket. Exiting.\n");
exit(EXIT_FAILURE);
}
ret = listen(wm->socket_fd, 512);
if (ret == -1) {
fprintf(stderr, "wm: could not listen to socket. Exiting.\n");
exit(EXIT_FAILURE);
}
}
void wm_socket_cleanup(Wm *wm)
{
struct sockaddr s;
socklen_t t = 108;
getsockname(wm->socket_fd, &s, &t);
DEBUG_PRINT("socket cleanup path: %s\n", s.sa_data)
remove(s.sa_data);
}
void wm_sigint_handler(Wm *wm, int signum)
{
wm_socket_cleanup(wm);
exit(1);
}
// TODO: maybe move some parts to wmc
void wm_settings_message_parse(Wm *wm, char *c, int len)
{
if (len <= 0)
return;
int op; /* 0 = set, 1 = get */
int i = 0;
char ret[50];
char *token;
char tokens[20][50];
char *settings[] = {"border-width", "border-color", "focused-border-color",
"msfact", "focus-on-motion"};
while((token = strtok(c, " ")) != NULL) {
DEBUG_PRINT("message_parse token: %s\n", token)
strncpy(tokens[i], token, 50);
}
if (strcmp(tokens[1], "set") == 0) {
op = 0;
} else if (strcmp(c, "get") == 0) {
op = 1;
} else {
return;
}
for (i = 0; i < sizeof(settings)/sizeof(char*); i++) {
if (strcmp(tokens[1], settings[i])) {
if (op == 1) {
// wm_settings_get(settings[i], &ret);
} else {
// wm_settings_set(settings[i]);
}
}
}
}