Add helper lib "gnunet-qr-utils.h".
[oweals/gnunet.git] / src / util / gnunet-qr.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013-2019 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 #include <stdio.h>
22 #include <zbar.h>
23 #include <stdbool.h>
24 #include <getopt.h>
25
26 static const char *usage_note =
27   "gnunet-qr\n"
28   "Scan a QR code using a video device and import\n"
29   "\n"
30   "Arguments mandatory for long options are also mandatory for short options.\n"
31   "  -c, --config FILENAME      use configuration file FILENAME\n"
32   "  -d, --device DEVICE        use device DEVICE\n"
33   "  -s, --silent               do not show preview windows\n"
34   "  -h, --help                 print this help\n"
35   "  -v, --verbose              be verbose\n"
36   "Report bugs to gnunet-developers@gnu.org.\n"
37   "\n"
38   "GNUnet home page: https://gnunet.org/\n"
39   "General help using GNU software: https://www.gnu.org/gethelp/\n";
40
41 #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
42
43 int main (int argc, char **argv)
44 {
45   const char* configuration = NULL;
46   const char* device = "/dev/video0";
47   static bool verbose = false;
48   static bool silent = false;
49
50   static struct option long_options[] = {
51       {"verbose", no_argument,       0, 'v'},
52       {"silent",  no_argument,       0, 's'},
53       {"help",    no_argument,       0, 'h'},
54       {"config",  required_argument, 0, 'c'},
55       {"device",  required_argument, 0, 'd'},
56       {0, 0, 0, 0}
57     };
58   while (1) {
59     int opt;
60     opt = getopt_long (argc, argv, "c:hd:sv",
61                      long_options, NULL);
62     if (opt == -1)
63       break;
64
65     switch (opt) {
66     case 'h':
67       printf(usage_note);
68       return 0;
69     case 'c':
70       configuration = optarg;
71       break;
72     case 'd':
73       device = optarg;
74       break;
75     case 's':
76       silent = true;
77       break;
78     case 'v':
79       verbose = true;
80       break;
81     default:
82       printf(usage_note);
83       return 1;
84     }
85   }
86
87   /* create a Processor */
88   LOG("Initializing\n");
89   zbar_processor_t *proc = zbar_processor_create(1);
90
91   // FIXME: Wrap all this into a function which returns an error on
92   // failure. And here ensure the processor is destroyed at the end.
93
94   /* configure the Processor */
95   zbar_processor_parse_config(proc, "enable");
96
97   /* initialize the Processor */
98   LOG("Opening video device %s\n", device);
99   // FIXME: error handling
100   zbar_processor_init(proc, device, 1);
101
102   /* enable the preview window */
103   zbar_processor_set_visible(proc, 1);
104   zbar_processor_set_active(proc, 1);
105
106   /* keep scanning until user provides key/mouse input */
107   //zbar_processor_user_wait(proc, -1);
108
109   // read at least one barcode (or until window closed)
110   LOG("Capturing\n");
111   int n;
112   n = zbar_process_one(proc, -1);
113   LOG("Got %i images\n", n);
114   // FIXME: Error handling (n = -1)
115
116   // hide the preview window
117   zbar_processor_set_active(proc, 0);
118   zbar_processor_set_visible(proc, 0);
119
120   // extract results
121   int rc = 1;
122
123   const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc);
124   const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols);
125
126   if (symbol != NULL) {
127     const char* data = zbar_symbol_get_data(symbol);
128     LOG("Found %s \"%s\"\n",
129         zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
130
131     /* TODO
132     args = ["gnunet-uri",
133             // FIXME: "-c", configuration,
134             data];
135     if (verbose = true) {
136       // TODO: print arguments:
137       printf("Running `%s %s %s %s`", *args, "", ""); // FIXME variable num args
138     };
139     rc = popen("gnunet-uri", *args);
140     */
141     if (rc != 0) {
142       printf("Failed to add URI %s\n", data);
143     } else {
144       printf("Added URI %s\n", data);
145     }
146   }
147
148   /* clean up */
149   zbar_processor_destroy(proc);
150
151   return(rc);
152 }