File: screen.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (244 lines) | stat: -rw-r--r-- 5,156 bytes parent folder | download
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
/** \file server/screen.c
 * This file stores all the screen definition-handling code. Functions here
 * provide means to create new screens and destroy existing ones. Screens are
 * identified by client and by the client's own identifiers for screens.
 */

/* This file is part of LCDd, the lcdproc server.
 *
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 1999, William Ferrell, Selene Scriven
 *		 2003, Joris Robijn
 *               2008, Peter Marschall
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "shared/report.h"

#include "drivers.h"

#include "clients.h"
#include "widget.h"
#include "screenlist.h"
#include "menuscreens.h"
#include "main.h"
#include "render.h"

int  default_duration = 0;
int  default_timeout  = -1;

char *pri_names[] = {
	"hidden",
	"background",
	"info",
	"foreground",
	"alert",
	"input",
	NULL,
};


/** Create a screen.
 * \param id      Screen id; it's name.
 * \param client  Client, the screen belongs to.
 * \return        Pointer to freshly created screen.
 */
Screen *
screen_create(char *id, Client *client)
{
	Screen *s;

	debug(RPT_DEBUG, "%s(id=\"%.40s\", client=[%d])",
		 __FUNCTION__, id, (client?client->sock:-1));

	if (!id) {
		report(RPT_ERR, "%s: Need id string", __FUNCTION__);
		return NULL;
	}
	/* Client can be NULL for serverscreens and other client-less screens */

	s = malloc(sizeof(Screen));
	if (s == NULL) {
		report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
		return NULL;
	}

	s->id = strdup(id);
	if (s->id == NULL) {
		report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
		free(s);
		return NULL;
	}

	s->name = NULL;
	s->priority = PRI_INFO;
	s->duration = default_duration;
	s->heartbeat = HEARTBEAT_OPEN;
	s->width = display_props->width;
	s->height = display_props->height;
	s->keys = NULL;
	s->client = client;
	s->widgetlist = NULL;
	s->timeout = default_timeout; 	/*ignored unless greater than 0.*/
	s->backlight = BACKLIGHT_OPEN;		/*Lets the screen do it's own*/
						/*or do what the client says.*/
	s->cursor = CURSOR_OFF;
	s->cursor_x = 1;
	s->cursor_y = 1;

	s->widgetlist = LL_new();
	if (s->widgetlist == NULL) {
		report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
		free(s->id);
		free(s);
		return NULL;
	}

	menuscreen_add_screen(s);

	return s;
}


/** Destroy a screen.
 * \param s    Screen to destroy.
 * \retval <0  Error; no screen given.
 * \retval  0  Success.
 */
int
screen_destroy(Screen *s)
{
	Widget *w;

	debug(RPT_DEBUG, "%s(s=[%.40s])", __FUNCTION__, s->id);

	menuscreen_remove_screen(s);

	screenlist_remove(s);

	for (w = LL_GetFirst(s->widgetlist); w; w = LL_GetNext(s->widgetlist)) {
		/* Free a widget...*/
		widget_destroy(w);
	}
	LL_Destroy(s->widgetlist);
	s->widgetlist = NULL;

	if (s->id != NULL) {
		free(s->id);
		s->id = NULL;
	}
	if (s->name != NULL) {
		free(s->name);
		s->name = NULL;
	}

	free(s);
	s = NULL;

	return 0;
}


/** Add a widget to a screen.
 * \param s  Screen to add the widget \c w to.
 * \param w  Widget to be added to \c s.
 * \retval <0  Error.
 * \retval  0  Success.
 */
int
screen_add_widget(Screen *s, Widget *w)
{
	debug(RPT_DEBUG, "%s(s=[%.40s], widget=[%.40s])", __FUNCTION__, s->id, w->id);

	LL_Push(s->widgetlist, (void *) w);

	return 0;
}


/** Remove a widget from a screen.
 * \param s  Screen to remove the widget \c w from.
 * \param w  Widget to be removed from \c s.
 * \retval <0  Error.
 * \retval  0  Success.
 */
int
screen_remove_widget(Screen *s, Widget *w)
{
	debug(RPT_DEBUG, "%s(s=[%.40s], widget=[%.40s])", __FUNCTION__, s->id, w->id);

	LL_Remove(s->widgetlist, (void *) w, NEXT);

	return 0;
}


/** Find a widget on a screen by its id.
 * \param s   Screen where to look for the widget.
 * \param id  Identifier of the widget.
 * \return    Pointerr to the widget; \c NULL if widget was not found or error.
 */
Widget *
screen_find_widget(Screen *s, char *id)
{
	Widget *w;

	if (!s)
		return NULL;
	if (!id)
		return NULL;

	debug(RPT_DEBUG, "%s(s=[%.40s], id=\"%.40s\")", __FUNCTION__, s->id, id);

	for (w = LL_GetFirst(s->widgetlist); w != NULL; w = LL_GetNext(s->widgetlist)) {
		if (0 == strcmp(w->id, id)) {
			debug(RPT_DEBUG, "%s: Found %s", __FUNCTION__, id);
			return w;
		}
		/* Search subscreens recursively */
		if (w->type == WID_FRAME) {
			w = widget_search_subs(w, id);
			if (w != NULL)
				return w;
		}
	}
	debug(RPT_DEBUG, "%s: Not found", __FUNCTION__);
	return NULL;
}


/** Convert a priority name to the priority id.
 * \param priname  Name of the screen priority.
 * \return  Priority id associated with \c priname, -1 if no matching priority
 *          id could be found.
 */
Priority
screen_pri_name_to_pri(char *priname)
{
	Priority pri = -1;
	int i;

	for (i = 0; pri_names[i]; i++) {
		if (strcmp(pri_names[i], priname) == 0) {
			pri = i;
			break; /* it's valid: skip out...*/
		}
	}
	return pri;
}


/** Convert a priority id to the associated name.
 * \param pri  Priority id.
 * \return     Priority name associated with \c pri.
 */
char *
screen_pri_to_pri_name(Priority pri)
{
	return pri_names[pri];
}