From 57b48915703080a6eed23379c45c43fea1590176 Mon Sep 17 00:00:00 2001 From: Joseph <119084558+DerjenigeUberMensch@users.noreply.github.com> Date: Tue, 7 Jan 2025 02:06:41 +0800 Subject: [PATCH] Logic and currency Fixes --- include/main.h | 5 ++++ include/safebool.h | 67 ---------------------------------------------- src/client.c | 4 +-- src/main.c | 42 ++++++++++++++++++++--------- src/toggle.c | 39 ++++++++++++++++++++++++++- tools/util.h | 7 +++++ 6 files changed, 81 insertions(+), 83 deletions(-) delete mode 100644 include/safebool.h diff --git a/include/main.h b/include/main.h index 1e60e90..42825ab 100644 --- a/include/main.h +++ b/include/main.h @@ -238,4 +238,9 @@ void NonNull wakeupconnection(XCBDisplay *display, int screen); /* Error handler */ void NonNullArg(1) xerror(XCBDisplay *display, XCBGenericError *error); + +int LOCK_WM(void); +int UNLOCK_WM(void); + + #endif diff --git a/include/safebool.h b/include/safebool.h deleted file mode 100644 index 15f8304..0000000 --- a/include/safebool.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef __SAFE__BOOL__H__ -#define __SAFE__BOOL__H__ -/* prevent redefinitions of header file */ -#ifndef _STDBOOL_H - #define _STDBOOL_H -#endif - -/* undef defs */ -#ifdef bool -#undef bool -#endif -#ifdef true -#undef true -#endif -#ifdef True -#undef True -#endif -#ifdef false -#undef false -#endif -#ifdef False -#undef False -#endif - - -#ifndef Nill -#define Nill ((void *)0) -#endif - -#ifndef NULL -#define NULL ((void *)0) -#endif - -#ifndef null -#define null ((void *)0) -#endif - -#if !defined(__cplusplus) - #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 201710L - /* true and false are keywords */ - #define True true - #define False false - #else - typedef enum { False, True } boolc; - #if !defined(__STDC_VERSION__) - /* - * if not defined then must be c90 or c89 - */ - typedef enum { false, true } bool; - #else - typedef enum { false, true } booll; - typedef _Bool bool; - #endif - #endif -#else - #define True true - #define False false - typedef bool _Bool; -#endif - - -#ifndef __bool_true_false_are_defined -#define __bool_true_false_are_defined (1) -#endif - -/* __H__ */ -#endif diff --git a/src/client.c b/src/client.c index 70cc0cd..f88898b 100644 --- a/src/client.c +++ b/src/client.c @@ -19,8 +19,8 @@ extern XCBAtom motifatom; /* Macro definitions */ -u16 OLDWIDTH(Client *c) { return (c->oldw + (c->bw * 2)); } -u16 OLDHEIGHT(Client *c) { return (c->oldw + (c->bw * 2)); } +u16 OLDWIDTH(Client *c) { return (c->oldw + (c->oldbw * 2)); } +u16 OLDHEIGHT(Client *c) { return (c->oldw + (c->oldbw * 2)); } u16 WIDTH(Client *c) { return (c->w + (c->bw * 2)); } u16 HEIGHT(Client *c) { return (c->h + (c->bw * 2)); } /* Our custom states */ diff --git a/src/main.c b/src/main.c index cf8de70..2522137 100644 --- a/src/main.c +++ b/src/main.c @@ -33,8 +33,28 @@ #include "keybinds.h" /* for HELP/DebugGING see under main() or the bottom */ -extern void (*handler[XCBLASTEvent]) (XCBGenericEvent *); +int LOCK_WM(void) { + extern WM _wm; + int ret = EXIT_SUCCESS; + if(_wm.use_threads) + { ret = pthread_mutex_lock(&_wm.mutex); + } + return ret; + } +int UNLOCK_WM(void) { + extern WM _wm; + int ret = EXIT_SUCCESS; + if(_wm.use_threads) + { ret = pthread_mutex_unlock(&_wm.mutex); + } + return ret; + } + + + + +extern void (*handler[XCBLASTEvent]) (XCBGenericEvent *); WM _wm; UserSettings _cfg; @@ -120,21 +140,17 @@ cleanup(void) void __HOT__ eventhandler(XCBGenericEvent *ev) { - /* TODO: Remove references to other clients, and move _wm.use_threads to run() instead. */ - if(_wm.use_threads) - { pthread_mutex_lock(&_wm.mutex); - } - /* int for speed */ const int cleanev = XCB_EVENT_RESPONSE_TYPE(ev); - /* Debug("%s", XCBGetEventName(cleanev)); */ - if(LENGTH(handler) > cleanev) - { handler[cleanev](ev); - } - /* TODO: Remove references to other clients, and move _wm.use_threads to run() instead. */ - if(_wm.use_threads) - { pthread_mutex_unlock(&_wm.mutex); + if(LENGTH(handler) < cleanev || cleanev <= -1) + { return; } + + LOCK_WM(); + + handler[cleanev](ev); + + UNLOCK_WM(); } void diff --git a/src/toggle.c b/src/toggle.c index 6c6faf8..ae990ca 100644 --- a/src/toggle.c +++ b/src/toggle.c @@ -315,15 +315,20 @@ DragWindow( else { XCBRaiseWindow(_wm.dpy, win); } + XCBFlush(_wm.dpy); XCBGenericEvent *ev = NULL; running = 1; XCBMotionNotifyEvent *mev = NULL; XCBTimestamp lasttime = 0; + + /* Unlock as was previous locked */ + UNLOCK_WM(); do { if(ev) { + LOCK_WM(); eventhandler(ev); switch(XCB_EVENT_RESPONSE_TYPE(ev)) { @@ -381,9 +386,14 @@ DragWindow( } break; } + UNLOCK_WM(); free(ev); } } while(_wm.running && running && !XCBNextEvent(_wm.dpy, &ev)); + + /* relock to prevent race conditions */ + LOCK_WM(); + running = 0; XCBUngrabPointer(_wm.dpy, XCB_CURRENT_TIME); Monitor *m; @@ -403,6 +413,8 @@ DragWindow( arrange(_wm.selmon->desksel); XCBFlush(_wm.dpy); + + /* No unlock as lock previous state was locked, (aka no double lock) */ } void @@ -573,10 +585,15 @@ ResizeWindow(const Arg *arg) ev = NULL; XCBMotionNotifyEvent *mev = NULL; XCBTimestamp lasttime = 0; + + /* Unlock as was previous locked */ + UNLOCK_WM(); + do { if(ev) { + LOCK_WM(); eventhandler(ev); switch(XCB_EVENT_RESPONSE_TYPE(ev)) { @@ -637,9 +654,14 @@ ResizeWindow(const Arg *arg) } break; } + UNLOCK_WM(); free(ev); } } while(_wm.running && running && !XCBNextEvent(_wm.dpy, &ev)); + + /* relock to prevent race conditions */ + LOCK_WM(); + running = 0; XCBUngrabPointer(_wm.dpy, XCB_CURRENT_TIME); Monitor *m; @@ -658,6 +680,7 @@ ResizeWindow(const Arg *arg) } arrange(_wm.selmon->desksel); XCBFlush(_wm.dpy); + /* No unlock as lock previous state was locked, (aka no double lock) */ } void @@ -793,10 +816,14 @@ ResizeWindowAlt(const Arg *arg) running = 1; ev = NULL; XCBMotionNotifyEvent *mev = NULL; + + /* Unlock as was previous locked */ + UNLOCK_WM(); do { if(ev) { + LOCK_WM(); eventhandler(ev); switch(XCB_EVENT_RESPONSE_TYPE(ev)) { @@ -839,9 +866,14 @@ ResizeWindowAlt(const Arg *arg) } break; } + UNLOCK_WM(); free(ev); } } while(_wm.running && running && !XCBNextEvent(_wm.dpy, &ev)); + + /* relock to prevent race conditions */ + LOCK_WM(); + running = 0; XCBUngrabPointer(_wm.dpy, XCB_CURRENT_TIME); Monitor *m; @@ -860,6 +892,7 @@ ResizeWindowAlt(const Arg *arg) } arrange(_wm.selmon->desksel); XCBFlush(_wm.dpy); + /* No unlock as lock previous state was locked, (aka no double lock) */ } @@ -918,7 +951,11 @@ SpawnWindow(const Arg *arg) perror("setsid"); _exit(EXIT_FAILURE); } - + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = SIG_DFL; diff --git a/tools/util.h b/tools/util.h index 515b486..304ff8c 100644 --- a/tools/util.h +++ b/tools/util.h @@ -6,6 +6,8 @@ #include #include +#include "safebool.h" + typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; @@ -16,6 +18,11 @@ typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + typedef int8_t byte; typedef uint8_t ubyte;