split up wm.c into multiple files

This commit is contained in:
Akos Horvath 2022-09-18 18:57:48 +02:00
parent 5c64325f4e
commit 2e1d6eb6f6
6 changed files with 586 additions and 618 deletions

View File

@ -1,4 +1,4 @@
all:
gcc wm.c main.c -lXinerama -lX11 -o wm
gcc wm.c handler.c main.c -lXinerama -lX11 -o wm
debug:
gcc wm.c main.c -lXinerama -lX11 -o wm -g
gcc wm.c handler.c main.c -lXinerama -lX11 -o wm -g

240
handler.c Normal file
View File

@ -0,0 +1,240 @@
#include "handler.h"
#include "wm.h"
/* global configuration variables */
// TODO: active layout not working
static int wm_xerror_handler(Wm *wm, Display *display, XErrorEvent *e)
{
printf("wm: Error occured. id: %d req_code: %d\n",
e->error_code, e->request_code);
char etext[200];
XGetErrorText(wm->display, e->error_code, etext, 200);
printf("%s\n", etext);
if (e->error_code == BadAccess) {
wm->other_wm = true;
}
return 0;
}
static int wm_other_wm_handler(Display *display, XErrorEvent *e)
{
return 0;
}
// maybe need to handle non-normal windows too
void wm_create_handler(Wm *wm, XCreateWindowEvent e)
{
DEBUG_PRINT("CreateNotify event\n");
// Client *nc;
// Client *c;
// Client t;
// unsigned char *prop_ret;
// unsigned long nitems_ret;
// Atom type;
// bool is_normal = false;
// t.window = e.window;
// t.m = wm->smon;
// t.ws = wm->smon->selws;
// t.hidden = true;
// //wm_client_is_dock(&t);
// /*
// _NET_WM_WINDOW_TYPE_NORMAL indicates that this is a normal,
// top-level window, [...] windows without _NET_WM_WINDOW_TYPE,
// must be taken as this type [...]
// https://specifications.freedesktop.org/wm-spec
// */
// if (type == None)
// is_normal = true;
//
// if (!is_normal) {
// DEBUG_PRINT("Create handler: window %d is not normal, returning",
// (int)e.window)
// return;
// }
// nc = malloc(sizeof(Client*));
// *nc = t;
// if (wm->smon->clients == NULL) {
// wm->smon->clients = nc;
// nc->prev = NULL;
// } else {
// c = wm_get_last_client(*nc->m);
// //c = &wm_root;
// c->next = nc;
// nc->prev = c;
// nc->next = NULL;
// }
//
// XSelectInput(wm->display, nc->window, FocusChangeMask | EnterWindowMask);
// DEBUG_PRINT("%d window created,", nc->window);
// DEBUG_PRINT("client ws: %d\n", nc->ws);
// // wm_client_is_dock(nc);
// wm_mstack(nc->m);
//wm_client_focus(c);
}
// TODO
void wm_destroy_handler(Wm *wm, XDestroyWindowEvent e)
{
DEBUG_PRINT("DestroyNotify event\n");
Client *c;
// TODO: make function for linked list management
for (c = wm->smon->clients; c; c = c->next) {
if (e.window == c->window) {
if (c == wm->smon->clients) {
if (c->next)
wm->smon->clients = c->next;
else
wm->smon->clients = NULL;
} else {
if (!c->next)
c->prev->next = NULL;
else {
c->prev->next = c->next;
c->next->prev = c->prev;
}
}
free(c);
}
}
}
void wm_reparent_handler(XReparentEvent e)
{
DEBUG_PRINT("ReparentNotify event\n");
}
void wm_keyrelease_handler(XKeyReleasedEvent e)
{
DEBUG_PRINT("KeyReleased event, code: %d\n", e.keycode)
}
void wm_keypress_handler(Wm *wm, XKeyPressedEvent e)
{
DEBUG_PRINT("KeyPressed event, code: %d\n", e.keycode)
Client *c;
Keybind k;
DEBUG_PRINT("wm->cfg_kb_count: %d\n", wm->cfg_kb_count);
for (int i = 0; i < wm->cfg_kb_count; i++) {
k = wm->cfg_keybinds[i];
DEBUG_PRINT("Keybind keysym: %s\n", XKeysymToString(k.keysym));
if (k.mask == e.state && k.keysym == XLookupKeysym(&e, 0))
{
(*k.func)(wm, &k.args);
}
}
}
void wm_maprequest_handler(Wm *wm, XMapRequestEvent e)
{
DEBUG_PRINT("MapRequest event\n")
Client *c;
c = wm_client_find(wm, e.window);
if (c) {
DEBUG_PRINT("%s: client found, mapping window\n", __func__)
XMapWindow(wm->display, e.window);
c->hidden = false;
return;
}
if (wm_window_is_dock(wm, e.window))
{
DEBUG_PRINT("%s: window is dock, mapping window\n", __func__)
XMapWindow(wm->display, e.window);
return;
}
DEBUG_PRINT("%s: client not found, creating client\n", __func__)
c = wm_client_create(wm, e.window);
RETURN_IF_NULL(c)
//XMapWindow(wm->display, c->window);
wm_layout(wm, c->m);
}
void wm_motion_handler(Wm *wm, XMotionEvent e)
{
Client *c;
c = wm_client_find(wm, e.window);
RETURN_IF_NULL(c)
if(!wm_client_is_focused(wm, c) && wm->cfg_focus_on_motion)
wm_client_focus(wm, c);
}
// TODO
void wm_configure_handler(Wm *wm, XConfigureRequestEvent e)
{
DEBUG_PRINT("ConfigureRequest event\n");
Client *c;
unsigned char *prop_ret;
unsigned long nitems_ret;
Atom type;
bool is_normal = false;
XTextProperty xtp;
c = wm_client_create(wm, e.window);
XGetWMName(wm->display, e.window, &xtp);
DEBUG_PRINT("%s: created window %d name: %s\n", __func__, (int)e.window, xtp.value);
//XFree(xtp);
char *name = "_NET_WM_WINDOW_TYPE";
type = wm_client_get_atom(wm, c, "_NET_WM_WINDOW_TYPE", &prop_ret, &nitems_ret);
if (type == -1) {
fprintf(stderr, "wm_client_get_atom failed\n");
return;
}
for (int i = 0; i < nitems_ret; i++) {
DEBUG_PRINT("ConfigureRequest handler: window %d has property %s\n",
(int)e.window, XGetAtomName(wm->display, ((Atom*)prop_ret)[i]));
if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_NORMAL") == 0) {
is_normal = true;
} else if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_DOCK") == 0) {
wm->dock = e.window;
} else if (strcmp(XGetAtomName(wm->display, ((Atom*)prop_ret)[i]),
"_NET_WM_WINDOW_TYPE_DIALOG") == 0) {
c->is_floating = true;
}
}
if (!is_normal) {
DEBUG_PRINT("configure handler: window %d is not normal, returning\n",
(int)e.window)
wm_client_free(wm, c);
return;
}
wm_layout(wm, c->m);
}

25
handler.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef HANDLER_H
#define HANDLER_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include "wm.h"
static int wm_xerror_handler(Wm *wm, Display *display, XErrorEvent *e);
static int wm_other_wm_handler(Display *display, XErrorEvent *e);
void wm_create_handler(Wm *wm, XCreateWindowEvent e);
void wm_destroy_handler(Wm *wm, XDestroyWindowEvent e);
void wm_reparent_handler(XReparentEvent e);
void wm_keyrelease_handler(XKeyReleasedEvent e);
void wm_keypress_handler(Wm *wm, XKeyPressedEvent e);
void wm_maprequest_handler(Wm *wm, XMapRequestEvent e);
void wm_motion_handler(Wm *wm, XMotionEvent e);
void wm_configure_handler(Wm *wm, XConfigureRequestEvent e);
#endif

4
main.c
View File

@ -2,6 +2,8 @@
int main(int argc, char **arg)
{
wm_init();
Wm wm;
wm_init(&wm);
return 0;
}

803
wm.c

File diff suppressed because it is too large Load Diff

128
wm.h
View File

@ -1,3 +1,6 @@
#ifndef WM_H
#define WM_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
@ -37,6 +40,7 @@ typedef struct Client Client;
typedef struct Monitor Monitor;
typedef struct Keybind Keybind;
typedef struct Arg Arg;
typedef struct Wm Wm;
enum Direction {
UP,
@ -85,90 +89,84 @@ struct Arg {
struct Keybind {
unsigned int mask;
KeySym keysym;
void (*func)(Arg*);
void (*func)(Wm*, Arg*);
Arg args;
};
static Display *wm_display;
static Monitor *wm_monitors;
static Monitor *wm_smon;
static int wm_mc;
static bool wm_other_wm;
static Client wm_root;
static Window wm_dock = -1;
struct Wm {
// TODO INIT
Display *display;
Monitor *monitors;
Monitor *smon;
int wm_mc;
bool other_wm;
Client root;
Window dock;
/* global configuration variables */
// TODO: active layout not working
static void (*wm_active_layout)(Monitor*);
static float wm_cfg_ms_p = 0.5;
static char wm_cfg_border_col[] = "#222222";
static char wm_cfg_focused_border_col[] = "#444444";
static unsigned char wm_cfg_border_width = 2;
static Keybind *wm_cfg_keybinds;
static int wm_cfg_kb_count;
static bool wm_cfg_focus_on_motion = true;
// TODO: active layout not working
void (*active_layout)(Wm *wm, Monitor*);
float cfg_ms_p;
char *cfg_border_col;
char *cfg_focused_border_col;
unsigned char cfg_border_width;
Keybind *cfg_keybinds;
int cfg_kb_count;
bool cfg_focus_on_motion;
static int wm_socket_fd;
static int wm_xerror_handler(Display *display, XErrorEvent *e);
static int wm_other_wm_handler(Display *display, XErrorEvent *e);
void wm_create_handler(XCreateWindowEvent e);
void wm_destroy_handler(XDestroyWindowEvent e);
void wm_reparent_handler(XReparentEvent e);
void wm_configure_handler(XConfigureRequestEvent e);
void wm_keyrelease_handler(XKeyReleasedEvent e);
void wm_keypress_handler(XKeyPressedEvent e);
void wm_maprequest_handler(XMapRequestEvent e);
void wm_motion_handler(XMotionEvent e);
int socket_fd;
};
Monitor wm_monitor_open(Display *d, XineramaScreenInfo info);
void wm_monitors_open_all(Display *d);
void wm_monitors_open_all(Wm *wm, Display *d);
Display* wm_connect_display();
void wm_mstack(Monitor *m);
void wm_set_layout(void(*f)(Monitor*));
void wm_layout(Monitor *m);
void wm_mstack(Wm *wm, Monitor *m);
void wm_set_layout(Wm *wm, void(*f)(Wm *wm, Monitor*));
void wm_layout(Wm *wm, Monitor *m);
Client *wm_get_last_client(Monitor m);
Client *wm_get_last_client(Wm *wm, Monitor m);
void wm_client_set_atom(Client *c, const char *name, const unsigned char *data,
void wm_client_set_atom(Wm *wm, Client *c, const char *name, const unsigned char *data,
Atom type, int nelements);
Atom wm_client_get_atom(Client *c, const char *name, unsigned char **atom_ret,
Atom wm_client_get_atom(Wm *wm, Client *c, const char *name, unsigned char **atom_ret,
unsigned long *nitems_ret);
Client* wm_client_create(Window w);
void wm_client_free(Client *c);
void wm_client_kill(Client *c);
void wm_client_hide(Client *c);
void wm_client_show(Client *c);
void wm_client_focus(Client *c);
void wm_client_focus_dir(Client *c, int dir);
Client* wm_client_create(Wm *wm, Window w);
void wm_client_free(Wm *wm, Client *c);
void wm_client_kill(Wm *wm, Client *c);
void wm_client_hide(Wm *wm, Client *c);
void wm_client_show(Wm* wm, Client *c);
void wm_client_focus(Wm* wm, Client *c);
void wm_client_focus_dir(Wm* wm, Client *c, int dir);
Client* wm_client_get_dir_rel_c(Client *c, int dir);
Client* wm_client_get_focused();
void wm_client_border(Client *c);
void wm_client_border_focused(Client *c);
void wm_monitor_clients_border(Monitor *m);
bool wm_client_is_focused(Client *c);
bool wm_window_is_dock(Window w);
Client* wm_client_get_focused(Wm* wm);
void wm_client_border(Wm* wm, Client *c);
void wm_client_border_focused(Wm* wm, Client *c);
void wm_monitor_clients_border(Wm* wm, Monitor *m);
bool wm_client_is_focused(Wm* wm, Client *c);
bool wm_window_is_dock(Wm* wm, Window w);
XWindowChanges wm_client_to_xwchanges(Client c);
Client* wm_client_find(Window w);
Client* wm_client_find(Wm* wm, Window w);
void wm_spawn(char **str);
void wm_spawn(Wm* wm, char **str);
void wm_switch_ws(int ws);
void wm_mainloop();
void wm_init();
void wm_switch_ws(Wm* wm, int ws);
void wm_mainloop(Wm* wm);
void wm_init(Wm *wm);
void wm_init_cfg_def(Wm *wm);
void wm_grab_keys();
void wm_kb_spawn(Arg *args);
void wm_kb_kill(Arg *args);
void wm_kb_switch_ws(Arg *args);
void wm_kb_focus_dir(Arg *args);
void wm_grab_keys(Wm *wm);
void wm_kb_spawn(Wm *wm, Arg *args);
void wm_kb_kill(Wm *wm, Arg *args);
void wm_kb_switch_ws(Wm *wm, Arg *args);
void wm_kb_focus_dir(Wm *wm, Arg *args);
void wm_keybinds_init_def();
void wm_keybinds_init_def(Wm *wm);
struct sockaddr wm_socket_init();
void wm_socket_cleanup();
void wm_settings_message_parse(char *c, int len);
struct sockaddr wm_socket_init(Wm *wm);
void wm_socket_cleanup(Wm *wm);
void wm_settings_message_parse(Wm *wm, char *c, int len);
void wm_sigint_handler(int signum);
void wm_sigint_handler(Wm *wm, int signum);
#endif