PatchworkOS
Loading...
Searching...
No Matches
theme.c
Go to the documentation of this file.
1#include "internal.h"
2
5#include <stdbool.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11static bool isLoaded = false;
12static theme_t theme = {0};
13
14static void theme_colors_load(config_t* config, const char* section, theme_color_set_t* dest)
15{
16 dest->backgroundNormal = config_get_int(config, section, "background_normal", THEME_COLOR_INVALID);
17 dest->backgroundSelectedStart = config_get_int(config, section, "background_selected_start", THEME_COLOR_INVALID);
18 dest->backgroundSelectedEnd = config_get_int(config, section, "background_selected_end", THEME_COLOR_INVALID);
20 config_get_int(config, section, "background_unselected_start", THEME_COLOR_INVALID);
21 dest->backgroundUnselectedEnd = config_get_int(config, section, "background_unselected_end", THEME_COLOR_INVALID);
22
23 dest->foregroundNormal = config_get_int(config, section, "foreground_normal", THEME_COLOR_INVALID);
24 dest->foregroundInactive = config_get_int(config, section, "foreground_inactive", THEME_COLOR_INVALID);
25 dest->foregroundLink = config_get_int(config, section, "foreground_link", THEME_COLOR_INVALID);
26 dest->foregroundSelected = config_get_int(config, section, "foreground_selected", THEME_COLOR_INVALID);
27
28 dest->bezel = config_get_int(config, section, "bezel", THEME_COLOR_INVALID);
29 dest->highlight = config_get_int(config, section, "highlight", THEME_COLOR_INVALID);
30 dest->shadow = config_get_int(config, section, "shadow", THEME_COLOR_INVALID);
31}
32
33static void theme_lazy_load(void)
34{
35 if (isLoaded)
36 {
37 return;
38 }
39
40 // Config files have safe failures.
41
42 config_t* colorsConfig = config_open("theme", "colors");
43 if (colorsConfig == NULL)
44 {
45 printf("theme: failed to open colors config, using defaults\n");
46 }
47 theme_colors_load(colorsConfig, "button", &theme.button);
48 theme_colors_load(colorsConfig, "view", &theme.view);
49 theme_colors_load(colorsConfig, "element", &theme.element);
50 theme_colors_load(colorsConfig, "panel", &theme.panel);
51 theme_colors_load(colorsConfig, "deco", &theme.deco);
52 config_close(colorsConfig);
53
54 config_t* varsConfig = config_open("theme", "vars");
55 if (varsConfig == NULL)
56 {
57 printf("theme: failed to open vars config, using defaults\n");
58 }
59 strncpy(theme.wallpaper, config_get_string(varsConfig, "strings", "wallpaper", ""), MAX_PATH - 1);
60 strncpy(theme.fontsDir, config_get_string(varsConfig, "strings", "fonts_dir", ""), MAX_PATH - 1);
61 strncpy(theme.cursorArrow, config_get_string(varsConfig, "strings", "cursor_arrow", ""), MAX_PATH - 1);
62 strncpy(theme.defaultFont, config_get_string(varsConfig, "strings", "default_font", ""), MAX_PATH - 1);
63 strncpy(theme.iconClose, config_get_string(varsConfig, "strings", "icon_close", ""), MAX_PATH - 1);
64 strncpy(theme.iconMinimize, config_get_string(varsConfig, "strings", "icon_minimize", ""), MAX_PATH - 1);
65
66 theme.frameSize = config_get_int(varsConfig, "integers", "frame_size", 1);
67 theme.bezelSize = config_get_int(varsConfig, "integers", "bezel_size", 1);
68 theme.titlebarSize = config_get_int(varsConfig, "integers", "titlebar_size", 1);
69 theme.panelSize = config_get_int(varsConfig, "integers", "panel_size", 1);
70 theme.bigPadding = config_get_int(varsConfig, "integers", "big_padding", 1);
71 theme.smallPadding = config_get_int(varsConfig, "integers", "small_padding", 1);
72 theme.separatorSize = config_get_int(varsConfig, "integers", "separator_size", 1);
73 config_close(varsConfig);
74
75 config_t* ansiConfig = config_open("theme", "ansi");
76 if (ansiConfig == NULL)
77 {
78 printf("theme: failed to open ansi config, using defaults\n");
79 }
80 for (uint64_t i = 0; i < THEME_ANSI_COLOR_COUNT; i++)
81 {
82 char keyNormal[32];
83 char keyBright[32];
84 char keyDim[32];
85 snprintf(keyNormal, sizeof(keyNormal), "color%lu_normal", i);
86 snprintf(keyBright, sizeof(keyBright), "color%lu_bright", i);
87 snprintf(keyDim, sizeof(keyDim), "color%lu_dim", i);
88 theme.ansi.normal[i] = config_get_int(ansiConfig, "colors", keyNormal, THEME_COLOR_INVALID);
89 theme.ansi.bright[i] = config_get_int(ansiConfig, "colors", keyBright, THEME_COLOR_INVALID);
90 theme.ansi.dim[i] = config_get_int(ansiConfig, "colors", keyDim, THEME_COLOR_INVALID);
91 }
92 config_close(ansiConfig);
93
94 isLoaded = true;
95}
96
98{
100 return &theme;
101}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
config_t * config_open(const char *prefix, const char *name)
Open a configuration file.
Definition config.c:93
int64_t config_get_int(config_t *config, const char *section, const char *key, int64_t fallback)
Get an integer value from a configuration file.
Definition config.c:250
const char * config_get_string(config_t *config, const char *section, const char *key, const char *fallback)
Get a string value from a configuration file.
Definition config.c:228
void config_close(config_t *config)
Close a configuration file.
Definition config.c:202
#define THEME_COLOR_INVALID
Invalid color constant.
Definition theme.h:25
#define THEME_ANSI_COLOR_COUNT
Number of ANSI colors.
Definition theme.h:52
theme_t * theme_global_get(void)
Get the global theme.
Definition theme.c:97
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
_PUBLIC int snprintf(char *_RESTRICT s, size_t n, const char *_RESTRICT format,...)
Definition snprintf.c:3
_PUBLIC char * strncpy(char *_RESTRICT s1, const char *_RESTRICT s2, size_t n)
Definition strncpy.c:3
Opaque configuration structure.
Definition config.c:28
pixel_t dim[THEME_ANSI_COLOR_COUNT]
Definition theme.h:62
pixel_t bright[THEME_ANSI_COLOR_COUNT]
Definition theme.h:61
pixel_t normal[THEME_ANSI_COLOR_COUNT]
Definition theme.h:60
pixel_t backgroundNormal
Definition theme.h:35
pixel_t foregroundInactive
Definition theme.h:41
pixel_t foregroundLink
Definition theme.h:42
pixel_t backgroundSelectedEnd
Definition theme.h:37
pixel_t highlight
Definition theme.h:45
pixel_t backgroundUnselectedEnd
Definition theme.h:39
pixel_t backgroundUnselectedStart
Definition theme.h:38
pixel_t foregroundSelected
Definition theme.h:43
pixel_t foregroundNormal
Definition theme.h:40
pixel_t bezel
Definition theme.h:44
pixel_t shadow
Definition theme.h:46
pixel_t backgroundSelectedStart
Definition theme.h:36
Theme structure.
Definition theme.h:71
theme_color_set_t element
Definition theme.h:74
int64_t bezelSize
Definition theme.h:84
int64_t smallPadding
Definition theme.h:88
theme_ansi_t ansi
Definition theme.h:90
theme_color_set_t panel
Definition theme.h:75
char iconMinimize[MAX_PATH]
Definition theme.h:82
char defaultFont[MAX_PATH]
Definition theme.h:80
theme_color_set_t deco
Definition theme.h:76
theme_color_set_t view
Definition theme.h:73
theme_color_set_t button
Definition theme.h:72
int64_t panelSize
Definition theme.h:86
int64_t titlebarSize
Definition theme.h:85
char cursorArrow[MAX_PATH]
Definition theme.h:79
int64_t frameSize
Definition theme.h:83
char fontsDir[MAX_PATH]
Definition theme.h:78
char iconClose[MAX_PATH]
Definition theme.h:81
int64_t bigPadding
Definition theme.h:87
int64_t separatorSize
Definition theme.h:89
char wallpaper[MAX_PATH]
Definition theme.h:77
static bool isLoaded
Definition theme.c:11
static theme_t theme
Definition theme.c:12
static void theme_colors_load(config_t *config, const char *section, theme_color_set_t *dest)
Definition theme.c:14
static void theme_lazy_load(void)
Definition theme.c:33