-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
171 lines (154 loc) · 4.3 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "image.h"
#include "render.h"
#include "wl.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int FPS = 10;
static struct wayab_rule *wayab_rule_parse(const char *str) {
int left, count;
left = count = 0;
struct wayab_rule *ptr = malloc(sizeof(struct wayab_rule));
ptr->dir = NULL;
ptr->output_name = NULL;
ptr->resize = NONE;
ptr->anchor_x = 0.5;
ptr->anchor_y = 0.5;
wl_list_init(&ptr->link);
for (int i = 0; i < strlen(str); ++i) {
if (str[i] == ':') {
switch (count++) {
case 0: {
int output_len = i - left;
ptr->output_name = malloc((output_len + 1) * sizeof(char));
memcpy(ptr->output_name, str, output_len);
ptr->output_name[output_len] = '\0';
LOG("output_name: %s\n", ptr->output_name);
break;
}
case 1: {
int dir_len = i - left;
ptr->dir = malloc((dir_len + 1) * sizeof(char));
memcpy(ptr->dir, str + left, dir_len);
ptr->dir[dir_len] = '\0';
LOG("dir: %s\n", ptr->dir);
break;
}
case 2: {
int resize_len = i - left;
char resize[resize_len + 1];
memcpy(resize, str + left, resize_len);
resize[resize_len] = '\0';
LOG("resize: %s\n", resize);
if (strcmp(resize, "none") == 0) {
ptr->resize = NONE;
}
if (strcmp(resize, "fit") == 0) {
ptr->resize = FIT;
}
if (strcmp(resize, "fill") == 0) {
ptr->resize = FILL;
}
if (strcmp(resize, "stretch") == 0) {
ptr->resize = STRETCH;
}
if (strcmp(resize, "tile") == 0) {
ptr->resize = TILE;
}
// break;
}
case 3: {
for (int j = i + 1; j < strlen(str); j++) {
if (str[j] == ',') {
int xlen = j - i - 1;
int ylen = strlen(str) - j - 1;
char x[xlen + 1];
char y[ylen + 1];
memcpy(x, str + i + 1, xlen);
memcpy(y, str + i + 1 + xlen + 1, ylen);
x[xlen] = '\0';
y[ylen] = '\0';
ptr->anchor_x = atof(x);
ptr->anchor_y = atof(y);
LOG("x(%d): %f, y(%d): %f\n", xlen, ptr->anchor_x, ylen,
ptr->anchor_y);
}
}
break;
}
default:
break;
}
left = i + 1;
}
}
if (count == 1) {
int dir_len = strlen(str) - left;
ptr->dir = malloc((dir_len + 1) * sizeof(char));
memcpy(ptr->dir, str + left, dir_len);
ptr->dir[dir_len] = '\0';
LOG("dir: %s\n", ptr->dir);
}
if (ptr->dir == NULL || ptr->output_name == NULL) {
goto error;
}
return ptr;
error:
if (ptr) {
if (ptr->dir)
free(ptr->dir);
if (ptr->output_name)
free(ptr->output_name);
free(ptr);
}
return NULL;
}
int main(int argc, char **argv) {
struct wayab_config config = {0};
config.fps = FPS;
wl_list_init(&config.rules);
int opt = 0;
while ((opt = getopt(argc, argv, "hf:o:")) != -1) {
switch (opt) {
case 'h':
printf("Usage: wayab -f <fps> -o <output>:<path>:<resize>:<anchor>\n");
printf("\n");
printf("-f: fps, default to %d.\n", FPS);
printf(
"-o: output configs\n"
" - <output> is the output name, like eDP-1/DP-2/.., for all "
"outputs, use *\n"
" - <path> is the path which contains all frames(need to be PNG) of "
"some animated "
"image\n"
" - <resize>(optional) could be none/fill/fit/stretch/tile, default "
"to none\n"
" - <anchor>(optional) is a coordinate in range (0.0,0.0) to "
"(1.0,1.0), default to 0.5,0.5\n"
" - 0.0,0.0 means left-top\n"
" - 1.0,0.0 means right-top\n"
" - 0.0,1.0 means left-bottom\n"
" - 1.0,1.0 means right-bottom\n"
"For example: wayab -f 10 -o *:/tmp/a\n");
return 0;
case 'f':
config.fps = atoi(optarg);
break;
case 'o': {
struct wayab_rule *rule = wayab_rule_parse(optarg);
if (rule) {
wl_list_insert(&config.rules, &rule->link);
}
break;
}
}
}
struct wayab_wl *wl = wayab_wl_new(&config);
if (wl == NULL) {
LOG("wayab_wl_new");
return -1;
}
wayab_wl_loop(wl);
wayab_wl_destroy(wl);
return 0;
}