Base: LCDproc 0.5.2
[lcdproc-de200c.git] / server / screen.c
blobe8e036bdb86f99d9d886155f7f82a3d2aea4797b
1 /*
2 * screen.c
3 * This file is part of LCDd, the lcdproc server.
5 * This file is released under the GNU General Public License. Refer to the
6 * COPYING file distributed with this package.
8 * Copyright (c) 1999, William Ferrell, Scott Scriven
9 * 2003, Joris Robijn
12 * Does screen management
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
20 #include "shared/report.h"
22 #include "drivers.h"
24 #include "clients.h"
25 #include "widget.h"
26 #include "screenlist.h"
27 #include "screen.h"
28 #include "menuscreens.h"
29 #include "main.h"
30 #include "render.h"
32 int default_duration = 0;
33 int default_timeout = -1;
35 char *pri_names[] = {
36 "hidden",
37 "background",
38 "info",
39 "foreground",
40 "alert",
41 "input",
42 NULL,
45 Screen *
46 screen_create(char *id, Client *client)
48 Screen *s;
50 debug(RPT_DEBUG, "%s(id=\"%.40s\", client=[%d])", __FUNCTION__, id, (client?client->sock:-1));
52 s = malloc(sizeof(Screen));
53 if (!s) {
54 report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
55 return NULL;
57 if (!id) {
58 report(RPT_ERR, "%s: Need id string", __FUNCTION__);
59 return NULL;
61 /* Client can be NULL for serverscreens and other client-less screens */
63 s->id = strdup(id);
64 if (!s->id) {
65 report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
66 return NULL;
69 s->name = NULL;
70 s->priority = PRI_INFO;
71 s->duration = default_duration;
72 s->backlight = BACKLIGHT_OPEN;
73 s->heartbeat = HEARTBEAT_OPEN;
74 s->width = display_props->width;
75 s->height = display_props->height;
76 s->keys = NULL;
77 s->client = client;
78 s->widgetlist = NULL;
79 s->timeout = default_timeout; /*ignored unless greater than 0.*/
80 s->backlight = BACKLIGHT_OPEN; /*Lets the screen do it's own*/
81 /*or do what the client says.*/
82 s->cursor = CURSOR_OFF;
83 s->cursor_x = 1;
84 s->cursor_y = 1;
86 s->widgetlist = LL_new();
87 if (!s->widgetlist) {
88 report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
89 return NULL;
92 menuscreen_add_screen(s);
94 return s;
97 int
98 screen_destroy(Screen *s)
100 Widget *w;
102 debug(RPT_DEBUG, "%s(s=[%.40s])", __FUNCTION__, s->id);
104 menuscreen_remove_screen(s);
106 screenlist_remove(s);
108 for (w = LL_GetFirst(s->widgetlist); w; w = LL_GetNext(s->widgetlist)) {
109 /* Free a widget...*/
110 widget_destroy(w);
112 LL_Destroy(s->widgetlist);
114 if (s->id)
115 free(s->id);
116 if (s->name)
117 free(s->name);
119 free(s);
121 return 0;
125 screen_add_widget(Screen *s, Widget *w)
127 debug(RPT_DEBUG, "%s(s=[%.40s], widget=[%.40s])", __FUNCTION__, s->id, w->id);
129 LL_Push(s->widgetlist, (void *) w);
131 return 0;
135 screen_remove_widget(Screen *s, Widget *w)
137 debug(RPT_DEBUG, "%s(s=[%.40s], widget=[%.40s])", __FUNCTION__, s->id, w->id);
139 LL_Remove(s->widgetlist, (void *) w);
141 return 0;
144 Widget *
145 screen_find_widget(Screen *s, char *id)
147 Widget *w;
149 if (!s)
150 return NULL;
151 if (!id)
152 return NULL;
154 debug(RPT_DEBUG, "%s(s=[%.40s], id=\"%.40s\")", __FUNCTION__, s->id, id);
156 for (w = LL_GetFirst(s->widgetlist); w; w = LL_GetNext(s->widgetlist)) {
157 if (0 == strcmp(w->id, id)) {
158 debug(RPT_DEBUG, "%s: Found %s", __FUNCTION__, id);
159 return w;
161 /* Search subscreens recursively */
162 if (w->type == WID_FRAME) {
163 w = widget_search_subs(w, id);
164 if (w)
165 return w;
168 debug(RPT_DEBUG, "%s: Not found", __FUNCTION__);
169 return NULL;
172 Priority
173 screen_pri_name_to_pri(char *priname)
175 Priority pri = WID_NONE;
176 int i;
178 for (i = 0; pri_names[i]; i++) {
179 if (strcmp(pri_names[i], priname) == 0) {
180 pri = i;
181 break; /* it's valid: skip out...*/
184 return pri;
187 char *
188 screen_pri_to_pri_name(Priority pri)
190 return pri_names[pri];