Move position configuration parsing into `position_config`
This commit is contained in:
parent
00e13604d2
commit
7026d31522
|
@ -2,6 +2,7 @@
|
|||
#define HIKARI_POSITION_CONFIG_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <ucl.h>
|
||||
|
||||
struct hikari_position_config_absolute {
|
||||
int x;
|
||||
|
@ -49,6 +50,15 @@ hikari_position_config_set_relative(
|
|||
|
||||
bool
|
||||
hikari_position_config_relative_parse(
|
||||
enum hikari_position_config_relative *relative_config, const char *str);
|
||||
struct hikari_position_config *position_config, const char *str);
|
||||
|
||||
bool
|
||||
hikari_position_config_absolute_parse(
|
||||
struct hikari_position_config *position_config,
|
||||
const ucl_object_t *position_config_obj);
|
||||
|
||||
bool
|
||||
hikari_position_config_parse(struct hikari_position_config *position_config,
|
||||
const ucl_object_t *position_config_obj);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -47,6 +47,10 @@ hikari_view_config_resolve_position(struct hikari_view_config *view_config,
|
|||
int *x,
|
||||
int *y);
|
||||
|
||||
bool
|
||||
hikari_view_config_parse(struct hikari_view_config *view_config,
|
||||
const ucl_object_t *view_config_obj);
|
||||
|
||||
static inline bool
|
||||
hikari_view_config_resolve_focus(struct hikari_view_config *view_config)
|
||||
{
|
||||
|
|
|
@ -637,68 +637,6 @@ done:
|
|||
return success;
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_position(const ucl_object_t *position_obj, int *x, int *y)
|
||||
{
|
||||
bool success = false;
|
||||
ucl_object_iter_t it = ucl_object_iterate_new(position_obj);
|
||||
|
||||
int64_t ret_x = 0;
|
||||
int64_t ret_y = 0;
|
||||
bool parsed_x = false;
|
||||
bool parsed_y = false;
|
||||
|
||||
const ucl_object_t *cur;
|
||||
while ((cur = ucl_object_iterate_safe(it, false)) != NULL) {
|
||||
const char *key = ucl_object_key(cur);
|
||||
|
||||
if (!strcmp(key, "x")) {
|
||||
if (!ucl_object_toint_safe(cur, &ret_x)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected integer for \"x\"-coordinate\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
parsed_x = true;
|
||||
} else if (!strcmp(key, "y")) {
|
||||
if (!ucl_object_toint_safe(cur, &ret_y)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected integer for \"y\"-coordinate\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
parsed_y = true;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"configuration error: unknown \"position\" key \"%s\"\n",
|
||||
key);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
if (!parsed_x) {
|
||||
fprintf(stderr,
|
||||
"configuration error: missing \"x\"-coordinate in \"position\2\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!parsed_y) {
|
||||
fprintf(stderr,
|
||||
"configuration error: missing \"y\"-coordinate in \"position\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
*x = ret_x;
|
||||
*y = ret_y;
|
||||
|
||||
done:
|
||||
ucl_object_iterate_free(it);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_view_config(struct hikari_configuration *configuration,
|
||||
const ucl_object_t *view_config_obj,
|
||||
|
@ -760,44 +698,9 @@ parse_view_config(struct hikari_configuration *configuration,
|
|||
|
||||
(*view_config)->mark = &hikari_marks[mark_name[0] - 'a'];
|
||||
} else if (!strcmp(key, "position")) {
|
||||
ucl_type_t type = ucl_object_type(cur);
|
||||
int x;
|
||||
int y;
|
||||
const char *relative;
|
||||
enum hikari_position_config_relative relative_config;
|
||||
|
||||
switch (type) {
|
||||
case UCL_OBJECT:
|
||||
if (!parse_position(cur, &x, &y)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: failed to parse \"views\" "
|
||||
"\"position\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
hikari_position_config_set_absolute(&(*view_config)->position, x, y);
|
||||
break;
|
||||
|
||||
case UCL_STRING:
|
||||
if (!ucl_object_tostring_safe(cur, &relative) ||
|
||||
!hikari_position_config_relative_parse(
|
||||
&relative_config, relative)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: failed to parse \"views\" "
|
||||
"\"position\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
hikari_position_config_set_relative(
|
||||
&(*view_config)->position, relative_config);
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"configuration error: failed to parse \"views\" \"position\"\n");
|
||||
goto done;
|
||||
if (!hikari_position_config_parse(&(*view_config)->position, cur)) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
} else if (!strcmp(key, "focus")) {
|
||||
bool focus;
|
||||
|
||||
|
@ -1609,17 +1512,13 @@ parse_output_config(struct hikari_output_config *output_config,
|
|||
goto done;
|
||||
}
|
||||
} else if (!strcmp(key, "position")) {
|
||||
int x;
|
||||
int y;
|
||||
struct hikari_position_config position;
|
||||
|
||||
if (!parse_position(cur, &x, &y)) {
|
||||
if (!hikari_position_config_absolute_parse(&position, cur)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: failed to parse output \"position\"\n");
|
||||
"configuration error: could not parse \"output\" \"position\"");
|
||||
goto done;
|
||||
}
|
||||
|
||||
hikari_position_config_set_absolute(&position, x, y);
|
||||
hikari_output_config_set_position(output_config, position);
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
|
|
|
@ -30,38 +30,166 @@ hikari_position_config_set_relative(
|
|||
|
||||
bool
|
||||
hikari_position_config_relative_parse(
|
||||
enum hikari_position_config_relative *relative_config, const char *str)
|
||||
struct hikari_position_config *position_config, const char *str)
|
||||
{
|
||||
bool success = false;
|
||||
enum hikari_position_config_relative relative_config;
|
||||
|
||||
if (!strcmp("bottom-left", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_BOTTOM_LEFT;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_BOTTOM_LEFT;
|
||||
success = true;
|
||||
} else if (!strcmp("bottom-middle", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_BOTTOM_MIDDLE;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_BOTTOM_MIDDLE;
|
||||
success = true;
|
||||
} else if (!strcmp("bottom-right", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_BOTTOM_RIGHT;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_BOTTOM_RIGHT;
|
||||
success = true;
|
||||
} else if (!strcmp("center", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_CENTER;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_CENTER;
|
||||
success = true;
|
||||
} else if (!strcmp("center-left", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_CENTER_LEFT;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_CENTER_LEFT;
|
||||
success = true;
|
||||
} else if (!strcmp("center-right", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_CENTER_RIGHT;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_CENTER_RIGHT;
|
||||
success = true;
|
||||
} else if (!strcmp("top-left", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_TOP_LEFT;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_TOP_LEFT;
|
||||
success = true;
|
||||
} else if (!strcmp("top-middle", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_TOP_MIDDLE;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_TOP_MIDDLE;
|
||||
success = true;
|
||||
} else if (!strcmp("top-right", str)) {
|
||||
*relative_config = HIKARI_POSITION_CONFIG_RELATIVE_TOP_RIGHT;
|
||||
relative_config = HIKARI_POSITION_CONFIG_RELATIVE_TOP_RIGHT;
|
||||
success = true;
|
||||
} else {
|
||||
fprintf(stderr, "configuration error: failed to parse \"position\"\n");
|
||||
}
|
||||
|
||||
if (success) {
|
||||
hikari_position_config_set_relative(position_config, relative_config);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_position(const ucl_object_t *position_obj, int *x, int *y)
|
||||
{
|
||||
bool success = false;
|
||||
ucl_object_iter_t it = ucl_object_iterate_new(position_obj);
|
||||
|
||||
int64_t ret_x = 0;
|
||||
int64_t ret_y = 0;
|
||||
bool parsed_x = false;
|
||||
bool parsed_y = false;
|
||||
|
||||
const ucl_object_t *cur;
|
||||
while ((cur = ucl_object_iterate_safe(it, false)) != NULL) {
|
||||
const char *key = ucl_object_key(cur);
|
||||
|
||||
if (!strcmp(key, "x")) {
|
||||
if (!ucl_object_toint_safe(cur, &ret_x)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected integer for \"x\"-coordinate\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
parsed_x = true;
|
||||
} else if (!strcmp(key, "y")) {
|
||||
if (!ucl_object_toint_safe(cur, &ret_y)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected integer for \"y\"-coordinate\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
parsed_y = true;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"configuration error: unknown \"position\" key \"%s\"\n",
|
||||
key);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
if (!parsed_x) {
|
||||
fprintf(stderr,
|
||||
"configuration error: missing \"x\"-coordinate in \"position\2\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!parsed_y) {
|
||||
fprintf(stderr,
|
||||
"configuration error: missing \"y\"-coordinate in \"position\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
*x = ret_x;
|
||||
*y = ret_y;
|
||||
|
||||
done:
|
||||
ucl_object_iterate_free(it);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool
|
||||
hikari_position_config_absolute_parse(
|
||||
struct hikari_position_config *position_config,
|
||||
const ucl_object_t *position_config_obj)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
bool success = false;
|
||||
|
||||
if (!parse_position(position_config_obj, &x, &y)) {
|
||||
fprintf(stderr, "configuration error: failed to parse \"position\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
hikari_position_config_set_absolute(position_config, x, y);
|
||||
|
||||
success = true;
|
||||
|
||||
done:
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool
|
||||
hikari_position_config_parse(struct hikari_position_config *position_config,
|
||||
const ucl_object_t *position_config_obj)
|
||||
{
|
||||
ucl_type_t type = ucl_object_type(position_config_obj);
|
||||
const char *relative;
|
||||
bool success = false;
|
||||
|
||||
switch (type) {
|
||||
case UCL_OBJECT:
|
||||
if (!hikari_position_config_absolute_parse(
|
||||
position_config, position_config_obj)) {
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
|
||||
case UCL_STRING:
|
||||
if (!ucl_object_tostring_safe(position_config_obj, &relative) ||
|
||||
!hikari_position_config_relative_parse(position_config, relative)) {
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"configuration error: expected string or object for \"position\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
done:
|
||||
|
||||
return success;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <ucl.h>
|
||||
|
||||
#include <hikari/geometry.h>
|
||||
#include <hikari/memory.h>
|
||||
|
@ -138,3 +139,131 @@ hikari_view_config_resolve_position(struct hikari_view_config *view_config,
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static char *
|
||||
copy_in_config_string(const ucl_object_t *obj)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool
|
||||
hikari_view_config_parse(
|
||||
struct hikari_view_config *view_config, const ucl_object_t *view_config_obj)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
hikari_view_config_init(view_config);
|
||||
|
||||
ucl_object_iter_t it = ucl_object_iterate_new(view_config_obj);
|
||||
|
||||
const ucl_object_t *cur;
|
||||
while ((cur = ucl_object_iterate_safe(it, false)) != NULL) {
|
||||
const char *key = ucl_object_key(cur);
|
||||
|
||||
if (!strcmp(key, "group")) {
|
||||
char *group_name = copy_in_config_string(cur);
|
||||
if (group_name == NULL) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected string for \"views\" "
|
||||
"\"group\"\n");
|
||||
goto done;
|
||||
} else if (strlen(group_name) == 0) {
|
||||
hikari_free(group_name);
|
||||
fprintf(stderr,
|
||||
"configuration error: expected non-empty string for \"views\" "
|
||||
"\"group\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->group_name = group_name;
|
||||
|
||||
} else if (!strcmp(key, "sheet")) {
|
||||
int64_t sheet_nr;
|
||||
if (!ucl_object_toint_safe(cur, &sheet_nr)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected integer for \"views\" "
|
||||
"\"sheet\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->sheet_nr = sheet_nr;
|
||||
} else if (!strcmp(key, "mark")) {
|
||||
const char *mark_name;
|
||||
|
||||
if (!ucl_object_tostring_safe(cur, &mark_name)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected string for \"views\" \"mark\"");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (strlen(mark_name) != 1) {
|
||||
fprintf(stderr,
|
||||
"configuration error: invalid \"mark\" register \"%s\" for "
|
||||
"\"views\"\n",
|
||||
mark_name);
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->mark = &hikari_marks[mark_name[0] - 'a'];
|
||||
} else if (!strcmp(key, "position")) {
|
||||
if (!hikari_position_config_parse(&view_config->position, cur)) {
|
||||
goto done;
|
||||
}
|
||||
} else if (!strcmp(key, "focus")) {
|
||||
bool focus;
|
||||
|
||||
if (!ucl_object_toboolean_safe(cur, &focus)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected boolean for \"views\" "
|
||||
"\"focus\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->focus = focus;
|
||||
} else if (!strcmp(key, "invisible")) {
|
||||
bool invisible;
|
||||
|
||||
if (!ucl_object_toboolean_safe(cur, &invisible)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected boolean for \"views\" "
|
||||
"\"invisible\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->invisible = invisible;
|
||||
} else if (!strcmp(key, "floating")) {
|
||||
bool floating;
|
||||
|
||||
if (!ucl_object_toboolean_safe(cur, &floating)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected boolean for \"views\" "
|
||||
"\"floating\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->floating = floating;
|
||||
} else if (!strcmp(key, "public")) {
|
||||
bool publicview;
|
||||
|
||||
if (!ucl_object_toboolean_safe(cur, &publicview)) {
|
||||
fprintf(stderr,
|
||||
"configuration error: expected boolean for \"views\" "
|
||||
"\"public\"\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
view_config->publicview = publicview;
|
||||
} else {
|
||||
fprintf(
|
||||
stderr, "configuration error: unkown \"views\" key \"%s\"\n", key);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
success = true;
|
||||
|
||||
done:
|
||||
ucl_object_iterate_free(it);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue