Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Nov 2, 2023
1 parent 2b8019b commit 96534dd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 93 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ borders active_color=<color_hex> inactive_color=<color_hex> width=<float>
which determine the color of the currently focused window, the inactive window
and the width of the border respectively. The color hex shall be given in the
format: `0xAARRGGBB`, where `A` is the alpha channel, `R` the red channel,
`B` the blue channel and `B` the blue channel.

Any new user window, spawned after the launch of the `borders` process will
receive a border (TODO: Add borders to all existing windows).
`G` the green channel and `B` the blue channel.

### Bootstrap with yabai
For example, if you are using `yabai`, you could add:
Expand Down
23 changes: 3 additions & 20 deletions src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ extern struct windows g_windows;
extern struct borders g_borders;
extern pid_t g_pid;

bool g_first_time = true;

static void dump_event(void* data, size_t data_length) {
for (int i = 0; i < data_length; i++) {
printf("%02x ", *((unsigned char*)data + i));
Expand Down Expand Up @@ -48,28 +46,13 @@ static void window_spawn_handler(uint32_t event, void* data, size_t data_length,
CFTypeRef iterator = SLSWindowQueryResultCopyWindows(query);
if (iterator && SLSWindowIteratorGetCount(iterator) > 0) {
if (SLSWindowIteratorAdvance(iterator)) {
uint64_t tags = SLSWindowIteratorGetTags(iterator);
uint64_t attributes = SLSWindowIteratorGetAttributes(iterator);
uint32_t parent_wid = SLSWindowIteratorGetParentID(iterator);

if (((parent_wid == 0)
&& ((attributes & 0x2)
|| (tags & 0x400000000000000))
&& (((tags & 0x1))
|| ((tags & 0x2)
&& (tags & 0x80000000))))) {
ITERATOR_WINDOW_SUITABLE(iterator, {
windows_add_window(&g_windows, wid);
borders_add_border(&g_borders, wid, sid);
SLSRequestNotificationsForWindows(cid,
g_windows.wids,
g_windows.num_windows);

borders_add_border(&g_borders, wid, sid);
if (g_first_time) {
g_first_time = false;
borders_remove_border(&g_borders, wid, sid);
borders_add_border(&g_borders, wid, sid);
}
}
});
}
}
if (iterator) CFRelease(iterator);
Expand Down
125 changes: 56 additions & 69 deletions src/windows.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
#include "windows.h"
#include <string.h>

extern bool g_first_time;

void windows_init(struct windows* windows) {
memset(windows, 0, sizeof(struct windows));
}

static uint64_t window_space_id(int cid, uint32_t wid)
{
uint64_t sid = 0;
static uint64_t window_space_id(int cid, uint32_t wid) {
uint64_t sid = 0;

CFArrayRef window_list_ref = cfarray_of_cfnumbers(&wid, sizeof(uint32_t), 1, kCFNumberSInt32Type);
CFArrayRef space_list_ref = SLSCopySpacesForWindows(cid, 0x7, window_list_ref);
if (!space_list_ref) goto err;
CFArrayRef window_list_ref = cfarray_of_cfnumbers(&wid,
sizeof(uint32_t),
1,
kCFNumberSInt32Type);

int count = CFArrayGetCount(space_list_ref);
if (!count) goto free;
CFArrayRef space_list_ref = SLSCopySpacesForWindows(cid,
0x7,
window_list_ref);

CFNumberRef id_ref = CFArrayGetValueAtIndex(space_list_ref, 0);
CFNumberGetValue(id_ref, CFNumberGetType(id_ref), &sid);

free:
if (space_list_ref) {
int count = CFArrayGetCount(space_list_ref);
if (count > 0) {
CFNumberRef id_ref = CFArrayGetValueAtIndex(space_list_ref, 0);
CFNumberGetValue(id_ref, CFNumberGetType(id_ref), &sid);
}
CFRelease(space_list_ref);
err:
CFRelease(window_list_ref);
}
CFRelease(window_list_ref);

if (sid) return sid;
if (sid) return sid;

CFStringRef uuid = SLSCopyManagedDisplayForWindow(cid, wid);
if (uuid) {
uint64_t sid = SLSManagedDisplayGetCurrentSpace(cid, uuid);
CFRelease(uuid);
return sid;
}
CFStringRef uuid = SLSCopyManagedDisplayForWindow(cid, wid);
if (uuid) {
uint64_t sid = SLSManagedDisplayGetCurrentSpace(cid, uuid);
CFRelease(uuid);
return sid;
}

return 0;
return 0;
}

void windows_add_existing_windows(int cid, struct windows* windows, struct borders* borders) {
Expand Down Expand Up @@ -70,7 +72,6 @@ void windows_add_existing_windows(int cid, struct windows* windows, struct borde

uint64_t set_tags = 1;
uint64_t clear_tags = 0;
uint32_t options = 0x2;

CFArrayRef space_list_ref = cfarray_of_cfnumbers(space_list,
sizeof(uint64_t),
Expand All @@ -80,56 +81,42 @@ void windows_add_existing_windows(int cid, struct windows* windows, struct borde
CFArrayRef window_list_ref = SLSCopyWindowsWithOptionsAndTags(cid,
0,
space_list_ref,
options,
0x2,
&set_tags,
&clear_tags );
if (!window_list_ref) goto err;

int count = CFArrayGetCount(window_list_ref);
if (!count) goto out;

CFTypeRef query = SLSWindowQueryWindows(cid, window_list_ref, count);
CFTypeRef iterator = SLSWindowQueryResultCopyWindows(query);

while (SLSWindowIteratorAdvance(iterator)) {
uint64_t tags = SLSWindowIteratorGetTags(iterator);
uint64_t attributes = SLSWindowIteratorGetAttributes(iterator);
uint32_t parent_wid = SLSWindowIteratorGetParentID(iterator);
uint32_t wid = SLSWindowIteratorGetWindowID(iterator);
uint64_t sid = window_space_id(cid, wid);

if (((parent_wid == 0)
&& ((attributes & 0x2)
|| (tags & 0x400000000000000))
&& (((tags & 0x1))
|| ((tags & 0x2)
&& (tags & 0x80000000))))) {
windows_add_window(windows, wid);
SLSRequestNotificationsForWindows(cid,
windows->wids,
windows->num_windows);
struct border* border = borders_add_border(borders, wid, sid);
if (g_first_time) {
g_first_time = false;
borders_remove_border(borders, wid, sid);
border = borders_add_border(borders, wid, sid);
if (window_list_ref) {
int count = CFArrayGetCount(window_list_ref);
if (count > 0) {
CFTypeRef query = SLSWindowQueryWindows(cid, window_list_ref, count);
CFTypeRef iterator = SLSWindowQueryResultCopyWindows(query);

while (SLSWindowIteratorAdvance(iterator)) {
ITERATOR_WINDOW_SUITABLE(iterator, {
uint32_t wid = SLSWindowIteratorGetWindowID(iterator);
uint64_t sid = window_space_id(cid, wid);
windows_add_window(windows, wid);
struct border* border = borders_add_border(borders, wid, sid);
CFArrayRef border_ref = cfarray_of_cfnumbers(&border->wid,
sizeof(uint32_t),
1,
kCFNumberSInt32Type);

border->sid = sid;
SLSMoveWindowsToManagedSpace(cid, border_ref, sid);
CFRelease(border_ref);
});
}
CFArrayRef border_ref = cfarray_of_cfnumbers(&border->wid,
sizeof(uint32_t),
1,
kCFNumberSInt32Type);

border->sid = sid;
SLSMoveWindowsToManagedSpace(cid, border_ref, sid);
CFRelease(border_ref);

SLSRequestNotificationsForWindows(cid,
windows->wids,
windows->num_windows);


CFRelease(query);
CFRelease(iterator);
}
CFRelease(window_list_ref);
}

CFRelease(query);
CFRelease(iterator);
out:
CFRelease(window_list_ref);
err:
CFRelease(space_list_ref);
free(space_list);
}
Expand Down
14 changes: 14 additions & 0 deletions src/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
#include <stdlib.h>
#include "border.h"

#define ITERATOR_WINDOW_SUITABLE(iterator, code) { \
uint64_t tags = SLSWindowIteratorGetTags(iterator); \
uint64_t attributes = SLSWindowIteratorGetAttributes(iterator); \
uint32_t parent_wid = SLSWindowIteratorGetParentID(iterator); \
if (((parent_wid == 0) \
&& ((attributes & 0x2) \
|| (tags & 0x400000000000000)) \
&& (((tags & 0x1)) \
|| ((tags & 0x2) \
&& (tags & 0x80000000))))) { \
code \
} \
}

struct windows {
uint32_t num_windows;
uint32_t* wids;
Expand Down

0 comments on commit 96534dd

Please sign in to comment.