🎨 Improved code structure

Minor improvements to remove useless procedures, improve the readability
of the code, and soft preparation to properly handle ICCCM and EWMH
Hints.
This commit is contained in:
Paul Breugnot 2024-12-30 18:02:10 +01:00
parent 9aab7c087c
commit b7aa0cdd40
10 changed files with 176 additions and 64 deletions

View file

@ -7,24 +7,67 @@
#include "event_handlers/unmap.h" #include "event_handlers/unmap.h"
#include "event_handlers/reparent.h" #include "event_handlers/reparent.h"
#include "event_handlers/destroy.h" #include "event_handlers/destroy.h"
#include "log/log.h" #include "log/error.h"
Aswm _aswm; Aswm _aswm;
void aswm_open() { void aswm_open() {
_aswm.display = XOpenDisplay(NULL); _aswm.display = XOpenDisplay(NULL);
Window root_window = DefaultRootWindow(_aswm.display); _aswm.workspaces = malloc(0);
_aswm.workspace_count = 0;
printf("PointerRoot %lu\n", PointerRoot); XSetErrorHandler(aswm_error_handler);
printf("None %lu\n", None);
printf("Root window %lu\n", root_window);
XSelectInput(_aswm.display, root_window, SubstructureRedirectMask | SubstructureNotifyMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); Window x_root_window = DefaultRootWindow(_aswm.display);
XWindowAttributes x_root_attributes;
XGetWindowAttributes(_aswm.display, x_root_window, &x_root_attributes);
_aswm.root_window = XCreateSimpleWindow(
_aswm.display, x_root_window,
0, 0,
x_root_attributes.width,
x_root_attributes.height,
0, 0, 0);
// User windows are created from the X root window, while ASWM specific
// windows are created on the ASWM root window
XSelectInput(_aswm.display, _aswm.root_window, FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask);
XSelectInput(_aswm.display, x_root_window, SubstructureRedirectMask | SubstructureNotifyMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask);
XMapWindow(_aswm.display, _aswm.root_window);
XSync(_aswm.display, 0); XSync(_aswm.display, 0);
printf("PointerRoot %lu\n", PointerRoot);
printf("Useful constants:\n");
printf("\tNone: %lu\n", None);
printf("\tTrue / False: %i / %i\n", True, False);
printf("X Root window %lu\n", x_root_window);
printf("ASWM Root window %lu\n", _aswm.root_window);
atexit(aswm_close); atexit(aswm_close);
} }
void aswm_create_workspace() {
_aswm.workspaces = realloc(_aswm.workspaces, (_aswm.workspace_count+1) * sizeof(Window));
XWindowAttributes root_attributes;
XGetWindowAttributes(_aswm.display, _aswm.root_window, &root_attributes);
_aswm.workspaces[_aswm.workspace_count] = XCreateSimpleWindow(
_aswm.display, _aswm.root_window,
0, 0,
root_attributes.width, root_attributes.height,
0, 0, 0);
_aswm.current_workspace = _aswm.workspaces[_aswm.workspace_count];
_aswm.workspace_count++;
XSelectInput(_aswm.display, _aswm.current_workspace, SubstructureNotifyMask);
XMapWindow(_aswm.display, _aswm.current_workspace);
printf("Create workspace %lu\n", _aswm.current_workspace);
}
void aswm_event_loop() { void aswm_event_loop() {
for (;;) { for (;;) {
XEvent e; XEvent e;
@ -32,20 +75,19 @@ void aswm_event_loop() {
switch(e.type) { switch(e.type) {
case CreateNotify: case CreateNotify:
OnCreateNotify(&e.xcreatewindow); // Windows with override_redirect set to True can be safely
// ignored, according to the X server specification.
if(e.xcreatewindow.override_redirect == False)
OnCreateNotify(&_aswm, &e.xcreatewindow);
break; break;
case ConfigureRequest: case ConfigureRequest:
OnConfigureRequest(&e.xconfigurerequest); OnConfigureRequest(&e.xconfigurerequest);
break; break;
case MapRequest: case MapRequest:
OnMapRequest(&_aswm, &e.xmaprequest); OnMapRequest(&_aswm, &e.xmaprequest);
printf("Map tree:\n");
log_tree(_aswm.display, DefaultRootWindow(_aswm.display), 1);
break; break;
case UnmapNotify: case UnmapNotify:
OnUnmapNotify(&_aswm, &e.xunmap); OnUnmapNotify(&_aswm, &e.xunmap);
printf("Unmap tree:\n");
log_tree(_aswm.display, DefaultRootWindow(_aswm.display), 1);
break; break;
case DestroyNotify: case DestroyNotify:
OnDestroyNotify(&e.xdestroywindow); OnDestroyNotify(&e.xdestroywindow);
@ -75,6 +117,19 @@ void aswm_event_loop() {
case ButtonRelease: case ButtonRelease:
printf("Release %lu\n", e.xbutton.window); printf("Release %lu\n", e.xbutton.window);
break; break;
case ClientMessage:
// TODO: handle this
printf("Unhandled client message from window %lu:\n", e.xclient.window);
char* atom_name = XGetAtomName(e.xclient.display, e.xclient.message_type);
printf("\t%s\n", atom_name);
XFree(atom_name);
break;
case PropertyNotify:
// TODO: handle this
printf("Unhandled property change for window %lu:\n", e.xclient.window);
char* property_name = XGetAtomName(e.xclient.display, e.xclient.message_type);
printf("\t%s\n", property_name);
XFree(property_name);
default: default:
break; break;
/* LOG(WARNING) << "Ignored event"; */ /* LOG(WARNING) << "Ignored event"; */
@ -83,6 +138,7 @@ void aswm_event_loop() {
} }
void aswm_close() { void aswm_close() {
free(_aswm.workspaces);
XCloseDisplay(_aswm.display); XCloseDisplay(_aswm.display);
} }

View file

@ -5,10 +5,14 @@
typedef struct { typedef struct {
Display *display; Display *display;
Window es_de_window; Window root_window;
Window* workspaces;
unsigned int workspace_count;
Window current_workspace;
} Aswm; } Aswm;
void aswm_open(); void aswm_open();
void aswm_create_workspace();
void aswm_event_loop(); void aswm_event_loop();
void aswm_close(); void aswm_close();

View file

@ -2,25 +2,9 @@
#include <stdio.h> #include <stdio.h>
void OnConfigureRequest(const XConfigureRequestEvent* e) { void OnConfigureRequest(const XConfigureRequestEvent* e) {
printf("XConfigureRequestEvent %lu\n", e->window); printf("XConfigureRequestEvent %lu (ignored)\n", e->window);
printf("\tx = %i\n", e->x); printf("\tx = %i\n", e->x);
printf("\ty = %i\n", e->y); printf("\ty = %i\n", e->y);
printf("\tw = %i\n", e->width); printf("\tw = %i\n", e->width);
printf("\th = %i\n", e->height); printf("\th = %i\n", e->height);
int screen_number = XDefaultScreen(e->display);
int full_width = XDisplayWidth(e->display, screen_number);
int full_height = XDisplayWidth(e->display, screen_number);
// Copy fields from e to changes.
XWindowChanges changes;
changes.x = 0;
changes.y = 0;
changes.width = full_width;
changes.height = full_height;
changes.border_width = e->border_width;
changes.sibling = e->above;
changes.stack_mode = e->detail;
// Grant request by calling XConfigureWindow().
XConfigureWindow(e->display, e->window, e->value_mask, &changes);
} }

View file

@ -1,6 +1,34 @@
#include "create.h" #include "create.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <X11/Xutil.h>
void OnCreateNotify(const XCreateWindowEvent* e) { void OnCreateNotify(Aswm* aswm, const XCreateWindowEvent* e) {
printf("XCreateWindowEvent %lu\n", e->window); printf("XCreateWindowEvent %lu on parent %lu\n", e->window, e->parent);
printf("\tx = %i\n", e->x);
printf("\ty = %i\n", e->y);
printf("\tw = %i\n", e->width);
printf("\th = %i\n", e->height);
printf("\toverride_redirect = %i\n", e->override_redirect);
// The WM must subscribe to property changes, notably to implement the
// Extended Window Manager Hints (EWMH) propocol
XSelectInput(aswm->display, e->window, PropertyChangeMask);
{
XTextProperty text_property;
if(XGetWMName(e->display, e->window, &text_property)) {
printf("\tname: %s\n", text_property.value);
}
}
{
XClassHint class;
if(XGetClassHint(e->display, e->window, &class)) {
printf("\tclass: %s, %s\n", class.res_class, class.res_name);
XFree(class.res_class);
XFree(class.res_name);
}
}
if(aswm->workspace_count == 0)
aswm_create_workspace();
} }

View file

@ -1,3 +1,4 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include "aswm/aswm.h"
void OnCreateNotify(const XCreateWindowEvent*); void OnCreateNotify(Aswm* aswm, const XCreateWindowEvent*);

View file

@ -1,29 +1,27 @@
#include "map.h" #include "map.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include "aswm/log/log.h"
#include <X11/Xutil.h> #include "X11/Xutil.h"
void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) {
printf("XMapRequestEvent %lu\n", e->window); printf("XMapRequestEvent %lu\n", e->window);
{ /* XSizeHints* size = XAllocSizeHints(); */
XTextProperty text_property; /* long supplied; */
XGetWMName(e->display, e->window, &text_property); /* if(XGetWMNormalHints(aswm->display, e->window, size, &supplied)) { */
printf("\tname: %s\n", text_property.value); /* printf("\tWMNormalHints:\n"); */
if(text_property.value != nullptr && strcmp(text_property.value, "ES-DE") == 0) /* printf("\t\tMin w:%i\n", size->min_width); */
aswm->es_de_window = e->window; /* printf("\t\tMin h:%i\n", size->min_height); */
} /* printf("\t\tMax w:%i\n", size->max_width); */
{ /* printf("\t\tMax h:%i\n", size->max_height); */
XClassHint class; /* printf("\t\tBase w:%i\n", size->base_width); */
XGetClassHint(e->display, e->window, &class); /* printf("\t\tBase h:%i\n", size->base_height); */
printf("\tclass: %s, %s\n", class.res_class, class.res_name); /* } */
}
int screen_number = XDefaultScreen(e->display); // Assumes map requests are always received on root and adressed to the
int full_width = XDisplayWidth(e->display, screen_number); // current workspace
int full_height = XDisplayWidth(e->display, screen_number); aswm_map_new_window(e->display, aswm->current_workspace, e->window);
aswm_map_new_window(e->display, DefaultRootWindow(e->display), e->window); printf("Map tree:\n");
log_tree(aswm->display, DefaultRootWindow(aswm->display), 1);
/* XSelectInput(e->display, e->window, FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); */
} }

View file

@ -1,12 +1,17 @@
#include "unmap.h" #include "unmap.h"
#include <stdio.h> #include <stdio.h>
#include "aswm/log/log.h"
void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) { void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) {
printf("XUnmapWindow %lu tiled in %lu\n", e->window, e->event); printf("XUnmapWindow %lu tiled in %lu\n", e->window, e->event);
printf("ES-DE window %lu\n", aswm->es_de_window); /* printf("ES-DE window %lu\n", aswm->es_de_window); */
if(aswm->es_de_window != None) /* if(aswm->es_de_window != None) */
XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); /* XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); */
// Since SubstructureNotify is selected on each frame, e->event corresponds // Since SubstructureNotify is selected on each frame, e->event corresponds
// to the parent of the unmapped window (e->window) // to the parent of the unmapped window (e->window)
aswm_unmap_window(e->display, e->event);
// Only handles request that refer to the ASWM window hierarchy
aswm_unmap_window(e->display, e->event, e->window);
printf("Unmap tree:\n");
log_tree(aswm->display, DefaultRootWindow(aswm->display), 1);
} }

15
src/aswm/log/error.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef ASWM_ERROR_H
#define ASWM_ERROR_H
#include <X11/Xlib.h>
#include <stdio.h>
char message_buffer[1024];
int aswm_error_handler( Display *disp, XErrorEvent *xe ) {
XGetErrorText(xe->display, xe->error_code, message_buffer, 1024);
printf("%s", message_buffer);
return 0;
}
#endif

View file

@ -53,23 +53,23 @@ void h_split(Display* display, Window current_tile, Window window_to_map) {
XMapWindow(display, window_to_map); XMapWindow(display, window_to_map);
} }
void resize_children_to_tile(Display* display, Window tile) { void resize_children_to_parent(Display* display, Window parent) {
Window _root_window; // unused Window _root_window; // unused
Window _grand_parent; // unused Window _grand_parent; // unused
Window* children; Window* children;
unsigned int children_count; unsigned int children_count;
XQueryTree( XQueryTree(
display, tile, display, parent,
&_root_window, &_grand_parent, &_root_window, &_grand_parent,
&children, &children_count); &children, &children_count);
printf("Current siblings of %lu:\n", tile); printf(" Resize children of %lu:\n", parent);
for(auto i = 0; i < children_count; i++) for(auto i = 0; i < children_count; i++)
printf("%lu ", children[i]); printf("%lu ", children[i]);
printf("\n"); printf("\n");
// Resize new children of the parent tile // Resize new children of the parent tile
XWindowAttributes parent_attributes; XWindowAttributes parent_attributes;
XGetWindowAttributes(display, tile, &parent_attributes); XGetWindowAttributes(display, parent, &parent_attributes);
for(auto i = 0; i < children_count; i++) { for(auto i = 0; i < children_count; i++) {
// TODO: currently only hsplit supported // TODO: currently only hsplit supported
@ -82,11 +82,11 @@ void resize_children_to_tile(Display* display, Window tile) {
XFree(children); XFree(children);
} }
void aswm_unmap_window(Display* display, Window tile) { void aswm_unmap_window(Display* display, Window parent, Window window) {
// +-------------------------------+ // +-------------------------------+
// | root | // | root |
// |+-------------+ +-------------+| // |+-------------+ +-------------+|
// || tile | | || // || parent | | ||
// || +--------+ | | || // || +--------+ | | ||
// || | win 1 | | | || // || | win 1 | | | ||
// || +--------+ | | || // || +--------+ | | ||
@ -99,5 +99,26 @@ void aswm_unmap_window(Display* display, Window tile) {
// |+-------------+ +-------------+| // |+-------------+ +-------------+|
// +-------------------------------+ // +-------------------------------+
resize_children_to_tile(display, tile); Window _root_window; // unused
Window _grand_parent; // unused
Window* children;
unsigned int children_count;
XQueryTree(
display, parent,
&_root_window, &_grand_parent,
&children, &children_count);
printf(" Current children of %lu:\n", parent);
for(auto i = 0; i < children_count; i++)
printf("%lu ", children[i]);
printf("\n");
for(auto i = 0; i < children_count; i++) {
if(children[i] == window)
// If the unmap request does not come from a destroy window, we
// still want to remove the window from the ASWM hierarchy. It is
// the responsability of the client to decide to remap it latter or
// not.
XReparentWindow(display, window, XDefaultRootWindow(display), 0, 0);
}
resize_children_to_parent(display, parent);
} }

View file

@ -6,6 +6,6 @@
Window create_aswm_root_tile(Display* display, Window root_window); Window create_aswm_root_tile(Display* display, Window root_window);
void aswm_map_new_window(Display* display, Window current_node, Window window_to_map); void aswm_map_new_window(Display* display, Window current_node, Window window_to_map);
void aswm_unmap_window(Display* display, Window window); void aswm_unmap_window(Display* display, Window parent, Window window);
#endif #endif