From 3096ba0637f1b821bf9f278c6d1082e9f31310ec Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Sun, 15 Dec 2024 15:09:18 +0100 Subject: [PATCH 1/9] :truck: Improved project structure --- CMakeLists.txt | 12 +++--- include/{ => aswm/event_handlers}/configure.h | 0 include/{ => aswm/event_handlers}/create.h | 0 include/{ => aswm/event_handlers}/destroy.h | 0 include/{ => aswm/event_handlers}/map.h | 0 include/{ => aswm/event_handlers}/reparent.h | 0 include/{ => aswm/event_handlers}/unmap.h | 0 src/{ => aswm/event_handlers}/configure.c | 17 +-------- src/{ => aswm/event_handlers}/create.c | 2 +- src/{ => aswm/event_handlers}/destroy.c | 2 +- src/aswm/event_handlers/map.c | 38 +++++++++++++++++++ src/{ => aswm/event_handlers}/reparent.c | 2 +- src/{ => aswm/event_handlers}/unmap.c | 2 +- src/{ => aswm}/main.c | 10 ++--- src/map.c | 7 ---- 15 files changed, 55 insertions(+), 37 deletions(-) rename include/{ => aswm/event_handlers}/configure.h (100%) rename include/{ => aswm/event_handlers}/create.h (100%) rename include/{ => aswm/event_handlers}/destroy.h (100%) rename include/{ => aswm/event_handlers}/map.h (100%) rename include/{ => aswm/event_handlers}/reparent.h (100%) rename include/{ => aswm/event_handlers}/unmap.h (100%) rename src/{ => aswm/event_handlers}/configure.c (70%) rename src/{ => aswm/event_handlers}/create.c (75%) rename src/{ => aswm/event_handlers}/destroy.c (78%) create mode 100644 src/aswm/event_handlers/map.c rename src/{ => aswm/event_handlers}/reparent.c (73%) rename src/{ => aswm/event_handlers}/unmap.c (76%) rename src/{ => aswm}/main.c (79%) delete mode 100644 src/map.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c110d25..e08a56f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,11 @@ find_package(X11 REQUIRED) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") add_executable(aswm - src/main.c - src/create.c - src/configure.c - src/reparent.c - src/destroy.c - src/map.c) + src/aswm/main.c + src/aswm/event_handlers/create.c + src/aswm/event_handlers/configure.c + src/aswm/event_handlers/reparent.c + src/aswm/event_handlers/destroy.c + src/aswm/event_handlers/map.c) target_include_directories(aswm PUBLIC ${X11_INCLUDE_DIR} include) target_link_libraries(aswm ${X11_LIBRARIES}) diff --git a/include/configure.h b/include/aswm/event_handlers/configure.h similarity index 100% rename from include/configure.h rename to include/aswm/event_handlers/configure.h diff --git a/include/create.h b/include/aswm/event_handlers/create.h similarity index 100% rename from include/create.h rename to include/aswm/event_handlers/create.h diff --git a/include/destroy.h b/include/aswm/event_handlers/destroy.h similarity index 100% rename from include/destroy.h rename to include/aswm/event_handlers/destroy.h diff --git a/include/map.h b/include/aswm/event_handlers/map.h similarity index 100% rename from include/map.h rename to include/aswm/event_handlers/map.h diff --git a/include/reparent.h b/include/aswm/event_handlers/reparent.h similarity index 100% rename from include/reparent.h rename to include/aswm/event_handlers/reparent.h diff --git a/include/unmap.h b/include/aswm/event_handlers/unmap.h similarity index 100% rename from include/unmap.h rename to include/aswm/event_handlers/unmap.h diff --git a/src/configure.c b/src/aswm/event_handlers/configure.c similarity index 70% rename from src/configure.c rename to src/aswm/event_handlers/configure.c index b4e7c49..8cb90fe 100644 --- a/src/configure.c +++ b/src/aswm/event_handlers/configure.c @@ -1,32 +1,19 @@ -#include "configure.h" +#include "aswm/event_handlers/configure.h" #include -#include void OnConfigureRequest(const XConfigureRequestEvent* e) { - XWindowChanges changes; printf("XConfigureRequestEvent %lu\n", e->window); printf("\tx = %i\n", e->x); printf("\ty = %i\n", e->y); printf("\tw = %i\n", e->width); printf("\th = %i\n", e->height); - - { - XTextProperty text_property; - XGetWMName(e->display, e->window, &text_property); - printf("\tname: %s\n", text_property.value); - } - { - XClassHint class; - XGetClassHint(e->display, e->window, &class); - printf("\tclass: %s, %s\n", class.res_class, class.res_name); - } - 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; diff --git a/src/create.c b/src/aswm/event_handlers/create.c similarity index 75% rename from src/create.c rename to src/aswm/event_handlers/create.c index 95c3052..2254671 100644 --- a/src/create.c +++ b/src/aswm/event_handlers/create.c @@ -1,4 +1,4 @@ -#include "create.h" +#include "aswm/event_handlers/create.h" #include void OnCreateNotify(const XCreateWindowEvent* e) { diff --git a/src/destroy.c b/src/aswm/event_handlers/destroy.c similarity index 78% rename from src/destroy.c rename to src/aswm/event_handlers/destroy.c index 635c844..f1c0197 100644 --- a/src/destroy.c +++ b/src/aswm/event_handlers/destroy.c @@ -1,4 +1,4 @@ -#include "destroy.h" +#include "aswm/event_handlers/destroy.h" #include void OnDestroyNotify(const XDestroyWindowEvent* e) { diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c new file mode 100644 index 0000000..7d22a65 --- /dev/null +++ b/src/aswm/event_handlers/map.c @@ -0,0 +1,38 @@ +#include "aswm/event_handlers/map.h" +#include +#include + +void OnMapRequest(Window root, const XMapRequestEvent* e) { + printf("XMapRequestEvent %lu\n", e->window); + + { + XTextProperty text_property; + XGetWMName(e->display, e->window, &text_property); + printf("\tname: %s\n", text_property.value); + } + { + XClassHint class; + XGetClassHint(e->display, e->window, &class); + printf("\tclass: %s, %s\n", class.res_class, class.res_name); + } + + int screen_number = XDefaultScreen(e->display); + int full_width = XDisplayWidth(e->display, screen_number); + int full_height = XDisplayWidth(e->display, screen_number); + + // Automatically reconfigure position and size of the window + XWindowChanges changes; + changes.x = 0; + changes.y = 0; + changes.width = full_width; + changes.height = full_height; + + printf("\tx = %i\n", changes.x); + printf("\ty = %i\n", changes.y); + printf("\tw = %i\n", changes.width); + printf("\th = %i\n", changes.height); + + XConfigureWindow(e->display, e->window, CWX | CWY | CWWidth | CWHeight, &changes); + + XMapWindow(e->display, e->window); +} diff --git a/src/reparent.c b/src/aswm/event_handlers/reparent.c similarity index 73% rename from src/reparent.c rename to src/aswm/event_handlers/reparent.c index f2f3842..9c6d14a 100644 --- a/src/reparent.c +++ b/src/aswm/event_handlers/reparent.c @@ -1,4 +1,4 @@ -#include "reparent.h" +#include "aswm/event_handlers/reparent.h" #include void OnReparentNotify(const XReparentEvent* e) { diff --git a/src/unmap.c b/src/aswm/event_handlers/unmap.c similarity index 76% rename from src/unmap.c rename to src/aswm/event_handlers/unmap.c index 6f3ae67..26ecb2e 100644 --- a/src/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -1,4 +1,4 @@ -#include "unmap.h" +#include "aswm/event_handlers/unmap.h" #include void OnUnmapRequest(Window root, const XMapRequestEvent* e) { diff --git a/src/main.c b/src/aswm/main.c similarity index 79% rename from src/main.c rename to src/aswm/main.c index 766493e..277c6e1 100644 --- a/src/main.c +++ b/src/aswm/main.c @@ -1,9 +1,9 @@ #include -#include "create.h" -#include "configure.h" -#include "map.h" -#include "reparent.h" -#include "destroy.h" +#include "aswm/event_handlers/create.h" +#include "aswm/event_handlers/configure.h" +#include "aswm/event_handlers/map.h" +#include "aswm/event_handlers/reparent.h" +#include "aswm/event_handlers/destroy.h" Display *display; Window root; diff --git a/src/map.c b/src/map.c deleted file mode 100644 index c9a590c..0000000 --- a/src/map.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "map.h" -#include - -void OnMapRequest(Window root, const XMapRequestEvent* e) { - printf("XMapRequestEvent %lu\n", e->window); - XMapWindow(e->display, e->window); -} From 0923b1bf793625d7737b8805ada4a97884a0c3c6 Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Sun, 15 Dec 2024 15:24:29 +0100 Subject: [PATCH 2/9] :art: Aswm structure --- CMakeLists.txt | 1 + include/aswm/aswm.h | 11 +++++++++++ src/aswm/aswm.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/aswm/main.c | 47 ++++---------------------------------------- 4 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 include/aswm/aswm.h create mode 100644 src/aswm/aswm.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e08a56f..e30d5fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") add_executable(aswm src/aswm/main.c + src/aswm/aswm.c src/aswm/event_handlers/create.c src/aswm/event_handlers/configure.c src/aswm/event_handlers/reparent.c diff --git a/include/aswm/aswm.h b/include/aswm/aswm.h new file mode 100644 index 0000000..46800a2 --- /dev/null +++ b/include/aswm/aswm.h @@ -0,0 +1,11 @@ +#include + +typedef struct { + Display *display; + Window root; +} aswm; + +void AswmOpen(); +void AswmEventLoop(); +void AswmClose(); + diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c new file mode 100644 index 0000000..5a3f5b6 --- /dev/null +++ b/src/aswm/aswm.c @@ -0,0 +1,48 @@ +#include "aswm/aswm.h" +#include "aswm/event_handlers/create.h" +#include "aswm/event_handlers/configure.h" +#include "aswm/event_handlers/map.h" +#include "aswm/event_handlers/reparent.h" +#include "aswm/event_handlers/destroy.h" + +aswm _aswm; + +void AswmOpen() { + _aswm.display = XOpenDisplay(NULL); + _aswm.root = DefaultRootWindow(_aswm.display); + + XSelectInput(_aswm.display, _aswm.root, SubstructureRedirectMask | SubstructureNotifyMask); + XSync(_aswm.display, 0); +} + +void AswmEventLoop() { + for (;;) { + XEvent e; + XNextEvent(_aswm.display, &e); + + switch(e.type) { + case CreateNotify: + OnCreateNotify(&e.xcreatewindow); + break; + case ConfigureRequest: + OnConfigureRequest(&e.xconfigurerequest); + break; + case MapRequest: + OnMapRequest(_aswm.root, &e.xmaprequest); + break; + case DestroyNotify: + OnDestroyNotify(&e.xdestroywindow); + break; + case ReparentNotify: + OnReparentNotify(&e.xreparent); + break; + default: + break; + /* LOG(WARNING) << "Ignored event"; */ + } + } +} + +void AswmClose() { + XCloseDisplay(_aswm.display); +} diff --git a/src/aswm/main.c b/src/aswm/main.c index 277c6e1..46f5d3e 100644 --- a/src/aswm/main.c +++ b/src/aswm/main.c @@ -1,46 +1,7 @@ -#include -#include "aswm/event_handlers/create.h" -#include "aswm/event_handlers/configure.h" -#include "aswm/event_handlers/map.h" -#include "aswm/event_handlers/reparent.h" -#include "aswm/event_handlers/destroy.h" - -Display *display; -Window root; +#include "aswm/aswm.h" int main(int argc, char** argv) { - display = XOpenDisplay(NULL); - - root = DefaultRootWindow(display); - - XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask); - XSync(display, 0); - - for (;;) { - XEvent e; - XNextEvent(display, &e); - - switch(e.type) { - case CreateNotify: - OnCreateNotify(&e.xcreatewindow); - break; - case ConfigureRequest: - OnConfigureRequest(&e.xconfigurerequest); - break; - case MapRequest: - OnMapRequest(root, &e.xmaprequest); - break; - case DestroyNotify: - OnDestroyNotify(&e.xdestroywindow); - break; - case ReparentNotify: - OnReparentNotify(&e.xreparent); - break; - default: - break; - /* LOG(WARNING) << "Ignored event"; */ - } - } - - XCloseDisplay(display); + AswmOpen(); + AswmEventLoop(); + AswmClose(); } From ac58e5e6db60a2a629c966259786b94f1a53a7bb Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Wed, 25 Dec 2024 11:58:58 +0100 Subject: [PATCH 3/9] :construction: Basic tiling window creation Only supports creation of windows with horizontal splitting, and not removal of windows. --- CMakeLists.txt | 5 +- include/aswm/aswm.h | 17 ++++--- include/aswm/event_handlers/map.h | 3 +- include/aswm/mapper/mapper.h | 24 +++++++++ src/aswm/aswm.c | 21 +++++--- src/aswm/event_handlers/map.c | 17 +------ src/aswm/main.c | 6 +-- src/aswm/mapper/mapper.c | 85 +++++++++++++++++++++++++++++++ 8 files changed, 145 insertions(+), 33 deletions(-) create mode 100644 include/aswm/mapper/mapper.h create mode 100644 src/aswm/mapper/mapper.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e30d5fb..5b8e223 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,9 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.21) project(aswm) +set(CMAKE_C_STANDARD 23) + find_package(X11 REQUIRED) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") @@ -9,6 +11,7 @@ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") add_executable(aswm src/aswm/main.c src/aswm/aswm.c + src/aswm/mapper/mapper.c src/aswm/event_handlers/create.c src/aswm/event_handlers/configure.c src/aswm/event_handlers/reparent.c diff --git a/include/aswm/aswm.h b/include/aswm/aswm.h index 46800a2..1d538b3 100644 --- a/include/aswm/aswm.h +++ b/include/aswm/aswm.h @@ -1,11 +1,16 @@ -#include +#ifndef ASWM +#define ASWM + +#include "aswm/mapper/mapper.h" typedef struct { Display *display; - Window root; -} aswm; + Window root_window; + AswmTile* root_tile; +} Aswm; -void AswmOpen(); -void AswmEventLoop(); -void AswmClose(); +void aswm_open(); +void aswm_event_loop(); +void aswm_close(); +#endif diff --git a/include/aswm/event_handlers/map.h b/include/aswm/event_handlers/map.h index e8638ca..6270df5 100644 --- a/include/aswm/event_handlers/map.h +++ b/include/aswm/event_handlers/map.h @@ -1,3 +1,4 @@ #include +#include "aswm/aswm.h" -void OnMapRequest(Window root, const XMapRequestEvent* e); +void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e); diff --git a/include/aswm/mapper/mapper.h b/include/aswm/mapper/mapper.h new file mode 100644 index 0000000..3e7a757 --- /dev/null +++ b/include/aswm/mapper/mapper.h @@ -0,0 +1,24 @@ +#ifndef ASWM_MAPPER +#define ASWM_MAPPER + +#include + +struct AswmTile; + +typedef struct AswmTile { + int x, y; + int width, height; + bool free; + Display* display; + Window window; + bool has_children; + struct AswmTile* children[2]; +} AswmTile; + +AswmTile* aswm_alloc_tile(); +AswmTile* aswm_alloc_root_tile(Display* display, Window window); +void aswm_free_tile(AswmTile* tile); + +AswmTile* aswm_map_new_window(Display* display, Window window, AswmTile* tile); + +#endif diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index 5a3f5b6..ce9d359 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -1,3 +1,4 @@ +#include #include "aswm/aswm.h" #include "aswm/event_handlers/create.h" #include "aswm/event_handlers/configure.h" @@ -5,17 +6,22 @@ #include "aswm/event_handlers/reparent.h" #include "aswm/event_handlers/destroy.h" -aswm _aswm; +Aswm _aswm; -void AswmOpen() { +void aswm_open() { _aswm.display = XOpenDisplay(NULL); - _aswm.root = DefaultRootWindow(_aswm.display); + _aswm.root_window = DefaultRootWindow(_aswm.display); - XSelectInput(_aswm.display, _aswm.root, SubstructureRedirectMask | SubstructureNotifyMask); + + _aswm.root_tile = aswm_alloc_root_tile(_aswm.display, _aswm.root_window); + + XSelectInput(_aswm.display, _aswm.root_window, SubstructureRedirectMask | SubstructureNotifyMask); XSync(_aswm.display, 0); + + atexit(aswm_close); } -void AswmEventLoop() { +void aswm_event_loop() { for (;;) { XEvent e; XNextEvent(_aswm.display, &e); @@ -28,7 +34,7 @@ void AswmEventLoop() { OnConfigureRequest(&e.xconfigurerequest); break; case MapRequest: - OnMapRequest(_aswm.root, &e.xmaprequest); + OnMapRequest(&_aswm, &e.xmaprequest); break; case DestroyNotify: OnDestroyNotify(&e.xdestroywindow); @@ -43,6 +49,7 @@ void AswmEventLoop() { } } -void AswmClose() { +void aswm_close() { + aswm_free_tile(_aswm.root_tile); XCloseDisplay(_aswm.display); } diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index 7d22a65..a15fb92 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -2,7 +2,7 @@ #include #include -void OnMapRequest(Window root, const XMapRequestEvent* e) { +void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { printf("XMapRequestEvent %lu\n", e->window); { @@ -20,19 +20,6 @@ void OnMapRequest(Window root, const XMapRequestEvent* e) { int full_width = XDisplayWidth(e->display, screen_number); int full_height = XDisplayWidth(e->display, screen_number); - // Automatically reconfigure position and size of the window - XWindowChanges changes; - changes.x = 0; - changes.y = 0; - changes.width = full_width; - changes.height = full_height; - - printf("\tx = %i\n", changes.x); - printf("\ty = %i\n", changes.y); - printf("\tw = %i\n", changes.width); - printf("\th = %i\n", changes.height); - - XConfigureWindow(e->display, e->window, CWX | CWY | CWWidth | CWHeight, &changes); - + aswm_map_new_window(e->display, e->window, aswm->root_tile); XMapWindow(e->display, e->window); } diff --git a/src/aswm/main.c b/src/aswm/main.c index 46f5d3e..ce0202f 100644 --- a/src/aswm/main.c +++ b/src/aswm/main.c @@ -1,7 +1,7 @@ #include "aswm/aswm.h" int main(int argc, char** argv) { - AswmOpen(); - AswmEventLoop(); - AswmClose(); + aswm_open(); + aswm_event_loop(); + aswm_close(); } diff --git a/src/aswm/mapper/mapper.c b/src/aswm/mapper/mapper.c new file mode 100644 index 0000000..f56a100 --- /dev/null +++ b/src/aswm/mapper/mapper.c @@ -0,0 +1,85 @@ +#include +#include +#include "aswm/mapper/mapper.h" + +AswmTile* aswm_alloc_tile() { + AswmTile* tile = malloc(sizeof(AswmTile)); + for(int i = 0; i < 2; i++) + tile->children[i] = nullptr; + return tile; +} + +AswmTile* aswm_alloc_root_tile(Display* display, Window window) { + int screen_number = XDefaultScreen(display); + AswmTile* tile = aswm_alloc_tile(); + + tile->has_children = false; + tile->x = 0; + tile->y = 0; + tile->free = true; + tile->width = XDisplayWidth(display, screen_number); + tile->height = XDisplayHeight(display, screen_number); + + return tile; +} + +void aswm_free_tile(AswmTile* tile) { + for(int i = 0; i < 2; i++) + aswm_free_tile(tile->children[i]); + if(tile != nullptr) + free(tile); +} + +/** + * Returns the free tile built in the split process. + */ +AswmTile* h_split(AswmTile* tile); + +AswmTile* aswm_map_new_window(Display* display, Window window, AswmTile* tile) { + if(tile->has_children) { + // The tile is necessarily considered occupied + return aswm_map_new_window(display, window, tile->children[1]); + } + if(tile->free) { + // Leef of the tile tree, the window can be inserted there + tile->free = false; + tile->display = display; + tile->window = window; + + // Automatically reconfigure position and size of the window + XWindowChanges changes; + changes.x = tile->x; + changes.y = tile->y; + changes.width = tile->width; + changes.height = tile->height; + + printf("\tx = %i\n", changes.x); + printf("\ty = %i\n", changes.y); + printf("\tw = %i\n", changes.width); + printf("\th = %i\n", changes.height); + + XConfigureWindow(display, window, CWX | CWY | CWWidth | CWHeight, &changes); + return tile; + } + // Leef of tile tree, but occupied. Split the tile and map the window to the + // new tile. + return aswm_map_new_window(display, window, h_split(tile)); +} + +AswmTile* h_split(AswmTile* tile) { + tile->has_children = true; + for(int i = 0; i < 2; i++) { + tile->children[i] = aswm_alloc_tile(); + tile->children[i]->has_children = false; + tile->children[i]->free = true; + tile->children[i]->x = tile->x; + tile->children[i]->width = tile->width; + tile->children[i]->height = tile->height / 2; + } + tile->children[0]->y = tile->y; + tile->children[1]->y = tile->y + tile->children[0]->height; + + aswm_map_new_window(tile->display, tile->window, tile->children[0]); + + return tile->children[1]; +} From b8b13ebe079905b303c20113bb59e0def06c4b1b Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Thu, 26 Dec 2024 16:38:27 +0100 Subject: [PATCH 4/9] :truck: Merges src and include --- CMakeLists.txt | 3 +-- src/aswm/aswm.c | 18 ++++++++++++------ {include => src}/aswm/aswm.h | 4 +++- src/aswm/event_handlers/configure.c | 2 +- .../aswm/event_handlers/configure.h | 0 src/aswm/event_handlers/create.c | 2 +- {include => src}/aswm/event_handlers/create.h | 0 src/aswm/event_handlers/destroy.c | 2 +- {include => src}/aswm/event_handlers/destroy.h | 0 src/aswm/event_handlers/map.c | 2 +- {include => src}/aswm/event_handlers/map.h | 0 src/aswm/event_handlers/reparent.c | 2 +- .../aswm/event_handlers/reparent.h | 0 src/aswm/event_handlers/unmap.c | 2 +- {include => src}/aswm/event_handlers/unmap.h | 0 src/aswm/main.c | 7 ------- src/aswm/mapper/mapper.c | 5 ----- {include => src}/aswm/mapper/mapper.h | 0 18 files changed, 22 insertions(+), 27 deletions(-) rename {include => src}/aswm/aswm.h (75%) rename {include => src}/aswm/event_handlers/configure.h (100%) rename {include => src}/aswm/event_handlers/create.h (100%) rename {include => src}/aswm/event_handlers/destroy.h (100%) rename {include => src}/aswm/event_handlers/map.h (100%) rename {include => src}/aswm/event_handlers/reparent.h (100%) rename {include => src}/aswm/event_handlers/unmap.h (100%) delete mode 100644 src/aswm/main.c rename {include => src}/aswm/mapper/mapper.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b8e223..5fa353b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,6 @@ find_package(X11 REQUIRED) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") add_executable(aswm - src/aswm/main.c src/aswm/aswm.c src/aswm/mapper/mapper.c src/aswm/event_handlers/create.c @@ -17,5 +16,5 @@ add_executable(aswm src/aswm/event_handlers/reparent.c src/aswm/event_handlers/destroy.c src/aswm/event_handlers/map.c) -target_include_directories(aswm PUBLIC ${X11_INCLUDE_DIR} include) +target_include_directories(aswm PUBLIC ${X11_INCLUDE_DIR} src) target_link_libraries(aswm ${X11_LIBRARIES}) diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index ce9d359..e787c62 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -1,10 +1,10 @@ #include -#include "aswm/aswm.h" -#include "aswm/event_handlers/create.h" -#include "aswm/event_handlers/configure.h" -#include "aswm/event_handlers/map.h" -#include "aswm/event_handlers/reparent.h" -#include "aswm/event_handlers/destroy.h" +#include "aswm.h" +#include "event_handlers/create.h" +#include "event_handlers/configure.h" +#include "event_handlers/map.h" +#include "event_handlers/reparent.h" +#include "event_handlers/destroy.h" Aswm _aswm; @@ -53,3 +53,9 @@ void aswm_close() { aswm_free_tile(_aswm.root_tile); XCloseDisplay(_aswm.display); } + +int main(int argc, char** argv) { + aswm_open(); + aswm_event_loop(); + aswm_close(); +} diff --git a/include/aswm/aswm.h b/src/aswm/aswm.h similarity index 75% rename from include/aswm/aswm.h rename to src/aswm/aswm.h index 1d538b3..0358410 100644 --- a/include/aswm/aswm.h +++ b/src/aswm/aswm.h @@ -1,7 +1,7 @@ #ifndef ASWM #define ASWM -#include "aswm/mapper/mapper.h" +#include "mapper/mapper.h" typedef struct { Display *display; @@ -13,4 +13,6 @@ void aswm_open(); void aswm_event_loop(); void aswm_close(); +int main(int argc, char** argv); + #endif diff --git a/src/aswm/event_handlers/configure.c b/src/aswm/event_handlers/configure.c index 8cb90fe..ec316a4 100644 --- a/src/aswm/event_handlers/configure.c +++ b/src/aswm/event_handlers/configure.c @@ -1,4 +1,4 @@ -#include "aswm/event_handlers/configure.h" +#include "configure.h" #include void OnConfigureRequest(const XConfigureRequestEvent* e) { diff --git a/include/aswm/event_handlers/configure.h b/src/aswm/event_handlers/configure.h similarity index 100% rename from include/aswm/event_handlers/configure.h rename to src/aswm/event_handlers/configure.h diff --git a/src/aswm/event_handlers/create.c b/src/aswm/event_handlers/create.c index 2254671..95c3052 100644 --- a/src/aswm/event_handlers/create.c +++ b/src/aswm/event_handlers/create.c @@ -1,4 +1,4 @@ -#include "aswm/event_handlers/create.h" +#include "create.h" #include void OnCreateNotify(const XCreateWindowEvent* e) { diff --git a/include/aswm/event_handlers/create.h b/src/aswm/event_handlers/create.h similarity index 100% rename from include/aswm/event_handlers/create.h rename to src/aswm/event_handlers/create.h diff --git a/src/aswm/event_handlers/destroy.c b/src/aswm/event_handlers/destroy.c index f1c0197..635c844 100644 --- a/src/aswm/event_handlers/destroy.c +++ b/src/aswm/event_handlers/destroy.c @@ -1,4 +1,4 @@ -#include "aswm/event_handlers/destroy.h" +#include "destroy.h" #include void OnDestroyNotify(const XDestroyWindowEvent* e) { diff --git a/include/aswm/event_handlers/destroy.h b/src/aswm/event_handlers/destroy.h similarity index 100% rename from include/aswm/event_handlers/destroy.h rename to src/aswm/event_handlers/destroy.h diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index a15fb92..896cb5f 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -1,4 +1,4 @@ -#include "aswm/event_handlers/map.h" +#include "map.h" #include #include diff --git a/include/aswm/event_handlers/map.h b/src/aswm/event_handlers/map.h similarity index 100% rename from include/aswm/event_handlers/map.h rename to src/aswm/event_handlers/map.h diff --git a/src/aswm/event_handlers/reparent.c b/src/aswm/event_handlers/reparent.c index 9c6d14a..f2f3842 100644 --- a/src/aswm/event_handlers/reparent.c +++ b/src/aswm/event_handlers/reparent.c @@ -1,4 +1,4 @@ -#include "aswm/event_handlers/reparent.h" +#include "reparent.h" #include void OnReparentNotify(const XReparentEvent* e) { diff --git a/include/aswm/event_handlers/reparent.h b/src/aswm/event_handlers/reparent.h similarity index 100% rename from include/aswm/event_handlers/reparent.h rename to src/aswm/event_handlers/reparent.h diff --git a/src/aswm/event_handlers/unmap.c b/src/aswm/event_handlers/unmap.c index 26ecb2e..6f3ae67 100644 --- a/src/aswm/event_handlers/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -1,4 +1,4 @@ -#include "aswm/event_handlers/unmap.h" +#include "unmap.h" #include void OnUnmapRequest(Window root, const XMapRequestEvent* e) { diff --git a/include/aswm/event_handlers/unmap.h b/src/aswm/event_handlers/unmap.h similarity index 100% rename from include/aswm/event_handlers/unmap.h rename to src/aswm/event_handlers/unmap.h diff --git a/src/aswm/main.c b/src/aswm/main.c deleted file mode 100644 index ce0202f..0000000 --- a/src/aswm/main.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "aswm/aswm.h" - -int main(int argc, char** argv) { - aswm_open(); - aswm_event_loop(); - aswm_close(); -} diff --git a/src/aswm/mapper/mapper.c b/src/aswm/mapper/mapper.c index f56a100..566cd9f 100644 --- a/src/aswm/mapper/mapper.c +++ b/src/aswm/mapper/mapper.c @@ -53,11 +53,6 @@ AswmTile* aswm_map_new_window(Display* display, Window window, AswmTile* tile) { changes.width = tile->width; changes.height = tile->height; - printf("\tx = %i\n", changes.x); - printf("\ty = %i\n", changes.y); - printf("\tw = %i\n", changes.width); - printf("\th = %i\n", changes.height); - XConfigureWindow(display, window, CWX | CWY | CWWidth | CWHeight, &changes); return tile; } diff --git a/include/aswm/mapper/mapper.h b/src/aswm/mapper/mapper.h similarity index 100% rename from include/aswm/mapper/mapper.h rename to src/aswm/mapper/mapper.h From 000b7832794785534fbc9cdd65f2828278fcd9fe Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Sat, 28 Dec 2024 11:11:16 +0100 Subject: [PATCH 5/9] :construction: Basic ES-DE window detection and event logging --- CMakeLists.txt | 3 ++- src/aswm/aswm.c | 33 ++++++++++++++++++++++++++++++++- src/aswm/aswm.h | 1 + src/aswm/event_handlers/map.c | 5 +++++ src/aswm/event_handlers/unmap.c | 7 +++++-- src/aswm/event_handlers/unmap.h | 4 +++- src/aswm/mapper/mapper.h | 1 + 7 files changed, 49 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fa353b..6558b84 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(aswm src/aswm/event_handlers/configure.c src/aswm/event_handlers/reparent.c src/aswm/event_handlers/destroy.c - src/aswm/event_handlers/map.c) + src/aswm/event_handlers/map.c + src/aswm/event_handlers/unmap.c) target_include_directories(aswm PUBLIC ${X11_INCLUDE_DIR} src) target_link_libraries(aswm ${X11_LIBRARIES}) diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index e787c62..acf2eb1 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -1,8 +1,10 @@ #include +#include #include "aswm.h" #include "event_handlers/create.h" #include "event_handlers/configure.h" #include "event_handlers/map.h" +#include "event_handlers/unmap.h" #include "event_handlers/reparent.h" #include "event_handlers/destroy.h" @@ -15,7 +17,11 @@ void aswm_open() { _aswm.root_tile = aswm_alloc_root_tile(_aswm.display, _aswm.root_window); - XSelectInput(_aswm.display, _aswm.root_window, SubstructureRedirectMask | SubstructureNotifyMask); + printf("PointerRoot %lu\n", PointerRoot); + printf("None %lu\n", None); + printf("Root window %lu\n", _aswm.root_window); + + XSelectInput(_aswm.display, _aswm.root_window, SubstructureRedirectMask | SubstructureNotifyMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); XSync(_aswm.display, 0); atexit(aswm_close); @@ -36,12 +42,37 @@ void aswm_event_loop() { case MapRequest: OnMapRequest(&_aswm, &e.xmaprequest); break; + case UnmapNotify: + OnUnmapNotify(&_aswm, &e.xunmap); + break; case DestroyNotify: OnDestroyNotify(&e.xdestroywindow); break; case ReparentNotify: OnReparentNotify(&e.xreparent); break; + case FocusIn: + printf("FocusIn %lu\n", (&e.xfocus)->window); + break; + case FocusOut: + printf("FocusOut %lu\n", e.xfocus.window); + break; + case EnterNotify: + int revert_to; + Window window; + XGetInputFocus(e.xcrossing.display, &window, &revert_to); + printf("Enter %lu\n", e.xcrossing.window); + printf("Input focus %lu\n", window); + break; + case LeaveNotify: + printf("Leave %lu\n", e.xcrossing.window); + break; + case ButtonPress: + printf("Press %lu\n", e.xbutton.window); + break; + case ButtonRelease: + printf("Release %lu\n", e.xbutton.window); + break; default: break; /* LOG(WARNING) << "Ignored event"; */ diff --git a/src/aswm/aswm.h b/src/aswm/aswm.h index 0358410..c0944c5 100644 --- a/src/aswm/aswm.h +++ b/src/aswm/aswm.h @@ -7,6 +7,7 @@ typedef struct { Display *display; Window root_window; AswmTile* root_tile; + Window es_de_window; } Aswm; void aswm_open(); diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index 896cb5f..6e96a9a 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -1,5 +1,6 @@ #include "map.h" #include +#include #include void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { @@ -9,6 +10,8 @@ void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { XTextProperty text_property; XGetWMName(e->display, e->window, &text_property); printf("\tname: %s\n", text_property.value); + if(text_property.value != nullptr && strcmp(text_property.value, "ES-DE") == 0) + aswm->es_de_window = e->window; } { XClassHint class; @@ -21,5 +24,7 @@ void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { int full_height = XDisplayWidth(e->display, screen_number); aswm_map_new_window(e->display, e->window, aswm->root_tile); + + /* XSelectInput(e->display, e->window, FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); */ XMapWindow(e->display, e->window); } diff --git a/src/aswm/event_handlers/unmap.c b/src/aswm/event_handlers/unmap.c index 6f3ae67..c4431f6 100644 --- a/src/aswm/event_handlers/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -1,6 +1,9 @@ #include "unmap.h" #include -void OnUnmapRequest(Window root, const XMapRequestEvent* e) { - printf("XUnmapWindow %lu", e->window); +void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) { + printf("XUnmapWindow %lu\n", e->window); + printf("ES-DE window %lu\n", aswm->es_de_window); + if(aswm->es_de_window != None) + XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); } diff --git a/src/aswm/event_handlers/unmap.h b/src/aswm/event_handlers/unmap.h index f82fe1c..5f0aec4 100644 --- a/src/aswm/event_handlers/unmap.h +++ b/src/aswm/event_handlers/unmap.h @@ -1,3 +1,5 @@ #include -void OnUnmapRequest(Window root, const XMapRequestEvent* e); +#include "aswm/aswm.h" + +void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e); diff --git a/src/aswm/mapper/mapper.h b/src/aswm/mapper/mapper.h index 3e7a757..dce378d 100644 --- a/src/aswm/mapper/mapper.h +++ b/src/aswm/mapper/mapper.h @@ -20,5 +20,6 @@ AswmTile* aswm_alloc_root_tile(Display* display, Window window); void aswm_free_tile(AswmTile* tile); AswmTile* aswm_map_new_window(Display* display, Window window, AswmTile* tile); +AswmTile* aswm_unmap_window(Display* display, Window window, AswmTile* tile); #endif From 9aab7c087c8f64967050073251ae87e2f6ce2672 Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Sun, 29 Dec 2024 13:51:11 +0100 Subject: [PATCH 6/9] :sparkles: Very basic native htiles --- CMakeLists.txt | 6 +- src/aswm/aswm.c | 15 +-- src/aswm/aswm.h | 2 - src/aswm/event_handlers/map.c | 3 +- src/aswm/event_handlers/reparent.c | 2 +- src/aswm/event_handlers/unmap.c | 5 +- src/aswm/log/log.c | 17 +++ src/aswm/log/log.h | 8 ++ src/aswm/mapper/mapper.c | 159 +++++++++++++++++------------ src/aswm/mapper/mapper.h | 20 +--- 10 files changed, 137 insertions(+), 100 deletions(-) create mode 100644 src/aswm/log/log.c create mode 100644 src/aswm/log/log.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6558b84..404a449 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_C_STANDARD 23) find_package(X11 REQUIRED) -set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -pg") add_executable(aswm src/aswm/aswm.c @@ -16,6 +16,8 @@ add_executable(aswm src/aswm/event_handlers/reparent.c src/aswm/event_handlers/destroy.c src/aswm/event_handlers/map.c - src/aswm/event_handlers/unmap.c) + src/aswm/event_handlers/unmap.c + src/aswm/log/log.c +) target_include_directories(aswm PUBLIC ${X11_INCLUDE_DIR} src) target_link_libraries(aswm ${X11_LIBRARIES}) diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index acf2eb1..0f44bcd 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -7,21 +7,19 @@ #include "event_handlers/unmap.h" #include "event_handlers/reparent.h" #include "event_handlers/destroy.h" +#include "log/log.h" Aswm _aswm; void aswm_open() { _aswm.display = XOpenDisplay(NULL); - _aswm.root_window = DefaultRootWindow(_aswm.display); - - - _aswm.root_tile = aswm_alloc_root_tile(_aswm.display, _aswm.root_window); + Window root_window = DefaultRootWindow(_aswm.display); printf("PointerRoot %lu\n", PointerRoot); printf("None %lu\n", None); - printf("Root window %lu\n", _aswm.root_window); + printf("Root window %lu\n", root_window); - XSelectInput(_aswm.display, _aswm.root_window, SubstructureRedirectMask | SubstructureNotifyMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); + XSelectInput(_aswm.display, root_window, SubstructureRedirectMask | SubstructureNotifyMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); XSync(_aswm.display, 0); atexit(aswm_close); @@ -41,9 +39,13 @@ void aswm_event_loop() { break; case MapRequest: OnMapRequest(&_aswm, &e.xmaprequest); + printf("Map tree:\n"); + log_tree(_aswm.display, DefaultRootWindow(_aswm.display), 1); break; case UnmapNotify: OnUnmapNotify(&_aswm, &e.xunmap); + printf("Unmap tree:\n"); + log_tree(_aswm.display, DefaultRootWindow(_aswm.display), 1); break; case DestroyNotify: OnDestroyNotify(&e.xdestroywindow); @@ -81,7 +83,6 @@ void aswm_event_loop() { } void aswm_close() { - aswm_free_tile(_aswm.root_tile); XCloseDisplay(_aswm.display); } diff --git a/src/aswm/aswm.h b/src/aswm/aswm.h index c0944c5..bd4c9da 100644 --- a/src/aswm/aswm.h +++ b/src/aswm/aswm.h @@ -5,8 +5,6 @@ typedef struct { Display *display; - Window root_window; - AswmTile* root_tile; Window es_de_window; } Aswm; diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index 6e96a9a..357c063 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -23,8 +23,7 @@ void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { int full_width = XDisplayWidth(e->display, screen_number); int full_height = XDisplayWidth(e->display, screen_number); - aswm_map_new_window(e->display, e->window, aswm->root_tile); + aswm_map_new_window(e->display, DefaultRootWindow(e->display), e->window); /* XSelectInput(e->display, e->window, FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); */ - XMapWindow(e->display, e->window); } diff --git a/src/aswm/event_handlers/reparent.c b/src/aswm/event_handlers/reparent.c index f2f3842..e3b5675 100644 --- a/src/aswm/event_handlers/reparent.c +++ b/src/aswm/event_handlers/reparent.c @@ -2,5 +2,5 @@ #include void OnReparentNotify(const XReparentEvent* e) { - printf("XReparentEvent %lu\n", e->window); + printf("XReparentEvent from %lu: %lu to %lu\n", e->event, e->window, e->parent); } diff --git a/src/aswm/event_handlers/unmap.c b/src/aswm/event_handlers/unmap.c index c4431f6..a439967 100644 --- a/src/aswm/event_handlers/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -2,8 +2,11 @@ #include void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) { - printf("XUnmapWindow %lu\n", e->window); + printf("XUnmapWindow %lu tiled in %lu\n", e->window, e->event); printf("ES-DE window %lu\n", aswm->es_de_window); if(aswm->es_de_window != None) XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); + // Since SubstructureNotify is selected on each frame, e->event corresponds + // to the parent of the unmapped window (e->window) + aswm_unmap_window(e->display, e->event); } diff --git a/src/aswm/log/log.c b/src/aswm/log/log.c new file mode 100644 index 0000000..f08ef2b --- /dev/null +++ b/src/aswm/log/log.c @@ -0,0 +1,17 @@ +#include "log.h" +#include + +void log_tree(Display* display, Window window, int indent) { + for(int i = 0; i < indent; i++) + printf(" "); + printf("%lu\n", window); + + Window root_window; + Window parent_tile; + Window* children; + unsigned int children_count; + XQueryTree(display, window, &root_window, &parent_tile, &children, &children_count); + for(auto i = 0; i < children_count; i++) + log_tree(display, children[i], indent+1); + XFree(children); +} diff --git a/src/aswm/log/log.h b/src/aswm/log/log.h new file mode 100644 index 0000000..e58014a --- /dev/null +++ b/src/aswm/log/log.h @@ -0,0 +1,8 @@ +#ifndef ASWM_LOG +#define ASWM_LOG + +#include + +void log_tree(Display* display, Window root, int indent); + +#endif diff --git a/src/aswm/mapper/mapper.c b/src/aswm/mapper/mapper.c index 566cd9f..1d30688 100644 --- a/src/aswm/mapper/mapper.c +++ b/src/aswm/mapper/mapper.c @@ -1,80 +1,103 @@ #include #include #include "aswm/mapper/mapper.h" - -AswmTile* aswm_alloc_tile() { - AswmTile* tile = malloc(sizeof(AswmTile)); - for(int i = 0; i < 2; i++) - tile->children[i] = nullptr; - return tile; -} - -AswmTile* aswm_alloc_root_tile(Display* display, Window window) { - int screen_number = XDefaultScreen(display); - AswmTile* tile = aswm_alloc_tile(); - - tile->has_children = false; - tile->x = 0; - tile->y = 0; - tile->free = true; - tile->width = XDisplayWidth(display, screen_number); - tile->height = XDisplayHeight(display, screen_number); - - return tile; -} - -void aswm_free_tile(AswmTile* tile) { - for(int i = 0; i < 2; i++) - aswm_free_tile(tile->children[i]); - if(tile != nullptr) - free(tile); -} +#include "aswm/log/log.h" /** * Returns the free tile built in the split process. */ -AswmTile* h_split(AswmTile* tile); +void h_split(Display* display, Window current_node, Window window_to_map); -AswmTile* aswm_map_new_window(Display* display, Window window, AswmTile* tile) { - if(tile->has_children) { - // The tile is necessarily considered occupied - return aswm_map_new_window(display, window, tile->children[1]); - } - if(tile->free) { - // Leef of the tile tree, the window can be inserted there - tile->free = false; - tile->display = display; - tile->window = window; - - // Automatically reconfigure position and size of the window - XWindowChanges changes; - changes.x = tile->x; - changes.y = tile->y; - changes.width = tile->width; - changes.height = tile->height; - - XConfigureWindow(display, window, CWX | CWY | CWWidth | CWHeight, &changes); - return tile; - } - // Leef of tile tree, but occupied. Split the tile and map the window to the - // new tile. - return aswm_map_new_window(display, window, h_split(tile)); +void aswm_map_new_window(Display* display, Window current_node, Window window_to_map) { + h_split(display, current_node, window_to_map); } -AswmTile* h_split(AswmTile* tile) { - tile->has_children = true; - for(int i = 0; i < 2; i++) { - tile->children[i] = aswm_alloc_tile(); - tile->children[i]->has_children = false; - tile->children[i]->free = true; - tile->children[i]->x = tile->x; - tile->children[i]->width = tile->width; - tile->children[i]->height = tile->height / 2; +void h_split(Display* display, Window current_tile, Window window_to_map) { + // Current: + // +--------------+ + // | current tile | + // | +--------+ | + // | | child | | + // | +--------+ | + // +--------------+ + // Target: + // +---------------+ + // | current tile | + // | +-----------+ | + // | | child | | + // | +-----------+ | + // | +-----------+ | + // | | free tile | | + // | +-----------+ | + // +---------------+ + + XWindowAttributes current_tile_attributes; + XGetWindowAttributes(display, current_tile, ¤t_tile_attributes); + + XReparentWindow(display, window_to_map, current_tile, 0, 0); + + Window _root_window; // unused + Window _parent_window; // unused + Window* children; + unsigned int children_count; + + XQueryTree(display, current_tile, &_root_window, &_parent_window, &children, &children_count); + + for(auto i = 0; i < children_count; i++) { + XMoveResizeWindow(display, children[i], + 0, i * current_tile_attributes.height / children_count, + current_tile_attributes.width, + current_tile_attributes.height / children_count); } - tile->children[0]->y = tile->y; - tile->children[1]->y = tile->y + tile->children[0]->height; - - aswm_map_new_window(tile->display, tile->window, tile->children[0]); - - return tile->children[1]; + XFree(children); + XMapWindow(display, window_to_map); +} + +void resize_children_to_tile(Display* display, Window tile) { + Window _root_window; // unused + Window _grand_parent; // unused + Window* children; + unsigned int children_count; + XQueryTree( + display, tile, + &_root_window, &_grand_parent, + &children, &children_count); + printf("Current siblings of %lu:\n", tile); + for(auto i = 0; i < children_count; i++) + printf("%lu ", children[i]); + printf("\n"); + + // Resize new children of the parent tile + XWindowAttributes parent_attributes; + XGetWindowAttributes(display, tile, &parent_attributes); + + for(auto i = 0; i < children_count; i++) { + // TODO: currently only hsplit supported + XMoveResizeWindow(display, children[i], + 0, + i * parent_attributes.height / children_count, + parent_attributes.width, + parent_attributes.height / children_count); + } + XFree(children); +} + +void aswm_unmap_window(Display* display, Window tile) { + // +-------------------------------+ + // | root | + // |+-------------+ +-------------+| + // || tile | | || + // || +--------+ | | || + // || | win 1 | | | || + // || +--------+ | | || + // || +--------+ | | || + // || | del win| | | win 3 || + // || +--------+ | | || + // || +--------+ | | || + // || | win 2 | | | || + // || +--------+ | | || + // |+-------------+ +-------------+| + // +-------------------------------+ + + resize_children_to_tile(display, tile); } diff --git a/src/aswm/mapper/mapper.h b/src/aswm/mapper/mapper.h index dce378d..e14b576 100644 --- a/src/aswm/mapper/mapper.h +++ b/src/aswm/mapper/mapper.h @@ -3,23 +3,9 @@ #include -struct AswmTile; +Window create_aswm_root_tile(Display* display, Window root_window); -typedef struct AswmTile { - int x, y; - int width, height; - bool free; - Display* display; - Window window; - bool has_children; - struct AswmTile* children[2]; -} AswmTile; - -AswmTile* aswm_alloc_tile(); -AswmTile* aswm_alloc_root_tile(Display* display, Window window); -void aswm_free_tile(AswmTile* tile); - -AswmTile* aswm_map_new_window(Display* display, Window window, AswmTile* tile); -AswmTile* aswm_unmap_window(Display* display, Window window, AswmTile* tile); +void aswm_map_new_window(Display* display, Window current_node, Window window_to_map); +void aswm_unmap_window(Display* display, Window window); #endif From b7aa0cdd40123ba72d97b390e0be83913bce471b Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Mon, 30 Dec 2024 18:02:10 +0100 Subject: [PATCH 7/9] :art: 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. --- src/aswm/aswm.c | 78 +++++++++++++++++++++++++---- src/aswm/aswm.h | 6 ++- src/aswm/event_handlers/configure.c | 18 +------ src/aswm/event_handlers/create.c | 32 +++++++++++- src/aswm/event_handlers/create.h | 3 +- src/aswm/event_handlers/map.c | 38 +++++++------- src/aswm/event_handlers/unmap.c | 13 +++-- src/aswm/log/error.h | 15 ++++++ src/aswm/mapper/mapper.c | 35 ++++++++++--- src/aswm/mapper/mapper.h | 2 +- 10 files changed, 176 insertions(+), 64 deletions(-) create mode 100644 src/aswm/log/error.h diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index 0f44bcd..3144d3b 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -7,24 +7,67 @@ #include "event_handlers/unmap.h" #include "event_handlers/reparent.h" #include "event_handlers/destroy.h" -#include "log/log.h" +#include "log/error.h" Aswm _aswm; + void aswm_open() { _aswm.display = XOpenDisplay(NULL); - Window root_window = DefaultRootWindow(_aswm.display); + _aswm.workspaces = malloc(0); + _aswm.workspace_count = 0; - printf("PointerRoot %lu\n", PointerRoot); - printf("None %lu\n", None); - printf("Root window %lu\n", root_window); + XSetErrorHandler(aswm_error_handler); - 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); + 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); } +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() { for (;;) { XEvent e; @@ -32,20 +75,19 @@ void aswm_event_loop() { switch(e.type) { 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; case ConfigureRequest: OnConfigureRequest(&e.xconfigurerequest); break; case MapRequest: OnMapRequest(&_aswm, &e.xmaprequest); - printf("Map tree:\n"); - log_tree(_aswm.display, DefaultRootWindow(_aswm.display), 1); break; case UnmapNotify: OnUnmapNotify(&_aswm, &e.xunmap); - printf("Unmap tree:\n"); - log_tree(_aswm.display, DefaultRootWindow(_aswm.display), 1); break; case DestroyNotify: OnDestroyNotify(&e.xdestroywindow); @@ -75,6 +117,19 @@ void aswm_event_loop() { case ButtonRelease: printf("Release %lu\n", e.xbutton.window); 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: break; /* LOG(WARNING) << "Ignored event"; */ @@ -83,6 +138,7 @@ void aswm_event_loop() { } void aswm_close() { + free(_aswm.workspaces); XCloseDisplay(_aswm.display); } diff --git a/src/aswm/aswm.h b/src/aswm/aswm.h index bd4c9da..98e97d3 100644 --- a/src/aswm/aswm.h +++ b/src/aswm/aswm.h @@ -5,10 +5,14 @@ typedef struct { Display *display; - Window es_de_window; + Window root_window; + Window* workspaces; + unsigned int workspace_count; + Window current_workspace; } Aswm; void aswm_open(); +void aswm_create_workspace(); void aswm_event_loop(); void aswm_close(); diff --git a/src/aswm/event_handlers/configure.c b/src/aswm/event_handlers/configure.c index ec316a4..937211b 100644 --- a/src/aswm/event_handlers/configure.c +++ b/src/aswm/event_handlers/configure.c @@ -2,25 +2,9 @@ #include 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("\ty = %i\n", e->y); printf("\tw = %i\n", e->width); 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); } diff --git a/src/aswm/event_handlers/create.c b/src/aswm/event_handlers/create.c index 95c3052..5dec25d 100644 --- a/src/aswm/event_handlers/create.c +++ b/src/aswm/event_handlers/create.c @@ -1,6 +1,34 @@ #include "create.h" #include +#include +#include -void OnCreateNotify(const XCreateWindowEvent* e) { - printf("XCreateWindowEvent %lu\n", e->window); +void OnCreateNotify(Aswm* aswm, const XCreateWindowEvent* e) { + 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(); } diff --git a/src/aswm/event_handlers/create.h b/src/aswm/event_handlers/create.h index 3fc8812..bc60d72 100644 --- a/src/aswm/event_handlers/create.h +++ b/src/aswm/event_handlers/create.h @@ -1,3 +1,4 @@ #include +#include "aswm/aswm.h" -void OnCreateNotify(const XCreateWindowEvent*); +void OnCreateNotify(Aswm* aswm, const XCreateWindowEvent*); diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index 357c063..954cf2c 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -1,29 +1,27 @@ #include "map.h" #include -#include -#include +#include "aswm/log/log.h" +#include "X11/Xutil.h" void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { printf("XMapRequestEvent %lu\n", e->window); - { - XTextProperty text_property; - XGetWMName(e->display, e->window, &text_property); - printf("\tname: %s\n", text_property.value); - if(text_property.value != nullptr && strcmp(text_property.value, "ES-DE") == 0) - aswm->es_de_window = e->window; - } - { - XClassHint class; - XGetClassHint(e->display, e->window, &class); - printf("\tclass: %s, %s\n", class.res_class, class.res_name); - } + /* XSizeHints* size = XAllocSizeHints(); */ + /* long supplied; */ + /* if(XGetWMNormalHints(aswm->display, e->window, size, &supplied)) { */ + /* printf("\tWMNormalHints:\n"); */ + /* printf("\t\tMin w:%i\n", size->min_width); */ + /* 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); */ + /* printf("\t\tBase w:%i\n", size->base_width); */ + /* printf("\t\tBase h:%i\n", size->base_height); */ + /* } */ - int screen_number = XDefaultScreen(e->display); - int full_width = XDisplayWidth(e->display, screen_number); - int full_height = XDisplayWidth(e->display, screen_number); + // Assumes map requests are always received on root and adressed to the + // current workspace + aswm_map_new_window(e->display, aswm->current_workspace, e->window); - aswm_map_new_window(e->display, DefaultRootWindow(e->display), e->window); - - /* XSelectInput(e->display, e->window, FocusChangeMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask); */ + printf("Map tree:\n"); + log_tree(aswm->display, DefaultRootWindow(aswm->display), 1); } diff --git a/src/aswm/event_handlers/unmap.c b/src/aswm/event_handlers/unmap.c index a439967..6faa3b3 100644 --- a/src/aswm/event_handlers/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -1,12 +1,17 @@ #include "unmap.h" #include +#include "aswm/log/log.h" void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) { printf("XUnmapWindow %lu tiled in %lu\n", e->window, e->event); - printf("ES-DE window %lu\n", aswm->es_de_window); - if(aswm->es_de_window != None) - XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); + /* printf("ES-DE window %lu\n", aswm->es_de_window); */ + /* if(aswm->es_de_window != None) */ + /* XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); */ // Since SubstructureNotify is selected on each frame, e->event corresponds // 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); } diff --git a/src/aswm/log/error.h b/src/aswm/log/error.h new file mode 100644 index 0000000..d119390 --- /dev/null +++ b/src/aswm/log/error.h @@ -0,0 +1,15 @@ +#ifndef ASWM_ERROR_H +#define ASWM_ERROR_H + +#include +#include + +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 diff --git a/src/aswm/mapper/mapper.c b/src/aswm/mapper/mapper.c index 1d30688..cbf7e90 100644 --- a/src/aswm/mapper/mapper.c +++ b/src/aswm/mapper/mapper.c @@ -53,23 +53,23 @@ void h_split(Display* display, Window current_tile, Window 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 _grand_parent; // unused Window* children; unsigned int children_count; XQueryTree( - display, tile, + display, parent, &_root_window, &_grand_parent, &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++) printf("%lu ", children[i]); printf("\n"); // Resize new children of the parent tile XWindowAttributes parent_attributes; - XGetWindowAttributes(display, tile, &parent_attributes); + XGetWindowAttributes(display, parent, &parent_attributes); for(auto i = 0; i < children_count; i++) { // TODO: currently only hsplit supported @@ -82,11 +82,11 @@ void resize_children_to_tile(Display* display, Window tile) { XFree(children); } -void aswm_unmap_window(Display* display, Window tile) { +void aswm_unmap_window(Display* display, Window parent, Window window) { // +-------------------------------+ // | root | // |+-------------+ +-------------+| - // || tile | | || + // || parent | | || // || +--------+ | | || // || | 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); } diff --git a/src/aswm/mapper/mapper.h b/src/aswm/mapper/mapper.h index e14b576..ea82ecd 100644 --- a/src/aswm/mapper/mapper.h +++ b/src/aswm/mapper/mapper.h @@ -6,6 +6,6 @@ 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_unmap_window(Display* display, Window window); +void aswm_unmap_window(Display* display, Window parent, Window window); #endif From 9f876c8714b84d3bb7ae1a65f470e5eaa83fc54d Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Wed, 1 Jan 2025 18:20:38 +0100 Subject: [PATCH 8/9] :bug: Fixes unmap + analysis of various properties --- CMakeLists.txt | 1 + src/aswm/aswm.c | 9 ++-- src/aswm/event_handlers/create.c | 6 +++ src/aswm/event_handlers/map.c | 3 +- src/aswm/event_handlers/property.c | 68 ++++++++++++++++++++++++++++++ src/aswm/event_handlers/property.h | 7 +++ src/aswm/event_handlers/unmap.c | 20 ++++++--- src/aswm/log/log.c | 18 ++++++-- src/aswm/log/log.h | 3 +- 9 files changed, 118 insertions(+), 17 deletions(-) create mode 100644 src/aswm/event_handlers/property.c create mode 100644 src/aswm/event_handlers/property.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 404a449..e5764da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(aswm src/aswm/event_handlers/destroy.c src/aswm/event_handlers/map.c src/aswm/event_handlers/unmap.c + src/aswm/event_handlers/property.c src/aswm/log/log.c ) target_include_directories(aswm PUBLIC ${X11_INCLUDE_DIR} src) diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index 3144d3b..129d50d 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include "aswm.h" #include "event_handlers/create.h" #include "event_handlers/configure.h" @@ -7,6 +9,7 @@ #include "event_handlers/unmap.h" #include "event_handlers/reparent.h" #include "event_handlers/destroy.h" +#include "event_handlers/property.h" #include "log/error.h" Aswm _aswm; @@ -125,11 +128,9 @@ void aswm_event_loop() { XFree(atom_name); break; case PropertyNotify: + OnPropertyNotify(&_aswm, &e.xproperty); // 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: break; /* LOG(WARNING) << "Ignored event"; */ diff --git a/src/aswm/event_handlers/create.c b/src/aswm/event_handlers/create.c index 5dec25d..a3e16df 100644 --- a/src/aswm/event_handlers/create.c +++ b/src/aswm/event_handlers/create.c @@ -11,6 +11,12 @@ void OnCreateNotify(Aswm* aswm, const XCreateWindowEvent* e) { printf("\th = %i\n", e->height); printf("\toverride_redirect = %i\n", e->override_redirect); + XWMHints* hints = XGetWMHints(aswm->display, e->window); + if(hints != NULL) { + printf("\tWM_HINTS\n"); + printf("\t\tinitial_state: %i\n", hints->initial_state); + } + // The WM must subscribe to property changes, notably to implement the // Extended Window Manager Hints (EWMH) propocol XSelectInput(aswm->display, e->window, PropertyChangeMask); diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index 954cf2c..ba74771 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -17,11 +17,12 @@ void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { /* printf("\t\tBase w:%i\n", size->base_width); */ /* printf("\t\tBase h:%i\n", size->base_height); */ /* } */ + /* XFree(size); */ // Assumes map requests are always received on root and adressed to the // current workspace aswm_map_new_window(e->display, aswm->current_workspace, e->window); printf("Map tree:\n"); - log_tree(aswm->display, DefaultRootWindow(aswm->display), 1); + log_tree(aswm, DefaultRootWindow(aswm->display), 1); } diff --git a/src/aswm/event_handlers/property.c b/src/aswm/event_handlers/property.c new file mode 100644 index 0000000..fc628b2 --- /dev/null +++ b/src/aswm/event_handlers/property.c @@ -0,0 +1,68 @@ +#include "property.h" +#include +#include +#include +#include + +void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e) { + printf("Unhandled property change for window %lu:\n", e->window); + char* property_name = XGetAtomName(e->display, e->atom); + printf("\t%s\n", property_name); + if(strcmp(property_name, "_NET_WM_NAME") == 0) { + XTextProperty wm_name_property; + char** wm_name; + int wm_name_count; + XGetTextProperty(e->display, e->window, &wm_name_property, e->atom); + Xutf8TextPropertyToTextList(e->display, &wm_name_property, &wm_name, &wm_name_count); + for(auto i = 0; i < wm_name_count; i++) + printf("\t_NET_WM_NAME: %s\n", wm_name[i]); + XFreeStringList(wm_name); + } else if(strcmp(property_name, "WM_NAME") == 0) { + XTextProperty text_property; + if(XGetWMName(e->display, e->window, &text_property)) { + printf("\tWM_NAME: %s\n", text_property.value); + } + } else if(strcmp(property_name, "WM_NORMAL_HINTS") == 0) { + XSizeHints* size = XAllocSizeHints(); + long supplied; + if(XGetWMNormalHints(e->display, e->window, size, &supplied)) { + printf("\tWMNormalHints:\n"); + printf("\t\tMin w:%i\n", size->min_width); + 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); + printf("\t\tBase w:%i\n", size->base_width); + printf("\t\tBase h:%i\n", size->base_height); + } + XFree(size); + } else if(strcmp(property_name, "WM_HINTS") == 0) { + XWMHints* hints = XGetWMHints(aswm->display, e->window); + printf("\t\tflags Input: %li\n", hints->flags & InputHint); + printf("\t\tflags Input: %li\n", hints->flags & StateHint); + printf("\t\tflags IconPixmap: %li\n", hints->flags & IconPixmapHint); + printf("\t\tflags WindowHint: %li\n", hints->flags & IconWindowHint); + printf("\t\tflags IconPosition: %li\n", hints->flags & IconPositionHint); + printf("\t\tflags IconMask: %li\n", hints->flags & IconMaskHint); + printf("\t\tflags WindowGroup: %li\n", hints->flags & WindowGroupHint); + printf("\t\tflags XUrgency: %li\n", hints->flags & XUrgencyHint); + printf("\t\tflags AllHints: %li\n", hints->flags & AllHints); + printf("\t\tinitial_state: %i\n", hints->initial_state); + XFree(hints); + } else if(strcmp(property_name, "WM_PROTOCOLS") == 0) { + Atom* protocols; + int count; + XGetWMProtocols(aswm->display, e->window, &protocols, &count); + if(count > 0) + printf("\t\t"); + for(auto i = 0; i < count; i++) { + char* name = XGetAtomName(aswm->display, protocols[i]); + printf("%s ", name); + XFree(name); + } + if(count > 0) + printf("\n"); + XFree(protocols); + + } + XFree(property_name); +} diff --git a/src/aswm/event_handlers/property.h b/src/aswm/event_handlers/property.h new file mode 100644 index 0000000..8915cfa --- /dev/null +++ b/src/aswm/event_handlers/property.h @@ -0,0 +1,7 @@ +#ifndef ASWM_PROPERTY +#define ASWM_PROPERTY + +#include "aswm/aswm.h" + +void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e); +#endif diff --git a/src/aswm/event_handlers/unmap.c b/src/aswm/event_handlers/unmap.c index 6faa3b3..6af785b 100644 --- a/src/aswm/event_handlers/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -1,17 +1,23 @@ #include "unmap.h" #include #include "aswm/log/log.h" +#include void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) { printf("XUnmapWindow %lu tiled in %lu\n", e->window, e->event); - /* printf("ES-DE window %lu\n", aswm->es_de_window); */ - /* if(aswm->es_de_window != None) */ - /* XSetInputFocus(e->display, aswm->es_de_window, RevertToPointerRoot, CurrentTime); */ + XWMHints* hints = XGetWMHints(aswm->display, e->window); + if(hints != NULL) { + printf("\tWM_HINTS\n"); + printf("\t\tinitial_state: %i\n", hints->initial_state); + } // Since SubstructureNotify is selected on each frame, e->event corresponds // to the parent of the unmapped window (e->window) - // 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); + // Unmap should have no effect on not map windows. Currently, all mapped + // windows are children of aswm->root_window. + if(e->event != XDefaultRootWindow(e->display)) { + aswm_unmap_window(e->display, e->event, e->window); + printf("Unmap tree:\n"); + log_tree(aswm, DefaultRootWindow(aswm->display), 1); + } } diff --git a/src/aswm/log/log.c b/src/aswm/log/log.c index f08ef2b..e267eca 100644 --- a/src/aswm/log/log.c +++ b/src/aswm/log/log.c @@ -1,17 +1,27 @@ #include "log.h" #include -void log_tree(Display* display, Window window, int indent) { +void log_tree(Aswm* aswm, Window window, int indent) { + XWindowAttributes window_attributes; + XGetWindowAttributes(aswm->display, window, &window_attributes); for(int i = 0; i < indent; i++) printf(" "); - printf("%lu\n", window); + printf("%lu ", window); + if(window == XDefaultRootWindow(aswm->display)) { + printf("(X root) "); + } else if (window == aswm->root_window) { + printf("(ASWM root) "); + } else if (window == aswm->current_workspace) { + printf("(current workspace) "); + } + printf("at (%i, %i): %ix%i\n", window_attributes.x, window_attributes.y, window_attributes.width, window_attributes.height); Window root_window; Window parent_tile; Window* children; unsigned int children_count; - XQueryTree(display, window, &root_window, &parent_tile, &children, &children_count); + XQueryTree(aswm->display, window, &root_window, &parent_tile, &children, &children_count); for(auto i = 0; i < children_count; i++) - log_tree(display, children[i], indent+1); + log_tree(aswm, children[i], indent+1); XFree(children); } diff --git a/src/aswm/log/log.h b/src/aswm/log/log.h index e58014a..1253e34 100644 --- a/src/aswm/log/log.h +++ b/src/aswm/log/log.h @@ -2,7 +2,8 @@ #define ASWM_LOG #include +#include "aswm/aswm.h" -void log_tree(Display* display, Window root, int indent); +void log_tree(Aswm* aswm, Window window, int indent); #endif From b96dd304be9c1e83231c451d9f809603ab4bdf60 Mon Sep 17 00:00:00 2001 From: Paul Breugnot Date: Sat, 4 Jan 2025 17:07:16 +0100 Subject: [PATCH 9/9] :sparkles: Basic handle of WM_NORMAL_HINTS property change This notably allows to display the Undertale window launched from Steam as expected. --- src/aswm/aswm.c | 32 +++++-- src/aswm/aswm.h | 12 ++- src/aswm/event_handlers/create.c | 2 +- src/aswm/event_handlers/map.c | 33 ++++--- src/aswm/event_handlers/property.c | 12 ++- src/aswm/event_handlers/unmap.c | 8 +- src/aswm/log/error.h | 2 +- src/aswm/log/log.c | 6 +- src/aswm/mapper/mapper.c | 147 ++++++++++++++++++++++++++++- src/aswm/mapper/mapper.h | 11 ++- 10 files changed, 228 insertions(+), 37 deletions(-) diff --git a/src/aswm/aswm.c b/src/aswm/aswm.c index 129d50d..5a27318 100644 --- a/src/aswm/aswm.c +++ b/src/aswm/aswm.c @@ -17,8 +17,8 @@ Aswm _aswm; void aswm_open() { _aswm.display = XOpenDisplay(NULL); - _aswm.workspaces = malloc(0); - _aswm.workspace_count = 0; + _aswm.desktops = malloc(0); + _aswm.desktops_count = 0; XSetErrorHandler(aswm_error_handler); @@ -53,22 +53,34 @@ void aswm_open() { } void aswm_create_workspace() { - _aswm.workspaces = realloc(_aswm.workspaces, (_aswm.workspace_count+1) * sizeof(Window)); + _aswm.desktops = realloc(_aswm.desktops, (_aswm.desktops_count+1) * sizeof(Desktop)); XWindowAttributes root_attributes; XGetWindowAttributes(_aswm.display, _aswm.root_window, &root_attributes); - _aswm.workspaces[_aswm.workspace_count] = XCreateSimpleWindow( + _aswm.desktops[_aswm.desktops_count].root_window = XCreateSimpleWindow( _aswm.display, _aswm.root_window, 0, 0, root_attributes.width, root_attributes.height, 0, 0, 0); + _aswm.desktops[_aswm.desktops_count].tile_root = XCreateSimpleWindow( + _aswm.display, _aswm.desktops[_aswm.desktops_count].root_window, + 0, 0, + root_attributes.width, root_attributes.height, + 0, 0, 0); + _aswm.desktops[_aswm.desktops_count].stack_root = XCreateSimpleWindow( + _aswm.display, _aswm.desktops[_aswm.desktops_count].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); + _aswm.current_desktop = _aswm.desktops[_aswm.desktops_count]; + _aswm.desktops_count++; + XSelectInput(_aswm.display, _aswm.current_desktop.tile_root, SubstructureNotifyMask); + XSelectInput(_aswm.display, _aswm.current_desktop.stack_root, SubstructureNotifyMask); + XMapWindow(_aswm.display, _aswm.current_desktop.root_window); + XMapWindow(_aswm.display, _aswm.current_desktop.tile_root); + printf("Create workspace %lu\n", _aswm.current_desktop.root_window); } void aswm_event_loop() { @@ -139,7 +151,7 @@ void aswm_event_loop() { } void aswm_close() { - free(_aswm.workspaces); + free(_aswm.desktops); XCloseDisplay(_aswm.display); } diff --git a/src/aswm/aswm.h b/src/aswm/aswm.h index 98e97d3..ce54b78 100644 --- a/src/aswm/aswm.h +++ b/src/aswm/aswm.h @@ -3,12 +3,18 @@ #include "mapper/mapper.h" +typedef struct { + Window root_window; + Window tile_root; + Window stack_root; +} Desktop; + typedef struct { Display *display; Window root_window; - Window* workspaces; - unsigned int workspace_count; - Window current_workspace; + Desktop* desktops; + unsigned int desktops_count; + Desktop current_desktop; } Aswm; void aswm_open(); diff --git a/src/aswm/event_handlers/create.c b/src/aswm/event_handlers/create.c index a3e16df..ee0d56c 100644 --- a/src/aswm/event_handlers/create.c +++ b/src/aswm/event_handlers/create.c @@ -35,6 +35,6 @@ void OnCreateNotify(Aswm* aswm, const XCreateWindowEvent* e) { XFree(class.res_name); } } - if(aswm->workspace_count == 0) + if(aswm->desktops_count == 0) aswm_create_workspace(); } diff --git a/src/aswm/event_handlers/map.c b/src/aswm/event_handlers/map.c index ba74771..60d940a 100644 --- a/src/aswm/event_handlers/map.c +++ b/src/aswm/event_handlers/map.c @@ -1,27 +1,30 @@ #include "map.h" #include #include "aswm/log/log.h" +#include "aswm/mapper/mapper.h" #include "X11/Xutil.h" void OnMapRequest(Aswm* aswm, const XMapRequestEvent* e) { printf("XMapRequestEvent %lu\n", e->window); - /* XSizeHints* size = XAllocSizeHints(); */ - /* long supplied; */ - /* if(XGetWMNormalHints(aswm->display, e->window, size, &supplied)) { */ - /* printf("\tWMNormalHints:\n"); */ - /* printf("\t\tMin w:%i\n", size->min_width); */ - /* 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); */ - /* printf("\t\tBase w:%i\n", size->base_width); */ - /* printf("\t\tBase h:%i\n", size->base_height); */ - /* } */ - /* XFree(size); */ + XSizeHints* size = XAllocSizeHints(); + long supplied; + bool stacked = false; + if(XGetWMNormalHints(aswm->display, e->window, size, &supplied)) { + if((supplied & (PMinSize | PMaxSize)) && !aswm_is_resizeable(size)) { + printf("\tWMNormalHints:\n"); + printf("\t\tMin w:%i\n", size->min_width); + 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); + aswm_stack_window(e->display, aswm->current_desktop.stack_root, e->window, size->max_width, size->max_height); + stacked = true; + } + } + XFree(size); - // Assumes map requests are always received on root and adressed to the - // current workspace - aswm_map_new_window(e->display, aswm->current_workspace, e->window); + if(!stacked) + aswm_tile_window(e->display, aswm->current_desktop.tile_root, e->window); printf("Map tree:\n"); log_tree(aswm, DefaultRootWindow(aswm->display), 1); diff --git a/src/aswm/event_handlers/property.c b/src/aswm/event_handlers/property.c index fc628b2..be4c4db 100644 --- a/src/aswm/event_handlers/property.c +++ b/src/aswm/event_handlers/property.c @@ -3,12 +3,14 @@ #include #include #include +#include "aswm/mapper/mapper.h" void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e) { - printf("Unhandled property change for window %lu:\n", e->window); + printf("Property change for window %lu:\n", e->window); char* property_name = XGetAtomName(e->display, e->atom); printf("\t%s\n", property_name); if(strcmp(property_name, "_NET_WM_NAME") == 0) { + printf("\t-> Unhandled\n"); XTextProperty wm_name_property; char** wm_name; int wm_name_count; @@ -18,6 +20,7 @@ void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e) { printf("\t_NET_WM_NAME: %s\n", wm_name[i]); XFreeStringList(wm_name); } else if(strcmp(property_name, "WM_NAME") == 0) { + printf("\t-> Unhandled\n"); XTextProperty text_property; if(XGetWMName(e->display, e->window, &text_property)) { printf("\tWM_NAME: %s\n", text_property.value); @@ -33,9 +36,13 @@ void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e) { printf("\t\tMax h:%i\n", size->max_height); printf("\t\tBase w:%i\n", size->base_width); printf("\t\tBase h:%i\n", size->base_height); + aswm_handle_normal_hints( + aswm->display, aswm->current_desktop.stack_root, + e->window, size, supplied); } XFree(size); } else if(strcmp(property_name, "WM_HINTS") == 0) { + printf("\t-> Unhandled\n"); XWMHints* hints = XGetWMHints(aswm->display, e->window); printf("\t\tflags Input: %li\n", hints->flags & InputHint); printf("\t\tflags Input: %li\n", hints->flags & StateHint); @@ -49,6 +56,7 @@ void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e) { printf("\t\tinitial_state: %i\n", hints->initial_state); XFree(hints); } else if(strcmp(property_name, "WM_PROTOCOLS") == 0) { + printf("\t-> Unhandled\n"); Atom* protocols; int count; XGetWMProtocols(aswm->display, e->window, &protocols, &count); @@ -63,6 +71,8 @@ void OnPropertyNotify(Aswm* aswm, XPropertyEvent* e) { printf("\n"); XFree(protocols); + } else { + printf("\t-> Unhandled\n"); } XFree(property_name); } diff --git a/src/aswm/event_handlers/unmap.c b/src/aswm/event_handlers/unmap.c index 6af785b..c70bad2 100644 --- a/src/aswm/event_handlers/unmap.c +++ b/src/aswm/event_handlers/unmap.c @@ -16,7 +16,13 @@ void OnUnmapNotify(Aswm* aswm, const XUnmapEvent* e) { // Unmap should have no effect on not map windows. Currently, all mapped // windows are children of aswm->root_window. if(e->event != XDefaultRootWindow(e->display)) { - aswm_unmap_window(e->display, e->event, e->window); + if(e->event == aswm->current_desktop.tile_root) { + printf("aswm untile\n"); + aswm_untile_window(e->display, e->event, e->window); + } else if(e->event == aswm->current_desktop.stack_root) { + printf("aswm unstack\n"); + aswm_unstack_window(e->display, e->event); + } printf("Unmap tree:\n"); log_tree(aswm, DefaultRootWindow(aswm->display), 1); } diff --git a/src/aswm/log/error.h b/src/aswm/log/error.h index d119390..fd00c24 100644 --- a/src/aswm/log/error.h +++ b/src/aswm/log/error.h @@ -8,7 +8,7 @@ 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); + printf("%s\n", message_buffer); return 0; } diff --git a/src/aswm/log/log.c b/src/aswm/log/log.c index e267eca..0cdebac 100644 --- a/src/aswm/log/log.c +++ b/src/aswm/log/log.c @@ -11,8 +11,12 @@ void log_tree(Aswm* aswm, Window window, int indent) { printf("(X root) "); } else if (window == aswm->root_window) { printf("(ASWM root) "); - } else if (window == aswm->current_workspace) { + } else if (window == aswm->current_desktop.root_window) { printf("(current workspace) "); + } else if (window == aswm->current_desktop.tile_root) { + printf("(tile root) "); + } else if (window == aswm->current_desktop.stack_root) { + printf("(stack root) "); } printf("at (%i, %i): %ix%i\n", window_attributes.x, window_attributes.y, window_attributes.width, window_attributes.height); diff --git a/src/aswm/mapper/mapper.c b/src/aswm/mapper/mapper.c index cbf7e90..70f5049 100644 --- a/src/aswm/mapper/mapper.c +++ b/src/aswm/mapper/mapper.c @@ -2,13 +2,19 @@ #include #include "aswm/mapper/mapper.h" #include "aswm/log/log.h" +#include + +bool aswm_is_resizeable(XSizeHints* size) { + return size->min_width != size->max_width + || size->min_height != size->max_height; +} /** * Returns the free tile built in the split process. */ void h_split(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_tile_window(Display* display, Window current_node, Window window_to_map) { h_split(display, current_node, window_to_map); } @@ -82,7 +88,7 @@ void resize_children_to_parent(Display* display, Window parent) { XFree(children); } -void aswm_unmap_window(Display* display, Window parent, Window window) { +void aswm_untile_window(Display* display, Window parent, Window window) { // +-------------------------------+ // | root | // |+-------------+ +-------------+| @@ -122,3 +128,140 @@ void aswm_unmap_window(Display* display, Window parent, Window window) { resize_children_to_parent(display, parent); } + +/** + * Currently, resize to the max width and max height of any children, but should + * be reconsidered as it can create black square artifacts if the max width and + * height are not for the same window. + */ +void resize_stack(Display* display, Window stack) { + Window root; // unused + Window parent; + Window* children; + unsigned int children_count; + XQueryTree(display, stack, &root, &parent, &children, &children_count); + + unsigned int width = 0; + unsigned int height = 0; + for(auto i = 0; i < children_count; i++) { + XWindowAttributes child_attributes; + XGetWindowAttributes(display, children[i], &child_attributes); + if(child_attributes.map_state != IsUnmapped) { + if(child_attributes.width > width) + width = child_attributes.width; + if(child_attributes.height > height) + height = child_attributes.height; + } + } + + // The current method is always called when there is at least a child, so + // children is never NULL + XFree(children); + + XWindowAttributes parent_attributes; + XGetWindowAttributes(display, parent, &parent_attributes); + XMoveResizeWindow(display, stack, + (parent_attributes.width - width) / 2, + (parent_attributes.height - height) / 2, + width, height); +} + +void aswm_stack_window(Display* display, Window stack, Window window_to_map, int width, int height) { + XWindowAttributes stack_attributes; + XGetWindowAttributes(display, stack, &stack_attributes); + + XSetWindowAttributes stacked_attributes; + stack_attributes.width = width; + stack_attributes.height = height; + stack_attributes.win_gravity = CenterGravity; + XChangeWindowAttributes(display, window_to_map, + CWWidth | CWHeight | CWWinGravity, + &stacked_attributes); + XReparentWindow(display, window_to_map, stack, + (stack_attributes.width - width) / 2, + (stack_attributes.height - height) / 2 + ); + // Map before resize so window_to_map can be taken into account in resizing + // of stack + XMapWindow(display, window_to_map); + + resize_stack(display, stack); + + if(stack_attributes.map_state == IsUnmapped) { + XMapWindow(display, stack); + } +} + +void aswm_unstack_window(Display* display, Window stack) { + Window root; // unused + Window parent; + Window* children; + unsigned int children_count; + XQueryTree(display, stack, &root, &parent, &children, &children_count); + + bool visible_child = false; + int i = 0; + while(!visible_child && i < children_count) { + XWindowAttributes child_attributes; + XGetWindowAttributes(display, children[i], &child_attributes); + visible_child = (child_attributes.map_state == IsViewable); + i++; + } + XFree(children); + + if(visible_child) { + printf("Resize stack %lu\n", stack); + resize_stack(display, stack); + } else { + printf("Nothing in stack, unmap %lu\n", stack); + XWindowAttributes parent_size; + XGetWindowAttributes(display, parent, &parent_size); + + XMoveResizeWindow(display, stack, 0, 0, parent_size.width, parent_size.height); + XUnmapWindow(display, stack); + } +} + +bool is_in_stack(Display* display, Window stack_root, Window window) { + Window _root; // unused + Window parent; + Window* _children; // unused + unsigned int _children_count; // unused + + XQueryTree(display, window, &_root, &parent, &_children, &_children_count); + return parent == stack_root; +} + +void aswm_handle_normal_hints(Display* display, Window stack_root, Window window, XSizeHints* size, long supplied) { + printf("Handle normal hints for %lu\n", window); + XWindowAttributes win_attributes; + XGetWindowAttributes(display, window, &win_attributes); + + if(win_attributes.map_state != IsUnmapped) { + // TODO: handle multiple desktops + if((supplied & (PMinSize | PMaxSize))) { + if(is_in_stack(display, stack_root, window)) { + if(aswm_is_resizeable(size)) { + // Remap to tile root + XUnmapWindow(display, window); + XMapWindow(display, window); + } else { + XResizeWindow(display, window, size->max_width, size->max_height); + resize_stack(display, stack_root); + } + } else { + if(!aswm_is_resizeable(size)) { + // Resize window to its fixed size before stacking it + XResizeWindow(display, window, size->max_width, size->max_height); + + // XMapWindow does not trigger a MapRequest for the current + // client, so we need to call aswm_stack_window directly. + // aswm_stack_window automatically triggers the appropriate + // unmap from tile when reparenting from tile to stack + aswm_stack_window(display, stack_root, window, size->max_width, size->max_height); + } + } + } + } +} + diff --git a/src/aswm/mapper/mapper.h b/src/aswm/mapper/mapper.h index ea82ecd..2e6af65 100644 --- a/src/aswm/mapper/mapper.h +++ b/src/aswm/mapper/mapper.h @@ -2,10 +2,17 @@ #define ASWM_MAPPER #include +#include 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_unmap_window(Display* display, Window parent, Window window); +void aswm_tile_window(Display* display, Window current_node, Window window_to_map); +void aswm_untile_window(Display* display, Window parent, Window window); + +void aswm_stack_window(Display* display, Window stack, Window window_to_map, int width, int height); +void aswm_unstack_window(Display* display, Window stack); +void aswm_handle_normal_hints(Display* display, Window stack_root, Window window, XSizeHints* size, long supplied); + +bool aswm_is_resizeable(XSizeHints* size); #endif