1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #include "../shared/debug.h" |
---|
6 | |
---|
7 | #include "drivers/lcd.h" |
---|
8 | |
---|
9 | #include "clients.h" |
---|
10 | #include "client_data.h" |
---|
11 | #include "widget.h" |
---|
12 | #include "screenlist.h" |
---|
13 | #include "screen.h" |
---|
14 | |
---|
15 | |
---|
16 | screen * screen_create() |
---|
17 | { |
---|
18 | screen *s; |
---|
19 | |
---|
20 | s = malloc(sizeof(screen)); |
---|
21 | if(!s) |
---|
22 | { |
---|
23 | fprintf(stderr, "screen_create: Error allocating new screen\n"); |
---|
24 | return NULL; |
---|
25 | } |
---|
26 | |
---|
27 | s->id=NULL; |
---|
28 | s->name=NULL; |
---|
29 | s->priority=128; |
---|
30 | // TODO: Make the screen duration configurable! |
---|
31 | s->duration = 128; |
---|
32 | s->heartbeat = 1; |
---|
33 | s->wid = lcd.wid; |
---|
34 | s->hgt = lcd.hgt; |
---|
35 | s->parent = NULL; |
---|
36 | s->widgets=NULL; |
---|
37 | |
---|
38 | s->widgets = LL_new(); |
---|
39 | if(!s->widgets) |
---|
40 | { |
---|
41 | fprintf(stderr, "screen_create: Error allocating widget list\n"); |
---|
42 | return NULL; |
---|
43 | } |
---|
44 | |
---|
45 | return s; |
---|
46 | } |
---|
47 | |
---|
48 | int screen_destroy(screen *s) |
---|
49 | { |
---|
50 | widget *w; |
---|
51 | |
---|
52 | if(!s) return -1; |
---|
53 | |
---|
54 | LL_Rewind(s->widgets); |
---|
55 | do { |
---|
56 | // Free a widget... |
---|
57 | w = LL_Get(s->widgets); |
---|
58 | widget_destroy(w); |
---|
59 | } while (LL_Next(s->widgets) == 0); |
---|
60 | LL_Destroy(s->widgets); |
---|
61 | |
---|
62 | |
---|
63 | if(s->id) |
---|
64 | free(s->id); |
---|
65 | if(s->name) |
---|
66 | free(s->name); |
---|
67 | |
---|
68 | free(s); |
---|
69 | |
---|
70 | return 0; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | screen *screen_find(client *c, char *id) |
---|
75 | { |
---|
76 | screen *s; |
---|
77 | |
---|
78 | if(!c) return NULL; |
---|
79 | if(!id) return NULL; |
---|
80 | |
---|
81 | debug("client_find_screen(%s)\n", id); |
---|
82 | |
---|
83 | LL_Rewind(c->data->screenlist); |
---|
84 | do { |
---|
85 | s = LL_Get(c->data->screenlist); |
---|
86 | if( (s) && (0 == strcmp(s->id, id))) |
---|
87 | { |
---|
88 | debug("client_find_screen: Found %s\n", id); |
---|
89 | return s; |
---|
90 | } |
---|
91 | } while(LL_Next(c->data->screenlist) == 0); |
---|
92 | |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | int screen_add(client *c, char *id) |
---|
98 | { |
---|
99 | screen *s; |
---|
100 | |
---|
101 | if(!c) return -1; |
---|
102 | if(!id) return -1; |
---|
103 | |
---|
104 | // Make sure this screen doesn't already exist... |
---|
105 | s = screen_find(c, id); |
---|
106 | if(s) |
---|
107 | { |
---|
108 | return 1; |
---|
109 | } |
---|
110 | |
---|
111 | s = screen_create(); |
---|
112 | if(!s) |
---|
113 | { |
---|
114 | fprintf(stderr, "screen_add: Error creating screen\n"); |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | s->parent = c; |
---|
119 | |
---|
120 | s->id = strdup(id); |
---|
121 | if(!s->id) |
---|
122 | { |
---|
123 | fprintf(stderr, "screen_add: Error allocating name\n"); |
---|
124 | return -1; |
---|
125 | } |
---|
126 | |
---|
127 | // TODO: Check for errors here? |
---|
128 | LL_Push(c->data->screenlist, (void *)s); |
---|
129 | |
---|
130 | |
---|
131 | // Now, add it to the screenlist... |
---|
132 | if(screenlist_add(s) < 0) |
---|
133 | { |
---|
134 | fprintf(stderr, "screen_add: Error queueing new screen\n"); |
---|
135 | return -1; |
---|
136 | } |
---|
137 | |
---|
138 | return 0; |
---|
139 | } |
---|
140 | |
---|
141 | int screen_remove(client *c, char *id) |
---|
142 | { |
---|
143 | screen *s; |
---|
144 | |
---|
145 | if(!c) return -1; |
---|
146 | if(!id) return -1; |
---|
147 | |
---|
148 | // Make sure this screen *does* exist... |
---|
149 | s = screen_find(c, id); |
---|
150 | if(!s) |
---|
151 | { |
---|
152 | fprintf(stderr, "screen_remove: Error finding screen %s\n", id); |
---|
153 | return 1; |
---|
154 | } |
---|
155 | |
---|
156 | // TODO: Check for errors here? |
---|
157 | LL_Remove(c->data->screenlist, (void *)s); |
---|
158 | |
---|
159 | // ... and here? |
---|
160 | screen_destroy(s); |
---|
161 | |
---|
162 | // Now, remove it from the screenlist... |
---|
163 | if(screenlist_remove_all(s) < 0) |
---|
164 | { |
---|
165 | // Not a serious error.. |
---|
166 | fprintf(stderr, "screen_remove: Error dequeueing screen\n"); |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | return 0; |
---|
171 | } |
---|