File: widget_commands.c

package info (click to toggle)
lcdproc 0.5.7-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,012 kB
  • ctags: 6,532
  • sloc: ansic: 58,028; sh: 2,005; perl: 681; makefile: 482
file content (451 lines) | stat: -rw-r--r-- 11,272 bytes parent folder | download | duplicates (6)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/** \file server/commands/widget_commands.c
 * Implements handlers for client commands concerning widgets.
 *
 * This contains definitions for all the functions which clients can run.
 * The functions here are to be called only from parse.c's interpreter.
 *
 * The client's available function set is defined here, as is the syntax
 * for each command.
 */

/* 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
 *               2002, Joris Robijn
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "shared/report.h"
#include "shared/sockets.h"

#include "client.h"
#include "screen.h"
#include "widget.h"
#include "drivers.h"
#include "widget_commands.h"


/**
 * Adds a widget to a screen, but doesn't give it a value
 *
 *\verbatim
 * Usage: widget_add <screenid> <widgetid> <widgettype> [-in <id>]
 *\endverbatim
 */
int
widget_add_func(Client *c, int argc, char **argv)
{
	int err;
	char *sid;
	char *wid;
	WidgetType wtype;
	Screen * s;
	Widget * w;

	if (c->state != ACTIVE)
		return 1;

	if ((argc < 4) || (argc > 6)) {
		sock_send_error(c->sock, "Usage: widget_add <screenid> <widgetid> <widgettype> [-in <id>]\n");
		return 0;
	}

	sid = argv[1];
	wid = argv[2];

	s = client_find_screen(c, sid);
	if (s == NULL) {
		sock_send_error(c->sock, "Invalid screen id\n");
		return 0;
	}

	/* Find widget type */
	wtype = widget_typename_to_type(argv[3]);
	if (wtype == WID_NONE) {
		sock_send_error(c->sock, "Invalid widget type\n");
		return 0;
	}

	/* Check for additional flags...*/
	if (argc > 4) {
		char *p = argv[4];

		/* ignore leading '-' in options: we allow both forms */
		if (*p == '-')
			p++;

		/* Handle the "in" flag to place widgets in a container...*/
		if (strcmp(p, "in") == 0) {
			Widget *frame;

			if (argc < 6) {
				sock_send_error(c->sock, "Specify a frame to place widget in\n");
				return 0;
			}

			/* Now we replace s with the framescreen.
			 * This way it will not be plaed in the normal screen
			 * but in the framescreen.
			 */
			frame = screen_find_widget(s, argv[5]);
			if (frame == NULL) {
				sock_send_error(c->sock, "Error finding frame\n");
				return 0;
			}
			s = frame->frame_screen;
		}
	}

	/* Create the widget */
	w = widget_create(wid, wtype, s);
	if (w == NULL) {
		sock_send_error(c->sock, "Error adding widget\n");
		return 0;
	}

	/* Add the widget to the screen */
	err = screen_add_widget(s, w);
	if (err == 0)
		sock_send_string(c->sock, "success\n");
	else
		sock_send_error(c->sock, "Error adding widget\n");

	return 0;
}

/**
 * Removes a widget from a screen, and forgets about it
 *
 *\verbatim
 * Usage: widget_del <screenid> <widgetid>
 *\endverbatim
 */
int
widget_del_func(Client *c, int argc, char **argv)
{
	int err = 0;

	char *sid;
	char *wid;
	Screen *s;
	Widget *w;

	if (c->state != ACTIVE)
		return 1;

	if (argc != 3) {
		sock_send_error(c->sock, "Usage: widget_del <screenid> <widgetid>\n");
		return 0;
	}

	sid = argv[1];
	wid = argv[2];

	debug(RPT_DEBUG, "screen_del: Deleting widget %s.%s", sid, wid);

	s = client_find_screen(c, sid);
	if (s == NULL) {
		sock_send_error(c->sock, "Invalid screen id\n");
		return 0;
	}

	w = screen_find_widget(s, wid);
	if (w == NULL) {
		sock_send_error(c->sock, "Invalid widget id\n");
		return 0;
	}

	err = screen_remove_widget(s, w);
	if (err == 0)
		sock_send_string(c->sock, "success\n");
	else
		sock_send_error(c->sock, "Error removing widget\n");

	return 0;
}

/**
 * Configures information about a widget, such as its size, shape,
 * contents, position, speed, etc.
 *
 *\verbatim
 * widget_set <screenid> <widgetid> <widget-SPECIFIC-data>
 *\endverbatim
 */
int
widget_set_func(Client *c, int argc, char **argv)
{
	int i;
	char *wid;
	char *sid;

	int x, y;
	int left, top, right, bottom;
	int length, direction;
	int width, height;
	int speed;
	Screen *s;
	Widget *w;

	if (c->state != ACTIVE)
		return 1;

	/* If there weren't enough parameters...
	 * We can't test for too many, since each widget may have a
	 * different number - plus, if the argument count is wrong, what ELSE
	 * could be wrong...?
	 */

	if (argc < 4) {
		sock_send_error(c->sock, "Usage: widget_set <screenid> <widgetid> <widget-SPECIFIC-data>\n");
		return 0;
	}

	/* Find screen */
	sid = argv[1];
	s = client_find_screen(c, sid);
	if (s == NULL) {
		sock_send_error(c->sock, "Unknown screen id\n");
		return 0;
	}
	/* Find widget */
	wid = argv[2];
	w = screen_find_widget(s, wid);
	if (w == NULL) {
		sock_send_error(c->sock, "Unknown widget id\n");
		/* Client Debugging...*/
		{
			int i;
			report(RPT_WARNING, "Unknown widget id (%s)", argv[2]);
			for (i = 0; i < argc; i++)
				report(RPT_WARNING, "    %.40s ", argv[i]);
		}
		return 0;
	}
	i = 3;
	switch (w->type) {
	case WID_STRING:		/* String takes "x y text" */
		if (argc != i + 3)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if ((!isdigit((unsigned int) argv[i][0])) ||
			    (!isdigit((unsigned int) argv[i + 1][0]))) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			}
			else {					  /* Set all the data...*/
				x = atoi(argv[i]);
				y = atoi(argv[i + 1]);
				w->x = x;
				w->y = y;
				if (w->text != NULL)
					free(w->text);
				w->text = strdup(argv[i + 2]);
				if (w->text == NULL) {
					report(RPT_WARNING, "widget_set_func: Allocation error");
					return -1;
				}
				debug(RPT_DEBUG, "Widget %s set to %s", wid, w->text);
				sock_send_string(c->sock, "success\n");
			}
		}
		break;
	case WID_HBAR:			/* Hbar takes "x y length" */
		if (argc != i + 3)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if ((!isdigit((unsigned int) argv[i][0])) ||
			    (!isdigit((unsigned int) argv[i + 1][0]))) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			} else {
				x = atoi(argv[i]);
				y = atoi(argv[i + 1]);
				length = atoi(argv[i + 2]);
				w->x = x;
				w->y = y;
				w->length = length;	/* This is the length in pixels */
			}
			debug(RPT_DEBUG, "Widget %s set to %i", wid, w->length);
			sock_send_string(c->sock, "success\n");
		}
		break;
	case WID_VBAR:			/* Vbar takes "x y length" */
		if (argc != i + 3)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if ((!isdigit((unsigned int) argv[i][0])) ||
			    (!isdigit((unsigned int) argv[i + 1][0]))) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			} else {
				x = atoi(argv[i]);
				y = atoi(argv[i + 1]);
				length = atoi(argv[i + 2]);
				w->x = x;
				w->y = y;
				w->length = length;
			}
			debug(RPT_DEBUG, "Widget %s set to %i", wid, w->length);
			sock_send_string(c->sock, "success\n");
		}
		break;
	case WID_ICON:			/* Icon takes "x y icon" */
		if (argc != i + 3)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if ((!isdigit((unsigned int) argv[i][0])) ||
			    (!isdigit((unsigned int) argv[i + 1][0]))) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			} else {
				int icon;

				x = atoi(argv[i]);
				y = atoi(argv[i + 1]);
				icon = widget_iconname_to_icon(argv[i + 2]);
				if (icon == -1) {
					sock_send_error(c->sock, "Invalid icon name\n");
				}
				else {
					w->x = x;
					w->y = y;
					w->length = icon;
					sock_send_string(c->sock, "success\n");
				}
			}
		}
		break;
	case WID_TITLE:			/* title takes "text" */
		if (argc != i + 1)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if (w->text != NULL)
				free(w->text);
			w->text = strdup(argv[i]);
			if (w->text == NULL) {
				report(RPT_WARNING, "widget_set_func: Allocation error");
				return -1;
			}
			/* Set width too */
			w->width = display_props->width;
			debug(RPT_DEBUG, "Widget %s set to %s", wid, w->text);
			sock_send_string(c->sock, "success\n");
		}
		break;
	case WID_SCROLLER:		/* Scroller takes "left top right bottom direction speed text" */
		if (argc != i + 7)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if ((!isdigit((unsigned int) argv[i][0])) ||
			    (!isdigit((unsigned int) argv[i + 1][0])) ||
			    (!isdigit((unsigned int) argv[i + 2][0])) ||
			    (!isdigit((unsigned int) argv[i + 3][0]))) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			}
			else {
				left = atoi(argv[i]);
				top = atoi(argv[i + 1]);
				right = atoi(argv[i + 2]);
				bottom = atoi(argv[i + 3]);
				direction = (int) (argv[i + 4][0]);
				speed = atoi(argv[i + 5]);
				/* Direction must be m, v or h*/
				if (((char) direction != 'h') && ((char) direction != 'v') &&
				    ((char) direction != 'm')) {
					sock_send_error(c->sock, "Invalid direction\n");
				}
				else {
					w->left = left;
					w->top = top;
					w->right = right;
					w->bottom = bottom;
					w->length = direction;
					w->speed = speed;
					if (w->text != NULL)
						free(w->text);
					w->text = strdup(argv[i + 6]);
					if (w->text == NULL) {
						sock_send_error(c->sock, "Allocation error\n");
						return -1;
					}
					debug(RPT_DEBUG, "Widget %s set to %s", wid, w->text);
					sock_send_string(c->sock, "success\n");
				}
			}
		}
		break;
	case WID_FRAME:			/* Frame takes "left top right bottom wid hgt direction speed" */
		if (argc != i + 8)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if ((!isdigit((unsigned int) argv[i][0])) ||
			    (!isdigit((unsigned int) argv[i + 1][0])) ||
			    (!isdigit((unsigned int) argv[i + 2][0])) ||
			    (!isdigit((unsigned int) argv[i + 3][0])) ||
			    (!isdigit((unsigned int) argv[i + 4][0])) ||
			    (!isdigit((unsigned int) argv[i + 5][0]))) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			}
			else {
				left = atoi(argv[i]);
				top = atoi(argv[i + 1]);
				right = atoi(argv[i + 2]);
				bottom = atoi(argv[i + 3]);
				width = atoi(argv[i + 4]);
				height = atoi(argv[i + 5]);
				direction = (int) (argv[i + 6][0]);
				speed = atoi(argv[i + 7]);
				/* Direction must be v or h*/
				if (((char) direction != 'h') && ((char) direction != 'v')) {
					sock_send_error(c->sock, "Invalid direction\n");
				}
				else {
					w->left = left;
					w->top = top;
					w->right = right;
					w->bottom = bottom;
					w->width = width;
					w->height = height;
					w->length = direction;
					w->speed = speed;
					debug(RPT_DEBUG, "Widget %s set to (%i,%i)-(%i,%i) %ix%i", wid, left, top, right, bottom, width, height);
					sock_send_string(c->sock, "success\n");
				}
			}
		}
		break;
	case WID_NUM:			/* Num takes "x num" */
		if (argc != i + 2)
			sock_send_error(c->sock, "Wrong number of arguments\n");
		else {
			if (!isdigit((unsigned int) argv[i][0])) {
				sock_send_error(c->sock, "Invalid coordinates\n");
			}
			else if (!isdigit((unsigned int) argv[i + 1][0])) {
				sock_send_error(c->sock, "Invalid number\n");
			}
			else {
				x = atoi(argv[i]);
				y = atoi(argv[i + 1]);
				w->x = x;
				w->y = y;
			}
			debug(RPT_DEBUG, "Widget %s set to %i", wid, w->y);
			sock_send_string(c->sock, "success\n");
		}
		break;
	case WID_NONE:
	default:
		sock_send_error(c->sock, "Widget has no type\n");
		break;
	}

	return 0;
}