File: irsimsend.cpp

package info (click to toggle)
lirc 0.10.2-0.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,268 kB
  • sloc: ansic: 26,981; cpp: 9,187; sh: 5,875; python: 4,507; makefile: 1,049; xml: 63
file content (295 lines) | stat: -rw-r--r-- 7,461 bytes parent folder | download | duplicates (4)
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
/****************************************************************************
** irsimsend.c *************************************************************
****************************************************************************
*
* irsimsend - send all codes defined in a given config file.
*
*/

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <getopt.h>

#include "lirc_private.h"
#include "lirc_client.h"

static const logchannel_t logchannel = LOG_APP;

static const char* const USAGE =
	"Send key symbols durations to fixed file 'simsend.out'.\n\n"
	"Synopsis:\n"
	"    irsimsend [-U path] [-s time] [-c count] <file>\n"
	"    irsimsend [-k keysym | -l listfile] [-U path] [-s time] [-c count]"
	" <file>\n"
	"    irsimsend [-h | -v]\n\n"
	"<file> is a lircd.conf type config file. In the first form, all keys in\n"
	"that file are sent.\n\n"
	"Options:\n"
	"    -U, --plugindir <path>:     Load drivers from <path>.\n"
	"    -c, --config <count>:       Repeat each key <count> times.\n"
	"    -l, --listfile <file>       Send symbols in file.\n"
	"    -k, --keysym <keysym>       Send a single keysym.\n"
	"    -s, --start-space <time>    Send a start space <time> us\n"
	"    -v, --version               Print version.\n"
	"    -h, --help                  Print this message.\n";

static struct option options[] = {
	{ "help",	 no_argument,	    NULL, 'h' },
	{ "version",	 no_argument,	    NULL, 'v' },
	{ "count",	 required_argument, NULL, 'c' },
	{ "keysym",	 required_argument, NULL, 'k' },
	{ "listfile",	 required_argument, NULL, 'l' },
	{ "pluginpath",	 required_argument, NULL, 'U' },
	{ "start-space", required_argument, NULL, 's' },
	{ 0,		 0,		    0,	  0   }
};

static const char* const OUTFILE = "simsend.out";

static int opt_count = 1;
static int opt_startspace = -1;
static const char* opt_keysym = NULL;
static const char* opt_listfile = NULL;


/** Set up default values for all command line options + filename. */
static void add_defaults(void)
{
	static char plugindir[128];
	const char* defaults[] = {
		"lircd:plugindir",	      plugindir,
		"irsimsend:driver",	      "devinput",
		"irsimsend:count",	      "1",
		"irsimsend:keysym",	      (const char*)NULL,
		"irsimsend:listfile",	      (const char*)NULL,
		"irsimsend:start-space"	      "-1",
		(const char*)NULL,	      (const char*)NULL
	};
	const char* s = getenv("LIRC_PLUGIN_PATH");

	strncpy(plugindir, s != NULL ? s : PLUGINDIR, sizeof(plugindir) - 1);
	options_add_defaults(defaults);
};


int parse_uint_arg(const char* optind, const char* errmsg)
{
	long c;

	c = strtol(optarg, NULL, 10);
	if (c > INT_MAX || c < 0 || errno == EINVAL || errno == ERANGE) {
		fputs(errmsg, stderr);
		exit(EXIT_FAILURE);
	}
	return (int)c;
}


static void parse_options(int argc, char** const argv)
{
	long c;

	add_defaults();

	while ((c = getopt_long(argc, argv, "c:hk:l:s:U:v", options, NULL))
	       != EOF) {
		switch (c) {
		case 'h':
			puts(USAGE);
			exit(EXIT_SUCCESS);
		case 'c':
			(void) parse_uint_arg(optarg, "Illegal count value\n");
			options_set_opt("irsimsend:count", optarg);
			break;
		case 'v':
			printf("%s\n", "irw " VERSION);
			exit(EXIT_SUCCESS);
		case 'U':
			options_set_opt("lircd:plugindir", optarg);
			break;
		case 'k':
			options_set_opt("irsimsend:keysym", optarg);
			break;
		case 'l':
			options_set_opt("irsimsend:listfile", optarg);
			break;
		case 's':
			(void) parse_uint_arg(optarg, "Illegal space value\n");
			options_set_opt("irsimsend:start-space", optarg);
			break;
		case '?':
			fprintf(stderr, "unrecognized option: -%c\n", optopt);
			fputs("Try `irsimsend -h' for more information.\n",
			      stderr);
			exit(EXIT_FAILURE);
		}
	}
	if (argc != optind + 1) {
		fputs(USAGE, stderr);
		exit(EXIT_FAILURE);
	}
}


static struct ir_remote* setup(const char* path)
{
	struct ir_remote* remote;
	FILE* f;

	if (hw_choose_driver("file") == -1) {
		fputs("Cannot load file driver (bad plugin path?)\n",
		      stderr);
		exit(EXIT_FAILURE);
	}
	unlink(OUTFILE);
	curr_driver->open_func(OUTFILE);
	curr_driver->init_func();

	if (opt_listfile != NULL && access(opt_listfile, R_OK) != 0) {
		fprintf(stderr, "Cannot open %s for read\n", opt_listfile);
		exit(EXIT_FAILURE);
	}
	f = fopen(path, "r");
	if (f == NULL) {
		fprintf(stderr, "Cannot open %s for read\n", path);
		exit(EXIT_FAILURE);
	}
	remote = read_config(f, path);
	fclose(f);
	if (remote == NULL) {
		fprintf(stderr, "Cannot parse %s\n", path);
		exit(EXIT_FAILURE);
	}
	remote->min_repeat = 0;
	return remote;
}


static void send_space(int duration)
{
	char buff[64];

	snprintf(buff, sizeof(buff), "send-space:%d", duration);
	drv_handle_options(buff);
}


static void send_code(struct ir_remote* remote, struct ir_ncode* code)
{
	int i;
	static char last_code[32] = { '\0' };

	code->transmit_state = NULL;
	if (has_toggle_mask(remote))
		remote->toggle_mask_state = 0;
	if (has_toggle_bit_mask(remote))
		remote->toggle_bit_mask_state =
			(remote->toggle_bit_mask_state ^ remote->toggle_bit_mask);
	code->transmit_state = NULL;
	remote->min_repeat = 0;
	if (strcmp(code->name, last_code) == 0)
		repeat_remote = remote;
	send_ir_ncode(remote, code, 0);
	repeat_remote = remote;
	for (i = 1; i < opt_count; i += 1)
		send_ir_ncode(remote, code, 0);
	repeat_remote = NULL;
	strncpy(last_code, code->name, sizeof(last_code));
}


static int simsend_remote(struct ir_remote* remote)
{
	struct ir_ncode* code;

	while (remote != NULL) {
		for (code = remote->codes; code->name != NULL; code++) {
			printf("%s\n", code->name);
			send_code(remote, code);
		}
		remote = remote->next;
	}
	return 0;
}


static int simsend_keysym(struct ir_remote* remote, const char* keysym)
{
	struct ir_ncode* code;

	code = get_code_by_name(remote, keysym);
	if (code != NULL) {
		send_code(remote, code);
		printf("%s\n", keysym);
	} else {
		fprintf(stderr, "No such key: %s\n", keysym);
		exit(EXIT_FAILURE);
	}
	return 0;
}


static int simsend_list(struct ir_remote* remote)
{
	char line[256];
	char keysym[32];
	struct ir_ncode* code;
	FILE* f;
	char* s;
	int r;

	f = fopen(opt_listfile, "r");
	s = fgets(line, sizeof(line), f);
	while (s != NULL) {
		r = sscanf(line, "%*x %*x %32s %*s", keysym);
		if (r != 1)
			r = sscanf(line, "%32s", keysym);
		if (r != 1) {
			printf("Cannot parse line: %s\n", line);
			continue;
		}
		code = get_code_by_name(remote, keysym);
		if (code == NULL) {
			printf("Illegal keycode: %s\n", keysym);
		} else {
			printf("%s\n", code->name);
			send_code(remote, code);
		}
		s = fgets(line, sizeof(line), f);
	}
	fclose(f);
	return 0;
}

int main(int argc, char* argv[])
{
	struct ir_remote* remote;
	char path[128];
	const loglevel_t level = options_get_app_loglevel("irsimsend");

	lirc_log_get_clientlog("irsimsend", path, sizeof(path));
	lirc_log_set_file(path);
	lirc_log_open("irsimsend", 1, level);

	options_load(argc, argv, NULL, parse_options);
        opt_startspace = options_getint("irsimsend:start-space");
        opt_count = options_getint("irsimsend:count");
        opt_keysym = options_getstring("irsimsend:keysym");
        opt_listfile = options_getstring("irsimsend:listfile");

	remote = setup(argv[optind]);
	if (opt_startspace != -1)
		send_space(opt_startspace);
	if (opt_keysym != NULL)
		simsend_keysym(remote, opt_keysym);
	else if (opt_listfile != NULL)
		simsend_list(remote);
	else
		simsend_remote(remote);
	return 0;
}