add config.h and config.c

This commit is contained in:
Akos Horvath 2023-07-04 19:30:37 +02:00
parent 6db707686a
commit 12351bdf99
2 changed files with 201 additions and 0 deletions

142
src/config.c Normal file
View File

@ -0,0 +1,142 @@
#include "config.h"
#include "wm.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void wm_cfg_init_def(Config *config)
{
config->ms_p = 0.5;
config->border_col = "#222222";
config->focused_border_col = "#444444";
config->border_width = 2;
config->focus_on_motion = true;
wm_keybinds_init_def(config);
}
void wm_keybinds_init_def(Config *config)
{
char *st[] = {"alacritty", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
char *dmenu[] = {"i3-dmenu-desktop", NULL};
char **dmenuh = malloc(sizeof(dmenu));
memcpy(dmenuh, dmenu, sizeof(dmenu));
int size;
Keybind k[] = {
(Keybind) {Mod4Mask, XK_Return, *wm_kb_spawn,
(Arg) {.sl = sth, .count = 2}},
(Keybind) {Mod4Mask | ShiftMask, XK_q, *wm_kb_exit,
(Arg) {0}},
(Keybind) {Mod4Mask, XK_d, *wm_kb_spawn,
(Arg) {.sl = dmenuh, .count = 2}},
(Keybind) {Mod4Mask, XK_c, *wm_kb_kill,
(Arg) {.c = NULL}},
(Keybind) {Mod4Mask, XK_1, *wm_kb_switch_ws,
(Arg) {.i = 0}},
(Keybind) {Mod4Mask, XK_2, *wm_kb_switch_ws,
(Arg) {.i = 1}},
(Keybind) {Mod4Mask, XK_3, *wm_kb_switch_ws,
(Arg) {.i = 2}},
(Keybind) {Mod4Mask, XK_4, *wm_kb_switch_ws,
(Arg) {.i = 3}},
(Keybind) {Mod4Mask, XK_5, *wm_kb_switch_ws,
(Arg) {.i = 4}},
(Keybind) {Mod4Mask, XK_6, *wm_kb_switch_ws,
(Arg) {.i = 5}},
(Keybind) {Mod4Mask, XK_7, *wm_kb_switch_ws,
(Arg) {.i = 6}},
(Keybind) {Mod4Mask, XK_8, *wm_kb_switch_ws,
(Arg) {.i = 7}},
(Keybind) {Mod4Mask, XK_9, *wm_kb_switch_ws,
(Arg) {.i = 8}},
(Keybind) {Mod4Mask, XK_h, *wm_kb_focus_dir,
(Arg) {.i = LEFT}},
(Keybind) {Mod4Mask, XK_j, *wm_kb_focus_dir,
(Arg) {.i = DOWN}},
(Keybind) {Mod4Mask, XK_k, *wm_kb_focus_dir,
(Arg) {.i = UP}},
(Keybind) {Mod4Mask, XK_l, *wm_kb_focus_dir,
(Arg) {.i = RIGHT}},
(Keybind) {Mod4Mask, XK_b, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_VERTICAL}},
(Keybind) {Mod4Mask, XK_v, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_HORIZ}},
(Keybind) {Mod4Mask, XK_t, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_TAB}},
};
size = sizeof(k);
config->kb_count = size / sizeof(Keybind);
DEBUG_PRINT("sizeof k: %d\n", size);
config->keybinds = malloc(size);
memcpy(config->keybinds, k, size);
}
void wm_configfile_init(ConfigFile *config, const char *filename)
{
assert(config);
assert(filename);
strncpy(config->file_path, filename, PATH_MAX);
ConfigCommand commands[] = {
(ConfigCommand) {.string = "border_color", .arg_count = 1},
(ConfigCommand) {.string = "focused_border_color", .arg_count = 1},
(ConfigCommand) {.string = "border_width", .arg_count = 1},
(ConfigCommand) {.string = "keybind", .arg_count = 0},
(ConfigCommand) {.string = "focus_on_motion", .arg_count = 1},
};
config->command_count = sizeof(commands) / sizeof (commands[0]);
config->available_commands = calloc(sizeof(commands), 0);
memcpy(config->available_commands, commands, sizeof(commands));
}
void wm_configfile_read(ConfigFile *config)
{
assert(config);
FILE *fp = fopen(config->file_path, "r");
if (!fp) {
fprintf(stderr, "wm: could not open config file: %s\n", strerror(errno));
exit(1);
}
const int max_line_len = 4096;
char line[max_line_len];
while(fgets(line, max_line_len, fp) != NULL) {
// comment
if (line[0] == '#')
continue;
}
fclose(fp);
}

59
src/config.h Normal file
View File

@ -0,0 +1,59 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <limits.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#define CONFIG_COMMAND_MAX_LEN 128
typedef struct Client Client;
typedef struct Wm Wm;
typedef struct Arg Arg;
typedef struct Keybind Keybind;
struct Arg {
int i;
unsigned int ui;
Client *c;
char* s;
char **sl;
int count;
};
struct Keybind {
unsigned int mask;
KeySym keysym;
void (*func)(Wm*, Arg*);
Arg args;
};
typedef struct {
char string[CONFIG_COMMAND_MAX_LEN];
size_t arg_count;
} ConfigCommand;
typedef struct {
char file_path[PATH_MAX];
ConfigCommand *available_commands;
size_t command_count;
} ConfigFile;
typedef struct {
float ms_p;
char *border_col;
char *focused_border_col;
unsigned char border_width;
Keybind *keybinds;
int kb_count;
bool focus_on_motion;
} Config;
void wm_cfg_init_def(Config *config);
void wm_keybinds_init_def(Config *config);
void wm_configfile_init(ConfigFile *config, const char *path);
void wm_configfile_read(ConfigFile *config);
#endif // CONFIG_H