kbgwm

sucklessy floating window manager
git clone https://git.neuralcrash.com/kbgwm.git
Log | Files | Refs | README | LICENSE

commit 2ad34c220c7c4df47061acfacd205450aa8ea315
parent 02240121694046ff7043db702dfb98e0a130e556
Author: Kebigon <git@kebigon.xyz>
Date:   Tue, 10 Aug 2021 13:39:04 +0900

Add basic logging levels

Diffstat:
MMakefile | 2+-
Mclient.c | 20+++++++++++---------
Mevents.c | 3++-
Mkbgwm.c | 161+++++++++++++------------------------------------------------------------------
Alog.c | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alog.h | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mxcbutils.c | 8+++++---
7 files changed, 261 insertions(+), 150 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -OBJ = kbgwm.o xcbutils.o events.o client.o list.o +OBJ = kbgwm.o xcbutils.o events.o client.o list.o log.o CFLAGS+=-g -std=c99 -Wall -Wextra -pedantic -Wstrict-overflow -fno-strict-aliasing -I/usr/local/include -I/usr/X11R6/include -march=native LDFLAGS+=-L/usr/local/lib -L/usr/X11R6/lib -lxcb -lxcb-icccm -lxcb-keysyms diff --git a/client.c b/client.c @@ -18,6 +18,7 @@ #include "client.h" #include "kbgwm.h" #include "list.h" +#include "log.h" #include "xcbutils.h" #include <assert.h> @@ -62,7 +63,7 @@ void client_add_workspace(struct client *client, uint_fast8_t workspace) void client_create(xcb_window_t id) { - printf("client_create: id=%d\n", id); + LOG_DEBUG_VA("client_create: id=%d", id); // Request the information for the window @@ -96,11 +97,12 @@ void client_create(xcb_window_t id) client_sanitize_dimensions(new_client); - printf("new window: id=%d x=%d y=%d width=%d height=%d min_width=%d min_height=%d max_width=%d " - "max_height=%d\n", - id, new_client->x, new_client->y, new_client->width, new_client->height, - new_client->min_width, new_client->min_height, new_client->max_width, - new_client->max_height); + LOG_DEBUG_VA( + "new window: id=%d x=%d y=%d width=%d height=%d min_width=%d min_height=%d max_width=%d " + "max_height=%d", + id, new_client->x, new_client->y, new_client->width, new_client->height, + new_client->min_width, new_client->min_height, new_client->max_width, + new_client->max_height); xcb_configure_window( c, id, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_BORDER_WIDTH, @@ -116,7 +118,7 @@ void client_create(xcb_window_t id) client_add(new_client); focus_apply(); - printf("client_create: done\n"); + LOG_DEBUG("client_create: done"); } // Find a client in the current workspace list @@ -200,7 +202,7 @@ void client_sanitize_dimensions(struct client *client) void client_kill(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: client_kill ]=======\n"); + LOG_DEBUG("=======[ user action: client_kill ]======="); struct item *item = list_head(&workspaces[current_workspace]); @@ -219,7 +221,7 @@ void client_kill(__attribute__((unused)) const union Arg *arg) void client_toggle_maximize(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: client_toggle_maximize ]=======\n"); + LOG_DEBUG("=======[ user action: client_toggle_maximize ]======="); struct item *item = list_head(&workspaces[current_workspace]); diff --git a/events.c b/events.c @@ -19,6 +19,7 @@ #include "client.h" #include "kbgwm.h" #include "list.h" +#include "log.h" #include "xcbutils.h" #include <assert.h> @@ -253,7 +254,7 @@ void handle_event(xcb_generic_event_t *event) { void (*event_handler)(xcb_generic_event_t *) = event_handlers[event->response_type & ~0x80]; if (event_handler == NULL) - printf("Received unhandled event, response type %d\n", event->response_type & ~0x80); + LOG_INFO_VA("Received unhandled event, response type %d", event->response_type & ~0x80); else event_handler(event); } diff --git a/kbgwm.c b/kbgwm.c @@ -18,6 +18,7 @@ #include "kbgwm.h" #include "events.h" #include "list.h" +#include "log.h" #include "xcbutils.h" #include <X11/keysym.h> #include <assert.h> @@ -47,121 +48,9 @@ xcb_atom_t wm_delete_window; uint_fast8_t current_workspace = 0; struct item *workspaces[NB_WORKSPACES]; -static inline void debug_print_globals() -{ - printf("current_workspace=%d\n", current_workspace); - - struct item *item; - struct client *client; - - for (uint_fast8_t workspace = 0; workspace != workspaces_length; workspace++) - { - if ((item = list_head(&workspaces[workspace])) != NULL) - { - do - { - client = item->data; - printf("%d\tid=%d x=%d y=%d width=%d height=%d min_width=%d min_height=%d " - "max_width=%d max_height=%d\n", - workspace, client->id, client->x, client->y, client->width, client->height, - client->min_width, client->min_height, client->max_width, - client->max_height); - } while ((item = item->next) != NULL); - } - else - printf("%d\tNULL\n", workspace); - } -} - -static void debug_print_event(xcb_generic_event_t *event) -{ - switch (event->response_type & ~0x80) - { - case XCB_KEY_PRESS: { - xcb_key_press_event_t *event2 = (xcb_key_press_event_t *)event; - printf("=======[ event: XCB_KEY_PRESS ]=======\n"); - printf("keycode=%d modifiers=%d\n", event2->detail, event2->state); - debug_print_globals(); - break; - } - case XCB_BUTTON_PRESS: { - xcb_button_press_event_t *event2 = (xcb_button_press_event_t *)event; - printf("=======[ event: XCB_BUTTON_PRESS ]=======\n"); - printf("window=%d child=%d modifiers=%d button=%d\n", event2->event, event2->child, - event2->state, event2->detail); - debug_print_globals(); - break; - } - case XCB_BUTTON_RELEASE: { - printf("=======[ event: XCB_BUTTON_RELEASE ]=======\n"); - debug_print_globals(); - break; - } - case XCB_MOTION_NOTIFY: { - xcb_motion_notify_event_t *event2 = (xcb_motion_notify_event_t *)event; - printf("=======[ event: XCB_MOTION_NOTIFY ]=======\n"); - printf("root_x=%d root_y=%d event_x=%d event_y=%d\n", event2->root_x, event2->root_y, - event2->event_x, event2->event_y); - debug_print_globals(); - break; - } - case XCB_DESTROY_NOTIFY: { - xcb_destroy_notify_event_t *event2 = (xcb_destroy_notify_event_t *)event; - printf("=======[ event: XCB_DESTROY_NOTIFY ]=======\n"); - printf("window=%d\n", event2->window); - debug_print_globals(); - break; - } - case XCB_UNMAP_NOTIFY: { - xcb_unmap_notify_event_t *event2 = (xcb_unmap_notify_event_t *)event; - printf("=======[ event: XCB_UNMAP_NOTIFY ]=======\n"); - printf("window=%d event=%d from_configure=%d send_event=%d\n", event2->window, - event2->event, event2->from_configure, event->response_type & 0x80); - debug_print_globals(); - break; - } - case XCB_MAP_NOTIFY: { - xcb_map_notify_event_t *event2 = (xcb_map_notify_event_t *)event; - printf("=======[ event: XCB_MAP_NOTIFY ]=======\n"); - printf("window=%d\n", event2->window); - debug_print_globals(); - break; - } - case XCB_MAP_REQUEST: { - xcb_map_request_event_t *event2 = (xcb_map_request_event_t *)event; - printf("=======[ event: XCB_MAP_REQUEST ]=======\n"); - printf("parent %d window %d\n", event2->parent, event2->window); - debug_print_globals(); - break; - } - case XCB_CONFIGURE_REQUEST: { - xcb_configure_request_event_t *event2 = (xcb_configure_request_event_t *)event; - printf("=======[ event: XCB_CONFIGURE_REQUEST ]=======\n"); - printf("parent %d window %d\n", event2->parent, event2->window); - debug_print_globals(); - break; - } - case XCB_MAPPING_NOTIFY: { - xcb_mapping_notify_event_t *event2 = (xcb_mapping_notify_event_t *)event; - printf("=======[ event: XCB_MAPPING_NOTIFY ]=======\n"); - printf("sequence %d\n", event2->sequence); - printf("request %d\n", event2->request); - printf("first_keycode %d\n", event2->first_keycode); - printf("count %d\n", event2->count); - debug_print_globals(); - break; - } - default: { - printf("=======[ event: unlogged, response type %d ]=======\n", - event->response_type & ~0x80); - break; - } - } -} - void mousemove(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: mousemove ]=======\n"); + LOG_DEBUG("=======[ user action: mousemove ]======="); moving = true; xcb_grab_pointer( @@ -172,7 +61,7 @@ void mousemove(__attribute__((unused)) const union Arg *arg) void mouseresize(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: mouseresize ]=======\n"); + LOG_DEBUG("=======[ user action: mouseresize ]======="); resizing = true; xcb_grab_pointer( @@ -186,19 +75,19 @@ void eventLoop() while (running) { xcb_generic_event_t *event = xcb_wait_for_event(c); - debug_print_event(event); + log_debug_event(event); handle_event(event); free(event); - printf("=======[ event: DONE ]=======\n\n"); + LOG_DEBUG("=======[ event: DONE ]=======\n"); } } void start(const union Arg *arg) { - printf("=======[ user action: start ]=======\n"); - printf("cmd %s\n", arg->cmd[0]); + LOG_DEBUG("=======[ user action: start ]======="); + LOG_DEBUG_VA("cmd %s", arg->cmd[0]); if (fork() == 0) { @@ -237,14 +126,14 @@ void focus_apply() client_grab_buttons(client, true); xcb_flush(c); - printf("focus_apply: done\n"); + LOG_DEBUG("focus_apply: done"); } // Focus the next client in the current workspace list // arg->b : reverse mode void focus_next(const union Arg *arg) { - printf("=======[ user action: focus_next ]=======\n"); + LOG_DEBUG("=======[ user action: focus_next ]======="); // No clients in the current workspace list // Only one client in the current workspace list @@ -283,7 +172,7 @@ void focus_unfocus() void quit(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: quit ]=======\n"); + LOG_DEBUG("=======[ user action: quit ]======="); running = false; } @@ -298,14 +187,14 @@ void setup_keyboard() xcb_get_modifier_mapping_reply(c, xcb_get_modifier_mapping_unchecked(c), NULL); if (!reply) { - printf("Unable to retrieve midifier mapping"); + LOG_ERROR("Unable to retrieve midifier mapping"); exit(-1); } xcb_keycode_t *modmap = xcb_get_modifier_mapping_keycodes(reply); if (!modmap) { - printf("Unable to retrieve midifier mapping keycodes"); + LOG_ERROR("Unable to retrieve midifier mapping keycodes"); exit(-1); } @@ -321,7 +210,7 @@ void setup_keyboard() if (keycode == *numlock) { numlockmask = (1 << i); - printf("numlock is %d", keycode); + LOG_DEBUG_VA("numlock is %d", keycode); } } } @@ -335,7 +224,7 @@ void setup_screen() xcb_query_tree_reply_t *reply = xcb_query_tree_reply(c, xcb_query_tree(c, screen->root), 0); if (NULL == reply) { - printf("Unable to retrieve the root window's children"); + LOG_ERROR("Unable to retrieve the root window's children"); exit(-1); } @@ -355,30 +244,30 @@ void setup_screen() void workspace_change(const union Arg *arg) { - printf("=======[ user action: workspace_change ]=======\n"); - printf("i=%d\n", arg->i); + LOG_DEBUG("=======[ user action: workspace_change ]======="); + LOG_DEBUG_VA("i=%d", arg->i); workspace_set(arg->i); } void workspace_next(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: workspace_next ]=======\n"); + LOG_DEBUG("=======[ user action: workspace_next ]======="); workspace_set(current_workspace + 1 == workspaces_length ? 0 : current_workspace + 1); } void workspace_previous(__attribute__((unused)) const union Arg *arg) { - printf("=======[ user action: workspace_previous ]=======\n"); + LOG_DEBUG("=======[ user action: workspace_previous ]======="); workspace_set(current_workspace == 0 ? workspaces_length - 1 : current_workspace - 1); } void workspace_send(const union Arg *arg) { - printf("=======[ user action: workspace_send ]=======\n"); - printf("i=%d\n", arg->i); - debug_print_globals(); + LOG_DEBUG("=======[ user action: workspace_send ]======="); + LOG_DEBUG_VA("i=%d", arg->i); + log_debug_globals(); uint_fast8_t new_workspace = arg->i; @@ -390,12 +279,12 @@ void workspace_send(const union Arg *arg) xcb_unmap_window(c, client->id); xcb_flush(c); - printf("workspace_send: done\n"); + LOG_DEBUG("workspace_send: done"); } void workspace_set(uint_fast8_t new_workspace) { - printf("workspace_set: old=%d new=%d\n", current_workspace, new_workspace); + LOG_DEBUG_VA("workspace_set: old=%d new=%d", current_workspace, new_workspace); if (current_workspace == new_workspace) return; // Nothing to be done @@ -426,7 +315,7 @@ void workspace_set(uint_fast8_t new_workspace) if (!list_is_empty(&workspaces[current_workspace])) focus_apply(); - printf("workspace_set: done\n"); + LOG_DEBUG("workspace_set: done"); } /* @@ -443,7 +332,7 @@ int main(void) if (xcb_connection_has_error(c)) { - printf("xcb_connect failed: %d", xcb_connection_has_error(c)); + LOG_ERROR_VA("xcb_connect failed: %d", xcb_connection_has_error(c)); exit(1); } diff --git a/log.c b/log.c @@ -0,0 +1,148 @@ +/* + * kbgwm, a sucklessy floating window manager + * Copyright (c) 2020-2021, Kebigon <git@kebigon.xyz> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "log.h" + +#if LOG_LEVEL < 4 + +void log_debug_globals() +{ +} + +void log_debug_event(__attribute__((unused)) xcb_generic_event_t *event) +{ +} + +#else + +#include "client.h" +#include "kbgwm.h" +#include "list.h" + +void log_debug_globals() +{ + LOG_DEBUG_VA("current_workspace=%d", current_workspace); + + struct item *item; + struct client *client; + + for (uint_fast8_t workspace = 0; workspace != workspaces_length; workspace++) + { + if ((item = list_head(&workspaces[workspace])) != NULL) + { + do + { + client = item->data; + LOG_DEBUG_VA("%d\tid=%d x=%d y=%d width=%d height=%d min_width=%d min_height=%d " + "max_width=%d max_height=%d", + workspace, client->id, client->x, client->y, client->width, + client->height, client->min_width, client->min_height, + client->max_width, client->max_height); + } while ((item = item->next) != NULL); + } + else + LOG_DEBUG_VA("%d\tNULL", workspace); + } +} + +void log_debug_event(xcb_generic_event_t *event) +{ + switch (event->response_type & ~0x80) + { + case XCB_KEY_PRESS: { + xcb_key_press_event_t *event2 = (xcb_key_press_event_t *)event; + LOG_DEBUG("=======[ event: XCB_KEY_PRESS ]======="); + LOG_DEBUG_VA("keycode=%d modifiers=%d", event2->detail, event2->state); + log_debug_globals(); + break; + } + case XCB_BUTTON_PRESS: { + xcb_button_press_event_t *event2 = (xcb_button_press_event_t *)event; + LOG_DEBUG("=======[ event: XCB_BUTTON_PRESS ]======="); + LOG_DEBUG_VA("window=%d child=%d modifiers=%d button=%d", event2->event, event2->child, + event2->state, event2->detail); + log_debug_globals(); + break; + } + case XCB_BUTTON_RELEASE: { + LOG_DEBUG("=======[ event: XCB_BUTTON_RELEASE ]======="); + log_debug_globals(); + break; + } + case XCB_MOTION_NOTIFY: { + xcb_motion_notify_event_t *event2 = (xcb_motion_notify_event_t *)event; + LOG_DEBUG("=======[ event: XCB_MOTION_NOTIFY ]======="); + LOG_DEBUG_VA("root_x=%d root_y=%d event_x=%d event_y=%d", event2->root_x, event2->root_y, + event2->event_x, event2->event_y); + log_debug_globals(); + break; + } + case XCB_DESTROY_NOTIFY: { + xcb_destroy_notify_event_t *event2 = (xcb_destroy_notify_event_t *)event; + LOG_DEBUG("=======[ event: XCB_DESTROY_NOTIFY ]======="); + LOG_DEBUG_VA("window=%d", event2->window); + log_debug_globals(); + break; + } + case XCB_UNMAP_NOTIFY: { + xcb_unmap_notify_event_t *event2 = (xcb_unmap_notify_event_t *)event; + LOG_DEBUG("=======[ event: XCB_UNMAP_NOTIFY ]======="); + LOG_DEBUG_VA("window=%d event=%d from_configure=%d send_event=%d", event2->window, + event2->event, event2->from_configure, event->response_type & 0x80); + log_debug_globals(); + break; + } + case XCB_MAP_NOTIFY: { + xcb_map_notify_event_t *event2 = (xcb_map_notify_event_t *)event; + LOG_DEBUG("=======[ event: XCB_MAP_NOTIFY ]======="); + LOG_DEBUG_VA("window=%d", event2->window); + log_debug_globals(); + break; + } + case XCB_MAP_REQUEST: { + xcb_map_request_event_t *event2 = (xcb_map_request_event_t *)event; + LOG_DEBUG("=======[ event: XCB_MAP_REQUEST ]======="); + LOG_DEBUG_VA("parent %d window %d", event2->parent, event2->window); + log_debug_globals(); + break; + } + case XCB_CONFIGURE_REQUEST: { + xcb_configure_request_event_t *event2 = (xcb_configure_request_event_t *)event; + LOG_DEBUG("=======[ event: XCB_CONFIGURE_REQUEST ]======="); + LOG_DEBUG_VA("parent %d window %d", event2->parent, event2->window); + log_debug_globals(); + break; + } + case XCB_MAPPING_NOTIFY: { + xcb_mapping_notify_event_t *event2 = (xcb_mapping_notify_event_t *)event; + LOG_DEBUG("=======[ event: XCB_MAPPING_NOTIFY ]======="); + LOG_DEBUG_VA("sequence %d", event2->sequence); + LOG_DEBUG_VA("request %d", event2->request); + LOG_DEBUG_VA("first_keycode %d", event2->first_keycode); + LOG_DEBUG_VA("count %d", event2->count); + log_debug_globals(); + break; + } + default: { + LOG_DEBUG_VA("=======[ event: unlogged, response type %d ]=======", + event->response_type & ~0x80); + break; + } + } +} + +#endif diff --git a/log.h b/log.h @@ -0,0 +1,69 @@ +/* + * kbgwm, a sucklessy floating window manager + * Copyright (c) 2020-2021, Kebigon <git@kebigon.xyz> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#pragma once + +#include <stdio.h> +#include <xcb/xcb.h> + +/* + * Logging level: + * - 1: ERROR + * - 2: WARN + * - 3: INFO + * - 4: DEBUG + */ +#define LOG_LEVEL 4 + +#if LOG_LEVEL >= 4 +#define LOG_DEBUG(message) printf("[%s:%d] DEBUG: " message "\n", __FILE__, __LINE__) +#define LOG_DEBUG_VA(message, ...) \ + printf("[%s:%d] DEBUG: " message "\n", __FILE__, __LINE__, __VA_ARGS__) +#else +#define LOG_DEBUG(message) +#define LOG_DEBUG_VA(message, ...) +#endif + +#if LOG_LEVEL >= 3 +#define LOG_WARN(message) printf("[%s:%d] WARN: " message "\n", __FILE__, __LINE__) +#define LOG_WARN_VA(message, ...) \ + printf("[%s:%d] WARN: " message "\n", __FILE__, __LINE__, __VA_ARGS__) +#else +#define LOG_WARN(message) +#define LOG_WARN_VA(message, ...) +#endif + +#if LOG_LEVEL >= 2 +#define LOG_INFO(message) printf("[%s:%d] INFO: " message "\n", __FILE__, __LINE__) +#define LOG_INFO_VA(message, ...) \ + printf("[%s:%d] INFO: " message "\n", __FILE__, __LINE__, __VA_ARGS__) +#else +#define LOG_INFO(message) +#define LOG_INFO_VA(message, ...) +#endif + +#if LOG_LEVEL >= 1 +#define LOG_ERROR(message) printf("[%s:%d] ERROR: " message "\n", __FILE__, __LINE__) +#define LOG_ERROR_VA(message, ...) \ + printf("[%s:%d] ERROR: " message "\n", __FILE__, __LINE__, __VA_ARGS__) +#else +#define LOG_ERROR(message) +#define LOG_ERROR_VA(message, ...) +#endif + +void log_debug_globals(); +void log_debug_event(xcb_generic_event_t *); diff --git a/xcbutils.c b/xcbutils.c @@ -24,6 +24,8 @@ #include <xcb/xcb_icccm.h> #include <xcb/xproto.h> +#include "log.h" + extern xcb_connection_t *c; extern xcb_window_t root; extern uint16_t numlockmask; @@ -33,11 +35,11 @@ void *emalloc(size_t size) { void *p; - printf("emalloc size=%zu\n", size); + LOG_DEBUG_VA("emalloc size=%zu", size); if (!(p = malloc(size))) { - printf("Out of memory"); + LOG_ERROR("Out of memory"); exit(-1); } @@ -53,7 +55,7 @@ void xcb_register_key_events(struct Key key) xcb_keycode_t *keycodes; xcb_keycode_t keycode; - printf("Registering key press event for key %d / key %d\n", key.modifiers, key.keysym); + LOG_DEBUG_VA("Registering key press event for key %d / key %d", key.modifiers, key.keysym); keycodes = xcb_get_keycodes(key.keysym); uint16_t modifiers[] = {0, numlockmask, XCB_MOD_MASK_LOCK, numlockmask | XCB_MOD_MASK_LOCK};