aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorHartmut Goebel <h.goebel@crazy-compilers.com>2019-03-02 11:00:53 +0100
committerHartmut Goebel <h.goebel@crazy-compilers.com>2019-03-13 18:22:01 +0100
commitd5cfe4c295d7136a4c82fea0ccd927f2a9a900d3 (patch)
tree0df4f66c5c280d05f2153ddd2a8c7412fcb2896e /src/util
parentc7ac8e24b867e3d6c84f778f4bb6623a92ed37d7 (diff)
downloadgnunet-d5cfe4c295d7136a4c82fea0ccd927f2a9a900d3.tar.gz
gnunet-d5cfe4c295d7136a4c82fea0ccd927f2a9a900d3.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')
-rw-r--r--src/util/gnunet-qr.c160
-rwxr-xr-xsrc/util/gnunet-qr.in46
-rw-r--r--src/util/gnunet-qr.py110
3 files changed, 160 insertions, 156 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}
diff --git a/src/util/gnunet-qr.in b/src/util/gnunet-qr.in
deleted file mode 100755
index ce7a19b69..000000000
--- a/src/util/gnunet-qr.in
+++ /dev/null
@@ -1,46 +0,0 @@
1#!/bin/sh
2#
3# From curl's buildconf, making this script subject to the
4# curl license: https://curl.haxx.se/docs/copyright.html
5# Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
6# Copyright (C) 2019 GNUnet e.V.
7
8# findtool works like which without relying on which (which is a problem
9# for some limited shells.
10findtool(){
11 file="$1"
12
13 if { echo "$file" | grep "/" >/dev/null 2>&1; } then
14 # when file is given with a path check it first
15 if test -f "$file"; then
16 echo "$file"
17 return
18 fi
19 fi
20
21 old_IFS=$IFS; IFS=':'
22 for path in $PATH
23 do
24 IFS=$old_IFS
25 # echo "checks for $file in $path" >&2
26 if test "$path" -a "$path" != '.' -a -f "$path/$file"; then
27 echo "$path/$file"
28 return
29 fi
30 done
31 IFS=$old_IFS
32}
33
34# end curl licensed code
35pythonize=`findtool python2.7 2>/dev/null`
36if test ! -x "$pythonize"; then
37 pythonize=`findtool ${PYTHON2:-python2.7}`
38fi
39
40if test -z "$pythonize"; then
41 echo "ERROR: python2.7 not found."
42 echo " You need python2.7 installed."
43 exit 1
44fi
45
46${pythonize} @PREFIX@/share/gnunet/gnunet-qr.py $@
diff --git a/src/util/gnunet-qr.py b/src/util/gnunet-qr.py
deleted file mode 100644
index 0ee0b9507..000000000
--- a/src/util/gnunet-qr.py
+++ /dev/null
@@ -1,110 +0,0 @@
1import sys
2import getopt
3import subprocess
4from sys import argv
5try:
6 import zbar
7except ImportError as e:
8 print('Cannot run gnunet-qr, please install the zbar module.')
9 print('For Debian, you can obtain it as "python-zbar".')
10 print('Upstream: http://zbar.sourceforge.net/')
11 sys.exit(1)
12
13
14def help():
15 print('gnunet-qr\n\
16Scan a QR code using a video device and import\n\
17Arguments mandatory for long options are also mandatory for short options.\n\
18 -c, --config FILENAME use configuration file FILENAME\n\
19 -d, --device DEVICE use device DEVICE\n\
20 -s, --silent do not show preview windows\n\
21 -h, --help print this help\n\
22 -v, --verbose be verbose\n\
23Report bugs to gnunet-developers@gnu.org.\n\
24GNUnet home page: https://gnunet.org/\n\
25General help using GNU software: https://www.gnu.org/gethelp/')
26
27
28if __name__ == '__main__':
29 configuration = ''
30 device = '/dev/video0'
31 url = ''
32 verbose = False
33 silent = False
34 # Parse arguments
35 try:
36 opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
37 except getopt.GetoptError as e:
38 help()
39 print(str(e))
40 exit(1)
41 for o, a in opts:
42 if o in ("-h", "--help"):
43 help()
44 sys.exit(0)
45 elif o in ("-c", "--config"):
46 configuration = a
47 elif o in ("-d", "--device"):
48 device = a
49 elif o in ("-s", "--silent"):
50 silent = True
51 elif o in ("-v", "--verbose"):
52 verbose = True
53 if (True == verbose):
54 print('Initializing')
55 # create a Processor
56 proc = zbar.Processor()
57
58 # configure the Processor
59 proc.parse_config('enable')
60
61 # initialize the Processor
62 try:
63 if (True == verbose):
64 print('Opening video device ' + device)
65 proc.init(device)
66 except Exception as e:
67 print('Failed to open device ' + device)
68 exit(1)
69
70 # enable the preview window
71 # if (True == silent):
72 # proc.visible = True
73 # else:
74 # proc.visible = False
75
76 proc.visible = True
77 # read at least one barcode (or until window closed)
78 try:
79 if (True == verbose):
80 print('Capturing')
81 proc.process_one()
82 except Exception as e:
83 # Window was closed without finding code
84 exit(1)
85
86 # hide the preview window
87 proc.visible = False
88
89 # extract results
90 for symbol in proc.results:
91 # do something useful with results
92 if (True == verbose):
93 print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
94 args = list()
95 args.append("gnunet-uri")
96 if (configuration != ''):
97 args.append(str("-c " + str(configuration)))
98 args.append(str(symbol.data))
99 cmd = ''
100 for a in args:
101 cmd += " " + str(a)
102 if (verbose):
103 print('Running `' + cmd +'`')
104 res = subprocess.call(args)
105 if (0 != res):
106 print('Failed to add URI ' + str(symbol.data))
107 else:
108 print('Added URI ' + str(symbol.data))
109 exit(res)
110 exit(1)