-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.c
149 lines (143 loc) · 4.44 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "config.h"
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const struct option long_options[] = {
{"start-with-english", no_argument, NULL, 'e'},
{"dock", required_argument, NULL, 'd'},
{"font", required_argument, NULL, 'f'},
{"top", no_argument, NULL, 't'},
{"text-color", required_argument, NULL, 'T'},
{"background-color", required_argument, NULL, 'b'},
{"selection-color", required_argument, NULL, 's'},
{"selection-text-color",required_argument, NULL, 'S'},
{"no-tray-icon", no_argument, NULL, 'n'},
{"force-default-keymap", no_argument, NULL, 1},
{0},
};
static const char help[] = "\
Usage: %s [OPTIONS]...\n\
\n\
-e, --start-with-english Start with English mode\n\
-d, --dock=(dock|yield|no) Set candidate panel behavior, default to dock\n\
dock Configure as a dock\n\
Do not overlap with any surfaces\n\
yield Yield to existing dock surface\n\
May be put on top of normal surfaces\n\
no Do not either dock or yield\n\
May be put on top of any sufaces\n\
-f, --font=FONT Pango font description to use\n\
--force-default-keymap Force to use xkbcommon default keymap to\n\
translate keycodes for libchewing\n\
-t, --top Anchor candidate panel to top instead of bottom\n\
-T, --text-color=COLOR Set candidate panel text color\n\
-b, --background-color=COLOR Set candidate panel background color\n\
-s, --selection-color=COLOR Set candidate panel selection highlight color\n\
-S, --selection-text-color=COLOR\n\
Set candidate panel selection text color\n\
-n, --no-tray-icon Disable tray icon.\n\
\n\
COLOR is color specified as either #RRGGBB or #RRGGBBAA.\n";
struct wlchewing_config *config_new() {
struct wlchewing_config *config = calloc(1,
sizeof(struct wlchewing_config));
if (!config) {
return NULL;
}
config->text_color[0] = 1.0;
config->text_color[1] = 1.0;
config->text_color[2] = 1.0;
config->text_color[3] = 1.0;
config->selection_text_color[0] = 1.0;
config->selection_text_color[1] = 1.0;
config->selection_text_color[2] = 1.0;
config->selection_text_color[3] = 1.0;
config->background_color[3] = 1.0;
config->selection_color[0] = 0.25;
config->selection_color[1] = 0.25;
config->selection_color[2] = 0.25;
config->selection_color[3] = 1.0;
config->tray_icon = true;
return config;
}
static int decode_color(const char *str, double *rgba) {
int r, g, b, a;
char dummy;
if (sscanf(str, "#%2x%2x%2x%2x%c", &r, &g, &b, &a, &dummy) == 4) {
rgba[0] = r / 255.0;
rgba[1] = g / 255.0;
rgba[2] = b / 255.0;
rgba[3] = a / 255.0;
return 0;
} else if (sscanf(str, "#%2x%2x%2x%c", &r, &g, &b, &dummy) == 3) {
rgba[0] = r / 255.0;
rgba[1] = g / 255.0;
rgba[2] = b / 255.0;
return 0;
}
return -EINVAL;
}
int config_read_opts(int argc, char *argv[], struct wlchewing_config *config) {
int opt;
while ((opt = getopt_long(argc, argv, "ed:f:tT:b:s:S:n", long_options, NULL)) != -1) {
if (opt == '?') {
fprintf(stderr, help, argv[0]);
return -EINVAL;
}
switch (opt) {
case 'e':
config->start_eng = true;
break;
case 'd':
if (!strcmp(optarg, "dock")) {
config->dock = DOCK_DOCK;
} else if (!strcmp(optarg, "yield")) {
config->dock = DOCK_YEILD;
} else if (!strcmp(optarg, "no")) {
config->dock = DOCK_NO;
} else {
fprintf(stderr, help, argv[0]);
return -EINVAL;
}
break;
case 'f':
config->font = optarg;
break;
case 't':
config->anchor_top = true;
break;
case 'T':
if (decode_color(optarg, config->text_color) < 0) {
fprintf(stderr, help, argv[0]);
return -EINVAL;
}
break;
case 'b':
if (decode_color(optarg, config->background_color) < 0) {
fprintf(stderr, help, argv[0]);
return -EINVAL;
}
break;
case 's':
if (decode_color(optarg, config->selection_color) < 0) {
fprintf(stderr, help, argv[0]);
return -EINVAL;
}
break;
case 'S':
if (decode_color(optarg, config->selection_text_color) < 0) {
fprintf(stderr, help, argv[0]);
return -EINVAL;
}
break;
case 'n':
config->tray_icon = false;
case 1:
config->chewing_use_xkb_default = true;
break;
}
}
return 0;
}