cleanned up the repo a little bit

This commit is contained in:
TheLastBilly 2022-08-13 22:43:04 -04:00
parent 624bff073b
commit 2f20f98ac4
4 changed files with 35 additions and 559 deletions

View File

@ -1,3 +0,0 @@
#!/bin/sh
cc -Wall -Wpedantic -g ./evwm.c -o ./evwm

437
old/wm.c
View File

@ -1,437 +0,0 @@
#include "wm.h"
#include "x.h"
#include "mema.h"
#define XERROR_MSG_LEN 255
#define wm_str_to_keycode(disp, str) XKeysymToKeycode((disp), XStringToKeysym((str)))
// Atoms strings. Used for easier setup and use
static char
*atom_str_identifiers[atom_no_id] =
{
[utf8_string] = (char *)"UTF8_STRING",
[wm_protocols] = (char *)"WM_PROTOCOLS",
[wm_delete_window] = (char *)"WM_DELETE_WINDOW",
[wm_state] = (char *)"WM_STATE",
[wm_take_focus] = (char *)"WM_TAKE_FOCUS",
[net_active_window] = (char *)"_NET_ACTIVE_WINDOW",
[net_wm_supported] = (char *)"_NET_SUPPORTED",
[net_wm_name] = (char *)"_NET_WM_NAME",
[net_wm_state] = (char *)"_NET_WM_STATE",
[net_supporting_wm_check] = (char *)"_NET_SUPPORTING_WM_CHECK",
[net_wm_state_fullscreen] = (char *)"_NET_WM_STATE_FULLSCREEN",
[net_wm_window_type] = (char *)"_NET_WM_WINDOW_TYPE",
[net_wm_window_type_dialog] = (char *)"_NET_WM_WINDOW_TYPE_DIALOG",
[net_client_list] = (char *)"_NET_CLIENT_LIST"
};
static void wm_window_grab_buttons(struct wm_t *wm, Window window);
static void wm_window_grab_keys(struct wm_t *wm, Window window);
static struct wm_window_t *
wm_find_window(struct wm_t *wm, Window window)
{
struct wm_window_t * wn = NULL;
wn = wm->window_root_node;
for(; wn != NULL; wn = wn->next)
if(wn->window == window) break;
return wn;
}
static struct wm_window_t *
wm_add_window(struct wm_t *wm, Window window)
{
struct wm_window_t * wn = NULL, * nw = NULL;
wn = wm_find_window(wm, window);
if(wn)
{
dbg("window (id: %lu) already on list", window);
return wn;
}
wn = wm->window_root_node;
for(; wn && wn->next != NULL; wn = wn->next);
nw = gcalloc(1, sizeof(struct wm_window_t));
if(wm->window_root_node == NULL)
wm->window_root_node = nw;
else
wn->next = nw;
nw->prev = wn;
nw->window = window;
wm_window_grab_keys(wm, window);
XGetWindowAttributes(wm->display, window, &nw->attributes);
wm_window_grab_buttons(wm, window);
dbg("window (id: %lu) added to list", window);
return nw;
}
static void
wm_remove_window(struct wm_t *wm, Window window)
{
struct wm_window_t * wn = NULL;
wn = wm_find_window(wm, window);
if(wn == NULL)
{
dbg("window (id: %lu) is not on list", window);
return;
}
if(wn->prev)
wn->prev->next = wn->next;
if(wn->next)
wn->next->prev = wn->prev;
if(wm->window_root_node == wn)
wm->window_root_node = wn->next;
dbg("window (id: %lu) removed from list", window);
gfree(wn);
}
static int
wm_count_windows(struct wm_t * wm)
{
int count = 0;
struct wm_window_t * wn = NULL;
wn = wm->window_root_node;
for(; wn && wn->next != NULL; wn = wn->next)
count++;
return count;
}
static void
wm_debug_window_nodes(struct wm_t *wm)
{
struct wm_window_t * w = NULL;
long unsigned int i = 0;
dbg("the following window node(s) were found:");
w = wm->window_root_node;
for(; w != NULL; w = w->next)
dbg("%lu. found window (id: %lu)", i++, w->window);
}
static void
wm_update_client_list(struct wm_t *wm)
{
lmema_init();
int i = 0, client_count = 0;
Window *client_list = NULL;
struct wm_window_t * wn = NULL;
client_count = wm_count_windows(wm);
client_list = lcalloc(client_count, sizeof(struct wm_window_t));
wn = wm->window_root_node;
for(; wn && wn->next != NULL; wn = wn->next)
client_list[i++] = wn->window;
XChangeProperty(wm->display, wm->root_window,
wm->atoms[net_client_list], XA_WINDOW, 32, PropModeReplace,
(unsigned char*) client_list, client_count);
lnreturn;
}
// Meant for internal use.
// Get's last X11 error generated
static const char*
xerror(struct wm_t *wm)
{
static char m[XERROR_MSG_LEN] = {0};
x_get_error_str(wm->display, m, arrlen(m));
return m;
}
static void
wm_register_bindings(struct wm_t *wm, struct wm_binding_t *bindings,
int bindings_len)
{
int i = 0;
if(wm->registered_bindings != NULL)
gfree(wm->registered_bindings);
wm->registered_bindings = (struct wm_registered_binding_t *)gcalloc(
bindings_len, sizeof(struct wm_binding_t));
for(; i < bindings_len; i++)
{
wm->registered_bindings[i] = (struct wm_registered_binding_t){
.mod = bindings[i].mod,
.keycode = wm_str_to_keycode(wm->display, bindings[i].key_str),
.callback = bindings[i].callback
};
}
wm->registered_bindings_len = bindings_len;
}
void
wm_window_grab_keys(struct wm_t *wm, Window window)
{
int i = 0, err = 0;
struct wm_registered_binding_t *binding = NULL;
for(i = 0; i < wm->registered_bindings_len; i++)
{
binding = &wm->registered_bindings[i];
err = XGrabKey(wm->display, binding->keycode, binding->mod,
window, 0, GrabModeAsync, GrabModeAsync);
if(err != Success)
err("cannot bind key \"%s\": %s",
XKeysymToString(binding->keycode), xerror(wm));
}
}
static void
wm_window_focus(struct wm_t *wm, Window window)
{
XSelectInput(wm->display, window, FocusChangeMask);
}
static int
wm_button_press_handler(struct wm_binding_ctx_t *ctx)
{
bool pressed = false;
XButtonEvent *event = NULL;
event = &ctx->event.xbutton;
pressed = ctx->event.type == ButtonPress;
if(pressed)
{
dbg("pressed! (mask: %08X, mod: %08X)", event->state, ctx->wm->modkey);
if(event->state & ctx->wm->modkey)
{
switch (event->button)
{
case Button1:
dbg("Moving window (id: %lu)", event->window);
ctx->wm->current_action = wm_action_move;
break;
case Button3:
dbg("Resizing window (id: %lu)", event->window);
ctx->wm->current_action = wm_action_resize;
break;
default:
break;
}
}
}
else
{
}
return 0;
}
static void
wm_window_grab_buttons(struct wm_t *wm, Window window)
{
int err = 0;
err = XGrabButton(
wm->display,
AnyButton,
AnyModifier,
wm->root_window,
True,
ButtonPressMask|ButtonReleaseMask|ButtonMotionMask,
GrabModeAsync,
GrabModeAsync,
None,
None
);
if(err == BadCursor || err == BadValue || err == BadWindow)
ftl("cannot grab buttons for window %lu: %s", window, x_get_error_name(err));
}
void
wm_create(struct wm_t *wm, uint modkey, struct wm_binding_t *bindings,
int bindings_len)
{
unused(wm_debug_window_nodes);
int i = 0;
memset((void*)wm, 0, sizeof(struct wm_t));
if((wm->display = XOpenDisplay(NULL)) == NULL)
ftl("cannot open X11 display");
// Setup X11 error handler
x_register_xerror_handler();
x_attach_display_error(wm->display);
// Get default screen and window
wm->screen = DefaultScreen(wm->display);
wm->root_window = DefaultRootWindow(wm->display);
wm_add_window(wm, wm->root_window);
// Get root window attributes
XGetWindowAttributes(wm->display, wm->root_window, &wm->attributes);
XSelectInput(wm->display, wm->root_window, SubstructureNotifyMask |
FocusChangeMask);
wm->modkey = modkey;
wm_window_grab_keys(wm, wm->root_window);
wm_register_bindings(wm, bindings, bindings_len);
wm_window_grab_buttons(wm, wm->root_window);
// Setup Atoms
for(i = 0; i < atom_no_id; i++)
{
if(i == net_start)
continue;
wm->atoms[i] = XInternAtom( wm->display, atom_str_identifiers[i],
false);
}
// Be nice with EWMH:
// https://specifications.freedesktop.org/wm-spec/1.3/ar01s03.html
// Taken from dwm.c
// Create empty, tiny window to apply the wm atoms spec into
wm->check_window = XCreateSimpleWindow(
wm->display,
wm->root_window,
0, 0, 1, 1, 0, 0, 0
);
// Do the _NET_SUPPORTING_WM_CHECK thing. I'm not gonna lie, I haven't done
// much research on this, so I should get back to this later
XChangeProperty(wm->display, wm->check_window,
wm->atoms[net_supporting_wm_check], XA_WINDOW, 32,
PropModeReplace, (unsigned char *) &wm->check_window, 1);
XChangeProperty(wm->display, wm->check_window,
wm->atoms[net_wm_name], wm->atoms[utf8_string], 8,
PropModeReplace, (unsigned char *) WM_NAME, arrlen(WM_NAME));
XChangeProperty(wm->display, wm->root_window,
wm->atoms[net_supporting_wm_check], XA_WINDOW, 32,
PropModeReplace, (unsigned char *) &wm->check_window, 1);
// Specify _NET_SUPPORTED
XChangeProperty(wm->display, wm->root_window,
wm->atoms[net_wm_supported], XA_ATOM, 32, PropModeReplace,
(unsigned char *)&wm->atoms[net_start + 1], atom_no_id - net_start -1);
}
void
wm_move_window(struct wm_window_t *window)
{
}
void
wm_destroy(struct wm_t *wm)
{
if(wm->display)
XCloseDisplay(wm->display);
}
int
wm_process_event(struct wm_t *wm)
{
static XEvent previous_event = {0};
int ret = 0;
XEvent event = {0};
ret = XNextEvent(wm->display, &event);
if(ret != Success)
{
err("cannot process event: %s", xerror(wm));
return 0;
}
if(event.type != previous_event.type)
dbg("received \"%s\"", x_get_event_name(event.type));
previous_event = event;
switch(event.type)
{
case CreateNotify:
{
Window window_id = event.xdestroywindow.window;
if(!window_id)
break;
dbg("received create event for window (id: %lu)", window_id);
wm_add_window(wm, event.xcreatewindow.window);
break;
}
case DestroyNotify:
{
Window window_id = event.xdestroywindow.window;
if(!window_id)
break;
dbg("received destroy event for window (id: %lu)", window_id);
wm_remove_window(wm, window_id);
wm_update_client_list(wm);
break;
}
case ButtonPress:
case ButtonRelease:
{
struct wm_binding_ctx_t ctx = {0};
struct wm_window_t *window = NULL;
if(!(window = wm_find_window(wm, event.xbutton.window)))
{
dbg("adding unregistered window (id: %lu) to list",
event.xbutton.window);
wm_add_window(wm, event.xbutton.window);
}
ctx.wm = wm;
ctx.event = event;
ctx.window = window;
if(wm_button_press_handler(&ctx))
XAllowEvents(wm->display, ReplayPointer, CurrentTime);
else
XAllowEvents(wm->display, SyncPointer, CurrentTime);
}
default:
break;
}
return 1;
}

110
old/wm.h
View File

@ -1,110 +0,0 @@
#ifndef __WM_H__
#define __WM_H__
#include "log.h"
#include "util.h"
#include "mema.h"
#include "x.h"
#ifndef WM_NAME
#define WM_NAME "evwm"
#endif
// These are the same atoms used by dwm as of the
// initial writing of this code
enum wm_atom_id
{
utf8_string,
wm_protocols,
wm_delete_window,
wm_state,
wm_take_focus,
// Used to specify the start of net_wm
// atoms, do not move it
net_start,
net_active_window,
net_wm_supported,
net_wm_name,
net_wm_state,
net_supporting_wm_check,
net_wm_state_fullscreen,
net_wm_window_type,
net_wm_window_type_dialog,
net_client_list,
atom_no_id
};
enum wm_action_id
{
wm_action_move,
wm_action_resize,
wm_action_none
};
struct wm_window_t
{
Window window;
XWindowAttributes attributes;
struct wm_window_t * prev, * next;
};
struct wm_binding_ctx_t
{
uint mod;
struct wm_t *wm;
const char *arg;
XEvent event;
struct wm_window_t *window;
};
typedef int (*wm_binding_callback_t) (struct wm_binding_ctx_t *ctx);
struct wm_binding_t
{
uint mod;
const char *key_str;
const char *arg;
wm_binding_callback_t callback;
};
struct wm_t
{
int screen;
Display* display;
Window root_window, check_window;
XWindowAttributes attributes;
struct wm_window_t * window_root_node;
uint modkey;
struct wm_registered_binding_t
{
uint mod;
int keycode;
wm_binding_callback_t callback;
} *registered_bindings;
size_t registered_bindings_len;
Atom atoms[atom_no_id];
enum wm_action_id current_action;
};
void wm_create(struct wm_t *wm, uint modkey, struct wm_binding_t *bindings, int bindings_len);
int wm_process_event(struct wm_t *wm);
void wm_destroy(struct wm_t *wm);
#endif

44
wm.c
View File

@ -43,8 +43,14 @@ typedef struct wm_t
static wm_t wm;
static const char*
static int
display_error( void )
{
return x_get_error_code(wm.display);
}
static const char*
display_error_str( void )
{
static char m[XERROR_MSG_LEN] = {0};
@ -301,24 +307,35 @@ wm_destroy( void )
return 0;
}
int
wm_focus_window( Window window )
{
if(!find_window(window))
add_window(window);
XSetInputFocus(wm.display, window, RevertToParent, CurrentTime);
XMapRaised(wm.display, window);
dbg("focusing on window %ld", window);
return 0;
}
int
wm_move_window( Window window, int x, int y )
{
int err = 0;
window_node_t *node = NULL;
if(window == wm.root_window)
return 0;
node = find_window(window);
if(!node)
return 1;
if(!find_window(window))
add_window(window);
err = XMoveWindow(wm.display, window, x, y);
if(err)
err("cannot move window (%s)", display_error())
if(err && display_error() != BadAtom)
err("cannot move window (%s)", display_error_str())
inf("window (%ld) moved to (%d, %d)", window, x, y);
dbg("window (%ld) moved to (%d, %d)", window, x, y);
return err;
}
@ -334,6 +351,8 @@ button_press_handler(Window window, XEvent xevent)
if(event->state & wm.modkey)
{
set_active_window(window, xevent);
if(window != wm.root_window)
wm_focus_window(window);
switch (event->button)
{
@ -367,6 +386,8 @@ wm_process_events()
int ret = 0;
XEvent event = {0};
static XEvent previous_event = {0};
ret = XNextEvent(wm.display, &event);
if(ret != Success)
{
@ -374,7 +395,12 @@ wm_process_events()
return 0;
}
dbg("received \"%s\" on window (id: %ld)", x_get_event_name(event.type), event.xany.window);
if(previous_event.type != event.type &&
previous_event.xany.window == event.xany.window)
{
dbg("received \"%s\" on window (id: %ld)", x_get_event_name(event.type), event.xany.window);
previous_event = event;
}
switch(event.type)
{