new environment variable system

This commit is contained in:
Felix Kratz 2021-12-12 13:22:41 +01:00
parent 338b7ea514
commit a455b44529
6 changed files with 96 additions and 38 deletions

View file

@ -3,6 +3,7 @@
#include "custom_events.h"
#include "graph.h"
#include "group.h"
#include "misc/env_vars.h"
#include "misc/helpers.h"
#include <stdint.h>
#include <string.h>
@ -20,6 +21,8 @@ void bar_item_clear_pointers(struct bar_item* bar_item) {
bar_item->bounding_rects = NULL;
bar_item->group = NULL;
bar_item->num_rects = 0;
bar_item->signal_args.env_vars.vars = NULL;
bar_item->signal_args.env_vars.count = 0;
text_clear_pointers(&bar_item->icon);
text_clear_pointers(&bar_item->label);
}
@ -87,12 +90,8 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
if (default_item) bar_item_inherit_from_item(bar_item, default_item);
strncpy(&bar_item->signal_args.name[0][0], "NAME", 255);
strncpy(&bar_item->signal_args.name[1][0], "SELECTED", 255);
strncpy(&bar_item->signal_args.name[4][0], "SENDER", 255);
strncpy(&bar_item->signal_args.name[5][0], "BUTTON", 255);
strncpy(&bar_item->signal_args.name[6][0], "MODIFIER", 255);
strncpy(&bar_item->signal_args.value[1][0], "false", 255);
env_vars_init(&bar_item->signal_args.env_vars);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SELECTED"), string_copy("false"));
}
void bar_item_append_associated_space(struct bar_item* bar_item, uint32_t bit) {
@ -102,7 +101,7 @@ void bar_item_append_associated_space(struct bar_item* bar_item, uint32_t bit) {
bar_item->associated_space = bit;
char sid_str[32];
sprintf(sid_str, "%u", get_set_bit_position(bit));
strncpy(&bar_item->signal_args.value[2][0], sid_str, 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SID"), string_copy(sid_str));
}
}
@ -113,7 +112,7 @@ void bar_item_append_associated_display(struct bar_item* bar_item, uint32_t bit)
bar_item->associated_display = bit;
char did_str[32];
sprintf(did_str, "%u", get_set_bit_position(bit));
strncpy(&bar_item->signal_args.value[3][0], did_str, 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("DID"), string_copy(did_str));
}
}
@ -149,8 +148,8 @@ bool bar_item_update(struct bar_item* bar_item, char* sender, bool forced) {
// Script Update
if (strlen(bar_item->script) > 0) {
if (sender) strncpy(&bar_item->signal_args.value[4][0], sender, 255);
else strncpy(&bar_item->signal_args.value[4][0], forced ? "forced" : "routine", 255);
if (sender) env_vars_set(&bar_item->signal_args.env_vars, string_copy("SENDER"), string_copy(sender));
else env_vars_set(&bar_item->signal_args.env_vars, string_copy("SENDER"), string_copy(forced ? "forced" : "routine"));
fork_exec(bar_item->script, &bar_item->signal_args);
}
@ -177,7 +176,7 @@ void bar_item_set_name(struct bar_item* bar_item, char* name) {
if (bar_item->name && strcmp(bar_item->name, name) == 0) { free(name); return; }
if (name != bar_item->name && !bar_item->name) free(bar_item->name);
bar_item->name = name;
strncpy(&bar_item->signal_args.value[0][0], name, 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("NAME"), string_copy(name));
}
void bar_item_set_type(struct bar_item* bar_item, char type) {
@ -190,10 +189,8 @@ void bar_item_set_type(struct bar_item* bar_item, char type) {
bar_item->update_mask |= UPDATE_SPACE_CHANGE;
bar_item->updates = false;
strncpy(&bar_item->signal_args.name[2][0], "SID", 255);
strncpy(&bar_item->signal_args.value[2][0], "0", 255);
strncpy(&bar_item->signal_args.name[3][0], "DID", 255);
strncpy(&bar_item->signal_args.value[3][0], "0", 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SID"), string_copy("0"));
env_vars_set(&bar_item->signal_args.env_vars, string_copy("DID"), string_copy("0"));
}
else if (type == BAR_COMPONENT_ALIAS) {
bar_item->update_frequency = 1;
@ -236,8 +233,8 @@ void bar_item_set_drawing(struct bar_item* bar_item, bool state) {
void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t modifier) {
if (!bar_item) return;
strncpy(&bar_item->signal_args.value[5][0], get_type_description(type), 255);
strncpy(&bar_item->signal_args.value[6][0], get_modifier_description(modifier), 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("BUTTON"), get_type_description(type));
env_vars_set(&bar_item->signal_args.env_vars, string_copy("MODIFIER"), get_modifier_description(modifier));
if (strlen(bar_item->click_script) > 0)
fork_exec(bar_item->click_script, &bar_item->signal_args);

View file

@ -303,12 +303,12 @@ void bar_manager_update_space_components(struct bar_manager* bar_manager, bool f
if ((!bar_item->selected || forced) && bar_item->associated_space & (1 << sid)) {
bar_item->selected = true;
bar_item->updates = true;
strncpy(&bar_item->signal_args.value[1][0], "true", 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SELECTED"), string_copy("true"));
}
else if ((bar_item->selected || forced) && !(bar_item->associated_space & (1 << sid))) {
bar_item->selected = false;
bar_item->updates = true;
strncpy(&bar_item->signal_args.value[1][0], "false", 255);
env_vars_set(&bar_item->signal_args.env_vars, string_copy("SELECTED"), string_copy("false"));
}
else {
bar_item->updates = false;

View file

@ -13,6 +13,7 @@
#include "misc/timing.h"
#include "misc/log.h"
#include "misc/env_vars.h"
#include "misc/helpers.h"
#include "misc/memory_pool.h"
#include "misc/socket.h"

View file

@ -437,7 +437,7 @@ void handle_message(int sockfd, char* message) {
bool bar_needs_refresh = false;
while (command.text && command.length > 0) {
if (token_equals(command, DOMAIN_SET)) {
struct token name = get_token(&message);
struct token name = get_token(&message);
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, name.text);
if (item_index_for_name < 0) {
fprintf(rsp, "Name: %s not found in bar items \n", name.text);

71
src/misc/env_vars.h Normal file
View file

@ -0,0 +1,71 @@
#ifndef ENV_VARS_H
#define ENV_VARS_H
#include <_types/_uint32_t.h>
#include <malloc/_malloc.h>
#include <string.h>
struct key_value_pair {
char* key;
char* value;
};
struct env_vars {
uint32_t count;
struct key_value_pair** vars;
};
void env_vars_init(struct env_vars* env_vars) {
env_vars->vars = NULL;
env_vars->count = 0;
}
void env_vars_unset(struct env_vars* env_vars, char* key) {
struct key_value_pair* key_value_pair = NULL;
for (int i = 0; i < env_vars->count; i++) {
if (strcmp(env_vars->vars[i]->key, key) == 0) key_value_pair = env_vars->vars[i];
}
if (key_value_pair == NULL) return;
if (env_vars->count == 1) {
free(env_vars->vars);
env_vars->vars = NULL;
env_vars->count = 0;
} else {
struct key_value_pair* tmp[env_vars->count - 1];
int count = 0;
for (int i = 0; i < env_vars->count; i++) {
if (env_vars->vars[i] == key_value_pair) continue;
tmp[count++] = env_vars->vars[i];
}
env_vars->count--;
env_vars->vars = realloc(env_vars->vars, sizeof(struct key_value_pair*)*env_vars->count);
memcpy(env_vars->vars, tmp, sizeof(struct key_value_pair*)*env_vars->count);
}
if (key_value_pair->key) free(key_value_pair->key);
if (key_value_pair->value) free(key_value_pair->value);
free(key_value_pair);
}
void env_vars_set(struct env_vars* env_vars, char* key, char* value) {
env_vars_unset(env_vars, key);
env_vars->count++;
env_vars->vars = realloc(env_vars->vars, env_vars->count * sizeof(struct key_value_pair*));
env_vars->vars[env_vars->count - 1] = malloc(sizeof(struct key_value_pair));
env_vars->vars[env_vars->count - 1]->key = key;
env_vars->vars[env_vars->count - 1]->value = value;
}
void env_vars_destroy(struct env_vars* env_vars) {
for (int i = 0; i < env_vars->count; i++) {
if (env_vars->vars[i]->key) free(env_vars->vars[i]->key);
if (env_vars->vars[i]->value) free(env_vars->vars[i]->value);
free(env_vars->vars[i]);
}
free(env_vars->vars);
}
#endif

View file

@ -12,15 +12,8 @@ extern CFArrayRef SLSCopyManagedDisplaySpaces(int cid);
extern uint32_t SLSGetActiveSpace(int cid);
extern int g_connection;
struct key_value_pair {
char* key;
char* value;
};
struct signal_args {
char name[7][255];
char value[7][255];
struct env_vars env_vars;
void *entity;
void *param1;
};
@ -309,15 +302,11 @@ static inline bool ensure_executable_permission(char *filename) {
return true;
}
static bool sync_exec(char *command, struct signal_args *args) {
if (args) {
if (*args->name[0]) setenv(args->name[0], args->value[0], 1);
if (*args->name[1]) setenv(args->name[1], args->value[1], 1);
if (*args->name[2]) setenv(args->name[2], args->value[2], 1);
if (*args->name[3]) setenv(args->name[3], args->value[3], 1);
if (*args->name[4]) setenv(args->name[4], args->value[4], 1);
if (*args->name[5]) setenv(args->name[5], args->value[5], 1);
if (*args->name[6]) setenv(args->name[6], args->value[6], 1);
static bool sync_exec(char *command, struct env_vars *env_vars) {
if (env_vars) {
for (int i = 0; i < env_vars->count; i++) {
setenv(env_vars->vars[i]->key, env_vars->vars[i]->value, 1);
}
}
char *exec[] = { "/usr/bin/env", "sh", "-c", command, NULL};
@ -332,7 +321,7 @@ static bool fork_exec(char *command, struct signal_args *args) {
if (pid != 0) return true;
alarm(FORK_TIMEOUT);
exit(sync_exec(command, args));
exit(sync_exec(command, &args->env_vars));
}
#pragma clang diagnostic pop