File: userui_usplash_core.c

package info (click to toggle)
tuxonice-userui 1.1%2Bdfsg1.gc3bdd83-4
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm, bullseye, buster, stretch
  • size: 664 kB
  • ctags: 2,034
  • sloc: ansic: 5,786; makefile: 91; sh: 63; python: 46
file content (321 lines) | stat: -rw-r--r-- 7,035 bytes parent folder | download | duplicates (3)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * userui_usplash_core.c - usplash userspace user interface module.
 *
 * Copyright (C) 2005-2007, Bernard Blackham <bernard@blackham.com.au>
 * Copyright (C) 2008-2009, Nigel Cunningham <nigel@tuxonice.net>
 * 
 * This file is subject to the terms and conditions of the GNU General Public
 * License v2.  See the file COPYING in the main directory of this archive for
 * more details.
 *
 */

#include <linux/vt.h>
#include <sys/resource.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "usplash_backend.h"
#include "libusplash.h"
#include "../userui.h"

#define MIN_ADVANCE_PERCENT 5

static int usplash_ready = 0;
static int userui_usplash_xres = 0;
static int userui_usplash_yres = 0;
static int userui_usplash_verbose = 1;

#if 0
#include <dlfcn.h>

static void *dl_handle;
static int (*usplash_setup) (int xres, int yres, int verbose);
static void (*usplash_restore_console) (void);
static void (*usplash_done) (void);
static void (*draw_progressbar) (int percentage);
static void (*clear_screen) (void);
static void (*clear_progressbar) (void);
static void (*clear_text) (void);
static void (*draw_text) (const char *text, size_t len);
#endif

/*
#ifndef fade_logo
void fade_logo(on, step) { }
#endif
*/

static void read_usplash_conf() {
    char s[1024];
    FILE *f;

    f = fopen("/etc/usplash.conf", "r");
    if (f == NULL)
	return;

    while (fgets(s, sizeof(s), f)) {
	char *p, *varname, *varvalue;

	/* Oh, how I long for a regex. */
	/* m/^\s*([a-z]+)\s*=\s*([0-9]+)\s*$/ */

	/* Trim leading whitespace */
	p = s;
	while (isspace(*p))
	    p++;

	/* Match [a-z]+ */
	if (*p == '\0' || !isalpha(*p))
	    continue;
	varname = p;
	while (isalpha(*p))
	    p++;

	/* Match \s*= and null-terminate */
	if (*p == '=')
	    *p++ = '\0';
        else if (isspace(*p)) {
	    *p++ = '\0';
	    while (isspace(*p))
		p++;
	    if (*p != '=')
		continue;
	    p++;
	} else
	    continue;

	/* Snip more whitespace */
	while (isspace(*p))
	    p++;

	/* Match [0-9]+ */
	if (*p == '\0' || !isdigit(*p))
	    continue;
	varvalue = p;
	while (isdigit(*p))
	    p++;

	/* Null terminate value */
	if (*p != '\0')
	    *p++ = '\0';

	/* Trim trailing whitespace */
	while (isspace(*p))
	    p++;

	/* Make sure we hit the end. */
	if (*p != '\0')
	    continue;

	/* Yay! Now see if it's useful. */
	if (strcmp(varname, "xres") == 0 && userui_usplash_xres == 0)
	    userui_usplash_xres = atoi(varvalue);
	else if (strcmp(varname, "yres") == 0 && userui_usplash_yres == 0)
	    userui_usplash_yres = atoi(varvalue);
    }

    fclose(f);
}

static void userui_usplash_prepare() {
    struct termios t;
    int have_termios;
    struct rlimit r;

    /* Read the usplash.conf file (not quite bash-like, but hopefully good
     * enough. */
    read_usplash_conf();

    /* Set RLIMIT_NPROC now to prevent svgalib from forking. */
    r.rlim_cur = r.rlim_max = 0;
    setrlimit(RLIMIT_NPROC, &r);

    /* Save termios as usplash does silly things with it.  (specifically, makes
     * \n send SIGQUIT). We should already have all the termios we need.
     */
    have_termios = (-1 != tcgetattr(STDIN_FILENO, &t));

    if (usplash_setup(userui_usplash_xres, userui_usplash_yres,
			userui_usplash_verbose)) {
	usplash_restore_console();
	if (have_termios)
	    tcsetattr(STDIN_FILENO, TCSANOW, &t);
	fprintf(stderr, "Failed to initialise usplash!\n");
	//exit (2);
	return;
    }

    if (have_termios)
	tcsetattr(STDIN_FILENO, TCSANOW, &t);

    clear_screen();
    clear_progressbar();
    clear_text();
    fade_logo(1, 10);

    usplash_ready = 1;
}

static void userui_usplash_cleanup() {
    if (!usplash_ready)
	return;

    fade_logo(1, 10);
    usplash_done();
    usplash_restore_console();
}

static void userui_usplash_message(__uint32_t section, __uint32_t level,
		__uint32_t normally_logged, char *msg) {
    if (!usplash_ready)
	return;

    if (section && !((1 << section) & suspend_debug))
	return;

    if (level > console_loglevel)
	return;

    draw_text(msg, strlen(msg));
}

static void userui_usplash_update_progress(__uint32_t value, __uint32_t maximum,
		char *msg) {
    static __uint32_t prev_maximum = -1;
    static __uint32_t old_percent = -1;

    __uint32_t percent;

    if (!usplash_ready)
	return;

    if (maximum == (__uint32_t)(-1))
	value = maximum = 1;

    if (value > maximum)
	value = maximum;

    if (maximum > 0)
	percent = value * 100 / maximum;
    else
	percent = 100;

    if (prev_maximum == -1)
	prev_maximum = maximum;
    else {
	__uint32_t adv = percent - old_percent;
	if (prev_maximum == maximum && percent != 100 &&
	        0 < adv && adv < MIN_ADVANCE_PERCENT)
	    return;
    }

    if (resuming)
       draw_progressbar(percent);
    else
       draw_progressbar(-percent);
    old_percent = percent;
}

static void userui_usplash_log_level_change(int loglevel) {
    if (!usplash_ready)
	return;
}

static void userui_usplash_redraw() {
    if (!usplash_ready)
	return;
    clear_screen();
}

static void userui_usplash_keypress(int key) {
    if (common_keypress_handler(key))
	return;
    switch (key) {
    }
}

static int usplash_option_handler(char c)
{
    switch (c) {
	case 'x':
	    sscanf(optarg, "%d", &userui_usplash_xres);
	    break;
	case 'y':
	    sscanf(optarg, "%d", &userui_usplash_yres);
	    break;
	case 'q':
	    userui_usplash_verbose = 0;
	    break;
	default:
	    return 0;
    }
    return 1;
}

static char *usplash_cmdline_options()
{
    return 
"\n"
"  USPLASH:\n"
"  -x <x-resolution>, --xres <x-resolution>\n"
"     Specifies the X resolution in pixels.\n"
"  -y <y-resolution>, --yres <y-resolution>\n"
"     Specifies the Y resolution in pixels.\n"
"  -q, --quiet\n"
"     Enables usplash's quiet mode (no text is shown).\n";
}

static struct option userui_usplash_longopts[] = {
    {"xres", 1, 0, 'x'},
    {"yres", 1, 0, 'y'},
    {"quiet", 0, 0, 'q'},
    {NULL, 0, 0, 0},
};

#if 0
static int userui_usplash_load(void)
{
	dl_handle = dlopen("libusplash.so", RTLD_NOW);
	if (!dl_handle) {
		fputs(dlerror(), stderr);
		return 1;
	}

	get_dlsym(usplash_setup);
	get_dlsym(usplash_restore_console);
	get_dlsym(usplash_done);
	get_dlsym(draw_progressbar);
	get_dlsym(clear_screen);
	get_dlsym(clear_progressbar);
	get_dlsym(clear_text);
	get_dlsym(draw_text);

	return 0;
};
#endif

struct userui_ops userui_usplash_ops = {
    .name = "usplash",
    //.load = userui_usplash_load,
    .prepare = userui_usplash_prepare,
    .cleanup = userui_usplash_cleanup,
    .message = userui_usplash_message,
    .update_progress = userui_usplash_update_progress,
    .log_level_change = userui_usplash_log_level_change,
    .redraw = userui_usplash_redraw,
    .keypress = userui_usplash_keypress,

    /* cmdline options */
    .optstring = "x:y:q",
    .longopts  = userui_usplash_longopts,
    .option_handler = usplash_option_handler,
    .cmdline_options = usplash_cmdline_options,
};