Fix typo in the Gentoo initscript.
[fbsplash.git] / core / src / kernel.c
blob56a98c286d9457fa7642320fe2c3fafe502ee8fb
1 /*
2 * kernel.c - the core of fbcondecor_helper
4 * Copyright (C) 2004-2007, Michal Januszewski <spock@gentoo.org>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License v2. See the file COPYING in the main directory of this archive for
8 * more details.
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/mman.h>
21 #include <linux/tiocl.h>
23 #include "common.h"
24 #include "render.h"
25 #include "fbcon_decor.h"
27 int arg_vc;
30 * Opens /dev/console as stdout and stderr. This will not work if console
31 * is set to a serial console, because ttySx are not initialized at the
32 * point where this function is used.
34 void prep_io()
36 int fd = 0;
38 fd = open(PATH_DEV "/console", O_WRONLY);
39 if (fd >= 0) {
40 dup2(fd,0);
41 dup2(fd,1);
42 dup2(fd,2);
46 int handle_init(bool update)
48 char buf[8];
49 int h;
50 stheme_t *theme;
51 #ifdef CONFIG_FBCON_DECOR
52 bool fbcon_decor = true;
53 #endif
55 /* If possible, make sure that the error messages don't go straight
56 * to /dev/null and are displayed on the screen instead. */
57 if (!update)
58 prep_io();
60 fbsplashr_init(true);
62 /* Parse the kernel command line to get splash settings. */
63 mkdir(PATH_PROC,0700);
64 h = mount("proc", PATH_PROC, "proc", 0, NULL);
65 fbsplash_parse_kcmdline(true);
66 if (h == 0)
67 umount(PATH_PROC);
69 if (config.reqmode == FBSPL_MODE_OFF)
70 return 0;
72 /* We don't want to use any effects if we're just updating the image.
73 * Nor do we want to mess with the verbose mode. */
74 if (update) {
75 config.effects = FBSPL_EFF_NONE;
76 #ifdef CONFIG_FBCON_DECOR
77 fbcon_decor = false;
78 #endif
81 theme = fbsplashr_theme_load();
82 if (!theme)
83 return -1;
85 #ifdef CONFIG_FBCON_DECOR
86 fd_fbcondecor = fbcon_decor_open(true);
87 if (fd_fbcondecor == -1) {
88 fprintf(stderr, "Failed to open the fbcon_decor control device.\n");
89 fbcon_decor = false;
92 if (!update && (config.reqmode & FBSPL_MODE_VERBOSE) && fbcon_decor) {
93 if (fbcon_decor_setcfg(FBCON_DECOR_IO_ORIG_USER, arg_vc, theme))
94 goto noverbose;
96 if (fbcon_decor_setpic(FBCON_DECOR_IO_ORIG_USER, arg_vc, theme))
97 goto noverbose;
98 } else {
99 noverbose:
100 fbcon_decor = false;
102 #endif
104 /* Activate verbose mode if it was explicitly requested. If silent mode
105 * was requested, the verbose background image will be set after the
106 * switch to the silent tty is complete. */
107 if (config.reqmode == FBSPL_MODE_VERBOSE) {
108 #ifdef CONFIG_FBCON_DECOR
109 /* Activate fbcon_decor on the first tty if the picture and
110 * the config file were successfully loaded. */
111 if (fbcon_decor) {
112 fbcon_decor_setstate(FBCON_DECOR_IO_ORIG_USER, arg_vc, 1);
113 return 0;
114 } else {
115 fprintf(stderr, "Failed to get background decoration image.\n");
116 return -1;
118 #else
119 fprintf(stderr, "This version of splashutils was compiled without support for fbcondecor.\n"
120 "Verbose mode will not be activated\n");
121 return -1;
122 #endif
125 if (!(theme->modes & FBSPL_MODE_SILENT))
126 return -1;
128 fd_tty0 = open("/dev/console", O_RDWR);
130 fbsplash_set_silent();
131 fbsplashr_tty_silent_init(true);
132 fbsplashr_tty_silent_update();
134 /* Redirect all kernel messages to tty1 so that they don't get
135 * printed over our silent splash image. */
136 buf[0] = TIOCL_SETKMSGREDIRECT;
137 buf[1] = 1;
138 ioctl(fd_tty[config.tty_s], TIOCLINUX, buf);
140 if (config.kdmode == KD_GRAPHICS)
141 ioctl(fd_tty[config.tty_s], KDSETMODE, KD_GRAPHICS);
143 fbsplashr_render_screen(theme, true, true, config.effects);
145 #ifdef CONFIG_FBCON_DECOR
146 if (fbcon_decor && config.reqmode & FBSPL_MODE_VERBOSE)
147 fbcon_decor_setstate(FBCON_DECOR_IO_ORIG_USER, arg_vc, 1);
148 #endif
150 /* We're the kernel helper. We try to do our task as efficiently
151 * as possible and don't care about any cleanup. */
152 return 0;
155 int main(int argc, char **argv)
157 char *tmpc;
158 int err = 0, i = 5;
159 u8 mounts = 0;
161 if (argc < 3)
162 goto out;
164 fbsplash_lib_init(fbspl_undef);
166 if (strcmp(argv[1],"2") && strcmp(argv[1], "1")) {
167 fprintf(stderr, "Splash protocol mismatch: %s\n", argv[1]);
168 fprintf(stderr, "This version of splashutils supports splash protocol v1 and v2.\n");
169 err = -1;
170 goto out;
173 if (argc > 3 && argv[3])
174 arg_vc = atoi(argv[3]);
175 else
176 arg_vc = 0;
178 if (arg_vc < 0)
179 goto out;
181 if (!strcmp(argv[1],"1")) {
182 i = 6;
185 /* On 'init' the theme isn't defined yet, and thus NULL is passed
186 * instead of any meaningful value. */
187 if (argc > i && argv[i]) {
188 fbsplash_acc_theme_set(argv[i]);
190 mkdir(PATH_SYS, 0700);
191 if (!mount("sysfs", PATH_SYS, "sysfs", 0, NULL))
192 mounts = 1;
195 if (!strcmp(argv[2],"init")) {
196 err = handle_init(false);
197 } else if (!strcmp(argv[2],"repaint")) {
198 err = handle_init(true);
200 #ifdef CONFIG_FBCON_DECOR
201 else if (config.theme) {
202 stheme_t *theme;
204 fbsplashr_init(false);
206 fd_fbcondecor = fbcon_decor_open(true);
207 if (fd_fbcondecor == -1) {
208 fprintf(stderr, "Failed to open the fbcon_decor control device.\n");
209 err = -1;
210 goto out;
213 theme = fbsplashr_theme_load();
214 if (!theme) {
215 fprintf(stderr, "Failed to load theme '%s'.\n", config.theme);
216 err = -1;
217 goto out;
220 if (!strcmp(argv[2],"getpic")) {
221 err = fbcon_decor_setpic(FBCON_DECOR_IO_ORIG_KERNEL, arg_vc, theme);
222 } else if (!strcmp(argv[2],"modechange")) {
223 if ((err = fbcon_decor_setcfg(FBCON_DECOR_IO_ORIG_KERNEL, arg_vc, theme)))
224 goto out;
226 if ((err = fbcon_decor_setpic(FBCON_DECOR_IO_ORIG_KERNEL, arg_vc, theme)))
227 goto out;
229 err = fbcon_decor_setstate(FBCON_DECOR_IO_ORIG_KERNEL, arg_vc, 1);
232 #endif
233 else {
234 fprintf(stderr, "Unrecognized splash command: %s.\n", argv[2]);
237 out:
238 if (mounts)
239 umount2(PATH_SYS, 0);
241 #ifdef CONFIG_FBCON_DECOR
242 if (fd_fbcondecor != -1)
243 close(fd_fbcondecor);
244 #endif
246 return err;