File: client_commands.c

package info (click to toggle)
lcdproc 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,352 kB
  • sloc: ansic: 53,582; sh: 4,288; perl: 681; makefile: 476
file content (304 lines) | stat: -rw-r--r-- 6,221 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
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
/** \file server/commands/client_commands.c
 * Implements handlers for general client commands.
 *
 * 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 "drivers.h"
#include "render.h"
#include "client.h"
#include "input.h"


/**
 * Debugging only..  prints out a list of arguments it receives
 */
int
test_func_func(Client *c, int argc, char **argv)
{
	int i;

	for (i = 0; i < argc; i++) {
		report(RPT_INFO, "%s: %i -> %s", __FUNCTION__, i, argv[i]);
		sock_printf(c->sock, "%s:  %i -> %s\n", __FUNCTION__, i, argv[i]);
	}
	return 0;
}

/**
 * The client must say "hello" before doing anything else.
 *
 * It sends back a string of info about the server to the client.
 *
 *\verbatim
 * Usage: hello
 *\endverbatim
 *
 * \todo  Give \em real info about the server/lcd
 */
int
hello_func(Client *c, int argc, char **argv)
{
	if (argc > 1) {
		sock_send_error(c->sock, "extra parameters ignored\n");
	}

	debug(RPT_INFO, "Hello!");

	sock_printf(c->sock, "connect LCDproc %s protocol %s lcd wid %i hgt %i cellwid %i cellhgt %i\n",
		VERSION, PROTOCOL_VERSION,
		display_props->width, display_props->height,
		display_props->cellwidth, display_props->cellheight);

	/* make note that client has sent hello */
	c->state = ACTIVE;

	return 0;
}

/**
 * The client should say "bye" before disconnecting
 *
 * The function does not respond to the client: it simply cuts connection.
 *
 *\verbatim
 * Usage: bye
 *\endverbatim
 */
int
bye_func(Client *c, int argc, char **argv)
{
	if (c != NULL) {
		debug(RPT_INFO, "Bye, %s!", (c->name != NULL) ? c->name : "unknown client");

		c->state = GONE;
		//sock_send_error(c->sock, "\"bye\" is currently ignored\n");
	}	
	return 0;
}

/**
 * Sets info about the client, such as its name
 *
 *\verbatim
 * Usage: client_set -name <id>
 *\endverbatim
 */
int
client_set_func(Client *c, int argc, char **argv)
{
	int i;

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

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

	i = 1;
	do {
		char *p = argv[i];

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

		/* Handle the "name" option */
		if (strcmp(p, "name") == 0) {
			i++;
			if (argv[i] == '\0') {
				sock_printf_error(c->sock, "internal error: no parameter #%d\n", i);
				continue;
			}

			debug(RPT_DEBUG, "client_set: name=\"%s\"", argv[i]);

			/* set the name...*/
			if (c->name != NULL)
				free(c->name);

			if ((c->name = strdup(argv[i])) == NULL) {
				sock_send_error(c->sock, "error allocating memory!\n");
			}
			else {
				sock_send_string(c->sock, "success\n");
				i++; /* bypass argument (name string)*/
			}
		}
		else {
			sock_printf_error(c->sock, "invalid parameter (%s)\n", p);
		}
	} while (++i < argc);

	return 0;
}

/**
 * Tells the server the client would like to accept keypresses
 * of a particular type
 *
 *\verbatim
 * Usage: client_add_key [-exclusively|-shared] {<key>}+
 *\endverbatim
 */
int
client_add_key_func(Client *c, int argc, char **argv)
{
	int exclusively = 0;
	int argnr;

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

	if (argc < 2) {
		sock_send_error(c->sock, "Usage: client_add_key [-exclusively|-shared] {<key>}+\n");
		return 0;
	}

	argnr = 1;
	if (argv[argnr][0] == '-') {
		if (strcmp( argv[argnr], "-shared") == 0) {
			exclusively = 0;
		}
		else if(strcmp(argv[argnr], "-exclusively") == 0) {
			exclusively = 1;
		}
		else {
			sock_printf_error(c->sock, "Invalid option: %s\n", argv[argnr]);
		}
		argnr++;
	}
	for ( ; argnr < argc; argnr++) {
		if (input_reserve_key(argv[argnr], exclusively, c) < 0) {
			sock_printf_error(c->sock, "Could not reserve key \"%s\"\n", argv[argnr]);
		}
	}
	sock_send_string(c->sock, "success\n");

	return 0;
}

/**
 * Tells the server the client would NOT like to accept keypresses
 * of a particular type
 *
 *\verbatim
 * Usage: client_del_key {<key>}+
 *\endverbatim
 */
int
client_del_key_func(Client *c, int argc, char **argv)
{
	int argnr;

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

	if (argc < 2) {
		sock_send_error(c->sock, "Usage: client_del_key {<key>}+\n");
		return 0;
	}

	for (argnr = 1; argnr < argc; argnr++) {
		input_release_key(argv[argnr], c);
	}
	sock_send_string(c->sock, "success\n");

	return 0;
}

/**
 * Toggles the backlight, if enabled.
 *
 *\verbatim
 * Usage: backlight {on|off|toggle|blink|flash}
 *\endverbatim
 */
int
backlight_func(Client *c, int argc, char **argv)
{
	if (c->state != ACTIVE)
		return 1;

	if (argc != 2) {
		sock_send_error(c->sock, "Usage: backlight {on|off|toggle|blink|flash}\n");
		return 0;
	}

	debug(RPT_DEBUG, "backlight(%s)", argv[1]);


	//backlight = (backlight && 1);  /* only preserves ON/OFF bit*/

	if (strcmp ("on", argv[1]) == 0) {
		c->backlight = BACKLIGHT_ON;
	}
	else if (strcmp ("off", argv[1]) == 0) {
		c->backlight = BACKLIGHT_OFF;
	}
	else if (strcmp ("toggle", argv[1]) == 0) {
		if (c->backlight == BACKLIGHT_ON)
			c->backlight = BACKLIGHT_OFF;
		else if (c->backlight == BACKLIGHT_OFF)
			c->backlight = BACKLIGHT_ON;
	}
	else if (strcmp ("blink", argv[1]) == 0) {
		c->backlight |= BACKLIGHT_BLINK;
	}
	else if (strcmp ("flash", argv[1]) == 0) {
		c->backlight |= BACKLIGHT_FLASH;
	}

	sock_send_string(c->sock, "success\n");

	return 0;

}

/**
 * Sends back information about the loaded drivers.
 *
 *\verbatim
 * Usage: info
 *\endverbatim
 */
int
info_func(Client *c, int argc, char **argv)
{
	if (c->state != ACTIVE)
		return 1;

	if (argc > 1) {
		sock_send_error(c->sock, "Extra arguments ignored...\n");
	}

	sock_printf(c->sock, "%s\n", drivers_get_info());

	return 0;
}