aboutsummaryrefslogtreecommitdiffstats
path: root/swaynag/types.c
blob: c92d0e89d8850d9a91af0141d81ceb4337b70558 (plain) (blame)
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
#define _XOPEN_SOURCE 500
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include "list.h"
#include "swaynag/config.h"
#include "swaynag/types.h"
#include "util.h"

void swaynag_types_add_default(list_t *types) {
	struct swaynag_type *type_error;
	type_error = calloc(1, sizeof(struct swaynag_type));
	type_error->name = strdup("error");
	type_error->button_background = 0x680A0AFF;
	type_error->background = 0x900000FF;
	type_error->text = 0xFFFFFFFF;
	type_error->border = 0xD92424FF;
	type_error->border_bottom = 0x470909FF;
	list_add(types, type_error);

	struct swaynag_type *type_warning;
	type_warning = calloc(1, sizeof(struct swaynag_type));
	type_warning->name = strdup("warning");
	type_warning->button_background = 0xFFC100FF;
	type_warning->background = 0xFFA800FF;
	type_warning->text = 0x000000FF;
	type_warning->border = 0xAB7100FF;
	type_warning->border_bottom = 0xAB7100FF;
	list_add(types, type_warning);
}

struct swaynag_type *swaynag_type_get(list_t *types, char *name) {
	for (int i = 0; i < types->length; i++) {
		struct swaynag_type *type = types->items[i];
		if (strcasecmp(type->name, name) == 0) {
			return type;
		}
	}
	return NULL;
}

struct swaynag_type *swaynag_type_clone(struct swaynag_type *type) {
	struct swaynag_type *clone;
	clone = calloc(1, sizeof(struct swaynag_type));
	clone->name = strdup(type->name);
	clone->button_background = type->button_background;
	clone->background = type->background;
	clone->text = type->text;
	clone->border = type->border;
	clone->border_bottom = type->border_bottom;
	return clone;
}

void swaynag_type_free(struct swaynag_type *type) {
	free(type->name);
	free(type);
}

void swaynag_types_free(list_t *types) {
	while (types->length) {
		struct swaynag_type *type = types->items[0];
		swaynag_type_free(type);
		list_del(types, 0);
	}
	list_free(types);
}

int swaynag_parse_type(int argc, char **argv, struct swaynag_type *type) {
	enum color_option {
		COLOR_BACKGROUND,
		COLOR_BORDER,
		COLOR_BORDER_BOTTOM,
		COLOR_BUTTON,
		COLOR_TEXT,
	};

	static struct option opts[] = {
		{"background", required_argument, NULL, COLOR_BACKGROUND},
		{"border", required_argument, NULL, COLOR_BORDER},
		{"border-bottom", required_argument, NULL, COLOR_BORDER_BOTTOM},
		{"button-background", required_argument, NULL, COLOR_BUTTON},
		{"text", required_argument, NULL, COLOR_TEXT},
		{0, 0, 0, 0}
	};

	optind = 1;
	while (1) {
		int c = getopt_long(argc, argv, "", opts, NULL);
		if (c == -1) {
			break;
		}
		switch (c) {
			case COLOR_BACKGROUND:
				type->background = parse_color(optarg);
				break;
			case COLOR_BORDER:
				type->border = parse_color(optarg);
				break;
			case COLOR_BORDER_BOTTOM:
				type->border_bottom = parse_color(optarg);
				break;
			case COLOR_BUTTON:
				type->button_background = parse_color(optarg);
				break;
			case COLOR_TEXT:
				type->text = parse_color(optarg);
				break;
			default:
				break;
		}
	}
	return 0;
}