Skip to content

Commit

Permalink
Moved setclient(netstate/wtype) to getprop.c/PropListen, Added PropLi…
Browse files Browse the repository at this point in the history
…sten Arg (#384)
  • Loading branch information
DerjenigeUberMensch authored Aug 3, 2024
1 parent cd3b4ff commit a9f8452
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 132 deletions.
31 changes: 31 additions & 0 deletions include/getprop.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,45 @@ enum PropertyType
PropManage,
PropUnmanage,

/* Net State Setters */
PropSetWtype,
PropUnsetWtype,
PropSetWState,
PropUnsetWState,

/* special */
PropExitThread,
PropLAST,
};

typedef union PropArg PropArg;

union PropArg
{
void *v;
void **vv;

/* char */
int8_t c[8];
uint8_t uc[8];

/* short */
int16_t s[4];
uint16_t us[4];

/* int */
int32_t i[2];
uint32_t ui[2];

/* long */
int64_t l[1];
uint64_t ul[1];
};

void PropInit(void);
void PropDestroy(void);
void PropListen(XCBDisplay *display, XCBWindow win, enum PropertyType type);
void PropListenArg(XCBDisplay *display, XCBWindow win, enum PropertyType type, PropArg arg);


#endif
1 change: 1 addition & 0 deletions include/x.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ uint8_t checksticky(int64_t x);
char *getnamefromreply(XCBWindowProperty *namerep);
/* Gets the icon property from the specified XCBWindowProperty. */
uint32_t *geticonprop(XCBWindowProperty *iconreply);
void XCBSetAtomState(XCBDisplay *display, XCBWindow win, XCBAtom type, XCBAtom atom, XCBWindowProperty *prop, uint8_t _delete);
char *GetAtomNameQuick(XCBDisplay *display, XCBAtom atom);

#endif
140 changes: 11 additions & 129 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "main.h"
#include "keybinds.h"
#include "hashing.h"
#include "getprop.h"

#include <string.h>

Expand Down Expand Up @@ -1358,146 +1359,27 @@ setclientstate(Client *c, u8 state)
void
setclientwtype(Client *c, XCBAtom atom, u8 state)
{
/* TODO manage race conditions without needing to lock the server */
const u8 _delete = !state;
const XCBWindow win = c->win;
const u32 NO_BYTE_OFFSET = 0L;
const u32 REQUEST_MAX_NEEDED_ITEMS = UINT32_MAX;

XCBCookie cookie = XCBGetWindowPropertyCookie(_wm.dpy, win, netatom[NetWMWindowType], NO_BYTE_OFFSET, REQUEST_MAX_NEEDED_ITEMS, False, XCB_ATOM_ATOM);
XCBWindowProperty *prop = XCBGetWindowPropertyReply(_wm.dpy, cookie);
void *data = NULL;
u32 len = 0;
u32 propmode = XCB_PROP_MODE_REPLACE;
if(prop)
{
XCBAtom *atoms = XCBGetPropertyValue(prop);
uint32_t ATOM_LENGTH = 0;
XCBGetPropertyValueLength(prop, sizeof(XCBAtom), &ATOM_LENGTH);

u32 i;
u32 offset = 0;
u8 set = 0;
for(i = 0; i < ATOM_LENGTH; ++i)
{
if(atoms[i] == atom)
{
offset = i;
set = 1;
break;
}
}

if(set)
{
if(_delete)
{
for(i = offset; i < ATOM_LENGTH - 1; ++i)
{ atoms[i] = atoms[i + 1];
}
data = atoms;
len = ATOM_LENGTH - 1;
}
else /* atom already exists do nothing */
{ goto CLEANUP;
}
}
else
{
if(_delete) /* prop not found mark as already deleted */
{ goto CLEANUP;
}
else /* set propmode to append cause we didnt find it */
{
propmode = XCB_PROP_MODE_APPEND;
len = 1;
data = &atom;
}
}
PropArg arg;
arg.ui[0] = atom;
if(state)
{ PropListenArg(_wm.dpy, c->win, PropSetWtype, arg);
}
else
{
len = 1;
data = &atom;
{ PropListenArg(_wm.dpy, c->win, PropUnsetWtype, arg);
}
XCBChangeProperty(_wm.dpy, win, netatom[NetWMWindowType], XCB_ATOM_ATOM, 32, propmode, (const char *)data, len);
goto CLEANUP;
CLEANUP:
free(prop);
}

void
setclientnetstate(Client *c, XCBAtom atom, u8 state)
{
/* TODO manage race conditions without needing to lock the server */
const u8 _delete = !state;
const XCBWindow win = c->win;
const u32 NO_BYTE_OFFSET = 0L;
const u32 REQUEST_MAX_NEEDED_ITEMS = UINT32_MAX;

XCBCookie cookie = XCBGetWindowPropertyCookie(_wm.dpy, win, netatom[NetWMState], NO_BYTE_OFFSET, REQUEST_MAX_NEEDED_ITEMS, False, XCB_ATOM_ATOM);
XCBWindowProperty *prop = XCBGetWindowPropertyReply(_wm.dpy, cookie);
void *data = NULL;
u32 len = 0;
u32 propmode = XCB_PROP_MODE_REPLACE;
if(prop)
{
XCBAtom *atoms = XCBGetPropertyValue(prop);
uint32_t ATOM_LENGTH = 0;
XCBGetPropertyValueLength(prop, sizeof(XCBAtom), &ATOM_LENGTH);

u32 i;
u32 offset = 0;
u8 set = 0;
for(i = 0; i < ATOM_LENGTH; ++i)
{
if(atoms[i] == atom)
{
offset = i;
set = 1;
break;
}
}

if(set)
{
if(_delete)
{
/* this gets optimized to memmove, cool!
* GCC v14.1.1 -O3
*/
for(i = offset; i < ATOM_LENGTH - 1; ++i)
{ atoms[i] = atoms[i + 1];
}
data = atoms;
len = ATOM_LENGTH - 1;
}
else /* atom already exists do nothing */
{ goto CLEANUP;
}
}
else
{
if(_delete) /* prop not found mark as already deleted */
{ goto CLEANUP;
}
else /* set propmode to append cause we didnt find it */
{
propmode = XCB_PROP_MODE_APPEND;
len = 1;
data = &atom;
}
}
PropArg arg;
arg.ui[0] = atom;
if(state)
{ PropListenArg(_wm.dpy, c->win, PropSetWState, arg);
}
else
{
len = 1;
data = &atom;
{ PropListenArg(_wm.dpy, c->win, PropUnsetWState, arg);
}
XCBChangeProperty(_wm.dpy, win, netatom[NetWMState], XCB_ATOM_ATOM, 32, propmode, (const char *)data, len);
goto CLEANUP;
CLEANUP:
free(prop);
}

void
Expand Down
7 changes: 4 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,13 @@ restoreclientsession(Desktop *desk, char *buff, u16 len)
XCBWindow WindowId;
XCBWindow WindowIdFocus;
XCBWindow WindowIdStack;
u32 Flags;
u32 _Flags;
u32 __EWMHFlag;
u32 BorderWidth;
u32 BorderColor;

x = y = ox = oy = w = h = ow = oh = WindowId = WindowIdFocus = WindowIdStack = BorderWidth = BorderColor = 0;
_Flags = __EWMHFlag = 0;

check = sscanf(buff,
"(x: %d, y: %d) (w: %u h: %u)" " "
Expand All @@ -574,7 +575,7 @@ restoreclientsession(Desktop *desk, char *buff, u16 len)
&WindowId,
&BorderWidth,
&BorderColor,
&Flags,
&_Flags,
&__EWMHFlag
);

Expand All @@ -600,7 +601,7 @@ restoreclientsession(Desktop *desk, char *buff, u16 len)
cclient->y = y;
cclient->w = w;
cclient->h = h;
cclient->flags = Flags;
cclient->flags = _Flags;
cclient->ewmhflags = __EWMHFlag;
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/x.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,66 @@ geticonprop(XCBWindowProperty *iconreply)
return ret;
}


void
XCBSetAtomState(XCBDisplay *display, XCBWindow win, XCBAtom type, XCBAtom atom, XCBWindowProperty *prop, u8 _delete)
{
if(!prop)
{ return;
}
void *data = NULL;
u32 len = 0;
u32 propmode = XCB_PROP_MODE_REPLACE;
XCBAtom *atoms = XCBGetPropertyValue(prop);
uint32_t ATOM_LENGTH = 0;
XCBGetPropertyValueLength(prop, sizeof(XCBAtom), &ATOM_LENGTH);

u32 i;
u32 offset = 0;
u8 set = 0;
for(i = 0; i < ATOM_LENGTH; ++i)
{
if(memcmp(atoms + i, &atom, sizeof(XCBAtom)))
{
offset = i;
set = 1;
break;
}
}
if(set)
{
if(_delete)
{
data = atoms;
len = ATOM_LENGTH - 1;
/* Check if its not last, and shift every element back so we remove it */
if(offset != len)
{ memmove(&atoms[offset], &atoms[offset + 1], (ATOM_LENGTH - offset) * sizeof(XCBAtom));
}
}
/* atom already exists do nothing */
else
{ return;
}
}
else
{
/* prop not found mark as already deleted */
if(_delete)
{ return;
}
/* set propmode to append cause we didnt find it */
else
{
propmode = XCB_PROP_MODE_APPEND;
len = 1;
data = &atom;
}
}
XCBChangeProperty(display, win, type, XCB_ATOM_ATOM, 32, propmode, (const char *)data, len);
}


char *
GetAtomNameQuick(XCBDisplay *display, XCBAtom atom)
{
Expand Down

0 comments on commit a9f8452

Please sign in to comment.