blob: 77cc1fc123f29ec8e5773496b92304caa360509d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen1c5b2581999-12-16 20:59:36 +00002/*
3 * Mini fbset implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen1c5b2581999-12-16 20:59:36 +00008 *
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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000011 * the GPL, and is (c) 1995-1999 by:
Erik Andersen1c5b2581999-12-16 20:59:36 +000012 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
13 */
14
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Erik Andersen1c5b2581999-12-16 20:59:36 +000016
Eric Andersen5f284552003-11-14 03:11:29 +000017#define DEFAULTFBDEV FB_0
Erik Andersen1c5b2581999-12-16 20:59:36 +000018#define DEFAULTFBMODE "/etc/fb.modes"
19
Eric Andersenc873d612000-09-21 04:09:58 +000020/* Stuff stolen from the kernel's fb.h */
Denis Vlasenko274b8c02006-09-30 16:22:59 +000021#define FB_ACTIVATE_ALL 64
Rob Landleybc68cd12006-03-10 19:22:06 +000022enum {
23 FBIOGET_VSCREENINFO = 0x4600,
24 FBIOPUT_VSCREENINFO = 0x4601
25};
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +010026
Eric Andersenc873d612000-09-21 04:09:58 +000027struct fb_bitfield {
Denis Vlasenko39d551f2006-09-30 16:28:30 +000028 uint32_t offset; /* beginning of bitfield */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020029 uint32_t length; /* length of bitfield */
Denis Vlasenko39d551f2006-09-30 16:28:30 +000030 uint32_t msb_right; /* !=0: Most significant bit is right */
Eric Andersenc873d612000-09-21 04:09:58 +000031};
32struct fb_var_screeninfo {
Denis Vlasenko39d551f2006-09-30 16:28:30 +000033 uint32_t xres; /* visible resolution */
Eric Andersen1eceb122003-07-14 19:32:40 +000034 uint32_t yres;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000035 uint32_t xres_virtual; /* virtual resolution */
Eric Andersen1eceb122003-07-14 19:32:40 +000036 uint32_t yres_virtual;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000037 uint32_t xoffset; /* offset from virtual to visible */
38 uint32_t yoffset; /* resolution */
Eric Andersenc873d612000-09-21 04:09:58 +000039
Denis Vlasenko39d551f2006-09-30 16:28:30 +000040 uint32_t bits_per_pixel;
41 uint32_t grayscale; /* !=0 Graylevels instead of colors */
Eric Andersenc873d612000-09-21 04:09:58 +000042
Denis Vlasenko39d551f2006-09-30 16:28:30 +000043 struct fb_bitfield red; /* bitfield in fb mem if true color, */
44 struct fb_bitfield green; /* else only length is significant */
Eric Andersenc873d612000-09-21 04:09:58 +000045 struct fb_bitfield blue;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000046 struct fb_bitfield transp; /* transparency */
Eric Andersenc873d612000-09-21 04:09:58 +000047
Denis Vlasenko39d551f2006-09-30 16:28:30 +000048 uint32_t nonstd; /* !=0 Non standard pixel format */
Eric Andersenc873d612000-09-21 04:09:58 +000049
Denis Vlasenko39d551f2006-09-30 16:28:30 +000050 uint32_t activate; /* see FB_ACTIVATE_x */
Eric Andersenc873d612000-09-21 04:09:58 +000051
Denis Vlasenko39d551f2006-09-30 16:28:30 +000052 uint32_t height; /* height of picture in mm */
53 uint32_t width; /* width of picture in mm */
Eric Andersenc873d612000-09-21 04:09:58 +000054
Denys Vlasenkofb132e42010-10-29 11:46:52 +020055 uint32_t accel_flags; /* acceleration flags (hints) */
Eric Andersenc873d612000-09-21 04:09:58 +000056
57 /* Timing: All values in pixclocks, except pixclock (of course) */
Denis Vlasenko39d551f2006-09-30 16:28:30 +000058 uint32_t pixclock; /* pixel clock in ps (pico seconds) */
59 uint32_t left_margin; /* time from sync to picture */
60 uint32_t right_margin; /* time from picture to sync */
61 uint32_t upper_margin; /* time from sync to picture */
Eric Andersen1eceb122003-07-14 19:32:40 +000062 uint32_t lower_margin;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000063 uint32_t hsync_len; /* length of horizontal sync */
64 uint32_t vsync_len; /* length of vertical sync */
65 uint32_t sync; /* see FB_SYNC_x */
66 uint32_t vmode; /* see FB_VMODE_x */
67 uint32_t reserved[6]; /* Reserved for future compatibility */
Eric Andersenc873d612000-09-21 04:09:58 +000068};
69
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +010070static void copy_if_gt0(uint32_t *src, uint32_t *dst, unsigned cnt)
71{
72 do {
73 if ((int32_t) *src > 0)
74 *dst = *src;
75 src++;
76 dst++;
77 } while (--cnt);
78}
79
80static NOINLINE void copy_changed_values(
81 struct fb_var_screeninfo *base,
82 struct fb_var_screeninfo *set)
83{
84 //if ((int32_t) set->xres > 0) base->xres = set->xres;
85 //if ((int32_t) set->yres > 0) base->yres = set->yres;
86 //if ((int32_t) set->xres_virtual > 0) base->xres_virtual = set->xres_virtual;
87 //if ((int32_t) set->yres_virtual > 0) base->yres_virtual = set->yres_virtual;
88 copy_if_gt0(&set->xres, &base->xres, 4);
89
90 if ((int32_t) set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
91 //copy_if_gt0(&set->bits_per_pixel, &base->bits_per_pixel, 1);
92
93 //if ((int32_t) set->pixclock > 0) base->pixclock = set->pixclock;
94 //if ((int32_t) set->left_margin > 0) base->left_margin = set->left_margin;
95 //if ((int32_t) set->right_margin > 0) base->right_margin = set->right_margin;
96 //if ((int32_t) set->upper_margin > 0) base->upper_margin = set->upper_margin;
97 //if ((int32_t) set->lower_margin > 0) base->lower_margin = set->lower_margin;
98 //if ((int32_t) set->hsync_len > 0) base->hsync_len = set->hsync_len;
99 //if ((int32_t) set->vsync_len > 0) base->vsync_len = set->vsync_len;
100 //if ((int32_t) set->sync > 0) base->sync = set->sync;
101 //if ((int32_t) set->vmode > 0) base->vmode = set->vmode;
102 copy_if_gt0(&set->pixclock, &base->pixclock, 9);
103}
104
105
106enum {
107 CMD_FB = 1,
108 CMD_DB = 2,
109 CMD_GEOMETRY = 3,
110 CMD_TIMING = 4,
111 CMD_ACCEL = 5,
112 CMD_HSYNC = 6,
113 CMD_VSYNC = 7,
114 CMD_LACED = 8,
115 CMD_DOUBLE = 9,
116/* CMD_XCOMPAT = 10, */
117 CMD_ALL = 11,
118 CMD_INFO = 12,
119 CMD_SHOW = 13,
120 CMD_CHANGE = 14,
121
122#if ENABLE_FEATURE_FBSET_FANCY
123 CMD_XRES = 100,
124 CMD_YRES = 101,
125 CMD_VXRES = 102,
126 CMD_VYRES = 103,
127 CMD_DEPTH = 104,
128 CMD_MATCH = 105,
129 CMD_PIXCLOCK = 106,
130 CMD_LEFT = 107,
131 CMD_RIGHT = 108,
132 CMD_UPPER = 109,
133 CMD_LOWER = 110,
134 CMD_HSLEN = 111,
135 CMD_VSLEN = 112,
136 CMD_CSYNC = 113,
137 CMD_GSYNC = 114,
138 CMD_EXTSYNC = 115,
139 CMD_BCAST = 116,
140 CMD_RGBA = 117,
141 CMD_STEP = 118,
142 CMD_MOVE = 119,
143#endif
144};
Eric Andersenc873d612000-09-21 04:09:58 +0000145
"Vladimir N. Oleynik"b399a962006-02-01 12:41:35 +0000146static const struct cmdoptions_t {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000147 const char name[9];
Aaron Lehmann60694412002-08-13 04:19:23 +0000148 const unsigned char param_count;
149 const unsigned char code;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000150} g_cmdoptions[] = {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000151 /*"12345678" + NUL */
152 { "fb" , 1, CMD_FB },
153 { "db" , 1, CMD_DB },
154 { "a" , 0, CMD_ALL },
155 { "i" , 0, CMD_INFO },
156 { "g" , 5, CMD_GEOMETRY },
157 { "t" , 7, CMD_TIMING },
158 { "accel" , 1, CMD_ACCEL },
159 { "hsync" , 1, CMD_HSYNC },
160 { "vsync" , 1, CMD_VSYNC },
161 { "laced" , 1, CMD_LACED },
162 { "double" , 1, CMD_DOUBLE },
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000163 { "show" , 0, CMD_SHOW },
164 { "s" , 0, CMD_SHOW },
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000165#if ENABLE_FEATURE_FBSET_FANCY
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000166 { "all" , 0, CMD_ALL },
167 { "xres" , 1, CMD_XRES },
168 { "yres" , 1, CMD_YRES },
169 { "vxres" , 1, CMD_VXRES },
170 { "vyres" , 1, CMD_VYRES },
171 { "depth" , 1, CMD_DEPTH },
172 { "match" , 0, CMD_MATCH },
173 { "geometry", 5, CMD_GEOMETRY },
174 { "pixclock", 1, CMD_PIXCLOCK },
175 { "left" , 1, CMD_LEFT },
176 { "right" , 1, CMD_RIGHT },
177 { "upper" , 1, CMD_UPPER },
178 { "lower" , 1, CMD_LOWER },
179 { "hslen" , 1, CMD_HSLEN },
180 { "vslen" , 1, CMD_VSLEN },
181 { "timings" , 7, CMD_TIMING },
182 { "csync" , 1, CMD_CSYNC },
183 { "gsync" , 1, CMD_GSYNC },
184 { "extsync" , 1, CMD_EXTSYNC },
185 { "bcast" , 1, CMD_BCAST },
186 { "rgba" , 1, CMD_RGBA },
187 { "step" , 1, CMD_STEP },
188 { "move" , 1, CMD_MOVE },
Erik Andersen1c5b2581999-12-16 20:59:36 +0000189#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000190};
191
Matt Kraai269e07c2000-10-28 16:56:32 +0000192/* taken from linux/fb.h */
Rob Landleybc68cd12006-03-10 19:22:06 +0000193enum {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000194 FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
195 FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
Denys Vlasenko6b01b712010-01-24 22:52:21 +0100196#if ENABLE_FEATURE_FBSET_READMODE
197 FB_VMODE_INTERLACED = 1, /* interlaced */
198 FB_VMODE_DOUBLE = 2, /* double scan */
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000199 FB_SYNC_EXT = 4, /* external sync */
200 FB_SYNC_COMP_HIGH_ACT = 8, /* composite sync high active */
Matt Kraai269e07c2000-10-28 16:56:32 +0000201#endif
Denys Vlasenko6b01b712010-01-24 22:52:21 +0100202};
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000203
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000204#if ENABLE_FEATURE_FBSET_READMODE
Denis Vlasenko8d523cb2008-07-27 21:16:30 +0000205static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
Denis Vlasenko09f5ecf2008-07-27 20:25:29 +0000206{
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000207 if (strcmp(buf, what) == 0)
Denis Vlasenko09f5ecf2008-07-27 20:25:29 +0000208 *x &= ~flag;
209 else
210 *x |= flag;
211}
212
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100213/* Mode db file contains mode definitions like this:
214 * mode "800x600-48-lace"
215 * # D: 36.00 MHz, H: 33.835 kHz, V: 96.39 Hz
216 * geometry 800 600 800 600 8
217 * timings 27778 56 80 79 11 128 12
218 * laced true
219 * hsync high
220 * vsync high
221 * endmode
222 */
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000223static int read_mode_db(struct fb_var_screeninfo *base, const char *fn,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 const char *mode)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000225{
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000226 char *token[2], *p, *s;
227 parser_t *parser = config_open(fn);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000229 while (config_read(parser, token, 2, 1, "# \t\r", PARSE_NORMAL)) {
230 if (strcmp(token[0], "mode") != 0 || !token[1])
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000231 continue;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000232 p = strstr(token[1], mode);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000233 if (!p)
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000234 continue;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000235 s = p + strlen(mode);
236 //bb_info_msg("CHECK[%s][%s][%d]", mode, p-1, *s);
237 /* exact match? */
238 if (((!*s || isspace(*s)) && '"' != s[-1]) /* end-of-token */
239 || ('"' == *s && '"' == p[-1]) /* ends with " but starts with " too! */
Denis Vlasenko8d523cb2008-07-27 21:16:30 +0000240 ) {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000241 //bb_info_msg("FOUND[%s][%s][%s][%d]", token[1], p, mode, isspace(*s));
242 break;
Denis Vlasenko8d523cb2008-07-27 21:16:30 +0000243 }
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000244 }
Eric Andersen6f96e672000-07-12 23:01:04 +0000245
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000246 if (!token[0])
247 return 0;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000248
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000249 while (config_read(parser, token, 2, 1, "# \t", PARSE_NORMAL)) {
250 int i;
251
252//bb_info_msg("???[%s][%s]", token[0], token[1]);
253 if (strcmp(token[0], "endmode") == 0) {
254//bb_info_msg("OK[%s]", mode);
255 return 1;
256 }
257 p = token[1];
258 i = index_in_strings(
259 "geometry\0timings\0interlaced\0double\0vsync\0hsync\0csync\0extsync\0",
260 token[0]);
261 switch (i) {
262 case 0:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100263 if (sizeof(int) == sizeof(base->xres)) {
264 sscanf(p, "%d %d %d %d %d",
265 &base->xres, &base->yres,
266 &base->xres_virtual, &base->yres_virtual,
267 &base->bits_per_pixel);
268 } else {
269 int base_xres, base_yres;
270 int base_xres_virtual, base_yres_virtual;
271 int base_bits_per_pixel;
272 sscanf(p, "%d %d %d %d %d",
273 &base_xres, &base_yres,
274 &base_xres_virtual, &base_yres_virtual,
275 &base_bits_per_pixel);
276 base->xres = base_xres;
277 base->yres = base_yres;
278 base->xres_virtual = base_xres_virtual;
279 base->yres_virtual = base_yres_virtual;
280 base->bits_per_pixel = base_bits_per_pixel;
281 }
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000282//bb_info_msg("GEO[%s]", p);
283 break;
284 case 1:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100285 if (sizeof(int) == sizeof(base->xres)) {
286 sscanf(p, "%d %d %d %d %d %d %d",
287 &base->pixclock,
288 &base->left_margin, &base->right_margin,
289 &base->upper_margin, &base->lower_margin,
290 &base->hsync_len, &base->vsync_len);
291 } else {
292 int base_pixclock;
293 int base_left_margin, base_right_margin;
294 int base_upper_margin, base_lower_margin;
295 int base_hsync_len, base_vsync_len;
296 sscanf(p, "%d %d %d %d %d %d %d",
297 &base_pixclock,
298 &base_left_margin, &base_right_margin,
299 &base_upper_margin, &base_lower_margin,
300 &base_hsync_len, &base_vsync_len);
301 base->pixclock = base_pixclock;
302 base->left_margin = base_left_margin;
303 base->right_margin = base_right_margin;
304 base->upper_margin = base_upper_margin;
305 base->lower_margin = base_lower_margin;
306 base->hsync_len = base_hsync_len;
307 base->vsync_len = base_vsync_len;
308 }
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000309//bb_info_msg("TIM[%s]", p);
310 break;
311 case 2:
312 case 3: {
313 static const uint32_t syncs[] = {FB_VMODE_INTERLACED, FB_VMODE_DOUBLE};
314 ss(&base->vmode, syncs[i-2], p, "false");
315//bb_info_msg("VMODE[%s]", p);
316 break;
317 }
318 case 4:
319 case 5:
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200320 case 6: {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000321 static const uint32_t syncs[] = {FB_SYNC_VERT_HIGH_ACT, FB_SYNC_HOR_HIGH_ACT, FB_SYNC_COMP_HIGH_ACT};
322 ss(&base->sync, syncs[i-4], p, "low");
323//bb_info_msg("SYNC[%s]", p);
324 break;
325 }
326 case 7:
327 ss(&base->sync, FB_SYNC_EXT, p, "false");
328//bb_info_msg("EXTSYNC[%s]", p);
329 break;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000330 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000331 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 return 0;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000333}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000334#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000335
Denys Vlasenkoef5bc2c2009-10-08 14:54:18 +0200336static NOINLINE void showmode(struct fb_var_screeninfo *v)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000337{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 double drate = 0, hrate = 0, vrate = 0;
339
340 if (v->pixclock) {
341 drate = 1e12 / v->pixclock;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000342 hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
343 vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 }
Aaron Lehmann60694412002-08-13 04:19:23 +0000345 printf("\nmode \"%ux%u-%u\"\n"
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000346#if ENABLE_FEATURE_FBSET_FANCY
Aaron Lehmann60694412002-08-13 04:19:23 +0000347 "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
Erik Andersen1c5b2581999-12-16 20:59:36 +0000348#endif
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000349 "\tgeometry %u %u %u %u %u\n"
350 "\ttimings %u %u %u %u %u %u %u\n"
351 "\taccel %s\n"
352 "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
353 "endmode\n\n",
354 v->xres, v->yres, (int) (vrate + 0.5),
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000355#if ENABLE_FEATURE_FBSET_FANCY
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000356 drate / 1e6, hrate / 1e3, vrate,
Aaron Lehmann60694412002-08-13 04:19:23 +0000357#endif
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000358 v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
359 v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
360 v->hsync_len, v->vsync_len,
361 (v->accel_flags > 0 ? "true" : "false"),
362 v->red.length, v->red.offset, v->green.length, v->green.offset,
363 v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000364}
365
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000366int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000367int fbset_main(int argc, char **argv)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000368{
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000369 enum {
370 OPT_CHANGE = (1 << 0),
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000371 OPT_SHOW = (1 << 1),
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000372 OPT_READMODE = (1 << 2),
373 OPT_ALL = (1 << 9),
374 };
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100375 struct fb_var_screeninfo var_old, var_set;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 int fh, i;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000377 unsigned options = 0;
378
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000379 const char *fbdev = DEFAULTFBDEV;
380 const char *modefile = DEFAULTFBMODE;
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100381 char *thisarg;
382 char *mode = mode; /* for compiler */
Erik Andersen1c5b2581999-12-16 20:59:36 +0000383
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100384 memset(&var_set, 0xff, sizeof(var_set)); /* set all to -1 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385
386 /* parse cmd args.... why do they have to make things so difficult? */
387 argv++;
388 argc--;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000389 for (; argc > 0 && (thisarg = *argv) != NULL; argc--, argv++) {
390 if (thisarg[0] == '-') for (i = 0; i < ARRAY_SIZE(g_cmdoptions); i++) {
391 if (strcmp(thisarg + 1, g_cmdoptions[i].name) != 0)
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000392 continue;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000393 if (argc <= g_cmdoptions[i].param_count)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000394 bb_show_usage();
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000395
396 switch (g_cmdoptions[i].code) {
397 case CMD_FB:
398 fbdev = argv[1];
399 break;
400 case CMD_DB:
401 modefile = argv[1];
402 break;
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000403 case CMD_ALL:
404 options |= OPT_ALL;
405 break;
406 case CMD_SHOW:
407 options |= OPT_SHOW;
408 break;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000409 case CMD_GEOMETRY:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100410 var_set.xres = xatou32(argv[1]);
411 var_set.yres = xatou32(argv[2]);
412 var_set.xres_virtual = xatou32(argv[3]);
413 var_set.yres_virtual = xatou32(argv[4]);
414 var_set.bits_per_pixel = xatou32(argv[5]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000415 break;
416 case CMD_TIMING:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100417 var_set.pixclock = xatou32(argv[1]);
418 var_set.left_margin = xatou32(argv[2]);
419 var_set.right_margin = xatou32(argv[3]);
420 var_set.upper_margin = xatou32(argv[4]);
421 var_set.lower_margin = xatou32(argv[5]);
422 var_set.hsync_len = xatou32(argv[6]);
423 var_set.vsync_len = xatou32(argv[7]);
424 break;
425 case CMD_ACCEL:
426 break;
427 case CMD_HSYNC:
428 var_set.sync |= FB_SYNC_HOR_HIGH_ACT;
429 break;
430 case CMD_VSYNC:
431 var_set.sync |= FB_SYNC_VERT_HIGH_ACT;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000432 break;
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000433#if ENABLE_FEATURE_FBSET_FANCY
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000434 case CMD_XRES:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100435 var_set.xres = xatou32(argv[1]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000436 break;
437 case CMD_YRES:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100438 var_set.yres = xatou32(argv[1]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000439 break;
440 case CMD_DEPTH:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100441 var_set.bits_per_pixel = xatou32(argv[1]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000442 break;
443#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 }
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000445 switch (g_cmdoptions[i].code) {
446 case CMD_FB:
447 case CMD_DB:
448 case CMD_ALL:
449 case CMD_SHOW:
450 break;
451 default:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100452 /* other commands imply changes */
453 options |= OPT_CHANGE;
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000454 }
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000455 argc -= g_cmdoptions[i].param_count;
456 argv += g_cmdoptions[i].param_count;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000457 goto contin;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000458 }
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100459 if (!ENABLE_FEATURE_FBSET_READMODE || argc != 1)
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000460 bb_show_usage();
461 mode = *argv;
462 options |= OPT_READMODE;
463 contin: ;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000464 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000465
Rob Landleyd921b2e2006-08-03 15:41:12 +0000466 fh = xopen(fbdev, O_RDONLY);
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100467 xioctl(fh, FBIOGET_VSCREENINFO, &var_old);
468
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000469 if (options & OPT_READMODE) {
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100470#if ENABLE_FEATURE_FBSET_READMODE
471 if (!read_mode_db(&var_old, modefile, mode)) {
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000472 bb_error_msg_and_die("unknown video mode '%s'", mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 }
Denis Vlasenko85c24712008-03-17 09:04:04 +0000474#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000475 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000476
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000477 if (options & OPT_CHANGE) {
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100478 copy_changed_values(&var_old, &var_set);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000479 if (options & OPT_ALL)
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100480 var_old.activate = FB_ACTIVATE_ALL;
481 xioctl(fh, FBIOPUT_VSCREENINFO, &var_old);
Denis Vlasenko274b8c02006-09-30 16:22:59 +0000482 }
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100483
484 if (options == 0 || (options & OPT_SHOW))
485 showmode(&var_old);
486
487 if (ENABLE_FEATURE_CLEAN_UP)
488 close(fh);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489
Matt Kraai3e856ce2000-12-01 02:55:13 +0000490 return EXIT_SUCCESS;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000491}