-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
310 lines (270 loc) · 6.47 KB
/
queue.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "queue.h"
#include "config.h"
#include <stdlib.h>
#include <string.h>
static enum srv_queue_gravity_t to_gravity(const char* str)
{
if(strcmp(str, "top_left") == 0)
return TOP_LEFT;
else if(strcmp(str, "top_right") == 0)
return TOP_RIGHT;
else if(strcmp(str, "bottom_left") == 0)
return BOTTOM_LEFT;
else if(strcmp(str, "bottom_right") == 0)
return BOTTOM_RIGHT;
else
return TOP_RIGHT;
}
static void load_queue_config(srv_screen_t* scr, srv_queue_t* q)
{
const char* gravity;
uint32_t hori;
uint32_t vert;
gravity = get_string("global.gravity");
if(!gravity)
q->gravity = TOP_RIGHT;
else
q->gravity = to_gravity(gravity);
if(has_entry("global.padding.vert"))
vert = get_int("global.padding.vert");
else
vert = 15;
q->init_dec = vert;
if(has_entry("global.padding.hori"))
hori = get_int("global.padding.hori");
else
hori = 15;
if(has_entry("global.padding.space"))
q->space = get_int("global.padding.space");
else
q->space = 15;
switch(q->gravity) {
case TOP_LEFT:
q->hori_dec = hori;
q->vert_dec = vert;
break;
case TOP_RIGHT:
q->hori_dec = scr->w - hori;
q->vert_dec = vert;
break;
case BOTTOM_LEFT:
q->hori_dec = hori;
q->vert_dec = scr->h - vert;
break;
case BOTTOM_RIGHT:
q->hori_dec = scr->w - hori;
q->vert_dec = scr->h - vert;
break;
default:
break;
}
}
srv_queue_t* init_queue(xcb_connection_t* c, srv_screen_t* scr)
{
srv_queue_t* q = malloc(sizeof(srv_queue_t));
q->c = c;
q->scr = scr;
q->first = NULL;
load_queue_config(scr, q);
return q;
}
static void free_queued(srv_queue_item_t* item)
{
if(item->next)
item->next->prev = item->prev;
if(item->prev)
item->prev->next = item->next;
if(item == item->parent->first)
item->parent->first = item->next;
if(item->timer)
free(item->timer);
free_notif(item->parent->c, item->notif);
free(item);
}
void close_queue(srv_queue_t* q)
{
clear_queue(q);
free(q);
}
void rm_top(srv_queue_t* q)
{
if(q->first)
rm_notif(q->first);
}
void clear_queue(srv_queue_t* q)
{
srv_queue_item_t* it = q->first;
srv_queue_item_t* tmp;
while(it) {
tmp = it->next;
free_queued(it);
it = tmp;
}
}
static void get_position(srv_queue_t* q, uint32_t* x, uint32_t* y, uint32_t w, uint32_t h)
{
switch(q->gravity)
{
case TOP_LEFT:
*x = q->hori_dec;
break;
case TOP_RIGHT:
*x = q->hori_dec - w;
break;
case BOTTOM_LEFT:
*x = q->hori_dec;
*y = q->vert_dec - *y - h;
break;
case BOTTOM_RIGHT:
*x = q->hori_dec - w;
*y = q->vert_dec - *y - h;
break;
default:
break;
}
}
static void queue_update(srv_queue_t* q)
{
uint32_t x, y, dy;
int toshow = 1;
srv_queue_item_t* it = q->first;
y = q->init_dec;
while(it) {
if(!toshow) {
it->on_screen = 0;
show_window(q->c, it->notif->win, 0);
it = it->next;
continue;
}
if(y + it->notif->win.height > q->scr->h) {
it->on_screen = 0;
show_window(q->c, it->notif->win, 0);
toshow = 0;
it = it->next;
continue;
}
dy = y;
get_position(q, &x, &dy, it->notif->win.width, it->notif->win.height);
it->on_screen = 1;
show_window(q->c, it->notif->win, 1);
move_window(q->c, it->notif->win, x, dy);
y += it->notif->win.height;
y += q->space;
it = it->next;
}
}
srv_queue_item_t* add_notif(srv_queue_t* q, uint32_t time, const char* name, const char* text)
{
srv_queue_item_t* last = q->first;
srv_queue_item_t* it = malloc(sizeof(srv_queue_item_t));
if(last) {
while(last->next) last = last->next;
it->prev = last;
last->next = it;
}
else {
it->prev = NULL;
q->first = it;
}
it->parent = q;
it->next = NULL;
it->notif = create_notif(q->c, q->scr, 0, name, text);
it->timer = start_timer(time);
queue_update(q);
return it;
}
void rm_notif(srv_queue_item_t* item)
{
srv_queue_t* q = item->parent;
free_queued(item);
queue_update(q);
}
void draw_queue(srv_queue_t* q)
{
srv_queue_item_t* it = q->first;
while(it && it->on_screen) {
draw_notif(q->c, it->notif);
it = it->next;
}
}
void rm_notif_cond(srv_queue_t* q)
{
srv_queue_item_t* it = q->first;
srv_queue_item_t* next;
while(it) {
next = it->next;
if(timer_ended(it->timer))
free_queued(it);
it = next;
}
queue_update(q);
}
uint32_t nearest_end(srv_queue_t* q)
{
srv_queue_item_t* it = q->first;
uint32_t nearest = 60000;
uint32_t left;
while(it) {
left = time_left(it->timer);
nearest = (nearest > left ? left : nearest);
it = it->next;
}
return nearest;
}
static int is_number(const char* str)
{
uint32_t i;
for(i = 0; i < strlen(str); ++i) {
if(str[i] < '0' || str[i] > '9')
return 0;
}
return 1;
}
static uint32_t to_number(const char* str)
{
uint32_t nb, i, exp, j;
nb = 0;
for(i = 0; i < strlen(str); ++i) {
exp = 1;
for(j = 0; j < strlen(str) - i - 1; ++j)
exp *= 10;
nb += (str[i] - '0') * exp;
}
return nb;
}
srv_queue_item_t* add_notif_str(srv_queue_t* q, const char* str)
{
uint32_t time;
const char* name;
const char* text;
char* buffer;
const char* part;
srv_queue_item_t* it;
buffer = malloc(strlen(str) + 1);
strcpy(buffer, str);
part = strtok(buffer, " ");
if(!part) {
free(buffer);
return NULL;
}
if(is_number(part)) {
time = to_number(part);
part = strtok(NULL, " ");
}
else
time = 60000;
if(!part || !has_gcontext(part)) {
free(buffer);
return NULL;
}
name = part;
part = strtok(NULL, "\n");
if(!part) {
free(buffer);
return NULL;
}
text = part;
it = add_notif(q, time, name, text);
free(buffer);
return it;
}