gnunet-qr: Implement functionality of gnunet-uri, don't spawn.
[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 "platform.h"
25 #include "gnunet_util_lib.h"
26
27 #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
28
29 // Command line options
30 static char* device = "/dev/video0";
31 static int verbose = false;
32 static int silent = false;
33
34 // Handler exit code
35 static long unsigned int exit_code = 1;
36
37 // Helper process we started.
38 static struct GNUNET_OS_Process *p;
39
40 // Pipe used to communicate shutdown via signal.
41 static struct GNUNET_DISK_PipeHandle *sigpipe;
42
43
44 /**
45  * Task triggered whenever we receive a SIGCHLD (child
46  * process died) or when user presses CTRL-C.
47  *
48  * @param cls closure, NULL
49  */
50 static void
51 maint_child_death (void *cls)
52 {
53   enum GNUNET_OS_ProcessStatusType type;
54
55   if ( (GNUNET_OK !=
56         GNUNET_OS_process_status (p, &type, &exit_code)) ||
57        (type != GNUNET_OS_PROCESS_EXITED) )
58     GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
59   GNUNET_OS_process_destroy (p);
60 }
61
62
63 /**
64  * Dispatch URIs to the appropriate GNUnet helper process
65  *
66  * @param cls closure
67  * @param uri uri to dispatch
68  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
69  * @param cfg configuration
70  */
71 static void
72 gnunet_uri (void *cls, const char *uri, const char *cfgfile,
73      const struct GNUNET_CONFIGURATION_Handle *cfg)
74 {
75   const char *orig_uri;
76   const char *slash;
77   char *subsystem;
78   char *program;
79   struct GNUNET_SCHEDULER_Task * rt;
80
81   orig_uri = uri;
82   if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) {
83     fprintf (stderr,
84              _("Invalid URI: does not start with `%s'\n"),
85              "gnunet://");
86     return;
87   }
88   uri += strlen ("gnunet://");
89   if (NULL == (slash = strchr (uri, '/')))
90   {
91     fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
92     return;
93   }
94   subsystem = GNUNET_strndup (uri, slash - uri);
95   if (GNUNET_OK !=
96       GNUNET_CONFIGURATION_get_value_string (cfg,
97                                              "uri",
98                                              subsystem,
99                                              &program))
100   {
101     fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
102     GNUNET_free (subsystem);
103     return;
104   }
105   GNUNET_free (subsystem);
106   rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
107                                        GNUNET_DISK_pipe_handle (sigpipe,
108                                                                 GNUNET_DISK_PIPE_END_READ),
109                                        &maint_child_death, NULL);
110   p = GNUNET_OS_start_process (GNUNET_NO, 0,
111                                NULL, NULL, NULL,
112                                program,
113                                program,
114                                orig_uri,
115                                NULL);
116   GNUNET_free (program);
117   if (NULL == p)
118     GNUNET_SCHEDULER_cancel (rt);
119 }
120
121
122 /**
123  * Main function that will be run by the scheduler.
124  *
125  * @param cls closure
126  * @param args remaining command-line arguments
127  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
128  * @param cfg configuration
129  */
130 static void
131 run (void *cls,
132      char *const *args,
133      const char *cfgfile,
134      const struct GNUNET_CONFIGURATION_Handle *cfg)
135 {
136   /* create a Processor */
137   LOG("Initializing\n");
138   zbar_processor_t *proc = zbar_processor_create(1);
139
140   // FIXME: Wrap all this into a function which returns an error on
141   // failure. And here ensure the processor is destroyed at the end.
142
143   /* configure the Processor */
144   zbar_processor_parse_config(proc, "enable");
145
146   /* initialize the Processor */
147   LOG("Opening video device %s\n", device);
148   // FIXME: error handling
149   zbar_processor_init(proc, device, 1);
150
151   /* enable the preview window */
152   zbar_processor_set_visible(proc, 1);
153   zbar_processor_set_active(proc, 1);
154
155   /* keep scanning until user provides key/mouse input */
156   //zbar_processor_user_wait(proc, -1);
157
158   // read at least one barcode (or until window closed)
159   LOG("Capturing\n");
160   int n;
161   n = zbar_process_one(proc, -1);
162   LOG("Got %i images\n", n);
163   // FIXME: Error handling (n = -1)
164
165   // hide the preview window
166   zbar_processor_set_active(proc, 0);
167   zbar_processor_set_visible(proc, 0);
168
169   // extract results
170   const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc);
171   const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols);
172
173   if (symbol != NULL) {
174     const char* data = zbar_symbol_get_data(symbol);
175     LOG("Found %s \"%s\"\n",
176         zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
177
178     gnunet_uri(cls, data, cfgfile, cfg);
179     if (exit_code != 0) {
180       printf("Failed to add URI %s\n", data);
181     } else {
182       printf("Added URI %s\n", data);
183     }
184   }
185
186   /* clean up */
187   zbar_processor_destroy(proc);
188 };
189
190
191 int
192 main (int argc, char *const *argv)
193 {
194   static int ret;
195   struct GNUNET_GETOPT_CommandLineOption options[] = {
196     GNUNET_GETOPT_option_string ('d', "device", "DEVICE",
197      gettext_noop ("use video-device DEVICE (default: /dev/video0"),
198      &device),
199     GNUNET_GETOPT_option_flag ('\0', "verbose",
200      gettext_noop ("be verbose"),
201      &verbose),
202     GNUNET_GETOPT_option_flag ('s', "silent",
203      gettext_noop ("do not show preview windows"),
204                                &silent),
205     GNUNET_GETOPT_OPTION_END
206   };
207   ret = GNUNET_PROGRAM_run (argc,
208                             argv,
209                             "gnunet-qr",
210                             gettext_noop ("Scan a QR code using a video device and import the uri read"),
211                             options, &run, NULL);
212   return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
213 }