File: usplash.c

package info (click to toggle)
usplash 0.3e
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 852 kB
  • ctags: 739
  • sloc: ansic: 7,936; sh: 254; makefile: 173; perl: 26
file content (475 lines) | stat: -rw-r--r-- 9,897 bytes parent folder | download
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* usplash
 *
 * Copyright © 2006 Canonical Ltd.
 * Copyright © 2006 Dennis Kaarsemaker <dennis@kaarsemaker.net>
 * Copyright © 2005 Matthew Garrett <mjg59@srcf.ucam.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <linux/vt.h>

#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "bogl.h"
#include "usplash.h"
#include "usplash-theme.h"


/* Prototypes of static functions */
static void switch_console    (int vt);
static void restore_console   (void);

static int  main_loop         (void);
static int  read_command      (void);
static int  parse_command     (const char *string, size_t len);

static void clear_screen      (void);

static void clear_progressbar (void);
static void draw_progressbar  (int percentage);

static void clear_text        (void);
static void draw_text         (const char *string, size_t len);
static void draw_line         (const char *string, size_t len);
static void draw_status       (const char *string, size_t len, int mode);


/* Default theme, used when no suitable alternative can be found */
extern struct usplash_theme testcard_theme;

/* Theme being used */
static struct usplash_theme *theme;

/* Distance to themed area from the top-left corner of the screen */
static int left_edge, top_edge;


/* Virtual terminal we switched away from */
static int saved_vt = 0;

/* Virtual terminal we switched to */
static int new_vt = 0;


/* File descriptor to read commands from */
static int fifo_fd;

/* Number of seconds to wait for a command before exiting */
static int timeout = 15;


int
main (int   argc,
      char *argv[])
{
	void *theme_handle;
	int   i, ret = 0;

	for (i = 1; i < argc; i++) {
		if (! strcmp (argv[i], "-c")) {
			switch_console (8);
		} else {
			fprintf (stderr, "unknown argument: %s\n", argv[i]);
			exit (1);
		}
	}


	if (chdir (USPLASH_DIR) < 0) {
		perror ("chdir");
		ret = 1;
		goto exit;
	}

	if (mkfifo (USPLASH_FIFO, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) < 0) {
		if (errno != EEXIST) {
			perror ("mkfifo");
			ret = 1;
			goto exit;
		}
	}

	fifo_fd = open (USPLASH_FIFO, O_RDONLY|O_NONBLOCK);
	if (fifo_fd < 0) {
		perror ("open");
		ret = 2;
		goto exit;
	}


	theme_handle = dlopen (USPLASH_THEME, RTLD_LAZY);
	if (theme_handle) {
		theme = dlsym (theme_handle, "usplash_theme");
		if ((theme == NULL) || (theme->version != THEME_VERSION)) {
			dlclose (theme_handle);
			theme = &testcard_theme;
		}
	} else {
		theme = &testcard_theme;
	}

	if (! bogl_init ()) {
		fprintf (stderr, "bogl_init failed\n");
		ret = 3;
		goto exit;
	}


	left_edge = (bogl_xres - theme->pixmap->width)  / 2;
	top_edge  = (bogl_yres - theme->pixmap->height) / 2;

	bogl_set_palette (0, theme->pixmap->ncols, theme->pixmap->palette);

	clear_screen ();
	clear_progressbar ();
	clear_text ();

	ret = main_loop ();

	bogl_done ();
exit:
	restore_console ();
	return ret;
}


static void
switch_console (int vt)
{
	char           vtname[10];
	struct vt_stat state;
	int            fd;

	assert ((vt >= 0) && (vt < 10));
	sprintf (vtname, "/dev/tty%d", vt);

	fd = open ("/dev/console", O_RDWR);
	ioctl (fd, VT_GETSTATE, &state);
	close (fd);

	saved_vt = state.v_active;
	new_vt = vt;

	fd = open (vtname, O_RDWR);
	ioctl (fd, VT_ACTIVATE, vt);
	ioctl (fd, VT_WAITACTIVE, vt);
	close (fd);
}

static void
restore_console (void)
{
	struct vt_stat state;
	int            fd;

	if (saved_vt != 0) {
                fd = open ("/dev/console", O_RDWR);
                ioctl (fd, VT_GETSTATE, &state);
                close (fd);

		/* Switch back if we're still on the console we switched to */
                if (state.v_active == new_vt)
			switch_console (saved_vt);
	}
}


static int
main_loop (void)
{

	for (;;) {
		struct timeval tv;
		fd_set         rfds;
		int            retval;

		FD_ZERO (&rfds);
		FD_SET (fifo_fd, &rfds);

		tv.tv_sec = timeout;
		tv.tv_usec = 0;

		retval = select (fifo_fd + 1, &rfds, NULL, NULL, &tv);

		if (retval < 0) {
			/* Error */
			return 1;
		} else if (retval > 0) {
			int ret;

			/* Data available */
			ret = read_command ();
			if (ret)
				return ret;
		} else {
			/* Timeout */
			return 0;
		}
	}

	/* Not reached */
	return 0;
}

static int
read_command (void)
{
	static char    buf[4096], *ptr;
	static size_t  buflen;
	static ssize_t len;

	len = read (fifo_fd, buf + buflen, sizeof (buf) - buflen);
	if (len < 0) {
		if (errno != EAGAIN) {
			/* Try opening again */
			close (fifo_fd);
			fifo_fd = open (USPLASH_FIFO, O_RDONLY|O_NONBLOCK);
			if (fifo_fd < 0)
				return 2;
		}

		return 0;
	} else if (len == 0) {
		/* Reopen to see if there's anything more for us */
		close (fifo_fd);
		fifo_fd = open (USPLASH_FIFO, O_RDONLY|O_NONBLOCK);
		if (fifo_fd < 0)
			return 2;

		return 0;
	}

	buflen += len;
	while ((ptr = memchr (buf, '\0', buflen)) != NULL) {
		int ret;

		ret = parse_command (buf, ptr - buf);
		if (ret)
			return ret;

		/* Move the rest of the buffer up */
		buflen -= ptr - buf + 1;
		memcpy (buf, ptr + 1, buflen);
	}

	return 0;
}

static size_t
strncspn (const char *s, size_t n, const char *reject)
{
	register size_t l;

	for (l = 0; l < n; l++)
		if (strchr (reject, s[l]))
			break;

	return l;
}

static int
parse_command (const char *string, size_t len)
{
	const char *command;
	size_t      commandlen;

	command = string;
	commandlen = strncspn (string, len, " ");

	if (string[commandlen] == ' ') {
		string += commandlen + 1;
		len -= commandlen + 1;
	} else {
		len = 0;
	}


	if (! strncmp (command, "QUIT", commandlen)) {
		return 1;

	} else if (! strncmp (command, "TIMEOUT", commandlen)) {
		timeout = atoi (string);

	} else if (! strncmp (command, "CLEAR", commandlen)) {
		clear_text ();

	} else if (! strncmp (command, "TEXT", commandlen)) {
		draw_text (string, len);

	} else if (! strncmp (command, "STATUS", commandlen)) {
		draw_status (string, len, 0);

	} else if (! strncmp (command, "SUCCESS", commandlen)) {
		draw_status (string, len, 1);

	} else if (! strncmp (command, "FAILURE", commandlen)) {
		draw_status (string, len, -1);

	} else if (! strncmp (command, "PROGRESS", commandlen)) {
		draw_progressbar (atoi (string));
	}

	return 0;
}


static void
clear_screen (void)
{
	const int colour_map[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
				   0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf };

	bogl_clear (0, 0, bogl_xres, bogl_yres, theme->background);

	bogl_put (left_edge, top_edge, theme->pixmap, colour_map);
}


static void
clear_progressbar (void)
{
	int x1, y1, x2, y2;

	x1 = left_edge + theme->progressbar_x;
	y1 = top_edge + theme->progressbar_y;

	x2 = x1 + theme->progressbar_width;
	y2 = y1 + theme->progressbar_height;

	bogl_clear (x1, y1, x2, y2, theme->progressbar_background);
}

static void
draw_progressbar (int percentage)
{
	int x1, y1, x2, y2, xx, bg, fg;

	if (percentage < 0) {
		bg = theme->progressbar_foreground;
		fg = theme->progressbar_background;
		percentage = -percentage;
	} else {
		bg = theme->progressbar_background;
		fg = theme->progressbar_foreground;
	}

	if (percentage > 100)
		return;


	x1 = left_edge + theme->progressbar_x;
	y1 = top_edge + theme->progressbar_y;

	x2 = x1 + theme->progressbar_width;
	y2 = y1 + theme->progressbar_height;

	xx = x1 + ((theme->progressbar_width * percentage) / 100);

	bogl_clear (x1, y1, xx, y2, fg);
	bogl_clear (xx, y1, x2, y2, bg);
}


static void
clear_text (void)
{
	int x1, y1, x2, y2;

	x1 = left_edge + theme->text_x;
	y1 = top_edge + theme->text_y;

	x2 = x1 + theme->text_width;
	y2 = y1 + theme->text_height;

	bogl_clear (x1, y1, x2, y2, theme->text_background);
}

static void
draw_text (const char *string, size_t len)
{
	/* FIXME some kind of better word wrapping here, perhaps? */
#if 0
	while (len > theme->line_length) {
		draw_line (string, theme->line_length);
		string += theme->line_length;
		len -= theme->line_length;
	}
#endif

	draw_line (string, len);
}

static void
draw_line (const char *string, size_t len)
{
	int x1, y1, x2, y2;

	x1 = left_edge + theme->text_x;
	x2 = x1 + theme->text_width;

	y2 = top_edge + theme->text_y + theme->text_height;
	y1 = y2 - theme->line_height;

	/* Move existing text up */
	bogl_move (x1, top_edge + theme->text_y + theme->line_height,
		   x1, top_edge + theme->text_y,
		   theme->text_width, theme->text_height - theme->line_height);


	bogl_clear (x1, y1, x2, y2, theme->text_background);

	bogl_text (x1, y1, string, len,
		   theme->text_foreground, theme->text_background,
		   0, theme->font);
}

static void
draw_status (const char *string, size_t len, int mode)
{
	int x1, y1, x2, y2, fg;

	if (mode < 0) {
		fg = theme->text_failure;
	} else if (mode > 0) {
		fg = theme->text_success;
	} else {
		fg = theme->text_foreground;
	}

	x2 = left_edge + theme->text_x + theme->text_width;
	y2 = top_edge + theme->text_y + theme->text_height;

	x1 = x2 - theme->status_width;
	y1 = y2 - theme->line_height;

	bogl_clear (x1, y1, x2, y2, theme->text_background);

	bogl_text (x1, y1, string, len, fg, theme->text_background,
		   0, theme->font);
}