2 This file is part of GNUnet.
3 Copyright (C) 2013-2019 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
25 #include "gnunet-qr-utils.h"
27 static const char *usage_note =
29 "Scan a QR code using a video device and import\n"
31 "Arguments mandatory for long options are also mandatory for short options.\n"
32 " -c, --config FILENAME use configuration file FILENAME\n"
33 " -d, --device DEVICE use device DEVICE\n"
34 " -s, --silent do not show preview windows\n"
35 " -h, --help print this help\n"
36 " -v, --verbose be verbose\n"
37 "Report bugs to gnunet-developers@gnu.org.\n"
39 "GNUnet home page: https://gnunet.org/\n"
40 "General help using GNU software: https://www.gnu.org/gethelp/\n";
42 #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
44 int main (int argc, char **argv)
46 const char* configuration = NULL;
47 const char* device = "/dev/video0";
48 static bool verbose = false;
49 static bool silent = false;
51 static struct option long_options[] = {
52 {"verbose", no_argument, 0, 'v'},
53 {"silent", no_argument, 0, 's'},
54 {"help", no_argument, 0, 'h'},
55 {"config", required_argument, 0, 'c'},
56 {"device", required_argument, 0, 'd'},
61 opt = getopt_long (argc, argv, "c:hd:sv",
71 configuration = optarg;
88 /* create a Processor */
89 LOG("Initializing\n");
90 zbar_processor_t *proc = zbar_processor_create(1);
92 // FIXME: Wrap all this into a function which returns an error on
93 // failure. And here ensure the processor is destroyed at the end.
95 /* configure the Processor */
96 zbar_processor_parse_config(proc, "enable");
98 /* initialize the Processor */
99 LOG("Opening video device %s\n", device);
100 // FIXME: error handling
101 zbar_processor_init(proc, device, 1);
103 /* enable the preview window */
104 zbar_processor_set_visible(proc, 1);
105 zbar_processor_set_active(proc, 1);
107 /* keep scanning until user provides key/mouse input */
108 //zbar_processor_user_wait(proc, -1);
110 // read at least one barcode (or until window closed)
113 n = zbar_process_one(proc, -1);
114 LOG("Got %i images\n", n);
115 // FIXME: Error handling (n = -1)
117 // hide the preview window
118 zbar_processor_set_active(proc, 0);
119 zbar_processor_set_visible(proc, 0);
124 const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc);
125 const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols);
127 if (symbol != NULL) {
128 const char* data = zbar_symbol_get_data(symbol);
129 LOG("Found %s \"%s\"\n",
130 zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
132 if (configuration == NULL) {
133 char* command_args[] = {"gnunet-uri", data, NULL };
134 LOG("Running `gnunet-uri %s`\n", data);
135 rc = fork_and_exec(BINDIR "gnunet-uri", command_args);
137 char* command_args[] = {"gnunet-uri", "-c", configuration, data, NULL };
138 LOG("Running `gnunet-uri -c '%s' %s`\n", configuration, data);
139 rc = fork_and_exec(BINDIR "gnunet-uri", command_args);
143 printf("Failed to add URI %s\n", data);
145 printf("Added URI %s\n", data);
150 zbar_processor_destroy(proc);