summaryrefslogtreecommitdiffstats
path: root/src/shared/qrcode-util.c
blob: c087136bd2b3d7857138371a8d9650b0e8c59c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "qrcode-util.h"

#if HAVE_QRENCODE
#include <qrencode.h>

#include "dlfcn-util.h"
#include "locale-util.h"
#include "log.h"
#include "strv.h"
#include "terminal-util.h"

#define ANSI_WHITE_ON_BLACK "\033[40;37;1m"
#define UNICODE_FULL_BLOCK       u8"█"
#define UNICODE_LOWER_HALF_BLOCK u8"▄"
#define UNICODE_UPPER_HALF_BLOCK u8"▀"

static void *qrcode_dl = NULL;

static DLSYM_FUNCTION(QRcode_encodeString);
static DLSYM_FUNCTION(QRcode_free);

int dlopen_qrencode(void) {
        int r;

        FOREACH_STRING(s, "libqrencode.so.4", "libqrencode.so.3") {
                r = dlopen_many_sym_or_warn(
                        &qrcode_dl, s, LOG_DEBUG,
                        DLSYM_ARG(QRcode_encodeString),
                        DLSYM_ARG(QRcode_free));
                if (r >= 0)
                        break;
        }

        return r;
}

static void print_border(FILE *output, unsigned width, unsigned row, unsigned column) {
        assert(output);
        assert(width);

        if (row != UINT_MAX && column != UINT_MAX) {
                int r, fd;

                fd = fileno(output);
                if (fd < 0)
                        return (void)log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");

                r = set_terminal_cursor_position(fd, row, column);
                if (r < 0)
                        log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");

                /* Four rows of border */
                for (unsigned y = 0; y < 4; y += 2) {
                        fputs(ANSI_WHITE_ON_BLACK, output);

                        for (unsigned x = 0; x < 4 + width + 4; x++)
                                fputs(UNICODE_FULL_BLOCK, output);

                        fputs(ANSI_NORMAL "\n", output);
                        r = set_terminal_cursor_position(fd, row + 1, column);
                        if (r < 0)
                                log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
                }
        } else {
                /* Four rows of border */
                for (unsigned y = 0; y < 4; y += 2) {
                        fputs(ANSI_WHITE_ON_BLACK, output);

                        for (unsigned x = 0; x < 4 + width + 4; x++)
                                fputs(UNICODE_FULL_BLOCK, output);

                        fputs(ANSI_NORMAL "\n", output);
                }
        }
}

static void write_qrcode(FILE *output, QRcode *qr, unsigned int row, unsigned int column) {
        assert(qr);

        if (!output)
                output = stdout;

        print_border(output, qr->width, row, column);

        if (row != UINT_MAX && column != UINT_MAX) {
                /* After printing two rows of top border, we need to move the cursor down two rows before starting to print the actual QR code */
                int r, fd, move_down = 2;
                fd = fileno(output);
                if (fd < 0)
                        return (void)log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");

                r = set_terminal_cursor_position(fd, row + move_down, column);
                if (r < 0)
                        log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");

                for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
                        const uint8_t *row1 = qr->data + qr->width * y;
                        const uint8_t *row2 = row1 + qr->width;

                        fputs(ANSI_WHITE_ON_BLACK, output);

                        for (unsigned x = 0; x < 4; x++)
                                fputs(UNICODE_FULL_BLOCK, output);

                        for (unsigned x = 0; x < (unsigned) qr->width; x++) {
                                bool a, b;

                                a = row1[x] & 1;
                                b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;

                                if (a && b)
                                        fputc(' ', output);
                                else if (a)
                                        fputs(UNICODE_LOWER_HALF_BLOCK, output);
                                else if (b)
                                        fputs(UNICODE_UPPER_HALF_BLOCK, output);
                                else
                                        fputs(UNICODE_FULL_BLOCK, output);
                        }

                        for (unsigned x = 0; x < 4; x++)
                                fputs(UNICODE_FULL_BLOCK, output);
                        r = set_terminal_cursor_position(fd, row + move_down, column);
                        if (r < 0)
                                log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
                        move_down += 1;
                        fputs(ANSI_NORMAL "\n", output);
                }

                print_border(output, qr->width, row + move_down, column);
        } else {

                for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
                        const uint8_t *row1 = qr->data + qr->width * y;
                        const uint8_t *row2 = row1 + qr->width;

                        fputs(ANSI_WHITE_ON_BLACK, output);
                        for (unsigned x = 0; x < 4; x++)
                                fputs(UNICODE_FULL_BLOCK, output);

                        for (unsigned x = 0; x < (unsigned) qr->width; x++) {
                                bool a, b;

                                a = row1[x] & 1;
                                b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;

                                if (a && b)
                                        fputc(' ', output);
                                else if (a)
                                        fputs(UNICODE_LOWER_HALF_BLOCK, output);
                                else if (b)
                                        fputs(UNICODE_UPPER_HALF_BLOCK, output);
                                else
                                        fputs(UNICODE_FULL_BLOCK, output);
                        }

                        for (unsigned x = 0; x < 4; x++)
                                fputs(UNICODE_FULL_BLOCK, output);
                        fputs(ANSI_NORMAL "\n", output);
                }

                print_border(output, qr->width, row, column);
        }

        fflush(output);
}

int print_qrcode_full(FILE *out, const char *header, const char *string, unsigned row, unsigned column, unsigned tty_width, unsigned tty_height) {
        QRcode* qr;
        int r;

        /* If this is not a UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
         * codes */
        if (!is_locale_utf8() || !colors_enabled())
                return -EOPNOTSUPP;

        r = dlopen_qrencode();
        if (r < 0)
                return r;

        qr = sym_QRcode_encodeString(string, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
        if (!qr)
                return -ENOMEM;

        if (row != UINT_MAX && column != UINT_MAX) {
                int fd;
                unsigned qr_code_width, qr_code_height;
                fd = fileno(out);
                if (fd < 0)
                        return log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
                qr_code_width = qr_code_height = qr->width + 8;

                if (column + qr_code_width > tty_width)
                        column = tty_width - qr_code_width;

                /* Terminal characters are twice as high as they are wide so it's qr_code_height / 2,
                * our QR code prints an extra new line, so we have -1 as well */
                if (row + qr_code_height > tty_height)
                        row = tty_height - (qr_code_height / 2 ) - 1;

                if (header) {
                        r = set_terminal_cursor_position(fd, row - 2, tty_width - qr_code_width - 2);
                        if (r < 0)
                                log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");

                        fprintf(out, "%s:\n\n", header);
                }
        } else
                if (header)
                        fprintf(out, "\n%s:\n\n", header);

        write_qrcode(out, qr, row, column);

        fputc('\n', out);

        sym_QRcode_free(qr);
        return 0;
}
#endif