Skip to content

Commit

Permalink
more modular argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Dec 10, 2023
1 parent e629ff3 commit 95b4386
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 76 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FILES = src/main.c src/mach.c src/hashtable.c src/events.c src/windows.c src/border.c
FILES = src/main.c src/parse.c src/mach.c src/hashtable.c src/events.c src/windows.c src/border.c
LIBS = -framework AppKit -F/System/Library/PrivateFrameworks/ -framework SkyLight

all: | bin
Expand Down
86 changes: 11 additions & 75 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "events.h"
#include "windows.h"
#include "mach.h"
#include "parse.h"
#include <stdio.h>

#define VERSION_OPT_LONG "--version"
Expand Down Expand Up @@ -34,81 +35,6 @@ static TABLE_COMPARE_FUNC(cmp_windows) {
return *(uint32_t *) key_a == *(uint32_t *) key_b;
}

static void event_callback(CFMachPortRef port, void* message, CFIndex size, void* context) {
int cid = SLSMainConnectionID();
CGEventRef event = SLEventCreateNextEvent(cid);
if (!event) return;
do {
CFRelease(event);
event = SLEventCreateNextEvent(cid);
} while (event);
}

static uint32_t parse_settings(struct settings* settings, int count, char** arguments) {
uint32_t update_mask = 0;
for (int i = 0; i < count; i++) {
if (sscanf(arguments[i],
"active_color=0x%x",
&settings->active_window.color) == 1) {
settings->active_window.stype = COLOR_STYLE_SOLID;
update_mask |= BORDER_UPDATE_MASK_ACTIVE;
}
else if (sscanf(arguments[i],
"active_color=gradient(top_left=0x%x,bottom_right=0x%x)",
&settings->active_window.gradient.color1,
&settings->active_window.gradient.color2) == 2) {
settings->active_window.stype = COLOR_STYLE_GRADIENT;
settings->active_window.gradient.direction = TL_TO_BR;
update_mask |= BORDER_UPDATE_MASK_ACTIVE;
}
else if (sscanf(arguments[i],
"active_color=gradient(top_right=0x%x,bottom_left=0x%x)",
&settings->active_window.gradient.color1,
&settings->active_window.gradient.color2) == 2) {
settings->active_window.stype = COLOR_STYLE_GRADIENT;
settings->active_window.gradient.direction = TR_TO_BL;
update_mask |= BORDER_UPDATE_MASK_ACTIVE;
}
else if (sscanf(arguments[i],
"inactive_color=0x%x",
&settings->inactive_window.color) == 1) {
settings->inactive_window.stype = COLOR_STYLE_SOLID;
update_mask |= BORDER_UPDATE_MASK_INACTIVE;
}
else if (sscanf(arguments[i],
"inactive_color=gradient(top_left=0x%x,bottom_right=0x%x)",
&settings->inactive_window.gradient.color1,
&settings->inactive_window.gradient.color2) == 2) {
settings->inactive_window.stype = COLOR_STYLE_GRADIENT;
settings->inactive_window.gradient.direction = TL_TO_BR;
update_mask |= BORDER_UPDATE_MASK_INACTIVE;
}
else if (sscanf(arguments[i],
"inactive_color=gradient(top_right=0x%x,bottom_left=0x%x)",
&settings->inactive_window.gradient.color1,
&settings->inactive_window.gradient.color2) == 2) {
settings->inactive_window.stype = COLOR_STYLE_GRADIENT;
settings->inactive_window.gradient.direction = TR_TO_BL;
update_mask |= BORDER_UPDATE_MASK_INACTIVE;
}
else if (sscanf(arguments[i], "width=%f", &settings->border_width) == 1) {
update_mask |= BORDER_UPDATE_MASK_ALL;
}
else if (sscanf(arguments[i], "style=%c", &settings->border_style) == 1) {
update_mask |= BORDER_UPDATE_MASK_ALL;
} else if (strcmp(arguments[i], "hidpi=on") == 0) {
update_mask |= BORDER_UPDATE_MASK_RECREATE_ALL;
settings->hidpi = true;
} else if (strcmp(arguments[i], "hidpi=off") == 0) {
update_mask |= BORDER_UPDATE_MASK_RECREATE_ALL;
settings->hidpi = false;
} else {
printf("[?] Borders: Invalid argument '%s'\n", arguments[i]);
}
}
return update_mask;
}

static void message_handler(void* data, uint32_t len) {
char* message = data;
uint32_t update_mask = 0;
Expand Down Expand Up @@ -150,6 +76,16 @@ static void send_args_to_server(mach_port_t port, int argc, char** argv) {
mach_send_message(port, message, message_length);
}

static void event_callback(CFMachPortRef port, void* message, CFIndex size, void* context) {
int cid = SLSMainConnectionID();
CGEventRef event = SLEventCreateNextEvent(cid);
if (!event) return;
do {
CFRelease(event);
event = SLEventCreateNextEvent(cid);
} while (event);
}

int main(int argc, char** argv) {
if (argc > 1 && ((strcmp(argv[1], VERSION_OPT_LONG) == 0)
|| (strcmp(argv[1], VERSION_OPT_SHRT) == 0))) {
Expand Down
64 changes: 64 additions & 0 deletions src/parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "parse.h"
#include "border.h"

static bool str_starts_with(char* string, char* prefix) {
if (!string || !prefix) return false;
if (strlen(string) < strlen(prefix)) return false;
if (strncmp(prefix, string, strlen(prefix)) == 0) return true;
return false;
}

static uint32_t parse_color(struct color_style* style, char* token) {
uint32_t update_mask = 0;
if (sscanf(token, "=0x%x", &style->color) == 1) {
style->stype = COLOR_STYLE_SOLID;
} else if (sscanf(token,
"=gradient(top_left=0x%x,bottom_right=0x%x)",
&style->gradient.color1,
&style->gradient.color2) == 2) {
style->stype = COLOR_STYLE_GRADIENT;
style->gradient.direction = TL_TO_BR;
update_mask |= BORDER_UPDATE_MASK_ACTIVE;
} else if (sscanf(token,
"=gradient(top_right=0x%x,bottom_left=0x%x)",
&style->gradient.color1,
&style->gradient.color2) == 2) {
style->stype = COLOR_STYLE_GRADIENT;
style->gradient.direction = TR_TO_BL;
update_mask |= BORDER_UPDATE_MASK_ACTIVE;
} else {
printf("[?] Borders: Invalid color argument color%s\n", token);
}
return update_mask;
}

uint32_t parse_settings(struct settings* settings, int count, char** arguments) {
static char active_color[] = "active_color";
static char inactive_color[] = "inactive_color";

uint32_t update_mask = 0;
for (int i = 0; i < count; i++) {
if (str_starts_with(arguments[i], active_color)) {
update_mask |= parse_color(&settings->active_window,
arguments[i] + strlen(active_color));
} else if (str_starts_with(arguments[i], inactive_color)) {
update_mask |= parse_color(&settings->inactive_window,
arguments[i] + strlen(inactive_color));
}
else if (sscanf(arguments[i], "width=%f", &settings->border_width) == 1) {
update_mask |= BORDER_UPDATE_MASK_ALL;
}
else if (sscanf(arguments[i], "style=%c", &settings->border_style) == 1) {
update_mask |= BORDER_UPDATE_MASK_ALL;
} else if (strcmp(arguments[i], "hidpi=on") == 0) {
update_mask |= BORDER_UPDATE_MASK_RECREATE_ALL;
settings->hidpi = true;
} else if (strcmp(arguments[i], "hidpi=off") == 0) {
update_mask |= BORDER_UPDATE_MASK_RECREATE_ALL;
settings->hidpi = false;
} else {
printf("[?] Borders: Invalid argument '%s'\n", arguments[i]);
}
}
return update_mask;
}
4 changes: 4 additions & 0 deletions src/parse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include "border.h"

uint32_t parse_settings(struct settings* settings, int count, char** arguments);

0 comments on commit 95b4386

Please sign in to comment.