core: add support for the autoverbose feature
[fbsplash.git] / core / src / util.c
bloba85fe291f6320ff3589f71a40f903bbc4a351d08
1 /*
2 * util.c - The core of splash_util
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 <sys/types.h>
16 #include <linux/kd.h>
17 #include <linux/vt.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <termios.h>
21 #include <sys/ioctl.h>
22 #include <sys/mman.h>
23 #include <getopt.h>
24 #include <unistd.h>
26 #include "common.h"
27 #include "render.h"
29 static struct option options[] = {
30 { "cmd", required_argument, NULL, 0x102 },
31 { "mode", required_argument, NULL, 0x100 },
32 #ifdef CONFIG_DEPRECATED
33 { "theme", required_argument, NULL, 0x103 },
34 { "progress",required_argument, NULL, 0x105 },
35 { "type", required_argument, NULL, 0x10d },
36 #ifdef CONFIG_TTF
37 { "mesg", required_argument, NULL, 0x109 },
38 #endif
39 #endif
40 { "help", no_argument, NULL, 'h'},
41 { "verbose", no_argument, NULL, 'v'},
42 { "quiet", no_argument, NULL, 'q'},
45 enum { none, getres, paint, setmode, getmode, repaint } arg_task;
47 struct cmd {
48 char *name;
49 int value;
52 static struct cmd cmds[] = {
53 #ifdef CONFIG_DEPRECATED
54 { "paint", paint },
55 { "repaint", repaint },
56 #endif
57 { "setmode", setmode },
58 { "getmode", getmode },
59 { "getres", getres },
62 static void usage()
64 printf(
65 "splash_util/splashutils-" PACKAGE_VERSION "\n"
66 "Usage: splash_util [options] -c <cmd>\n\n"
67 "Commands:\n"
68 #ifdef CONFIG_DEPRECATED
69 " paint paint the background picture\n"
70 " repaint paint the whole background picture (full refresh)\n"
71 #endif
72 " setmode set global splash mode\n"
73 " getmode get global splash mode\n"
74 " getres get the resolution which the silent splash will use\n\n"
75 "Options:\n"
76 " -c, --cmd=CMD execute command CMD\n"
77 " -v, --verbose display verbose error messages\n"
78 " -q, --quiet don't display any messages\n"
79 " -h, --help show this help message\n"
80 " -t, --theme=THEME use theme THEME\n"
81 " -m, --mode=(v|s) set silent (s) or verbose (v) mode\n"
82 #ifdef CONFIG_DEPRECATED
83 " -p, --progress=NUM set progress to NUM/65535 * 100%%\n"
84 #ifdef CONFIG_TTF
85 " --mesg=TEXT use TEXT as the main splash message\n"
86 #endif
87 " --type=TYPE TYPE can be: bootup, reboot, shutdown\n"
88 #endif
92 int util_main(int argc, char **argv)
94 unsigned int c, i;
95 int arg_vc = -1;
96 stheme_t *theme = NULL;
98 fbsplash_lib_init(fbspl_bootup);
99 fbsplashr_init(false);
101 arg_task = none;
102 arg_vc = -1;
104 config.reqmode = FBSPL_MODE_SILENT;
106 while ((c = getopt_long(argc, argv, "c:t:m:p:hvq", options, NULL)) != EOF) {
108 switch (c) {
109 case 'h':
110 usage();
111 return 0;
113 case 0x100:
114 case 'm':
115 if (optarg[0] == 'v')
116 config.reqmode = FBSPL_MODE_VERBOSE;
117 break;
119 case 0x103:
120 case 't':
121 fbsplash_acc_theme_set(optarg);
122 break;
124 case 0x102:
125 case 'c':
126 for (i = 0; i < sizeof(cmds) / sizeof(struct cmd); i++) {
127 if (!strcmp(optarg, cmds[i].name)) {
128 arg_task = cmds[i].value;
129 break;
132 break;
134 case 'p':
135 case 0x105:
136 config.progress = atoi(optarg);
137 break;
139 #ifdef CONFIG_TTF
140 case 0x109:
141 if (config.message)
142 free(config.message);
143 config.message = strdup(optarg);
144 break;
145 #endif
146 case 0x10d:
147 if (!strcmp(optarg, "reboot"))
148 config.type = fbspl_reboot;
149 else if (!strcmp(optarg, "shutdown"))
150 config.type = fbspl_shutdown;
151 else
152 config.type = fbspl_bootup;
153 break;
155 /* Verbosity level adjustment. */
156 case 'q':
157 config.verbosity = FBSPL_VERB_QUIET;
158 break;
160 case 'v':
161 config.verbosity = FBSPL_VERB_HIGH;
162 break;
166 if (arg_task == none) {
167 usage();
168 return 0;
171 switch (arg_task) {
172 /* Only load the theme if it will actually be used. */
173 #ifdef CONFIG_DEPRECATED
174 case paint:
175 case repaint:
176 #endif
177 theme = fbsplashr_theme_load();
178 if (!theme)
179 exit(1);
180 default:
181 break;
184 if (arg_task != setmode && arg_vc == -1)
185 arg_vc = 0;
187 switch (arg_task) {
189 case getres:
191 int xres = fbd.var.xres;
192 int yres = fbd.var.yres;
193 fbsplash_get_res(config.theme, &xres, &yres);
194 printf("%dx%d\n", xres, yres);
195 break;
198 case setmode:
199 if (config.reqmode & FBSPL_MODE_SILENT) {
200 fbsplashr_tty_silent_init(true);
201 fbsplash_set_silent();
202 } else {
203 fbsplashr_tty_silent_cleanup();
204 fbsplash_set_verbose(0);
206 break;
208 case getmode:
209 printf("%s\n", fbsplash_is_silent() ? "silent" : "verbose");
210 break;
212 #ifdef CONFIG_DEPRECATED
213 /* Deprecated. The daemon mode should be used instead. */
214 case paint:
215 fbsplashr_render_screen(theme, false, false, FBSPL_EFF_NONE);
216 break;
218 case repaint:
219 fbsplashr_render_screen(theme, true, false, FBSPL_EFF_NONE);
220 break;
221 #endif /* CONFIG_DEPRECATED */
223 default:
224 break;
227 fbsplashr_theme_free(theme);
228 fbsplashr_cleanup();
229 fbsplash_lib_cleanup();
230 return 0;
233 #ifndef UNIFIED_BUILD
234 int main(int argc, char **argv)
236 return util_main(argc, argv);
238 #endif