summaryrefslogtreecommitdiff
path: root/src/rmd_parseargs.c
blob: e4cfdd761ac6197527be1f2b309f9dd9c4d08d72 (plain)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/******************************************************************************
*                            recordMyDesktop                                  *
*******************************************************************************
*                                                                             *
*            Copyright (C) 2006,2007,2008 John Varouhakis                     *
*            Copyright (C) 2009           Martin Nordholts                    *
*                                                                             *
*                                                                             *
*   This program is free software; you can redistribute it and/or modify      *
*   it under the terms of the GNU General Public License as published by      *
*   the Free Software Foundation; either version 2 of the License, or         *
*   (at your option) any later version.                                       *
*                                                                             *
*   This program is distributed in the hope that it will be useful,           *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*   GNU General Public License for more details.                              *
*                                                                             *
*   You should have received a copy of the GNU General Public License         *
*   along with this program; if not, write to the Free Software               *
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
*                                                                             *
*                                                                             *
*                                                                             *
*   For further information contact me at johnvarouhakis@gmail.com            *
******************************************************************************/

#include "config.h"
#include "rmd_parseargs.h"

#include "rmd_types.h"

#include <popt.h>

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


#ifdef HAVE_LIBJACK
#define RMD_LIBJACK_STATUS "Enabled"
#define RMD_USE_JACK_EXTRA_FLAG 0
#else
#define RMD_LIBJACK_STATUS "Disabled"
#define RMD_USE_JACK_EXTRA_FLAG POPT_ARGFLAG_DOC_HIDDEN
#endif

#ifdef HAVE_LIBASOUND
#define RMD_LIBASOUND_STATUS "ALSA"
#else
#define RMD_LIBASOUND_STATUS "OSS"
#endif

#define RMD_ARG_DELAY 1
#define RMD_ARG_DUMMY_CURSOR 2
#define RMD_ARG_USE_JACK 3

#define RMD_OPTION_TABLE(name, table) \
        { NULL, '\0', \
          POPT_ARG_INCLUDE_TABLE, (table), \
          0, (name), NULL }


static void rmdPrintAndExit(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data);
static boolean rmdValidateArguments(const ProgArgs *args);


boolean rmdParseArgs(int argc, char **argv, ProgArgs *arg_return)
{
	poptContext popt_context = NULL;
	boolean no_cursor = FALSE;
	boolean quick_subsampling = FALSE;
	boolean compress_cache = FALSE;
	boolean success = TRUE;
	int arg_id = 0;

	// Setup the option tables
	struct poptOption generic_options[] = {
		{ NULL, '\0',
		  POPT_ARG_CALLBACK, (void *)rmdPrintAndExit, 0,
		  NULL, NULL },

		{ "help", 'h',
		  POPT_ARG_NONE, NULL, 0,
		  "Print this help and exit.",
		  NULL },

		{ "version", '\0',
		  POPT_ARG_NONE, NULL, 0,
		  "Print program version and exit.",
		  NULL },

		{ "print-config", '\0',
		  POPT_ARG_NONE, NULL, 0,
		  "Print info about options selected during compilation and exit.",
		  NULL },

		POPT_TABLEEND };

	struct poptOption image_options[] = {
		{ "windowid", '\0',
		  POPT_ARG_INT, &arg_return->windowid, 0,
		  "id of window to be recorded.",
		  "id_of_window" },

		{ "display", '\0',
		  POPT_ARG_STRING, &arg_return->display, 0,
		  "Display to connect to.",
		  "DISPLAY" },

		{ "x", 'x',
		  POPT_ARG_INT, &arg_return->x, 0,
		  "Offset in x direction.",
		  "N>=0" },

		{ "y", 'y',
		  POPT_ARG_INT, &arg_return->y, 0,
		  "Offset in y direction.",
		  "N>=0" },

		{ "width", '\0',
		  POPT_ARG_INT, &arg_return->width, 0,
		  "Width of recorded window.",
		  "N>0" },

		{ "height", '\0',
		  POPT_ARG_INT, &arg_return->height, 0,
		  "Height of recorded window.",
		  "N>0" },

		{ "dummy-cursor", '\0',
		  POPT_ARG_STRING, NULL, RMD_ARG_DUMMY_CURSOR,
		  "Color of the dummy cursor [black|white]",
		  "color" },

		{ "no-cursor", '\0',
		  POPT_ARG_NONE, &no_cursor, 0,
		  "Disable drawing of the cursor.",
		  NULL },

		{ "no-shared", '\0',
		  POPT_ARG_NONE, &arg_return->noshared, 0,
		  "Disable usage of MIT-shared memory extension(Not Recommended!).",
		  NULL },

		{ "full-shots", '\0',
		  POPT_ARG_NONE, &arg_return->full_shots, 0,
		  "Take full screenshot at every frame(Not recomended!).",
		  NULL },

		{ "follow-mouse", '\0',
		  POPT_ARG_NONE, &arg_return->follow_mouse, 0,
		  "Makes the capture area follow the mouse cursor. Autoenables --full-shots.",
		  NULL },

		{ "quick-subsampling", '\0',
		  POPT_ARG_NONE, &quick_subsampling, 0,
		  "Do subsampling of the chroma planes by discarding, not averaging.",
		  NULL },

		{ "fps", '\0',
		  POPT_ARG_FLOAT, &arg_return->fps, 0,
		  "A positive number denoting desired framerate.",
		  "N(number>0.0)" },

		POPT_TABLEEND };

	struct poptOption sound_options[] = {
		{ "channels", '\0',
		  POPT_ARG_INT, &arg_return->channels, 0,
		  "A positive number denoting desired sound channels in recording.",
		  "N" },

		{ "freq", '\0',
		  POPT_ARG_INT, &arg_return->frequency, 0,
		  "A positive number denoting desired sound frequency.",
		  "N" },

		{ "buffer-size", '\0',
		  POPT_ARG_INT, &arg_return->buffsize, 0,
		  "A positive number denoting the desired sound buffer size (in frames, when using ALSA or OSS)",
		  "N" },

		{ "ring-buffer-size", '\0',
		  POPT_ARG_FLOAT, &arg_return->jack_ringbuffer_secs, 0,
		  "A float number denoting the desired ring buffer size (in seconds, when using JACK only).",
		  "N" },

		{ "device", '\0',
		  POPT_ARG_STRING, &arg_return->device, 0,
		  "Sound device(default " DEFAULT_AUDIO_DEVICE ").",
		  "SOUND_DEVICE" },

		{ "use-jack", '\0',
		  POPT_ARG_STRING | RMD_USE_JACK_EXTRA_FLAG, &arg_return->x, RMD_ARG_USE_JACK,
		  "Record audio from the specified list of space-separated jack ports.",
		  "port1 port2... portn" },

		{ "no-sound", '\0',
		  POPT_ARG_NONE, &arg_return->nosound, 0,
		  "Do not record sound.",
		  NULL },

		POPT_TABLEEND };

	struct poptOption encoding_options[] = {
		{ "on-the-fly-encoding", '\0',
		  POPT_ARG_NONE, &arg_return->encOnTheFly, 0,
		  "Encode the audio-video data, while recording.",
		  NULL },

		{ "v_quality", '\0',
		  POPT_ARG_INT, &arg_return->v_quality, 0,
		  "A number from 0 to 63 for desired encoded video quality(default 63).",
		  "n" },

		{ "v_bitrate", '\0',
		  POPT_ARG_INT, &arg_return->v_bitrate, 0,
		  "A number from 0 to 2000000 for desired encoded video bitrate(default 0).",
		  "n" },

		{ "s_quality", '\0',
		  POPT_ARG_INT, &arg_return->s_quality, 0,
		  "Desired audio quality(-1 to 10).",
		  "n" },

		POPT_TABLEEND };

	struct poptOption misc_options[] = {
		{ "rescue", '\0',
		  POPT_ARG_STRING, &arg_return->rescue_path, 0,
		  "Encode data from a previous, crashed, session.",
		  "path_to_data" },

		{ "no-encode", '\0',
		  POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &arg_return->no_encode, 0,
		  "Do not encode any data after recording is complete. This is instead done manually afterwards with --rescue.",
		  NULL },

		{ "no-wm-check", '\0',
		  POPT_ARG_NONE, &arg_return->nowmcheck, 0,
		  "Do not try to detect the window manager(and set options according to it)",
		  NULL },

		{ "no-frame", '\0',
		  POPT_ARG_NONE, &arg_return->noframe, 0,
		  "Don not show the frame that visualizes the recorded area.",
		  NULL },

		{ "pause-shortcut", '\0',
		  POPT_ARG_STRING, &arg_return->pause_shortcut, 0,
		  "Shortcut that will be used for (un)pausing (default Control+Mod1+p).",
		  "MOD+KEY" },

		{ "stop-shortcut", '\0',
		  POPT_ARG_STRING, &arg_return->stop_shortcut, 0,
		  "Shortcut that will be used to stop the recording (default Control+Mod1+s).",
		  "MOD+KEY" },

		{ "compress-cache", '\0',
		  POPT_ARG_NONE, &compress_cache, 0,
		  "Image data are cached with light compression.",
		  NULL },

		{ "periodic-datasync-ms", '\0',
		  POPT_ARG_INT, &arg_return->periodic_datasync_ms, 0,
		  "Asynchronously fdatasync() cache files every specified ms while writing, 0 disables (default 100).",
		  "N>=0"},

		{ "workdir", '\0',
		  POPT_ARG_STRING, &arg_return->workdir, 0,
		  "Location where a temporary directory will be created to hold project files(default $HOME).",
		  "DIR" },

		{ "delay", '\0',
		  POPT_ARG_STRING, &arg_return->delay, RMD_ARG_DELAY,
		  "Number of secs(default), minutes or hours before capture starts(number can be float)",
		  "n[H|h|M|m]" },

		{ "overwrite", '\0',
		  POPT_ARG_NONE, &arg_return->overwrite, 0,
		  "If there is already a file with the same name, delete it (default is to add a number postfix to the new one).",
		  NULL },

		{ "output", 'o',
		  POPT_ARG_STRING, &arg_return->filename, 0,
		  "Name of recorded video(default out.ogv).",
		  "filename" },

		POPT_TABLEEND };

	struct poptOption rmd_args[] = {
		RMD_OPTION_TABLE("Generic Options", generic_options),
		RMD_OPTION_TABLE("Image Options", image_options),
		RMD_OPTION_TABLE("Sound Options", sound_options),
		RMD_OPTION_TABLE("Encoding Options", encoding_options),
		RMD_OPTION_TABLE("Misc Options", misc_options),
		POPT_TABLEEND };


	// Some trivial setup
	popt_context = poptGetContext(NULL, argc, (const char **)argv, rmd_args, 0);
	poptSetOtherOptionHelp(popt_context, "[OPTIONS]^filename");


	// Parse the arguments
	while ((arg_id = poptGetNextOpt(popt_context)) > 0) {
		char *arg = poptGetOptArg(popt_context);

		// Most arguments are handled completely by libpopt but we
		// handle some by ourself, namely those in this switch case
		switch (arg_id) {
			case RMD_ARG_DELAY:
			{
				float num = atof(arg);

				if (num > 0.0) {
					int k;
					for (k = 0; k < strlen(arg); k++) {
						if ((arg[k] == 'M') || (arg[k] == 'm')) {
							num *= 60.0;
							break;
						}
						else if ((arg[k] == 'H') || (arg[k] == 'h')) {
							num *= 3600.0;
							break;
						}
					}
					arg_return->delay = (int) num;
				} else {
					fprintf(stderr,
							"Argument Usage: --delay n[H|h|M|m]\n"
							"where n is a float number\n");
					success = FALSE;
				}
			}
			break;

			case RMD_ARG_DUMMY_CURSOR:
			{
				if (!strcmp(arg, "white"))
					arg_return->cursor_color = 0;
				else if (!strcmp(arg, "black"))
					arg_return->cursor_color = 1;
				else {
					fprintf(stderr,
							"Argument Usage:\n"
							" --dummy-cursor [black|white]\n");
					success = FALSE;
				}
				arg_return->have_dummy_cursor = TRUE;
				arg_return->xfixes_cursor = FALSE;
			}
			break;

			case RMD_ARG_USE_JACK:
			{
				arg_return->jack_nports = 0;

				while (arg) {

					arg_return->jack_nports++;

					arg_return->jack_port_names[arg_return->jack_nports - 1] = malloc(strlen(arg) + 1);
					strcpy(arg_return->jack_port_names[arg_return->jack_nports - 1], arg);

					arg = poptGetOptArg(popt_context);
				}

				if (arg_return->jack_nports > 0) {
					arg_return->use_jack = 1;
				} else {
					fprintf(stderr,
							"Argument Usage: --use-jack port1 port2... portn\n");
					success = FALSE;
				}

				if (RMD_USE_JACK_EXTRA_FLAG == POPT_ARGFLAG_DOC_HIDDEN) {
					fprintf(stderr,
							"Warning, will ignore --use-jack flags, no libjack support in build.\n");
				}
			}				

			default:
				break;
		}
	}

	if (arg_id == -1) {
		// Parsing is complete, perform final adjustments
		if (no_cursor)
			arg_return->xfixes_cursor = FALSE;

		if (quick_subsampling)
			arg_return->no_quick_subsample = FALSE;

		if (compress_cache)
			arg_return->zerocompression = FALSE;

		if (arg_return->follow_mouse)
			arg_return->full_shots = TRUE;

		if (!arg_return->filename)
			arg_return->filename = strdup(poptGetArg(popt_context));
	} else {
		// Parsing error, say what went wrong
		const char *str = poptBadOption(popt_context, 0);

		success = FALSE;

		fprintf(stderr,
				"Error when parsing `%s': ", str);

		switch (arg_id) {
			case POPT_ERROR_NOARG:
				fprintf(stderr, "Missing argument\n");
				break;
						
			case POPT_ERROR_BADNUMBER:
				fprintf(stderr, "Bad number\n");
				break;
						
			default:
				fprintf(stderr, "libpopt error: %d\n", arg_id);
				break;
		}
	}

	// Make sure argument ranges are valid
	success = success && rmdValidateArguments(arg_return);

	// Clean up
	poptFreeContext(popt_context);

	return success;
}

static boolean rmdValidateArguments(const ProgArgs *args)
{
	boolean success = TRUE;

	if (args->x < 0) {
		fprintf(stdout, "-x must not be less than 0.\n");
		success = FALSE;
	}
	if (args->y < 0) {
		fprintf(stdout, "-y must not be less than 0.\n");
		success = FALSE;
	}
	if (args->width < 0) {
		fprintf(stdout, "--width must be larger than 0.\n");
		success = FALSE;
	}
	if (args->height < 0) {
		fprintf(stdout, "--height must be larger than 0.\n");
		success = FALSE;
	}
	if (args->fps <= 0) {
		fprintf(stdout, "--fps must be larger than 0.\n");
		success = FALSE;
	}
	if (args->v_quality < 0 || args->v_quality > 63) {
		fprintf(stdout, "--v_quality must be within the inclusive range [0-63].\n");
		success = FALSE;
	}
	if (args->v_bitrate < 0 || args->v_quality > 2000000) {
		fprintf(stdout, "--v_bitrate must be within the inclusive range [0-2000000].\n");
		success = FALSE;
	}
	if (args->frequency <= 0) {
		fprintf(stdout, "--frequency must be larger than 0.\n");
		success = FALSE;
	}
	if (args->channels <= 0) {
		fprintf(stdout, "--channels must be larger than 0.\n");
		success = FALSE;
	}
	if (args->buffsize <= 0) {
		fprintf(stdout, "--buffer-size must be larger than 0.\n");
		success = FALSE;
	}
	if (args->jack_ringbuffer_secs <= 0) {
		fprintf(stdout, "--jack-buffer-size must be larger than 0.\n");
		success = FALSE;
	}

	return success;
}

static void rmdPrintAndExit(	poptContext con, 
				enum poptCallbackReason reason,
				const struct poptOption *opt,
				const char *arg,
				const void *data)
{
	if (strcmp(opt->longName, "version") == 0) {
		fprintf(stderr, "recordMyDesktop v%s\n\n", VERSION);
	} else if (strcmp(opt->longName, "help") == 0) {
		poptPrintHelp(con, stdout, 0);
		fprintf(stdout,
				"\n"
				"\tIf no other options are specified, filename can be given without the -o switch.\n"
				"\n"
				"\n");
	} else if (strcmp(opt->longName, "print-config") == 0) {
		fprintf(stdout,
				"\n"
				"recordMyDesktop was compiled with the following options:\n"
				"\n"
				"Jack:\t\t\t" RMD_LIBJACK_STATUS "\n"
				"Default Audio Backend:\t" RMD_LIBASOUND_STATUS "\n"
				"\n"
				"\n");
	}

	exit(0);
}
© All Rights Reserved