7d3cae2510a7bee1fad5e7f1393d1a2a0b35aab6
[oweals/busybox.git] / util-linux / fbset.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini fbset implementation for busybox
4  *
5  * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  *
9  * This is a from-scratch implementation of fbset; but the de facto fbset
10  * implementation was a good reference. fbset (original) is released under
11  * the GPL, and is (c) 1995-1999 by:
12  *     Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <sys/ioctl.h>
24 #include "busybox.h"
25
26 #define DEFAULTFBDEV  FB_0
27 #define DEFAULTFBMODE "/etc/fb.modes"
28
29 enum {
30         OPT_CHANGE   = (1 << 0),
31         OPT_INFO     = (1 << 1),
32         OPT_READMODE = (1 << 2),
33
34         CMD_FB = 1,
35         CMD_DB = 2,
36         CMD_GEOMETRY = 3,
37         CMD_TIMING = 4,
38         CMD_ACCEL = 5,
39         CMD_HSYNC = 6,
40         CMD_VSYNC = 7,
41         CMD_LACED = 8,
42         CMD_DOUBLE = 9,
43 /*      CMD_XCOMPAT =     10, */
44         CMD_ALL = 11,
45         CMD_INFO = 12,
46         CMD_CHANGE = 13,
47
48 #ifdef CONFIG_FEATURE_FBSET_FANCY
49         CMD_XRES = 100,
50         CMD_YRES = 101,
51         CMD_VXRES = 102,
52         CMD_VYRES = 103,
53         CMD_DEPTH = 104,
54         CMD_MATCH = 105,
55         CMD_PIXCLOCK = 106,
56         CMD_LEFT = 107,
57         CMD_RIGHT = 108,
58         CMD_UPPER = 109,
59         CMD_LOWER = 110,
60         CMD_HSLEN = 111,
61         CMD_VSLEN = 112,
62         CMD_CSYNC = 113,
63         CMD_GSYNC = 114,
64         CMD_EXTSYNC = 115,
65         CMD_BCAST = 116,
66         CMD_RGBA = 117,
67         CMD_STEP = 118,
68         CMD_MOVE = 119,
69 #endif
70 };
71
72 static unsigned int g_options = 0;
73
74 /* Stuff stolen from the kernel's fb.h */
75 enum {
76         FBIOGET_VSCREENINFO = 0x4600,
77         FBIOPUT_VSCREENINFO = 0x4601
78 };
79 struct fb_bitfield {
80         uint32_t offset;                        /* beginning of bitfield        */
81         uint32_t length;                        /* length of bitfield           */
82         uint32_t msb_right;             /* != 0 : Most significant bit is */
83                                         /* right */
84 };
85 struct fb_var_screeninfo {
86         uint32_t xres;                  /* visible resolution           */
87         uint32_t yres;
88         uint32_t xres_virtual;          /* virtual resolution           */
89         uint32_t yres_virtual;
90         uint32_t xoffset;                       /* offset from virtual to visible */
91         uint32_t yoffset;                       /* resolution                   */
92
93         uint32_t bits_per_pixel;                /* guess what                   */
94         uint32_t grayscale;             /* != 0 Graylevels instead of colors */
95
96         struct fb_bitfield red;         /* bitfield in fb mem if true color, */
97         struct fb_bitfield green;       /* else only length is significant */
98         struct fb_bitfield blue;
99         struct fb_bitfield transp;      /* transparency                 */
100
101         uint32_t nonstd;                        /* != 0 Non standard pixel format */
102
103         uint32_t activate;                      /* see FB_ACTIVATE_*            */
104
105         uint32_t height;                        /* height of picture in mm    */
106         uint32_t width;                 /* width of picture in mm     */
107
108         uint32_t accel_flags;           /* acceleration flags (hints)   */
109
110         /* Timing: All values in pixclocks, except pixclock (of course) */
111         uint32_t pixclock;                      /* pixel clock in ps (pico seconds) */
112         uint32_t left_margin;           /* time from sync to picture    */
113         uint32_t right_margin;          /* time from picture to sync    */
114         uint32_t upper_margin;          /* time from sync to picture    */
115         uint32_t lower_margin;
116         uint32_t hsync_len;             /* length of horizontal sync    */
117         uint32_t vsync_len;             /* length of vertical sync      */
118         uint32_t sync;                  /* see FB_SYNC_*                */
119         uint32_t vmode;                 /* see FB_VMODE_*               */
120         uint32_t reserved[6];           /* Reserved for future compatibility */
121 };
122
123
124 static const struct cmdoptions_t {
125         const char *name;
126         const unsigned char param_count;
127         const unsigned char code;
128 } g_cmdoptions[] = {
129         {
130         "-fb", 1, CMD_FB}, {
131         "-db", 1, CMD_DB}, {
132         "-a", 0, CMD_ALL}, {
133         "-i", 0, CMD_INFO}, {
134         "-g", 5, CMD_GEOMETRY}, {
135         "-t", 7, CMD_TIMING}, {
136         "-accel", 1, CMD_ACCEL}, {
137         "-hsync", 1, CMD_HSYNC}, {
138         "-vsync", 1, CMD_VSYNC}, {
139         "-laced", 1, CMD_LACED}, {
140         "-double", 1, CMD_DOUBLE}, {
141         "-n", 0, CMD_CHANGE}, {
142 #ifdef CONFIG_FEATURE_FBSET_FANCY
143         "-all", 0, CMD_ALL}, {
144         "-xres", 1, CMD_XRES}, {
145         "-yres", 1, CMD_YRES}, {
146         "-vxres", 1, CMD_VXRES}, {
147         "-vyres", 1, CMD_VYRES}, {
148         "-depth", 1, CMD_DEPTH}, {
149         "-match", 0, CMD_MATCH}, {
150         "-geometry", 5, CMD_GEOMETRY}, {
151         "-pixclock", 1, CMD_PIXCLOCK}, {
152         "-left", 1, CMD_LEFT}, {
153         "-right", 1, CMD_RIGHT}, {
154         "-upper", 1, CMD_UPPER}, {
155         "-lower", 1, CMD_LOWER}, {
156         "-hslen", 1, CMD_HSLEN}, {
157         "-vslen", 1, CMD_VSLEN}, {
158         "-timings", 7, CMD_TIMING}, {
159         "-csync", 1, CMD_CSYNC}, {
160         "-gsync", 1, CMD_GSYNC}, {
161         "-extsync", 1, CMD_EXTSYNC}, {
162         "-bcast", 1, CMD_BCAST}, {
163         "-rgba", 1, CMD_RGBA}, {
164         "-step", 1, CMD_STEP}, {
165         "-move", 1, CMD_MOVE}, {
166 #endif
167         0, 0, 0}
168 };
169
170 #ifdef CONFIG_FEATURE_FBSET_READMODE
171 /* taken from linux/fb.h */
172 enum {
173         FB_VMODE_INTERLACED = 1,        /* interlaced   */
174         FB_VMODE_DOUBLE = 2,    /* double scan */
175         FB_SYNC_HOR_HIGH_ACT = 1,       /* horizontal sync high active  */
176         FB_SYNC_VERT_HIGH_ACT = 2,      /* vertical sync high active    */
177         FB_SYNC_EXT = 4,        /* external sync                */
178         FB_SYNC_COMP_HIGH_ACT = 8       /* composite sync high active   */
179 };
180 #endif
181 static int readmode(struct fb_var_screeninfo *base, const char *fn,
182                                         const char *mode)
183 {
184 #ifdef CONFIG_FEATURE_FBSET_READMODE
185         FILE *f;
186         char buf[256];
187         char *p = buf;
188
189         f = bb_xfopen(fn, "r");
190         while (!feof(f)) {
191                 fgets(buf, sizeof(buf), f);
192                 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
193                         p += 5;
194                         if ((p = strstr(buf, mode))) {
195                                 p += strlen(mode);
196                                 if (!isspace(*p) && (*p != 0) && (*p != '"')
197                                         && (*p != '\r') && (*p != '\n'))
198                                         continue;       /* almost, but not quite */
199                                 while (!feof(f)) {
200                                         fgets(buf, sizeof(buf), f);
201
202                     if ((p = strstr(buf, "geometry "))) {
203                         p += 9;
204
205                         sscanf(p, "%d %d %d %d %d",
206                                 &(base->xres), &(base->yres),
207                                 &(base->xres_virtual), &(base->yres_virtual),
208                                 &(base->bits_per_pixel));
209                     } else if ((p = strstr(buf, "timings "))) {
210                         p += 8;
211
212                         sscanf(p, "%d %d %d %d %d %d %d",
213                                 &(base->pixclock),
214                                 &(base->left_margin), &(base->right_margin),
215                                 &(base->upper_margin), &(base->lower_margin),
216                                 &(base->hsync_len), &(base->vsync_len));
217                     } else if ((p = strstr(buf, "laced "))) {
218                         p += 6;
219
220                         if (strstr(buf, "false")) {
221                             base->vmode &= ~FB_VMODE_INTERLACED;
222                         } else {
223                             base->vmode |= FB_VMODE_INTERLACED;
224                         }
225                     } else if ((p = strstr(buf, "double "))) {
226                         p += 7;
227
228                         if (strstr(buf, "false")) {
229                             base->vmode &= ~FB_VMODE_DOUBLE;
230                         } else {
231                             base->vmode |= FB_VMODE_DOUBLE;
232                         }
233                     } else if ((p = strstr(buf, "vsync "))) {
234                         p += 6;
235
236                         if (strstr(buf, "low")) {
237                             base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
238                         } else {
239                             base->sync |= FB_SYNC_VERT_HIGH_ACT;
240                         }
241                     } else if ((p = strstr(buf, "hsync "))) {
242                         p += 6;
243
244                         if (strstr(buf, "low")) {
245                             base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
246                         } else {
247                             base->sync |= FB_SYNC_HOR_HIGH_ACT;
248                         }
249                     } else if ((p = strstr(buf, "csync "))) {
250                         p += 6;
251
252                         if (strstr(buf, "low")) {
253                             base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
254                         } else {
255                             base->sync |= FB_SYNC_COMP_HIGH_ACT;
256                         }
257                     } else if ((p = strstr(buf, "extsync "))) {
258                         p += 8;
259
260                         if (strstr(buf, "false")) {
261                             base->sync &= ~FB_SYNC_EXT;
262                         } else {
263                             base->sync |= FB_SYNC_EXT;
264                         }
265                     }
266
267                                         if (strstr(buf, "endmode"))
268                                                 return 1;
269                                 }
270                         }
271                 }
272         }
273 #else
274         bb_error_msg( "mode reading not compiled in");
275 #endif
276         return 0;
277 }
278
279 static inline void setmode(struct fb_var_screeninfo *base,
280                                         struct fb_var_screeninfo *set)
281 {
282         if ((int) set->xres > 0)
283                 base->xres = set->xres;
284         if ((int) set->yres > 0)
285                 base->yres = set->yres;
286         if ((int) set->xres_virtual > 0)
287                 base->xres_virtual = set->xres_virtual;
288         if ((int) set->yres_virtual > 0)
289                 base->yres_virtual = set->yres_virtual;
290         if ((int) set->bits_per_pixel > 0)
291                 base->bits_per_pixel = set->bits_per_pixel;
292 }
293
294 static inline void showmode(struct fb_var_screeninfo *v)
295 {
296         double drate = 0, hrate = 0, vrate = 0;
297
298         if (v->pixclock) {
299                 drate = 1e12 / v->pixclock;
300                 hrate =
301                         drate / (v->left_margin + v->xres + v->right_margin +
302                                          v->hsync_len);
303                 vrate =
304                         hrate / (v->upper_margin + v->yres + v->lower_margin +
305                                          v->vsync_len);
306         }
307         printf("\nmode \"%ux%u-%u\"\n"
308 #ifdef CONFIG_FEATURE_FBSET_FANCY
309         "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
310 #endif
311         "\tgeometry %u %u %u %u %u\n\ttimings %u %u %u %u %u %u %u\n\taccel %s\n\trgba %u/%u,%u/%u,%u/%u,%u/%u\nendmode\n\n",
312                    v->xres, v->yres, (int) (vrate + 0.5),
313 #ifdef CONFIG_FEATURE_FBSET_FANCY
314                    drate / 1e6, hrate / 1e3, vrate,
315 #endif
316                    v->xres, v->yres, v->xres_virtual, v->yres_virtual,
317                    v->bits_per_pixel, v->pixclock, v->left_margin,
318                    v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
319                    v->vsync_len, (v->accel_flags > 0 ? "true" : "false"), v->red.length,
320                    v->red.offset, v->green.length, v->green.offset, v->blue.length,
321                    v->blue.offset, v->transp.length, v->transp.offset);
322 }
323
324 #ifdef STANDALONE
325 int main(int argc, char **argv)
326 #else
327 int fbset_main(int argc, char **argv)
328 #endif
329 {
330         struct fb_var_screeninfo var, varset;
331         int fh, i;
332         char *fbdev = DEFAULTFBDEV;
333         char *modefile = DEFAULTFBMODE;
334         char *thisarg, *mode = NULL;
335
336         memset(&varset, 0xFF, sizeof(varset));
337
338         /* parse cmd args.... why do they have to make things so difficult? */
339         argv++;
340         argc--;
341         for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
342                 for (i = 0; g_cmdoptions[i].name; i++) {
343                         if (!strcmp(thisarg, g_cmdoptions[i].name)) {
344                                 if (argc - 1 < g_cmdoptions[i].param_count)
345                                         bb_show_usage();
346                                 switch (g_cmdoptions[i].code) {
347                                 case CMD_FB:
348                                         fbdev = argv[1];
349                                         break;
350                                 case CMD_DB:
351                                         modefile = argv[1];
352                                         break;
353                                 case CMD_GEOMETRY:
354                                         varset.xres = strtoul(argv[1], 0, 0);
355                                         varset.yres = strtoul(argv[2], 0, 0);
356                                         varset.xres_virtual = strtoul(argv[3], 0, 0);
357                                         varset.yres_virtual = strtoul(argv[4], 0, 0);
358                                         varset.bits_per_pixel = strtoul(argv[5], 0, 0);
359                                         break;
360                                 case CMD_TIMING:
361                                         varset.pixclock = strtoul(argv[1], 0, 0);
362                                         varset.left_margin = strtoul(argv[2], 0, 0);
363                                         varset.right_margin = strtoul(argv[3], 0, 0);
364                                         varset.upper_margin = strtoul(argv[4], 0, 0);
365                                         varset.lower_margin = strtoul(argv[5], 0, 0);
366                                         varset.hsync_len = strtoul(argv[6], 0, 0);
367                                         varset.vsync_len = strtoul(argv[7], 0, 0);
368                                         break;
369                 case CMD_CHANGE:
370                     g_options |= OPT_CHANGE;
371                     break;
372 #ifdef CONFIG_FEATURE_FBSET_FANCY
373                                 case CMD_XRES:
374                                         varset.xres = strtoul(argv[1], 0, 0);
375                                         break;
376                                 case CMD_YRES:
377                                         varset.yres = strtoul(argv[1], 0, 0);
378                                         break;
379                            case CMD_DEPTH:
380                                         varset.bits_per_pixel = strtoul(argv[1], 0, 0);
381                                         break;
382 #endif
383                                 }
384                                 argc -= g_cmdoptions[i].param_count;
385                                 argv += g_cmdoptions[i].param_count;
386                                 break;
387                         }
388                 }
389                 if (!g_cmdoptions[i].name) {
390                         if (argc == 1) {
391                                 mode = *argv;
392                                 g_options |= OPT_READMODE;
393                         } else {
394                                 bb_show_usage();
395                         }
396                 }
397         }
398
399         fh = bb_xopen(fbdev, O_RDONLY);
400         if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
401                 bb_perror_msg_and_die("fbset(ioctl)");
402         if (g_options & OPT_READMODE) {
403                 if (!readmode(&var, modefile, mode)) {
404                         bb_error_msg("Unknown video mode `%s'", mode);
405                         return EXIT_FAILURE;
406                 }
407         }
408
409         setmode(&var, &varset);
410         if (g_options & OPT_CHANGE)
411                 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
412                         bb_perror_msg_and_die("fbset(ioctl)");
413         showmode(&var);
414         /* Don't close the file, as exiting will take care of that */
415         /* close(fh); */
416
417         return EXIT_SUCCESS;
418 }