aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-qr.c
diff options
context:
space:
mode:
authorHartmut Goebel <h.goebel@crazy-compilers.com>2019-03-02 11:00:53 +0100
committerChristian Grothoff <christian@grothoff.org>2019-04-03 13:42:16 +0200
commitc7ec9cc03a383a33315c9cc126ebbf73135a17cc (patch)
treeef09e17d7334a26b4630281c585649ead0728f83 /src/util/gnunet-qr.c
parent63dcaed7c14b636b6175843520f13ba1b9a8ea1b (diff)
downloadgnunet-c7ec9cc03a383a33315c9cc126ebbf73135a17cc.tar.gz
gnunet-c7ec9cc03a383a33315c9cc126ebbf73135a17cc.zip
gnunet-qr: Reimplement in C - yet only a proof of concept.
Still to-do: * running gnunet-uri * Proper error handling * integration into build system (automake) Reimplementing in C was chosen since - official zbar python-bindings support python 2 only, - none of the other bindings available at PyPI supports the high-level "processor" interface which gnunet-qr uses - implementing bindings for zbar using ctypes required addin a lot of low-level error handling code, thus implementing in C seamed to be easier, - the programm is short, thus re-implementing is not such complicated, and - this allows to reduce the number of dependencies (here: another Python version), which should ease porting to other plattforms (zbar is a dependency anyway).
Diffstat (limited to 'src/util/gnunet-qr.c')
-rw-r--r--src/util/gnunet-qr.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/util/gnunet-qr.c b/src/util/gnunet-qr.c
new file mode 100644
index 000000000..c02212a51
--- /dev/null
+++ b/src/util/gnunet-qr.c
@@ -0,0 +1,160 @@
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
26static 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
41int main (int argc, char **argv)
42{
43 const char* configuration = NULL;
44 const char* device = "/dev/video0";
45 static bool verbose = false;
46 static bool silent = false;
47
48 static struct option long_options[] = {
49 {"verbose", no_argument, 0, 'v'},
50 {"silent", no_argument, 0, 's'},
51 {"help", no_argument, 0, 'h'},
52 {"config", required_argument, 0, 'c'},
53 {"device", required_argument, 0, 'd'},
54 {0, 0, 0, 0}
55 };
56 while (1) {
57 int opt;
58 opt = getopt_long (argc, argv, "c:hd:sv",
59 long_options, NULL);
60 if (opt == -1)
61 break;
62
63 switch (opt) {
64 case 'h':
65 printf(usage_note);
66 return 0;
67 case 'c':
68 configuration = optarg;
69 break;
70 case 'd':
71 device = optarg;
72 break;
73 case 's':
74 silent = true;
75 break;
76 case 'v':
77 verbose = true;
78 break;
79 default:
80 printf(usage_note);
81 return 1;
82 }
83 }
84
85 /* create a Processor */
86 if (verbose == true) {
87 printf("Initializing\n");
88 };
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 if (verbose == true) {
99 printf("Opening video device %s\n", device);
100 };
101 // FIXME: error handling
102 zbar_processor_init(proc, device, 1);
103
104 /* enable the preview window */
105 zbar_processor_set_visible(proc, 1);
106 zbar_processor_set_active(proc, 1);
107
108 /* keep scanning until user provides key/mouse input */
109 //zbar_processor_user_wait(proc, -1);
110
111 // read at least one barcode (or until window closed)
112 if (verbose == true) {
113 printf("Capturing\n");
114 }
115 int n;
116 n = zbar_process_one(proc, -1);
117 if (verbose == true) {
118 printf("Got %i images\n", n);
119 };
120 // FIXME: Error handling (n = -1)
121
122 // hide the preview window
123 zbar_processor_set_active(proc, 0);
124 zbar_processor_set_visible(proc, 0);
125
126 // extract results
127 int rc = 1;
128
129 const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc);
130 const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols);
131
132 if (symbol != NULL) {
133 const char* data = zbar_symbol_get_data(symbol);
134 if (verbose = true) {
135 zbar_symbol_type_t type =
136 printf("Found %s \"%s\"\n",
137 zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
138 }
139 /* TODO
140 args = ["gnunet-uri",
141 // FIXME: "-c", configuration,
142 data];
143 if (verbose = true) {
144 // TODO: print arguments:
145 printf("Running `%s %s %s %s`", *args, "", ""); // FIXME variable num args
146 };
147 rc = popen("gnunet-uri", *args);
148 */
149 if (rc != 0) {
150 printf("Failed to add URI %s\n", data);
151 } else {
152 printf("Added URI %s\n", data);
153 }
154 }
155
156 /* clean up */
157 zbar_processor_destroy(proc);
158
159 return(rc);
160}