Skip to content

Commit

Permalink
settings: Generalize list code for adding more types of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsmit committed Apr 3, 2021
1 parent 238ad00 commit f54df9c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 30 deletions.
30 changes: 15 additions & 15 deletions src/option_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ int string_parse_enum(const void *data, const char *s, void * ret) {
return false;
}

int string_parse_mouse_action_list(char **s, void *ret_void)
int string_parse_enum_list(const void *data, char **s, void *ret_void)
{
enum mouse_action **ret = (enum mouse_action **) ret_void;
enum mouse_action *tmp;
int **ret = (int **) ret_void;
int *tmp;
ASSERT_OR_RET(s, false);
ASSERT_OR_RET(ret, false);

int len = 0;
while (s[len])
len++;

tmp = g_malloc_n((len + 1), sizeof(enum mouse_action));
tmp = g_malloc_n((len + 1), sizeof(int));
for (int i = 0; i < len; i++) {
if (!string_parse_enum(&mouse_action_enum_data, s[i], tmp + i)) {
if (!string_parse_enum(data, s[i], tmp + i)) {
LOG_W("Unknown mouse action value: '%s'", s[i]);
g_free(tmp);
return false;
Expand All @@ -83,13 +83,15 @@ int string_parse_mouse_action_list(char **s, void *ret_void)
return true;
}

int string_parse_list(void *data, const char *s, void *ret) {
enum list_type type = *(int*) data;
char **arr = string_to_array(s);
int string_parse_list(const void *data, const char *s, void *ret) {
const enum list_type type = GPOINTER_TO_INT(data);
char **arr = NULL;
int success = false;
switch (type) {
case MOUSE_LIST:
success = string_parse_mouse_action_list(arr, ret);
arr = string_to_array(s, ",");
success = string_parse_enum_list(&mouse_action_enum_data,
arr, ret);
break;
default:
LOG_W("Don't know this list type: %i", type);
Expand Down Expand Up @@ -338,10 +340,8 @@ bool set_from_string(void *target, struct setting setting, const char *value) {
*(struct geometry*) target = x_parse_geometry(value);
return true;
case TYPE_LIST: ;
int type = *(enum list_type*)setting.parser_data;
LOG_D("list type %i", *(int*)setting.parser_data);
LOG_D("list type int %i", type);
return string_parse_list(&type, value, target);
LOG_D("list type %i", GPOINTER_TO_INT(setting.parser_data));
return string_parse_list(setting.parser_data, value, target);
default:
LOG_W("Setting type of '%s' is not known (type %i)", setting.name, setting.type);
return false;
Expand Down Expand Up @@ -599,9 +599,9 @@ char **cmdline_get_list(const char *key, const char *def, const char *descriptio
const char *str = cmdline_get_value(key);

if (str)
return string_to_array(str);
return string_to_array(str, ",");
else
return string_to_array(def);
return string_to_array(def, ",");
}

gint64 cmdline_get_time(const char *key, gint64 def, const char *description)
Expand Down
4 changes: 3 additions & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#include "notification.h"
#include "x11/x.h"

#define LIST_END (-1)

enum alignment { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT };
enum ellipsize { ELLIPSE_START, ELLIPSE_MIDDLE, ELLIPSE_END };
enum icon_position { ICON_LEFT, ICON_RIGHT, ICON_OFF };
enum vertical_alignment { VERTICAL_TOP, VERTICAL_CENTER, VERTICAL_BOTTOM };
enum separator_color { SEP_FOREGROUND, SEP_AUTO, SEP_FRAME, SEP_CUSTOM };
enum follow_mode { FOLLOW_NONE, FOLLOW_MOUSE, FOLLOW_KEYBOARD };
enum mouse_action { MOUSE_NONE, MOUSE_DO_ACTION, MOUSE_CLOSE_CURRENT, MOUSE_CLOSE_ALL, MOUSE_ACTION_END = -1 /* indicates the end of a list of mouse actions */ };
enum mouse_action { MOUSE_NONE, MOUSE_DO_ACTION, MOUSE_CLOSE_CURRENT, MOUSE_CLOSE_ALL, MOUSE_ACTION_END = LIST_END /* indicates the end of a list of mouse actions */ };
#ifndef ZWLR_LAYER_SHELL_V1_LAYER_ENUM
#define ZWLR_LAYER_SHELL_V1_LAYER_ENUM
// Needed for compiling without wayland dependency
Expand Down
12 changes: 6 additions & 6 deletions src/settings_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ static const struct rule empty_rule = {
.script = NULL,
};


#ifndef ZWLR_LAYER_SHELL_V1_LAYER_ENUM
#define ZWLR_LAYER_SHELL_V1_LAYER_ENUM
// Needed for compiling without wayland dependency
Expand All @@ -130,11 +131,10 @@ const enum zwlr_layer_shell_v1_layer {
#endif /* ZWLR_LAYER_SHELL_V1_LAYER_ENUM */

enum list_type {
MOUSE_LIST,
INVALID_LIST = 0,
MOUSE_LIST = 1,
};

static enum list_type mouse_list = MOUSE_LIST;

#define ENUM_END {NULL, 0}
static const struct string_to_enum_def verbosity_enum_data[] = {
{"critical", G_LOG_LEVEL_CRITICAL },
Expand Down Expand Up @@ -989,7 +989,7 @@ static const struct setting allowed_settings[] = {
.default_value = "close_current",
.value = &settings.mouse_left_click,
.parser = NULL,
.parser_data = &mouse_list,
.parser_data = GINT_TO_POINTER(MOUSE_LIST),
},
{
.name = "mouse_middle_click",
Expand All @@ -999,7 +999,7 @@ static const struct setting allowed_settings[] = {
.default_value = "do_action, close_current",
.value = &settings.mouse_middle_click,
.parser = NULL,
.parser_data = &mouse_list,
.parser_data = GINT_TO_POINTER(MOUSE_LIST),
},
{
.name = "mouse_right_click",
Expand All @@ -1009,7 +1009,7 @@ static const struct setting allowed_settings[] = {
.default_value = "close_all",
.value = &settings.mouse_right_click,
.parser = NULL,
.parser_data = &mouse_list,
.parser_data = GINT_TO_POINTER(MOUSE_LIST),
},
{
.name = "icon_path",
Expand Down
2 changes: 1 addition & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void string_strip_delimited(char *str, char a, char b)
}

/* see utils.h */
char **string_to_array(const char *string)
char **string_to_array(const char *string, const char *delimiter)
{
char **arr = NULL;
if (string) {
Expand Down
10 changes: 6 additions & 4 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@ char *string_strip_quotes(const char *value);
void string_strip_delimited(char *str, char a, char b);

/**
* Parse a comma-delimited string into a dynamic array of tokens
* Parse a string into a dynamic array of tokens, using the delimiter string.
*
* The string is split on commas and strips spaces from tokens. The last element
* of the array is NULL in order to avoid passing around a length variable.
* The string is split on the separator character and strips spaces from
* tokens. The last element of the array is NULL in order to avoid passing
* around a length variable.
*
* @param string The string to convert to an array
* @param delmiter The character that separates list entries
* @returns The array of tokens.
*/
char **string_to_array(const char *string);
char **string_to_array(const char *string, const char *delimiter);

/**
* Replace tilde and path-specific values with its equivalents
Expand Down
24 changes: 22 additions & 2 deletions test/option_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ TEST test_string_to_list(void)
struct setting s;
s.type = TYPE_LIST;
s.value = &val;
s.parser_data = &mouse_list;
s.parser_data = GINT_TO_POINTER(MOUSE_LIST);

const char* inputs[] = {
"close_current",
Expand Down Expand Up @@ -352,7 +352,7 @@ TEST test_string_to_list_invalid(void)
s.name = "test_list";
s.type = TYPE_LIST;
s.value = &val;
s.parser_data = &mouse_list;
s.parser_data = GINT_TO_POINTER(MOUSE_LIST);

const char* invalid_inputs[] = {
"0",
Expand Down Expand Up @@ -571,6 +571,25 @@ TEST test_string_to_sepcolor_invalid(void)
PASS();
}

#define TEST_ENUM(t) { \
ASSERT_EQ(sizeof(t), sizeof(int)); \
}

// The settings code relies on the enums being the same size as an int. If
// they're bigger it's not a big problem, but if they're smaller, other parts
// of the memory may be overwritten.
TEST test_enum_size(void)
{
TEST_ENUM(enum markup_mode);
TEST_ENUM(enum alignment);
TEST_ENUM(enum ellipsize);
TEST_ENUM(enum icon_position);
TEST_ENUM(enum vertical_alignment);
TEST_ENUM(enum follow_mode);
TEST_ENUM(enum mouse_action );
TEST_ENUM(enum zwlr_layer_shell_v1_layer);
PASS();
}

SUITE(suite_option_parser)
{
Expand Down Expand Up @@ -620,6 +639,7 @@ SUITE(suite_option_parser)
// Paths are now almost always considered valid
RUN_TEST(test_string_to_sepcolor);
RUN_TEST(test_string_to_sepcolor_invalid);
RUN_TEST(test_enum_size);
// geometry is left out, since we probably want to replace it soon
// anyways

Expand Down
1 change: 0 additions & 1 deletion test/setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ TEST test_dunstrc_nomarkup(void) {
PASS();
}


#define CHECK_EQUAL_OFFSET(t, offset, EQ_FUNC) { \
typeof(t) a = *(typeof(t)*) ((char*) &s_default + offset); \
typeof(t) b = *(typeof(t)*) ((char*) &s_dunstrc + offset); \
Expand Down

0 comments on commit f54df9c

Please sign in to comment.