core: add support for the autoverbose feature
[fbsplash.git] / core / src / common.c
blob710686724df93a5c04106af6ad9d4c5a44cf9ba6
1 /*
2 * common.c - miscellaneous functions used by both the kernel helper and
3 * user utilities.
5 * Copyright (C) 2004-2007, Michal Januszewski <spock@gentoo.org>
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License v2. See the file COPYING in the main directory of this archive for
9 * more details.
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <termios.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <dirent.h>
24 #include <errno.h>
26 #include "common.h"
27 #include "render.h"
29 fbspl_cfg_t *cfg;
31 int dev_create(char *fn, char *sys)
33 char buf[256];
34 unsigned int major = 0, minor = 0;
35 int fd;
36 int res;
38 // if (!access(fn, W_OK | R_OK))
39 // return 0;
41 fd = open(sys, O_RDONLY);
43 if (fd == -1) {
44 return 1;
47 read(fd, buf, 256);
48 close(fd);
50 buf[255] = 0;
52 sscanf(buf, "%u:%u", &major, &minor);
54 if (major == 0) {
55 return 2;
58 res = mknod(fn, 0600 | S_IFCHR, makedev(major, minor));
60 return res;
63 int fb_open(int fb, bool create)
65 char dev[64];
66 char sys[128];
67 int c;
68 bool first = true;
70 tryopen:
71 snprintf(dev, 64, PATH_DEV "/fb%d", fb);
72 if ((c = open(dev, O_RDWR)) == -1) {
73 snprintf(dev, 64, PATH_DEV "/fb/%d", fb);
74 c = open(dev, O_RDWR);
77 if (c == -1 && create && first) {
78 first = false;
79 snprintf(dev, 64, PATH_DEV "/fb%d", fb);
80 snprintf(sys, 128, PATH_SYS "/class/graphics/fb%d/dev", fb);
81 if (!dev_create(dev, sys))
82 goto tryopen;
85 return c;
88 void* fb_mmap(int fb)
90 return mmap(NULL, fbd.fix.line_length * fbd.var.yres,
91 PROT_WRITE | PROT_READ, MAP_SHARED, fb,
92 fbd.var.yoffset * fbd.fix.line_length);
95 void fb_unmap(u8 *fb)
97 munmap(fb, fbd.fix.line_length * fbd.var.yres);
100 int fb_get_settings(int fb)
102 if (ioctl(fb, FBIOGET_VSCREENINFO, &fbd.var) == -1) {
103 iprint(MSG_ERROR, "Failed to get fb_var info. (errno=%d)\n", errno);
104 return 2;
107 if (ioctl(fb, FBIOGET_FSCREENINFO, &fbd.fix) == -1) {
108 iprint(MSG_ERROR, "Failed to get fb_fix info. (errno=%d)\n", errno);
109 return 3;
112 fbd.bytespp = (fbd.var.bits_per_pixel + 7) >> 3;
114 /* Check if optimized code can be used. We use special optimizations for
115 * 24/32bpp modes in which all color components have a length of 8 bits. */
116 if (fbd.bytespp < 3 || fbd.var.blue.length != 8 || fbd.var.green.length != 8 || fbd.var.red.length != 8) {
117 fbd.opt = false;
119 if (fbd.fix.visual == FB_VISUAL_DIRECTCOLOR) {
120 fbd.blen = fbd.glen = fbd.rlen = min(min(fbd.var.red.length, fbd.var.green.length),fbd.var.blue.length);
121 } else {
122 fbd.rlen = fbd.var.red.length;
123 fbd.glen = fbd.var.green.length;
124 fbd.blen = fbd.var.blue.length;
126 } else {
127 fbd.opt = true;
129 /* Compute component offsets (ie. indexes in an array of u8's) */
130 fbd.ro = (fbd.var.red.offset >> 3);
131 fbd.go = (fbd.var.green.offset >> 3);
132 fbd.bo = (fbd.var.blue.offset >> 3);
134 if (endianess == big) {
135 fbd.ro = fbd.bytespp - 1 - fbd.ro;
136 fbd.go = fbd.bytespp - 1 - fbd.go;
137 fbd.bo = fbd.bytespp - 1 - fbd.bo;
141 return 0;
144 int cfg_check_sanity(stheme_t *theme, u8 mode)
146 char *pic;
148 /* Verbose mode needs a config file for the exact video mode that is
149 * currently in use. */
150 if (mode == 'v' && (fbd.var.xres != theme->xres || fbd.var.yres != theme->yres))
151 return -1;
153 /* If the user specified invalid values for the text field - correct it.
154 * Also setup default values (text field covering the whole screen). */
155 if (theme->tx > fbd.var.xres)
156 theme->tx = 0;
158 if (theme->ty > fbd.var.yres)
159 theme->ty = 0;
161 if (theme->tw > fbd.var.xres || theme->tw == 0)
162 theme->tw = fbd.var.xres;
164 if (theme->th > fbd.var.yres || theme->th == 0)
165 theme->th = fbd.var.yres;
167 if (fbd.var.bits_per_pixel == 8) {
168 pic = (mode == 'v') ? theme->pic256 : theme->silentpic256;
170 if (!pic) {
171 iprint(MSG_ERROR, "No 8bpp %s picture specified in the theme.\n", (mode == 'v') ? "verbose" : "silent");
172 return -2;
174 } else {
175 pic = (mode == 'v') ? theme->pic : theme->silentpic;
177 if (!pic) {
178 iprint(MSG_ERROR, "No %s picture specified in the theme.\n", (mode == 'v') ? "verbose" : "silent");
179 return -2;
183 return 0;
186 int tty_open(int tty)
188 char dev[64];
189 int c;
190 #ifdef TARGET_KERNEL
191 char sys[128];
192 bool first = true;
193 tryopen:
194 #endif
196 snprintf(dev, 64, PATH_DEV "/tty%d", tty);
197 if ((c = open(dev, O_RDWR | O_NOCTTY)) == -1) {
198 snprintf(dev, 64, PATH_DEV "/vc/%d", tty);
199 c = open(dev, O_RDWR | O_NOCTTY);
202 #ifdef TARGET_KERNEL
203 if (c == -1 && first) {
204 first = false;
205 snprintf(dev, 64, PATH_DEV "/tty%d", tty);
206 snprintf(sys, 128, PATH_SYS "/class/tty/tty%d/dev", tty);
207 if (!dev_create(dev, sys))
208 goto tryopen;
210 #endif
212 return c;