1 | /* |
---|
2 | * neercs console-based window manager |
---|
3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * This program is free software. It comes without any warranty, to |
---|
8 | * the extent permitted by applicable law. You can redistribute it |
---|
9 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
10 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <sys/types.h> |
---|
20 | #include <signal.h> |
---|
21 | #include <sys/wait.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <unistd.h> |
---|
24 | |
---|
25 | #include "neercs.h" |
---|
26 | |
---|
27 | |
---|
28 | int handle_recurrents(struct screen_list *screen_list) |
---|
29 | { |
---|
30 | int refresh = 0, i; |
---|
31 | /* Recurrent functions */ |
---|
32 | for (i = 0; i < screen_list->recurrent_list->count; i++) |
---|
33 | { |
---|
34 | if (screen_list->recurrent_list->recurrent[i]->function) |
---|
35 | { |
---|
36 | refresh |= |
---|
37 | screen_list->recurrent_list->recurrent[i]-> |
---|
38 | function(screen_list, |
---|
39 | screen_list->recurrent_list->recurrent[i], |
---|
40 | screen_list->recurrent_list->recurrent[i]->user, |
---|
41 | get_us()); |
---|
42 | } |
---|
43 | } |
---|
44 | /* Delete recurrent functions */ |
---|
45 | for (i = 0; i < screen_list->recurrent_list->count; i++) |
---|
46 | { |
---|
47 | if (screen_list->recurrent_list->recurrent[i]->kill_me) |
---|
48 | { |
---|
49 | remove_recurrent(screen_list->recurrent_list, i); |
---|
50 | refresh = 1; |
---|
51 | i = 0; |
---|
52 | } |
---|
53 | } |
---|
54 | return refresh; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | /* Add recurrent function. It will be called at each main loop iteration, |
---|
59 | unless it is removed */ |
---|
60 | int add_recurrent(struct recurrent_list *list, |
---|
61 | int (*func) (struct screen_list *, struct recurrent * rec, |
---|
62 | void *user, long long unsigned int t), |
---|
63 | void *user) |
---|
64 | { |
---|
65 | if (list == NULL || func == NULL) |
---|
66 | return -1; |
---|
67 | |
---|
68 | list->recurrent = (struct recurrent **)realloc(list->recurrent, |
---|
69 | sizeof(struct recurrent *) |
---|
70 | * (list->count + 1)); |
---|
71 | |
---|
72 | if (!list->recurrent) |
---|
73 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, |
---|
74 | __LINE__); |
---|
75 | |
---|
76 | list->recurrent[list->count] = malloc(sizeof(struct recurrent)); |
---|
77 | |
---|
78 | list->recurrent[list->count]->kill_me = 0; |
---|
79 | list->recurrent[list->count]->function = func; |
---|
80 | list->recurrent[list->count]->user = user; |
---|
81 | list->recurrent[list->count]->start_time = get_us(); |
---|
82 | list->count++; |
---|
83 | |
---|
84 | return list->count - 1; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | /* Remove recurrent. Do *NOT* call this from a recurrent function. */ |
---|
89 | int remove_recurrent(struct recurrent_list *list, int n) |
---|
90 | { |
---|
91 | |
---|
92 | if (n >= list->count) |
---|
93 | return -1; |
---|
94 | |
---|
95 | memmove(&list->recurrent[n], |
---|
96 | &list->recurrent[n + 1], |
---|
97 | sizeof(struct recurrent *) * (list->count - (n + 1))); |
---|
98 | |
---|
99 | free(list->recurrent[n]); |
---|
100 | list->recurrent = (struct recurrent **)realloc(list->recurrent, |
---|
101 | sizeof(sizeof |
---|
102 | (struct recurrent *)) |
---|
103 | * (list->count)); |
---|
104 | if (!list->recurrent) |
---|
105 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, |
---|
106 | __LINE__); |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | list->count--; |
---|
111 | return 0; |
---|
112 | } |
---|