summaryrefslogtreecommitdiff
path: root/src/options.c
blob: cbdaf3c304105c504ddd8f80e241f21081d2cdcc (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
/* Camicro - Microscope camera viewer.
 *
 * Copyright (C) 2019 Nicolas Schodet
 *
 * 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.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 */
#define _GNU_SOURCE
#include <printf.h>

#include "options.h"
#include "cli.h"
#include "utils.h"

void
options_add(GApplication *app)
{
    g_application_set_option_context_summary(app, "Microscope camera viewer");
    g_application_set_option_context_description(app,
	    "Without options, run the GUI, else switch to batch mode to dump"
	    " image from the camera.");
    g_application_add_main_option(app, "width", 'w', 0,
	    G_OPTION_ARG_INT, "image width", "VALUE");
    g_application_add_main_option(app, "exposure", 'e', 0,
	    G_OPTION_ARG_DOUBLE, "exposure value", "MS");
    g_application_add_main_option(app, "gain", 'g', 0,
	    G_OPTION_ARG_DOUBLE, "gain value", "VALUE");
    g_application_add_main_option(app, "count", 'n', 0,
	    G_OPTION_ARG_INT, "number of image to take", "N");
    g_application_add_main_option(app, "raw", 'r', 0,
	    G_OPTION_ARG_NONE, "do not convert image", NULL);
    g_application_add_main_option(app, "output", 'o', 0,
	    G_OPTION_ARG_FILENAME,
	    "output file pattern (default: out%02d.png)", "PATTERN");
}

gint
options_handle(GApplication *app, GVariantDict *options_dict)
{
    struct options options;
    options.width = -1;
    options.height = -1;
    options.exposure_ms = -1.0;
    options.gain = -1.0;
    options.count = 1;
    options.raw = false;
    options.out = NULL;
    bool option_set = false;
    if (g_variant_dict_lookup(options_dict, "width", "i", &options.width)) {
	option_set = true;
	if (options.width <= 0)
	    utils_fatal("invalid width");
    }
    if (g_variant_dict_lookup(options_dict, "exposure", "d",
		&options.exposure_ms)) {
	option_set = true;
	if (options.exposure_ms < 1.0)
	    utils_fatal("invalid exposure");
    }
    if (g_variant_dict_lookup(options_dict, "gain", "d", &options.gain)) {
	option_set = true;
	if (options.gain <= 0.0)
	    utils_fatal("invalid gain");
    }
    if (g_variant_dict_lookup(options_dict, "count", "i", &options.count)) {
	option_set = true;
	if (options.count <= 0)
	    utils_fatal("invalid count");
    }
    if (g_variant_dict_contains(options_dict, "raw")) {
	option_set = true;
	options.raw = true;
    }
    if (g_variant_dict_lookup(options_dict, "out", "^&ay", &options.out)) {
	option_set = true;
	int argtypes[1];
	int formats = parse_printf_format(options.out, 1, argtypes);
	if (formats != 1 || argtypes[0] != PA_INT)
	    utils_fatal("bad file pattern, use one %%d");
    } else {
	options.out = "out%02d.png";
    }
    if (option_set)
	return cli_run(&options);
    else
	return -1;
}