2 * Mini fbset implementation for busybox
4 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * This is a from-scratch implementation of fbset; but the de facto fbset
21 * implementation was a good reference. fbset (original) is released under
22 * the GPL, and is (c) 1995-1999 by:
23 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
33 #include <sys/ioctl.h>
36 #define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
38 #define DEFAULTFBDEV "/dev/fb0"
39 #define DEFAULTFBMODE "/etc/fb.modes"
42 #define OPT_INFO (1 << 1)
43 #define OPT_READMODE (1 << 2)
48 #define CMD_GEOMETRY 3
55 /* #define CMD_XCOMPAT 10 */
66 #define CMD_PIXCLOCK 19
75 #define CMD_EXTSYNC 28
82 static unsigned int g_options = 0;
86 unsigned char param_count;
89 { "-h", 0, CMD_HELP },
93 { "-i", 0, CMD_INFO },
94 { "-g", 5, CMD_GEOMETRY },
95 { "-t", 7, CMD_TIMING },
96 { "-accel", 1, CMD_ACCEL },
97 { "-hsync", 1, CMD_HSYNC },
98 { "-vsync", 1, CMD_VSYNC },
99 { "-laced", 1, CMD_LACED },
100 { "-double", 1, CMD_DOUBLE },
102 #ifdef BB_FBSET_FANCY
103 { "--help", 0, CMD_HELP },
104 { "-all", 0, CMD_ALL },
105 { "-xres", 1, CMD_XRES },
106 { "-yres", 1, CMD_YRES },
107 { "-vxres", 1, CMD_VXRES },
108 { "-vyres", 1, CMD_VYRES },
109 { "-depth", 1, CMD_DEPTH },
110 { "-match", 0, CMD_MATCH },
111 { "--geometry", 5, CMD_GEOMETRY },
113 { "-pixclock", 1, CMD_PIXCLOCK },
114 { "-left", 1, CMD_LEFT },
115 { "-right", 1, CMD_RIGHT },
116 { "-upper", 1, CMD_UPPER },
117 { "-lower", 1, CMD_LOWER },
118 { "-hslen", 1, CMD_HSLEN },
119 { "-vslen", 1, CMD_VSLEN },
120 { "--timings", 7, CMD_TIMING },
122 { "-csync", 1, CMD_CSYNC },
123 { "-gsync", 1, CMD_GSYNC },
124 { "-extsync", 1, CMD_EXTSYNC },
125 { "-bcast", 1, CMD_BCAST },
126 { "-rgba", 1, CMD_RGBA },
127 { "-step", 1, CMD_STEP },
128 { "-move", 1, CMD_MOVE },
133 static int readmode(struct fb_var_screeninfo *base, const char *fn,
136 #ifdef BB_FBSET_READMODE
141 if ((f = fopen(fn, "r")) == NULL) PERROR("readmode(fopen)");
143 fgets(buf, sizeof(buf), f);
144 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
146 if ((p = strstr(buf, mode))) {
148 if (!isspace(*p) && (*p != 0) && (*p != '"') && (*p != '\r')
149 && (*p != '\n')) continue; /* almost, but not quite */
151 fgets(buf, sizeof(buf), f);
152 if (!strstr(buf, "endmode")) return 1;
158 fprintf(stderr, "W: mode reading was disabled on this copy of fbset; ignoring request\n");
163 static void setmode(struct fb_var_screeninfo *base,
164 struct fb_var_screeninfo *set)
166 if ((int)set->xres > 0) base->xres = set->xres;
167 if ((int)set->yres > 0) base->yres = set->yres;
168 if ((int)set->xres_virtual > 0) base->xres_virtual = set->xres_virtual;
169 if ((int)set->yres_virtual > 0) base->yres_virtual = set->yres_virtual;
170 if ((int)set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
173 static void showmode(struct fb_var_screeninfo *v)
175 double drate = 0, hrate = 0, vrate = 0;
177 drate = 1e12 / v->pixclock;
178 hrate = drate / (v->left_margin+v->xres+v->right_margin+v->hsync_len);
179 vrate = hrate / (v->upper_margin+v->yres+v->lower_margin+v->vsync_len);
181 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int)(vrate+0.5));
182 #ifdef BB_FBSET_FANCY
183 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate/1e6, hrate/1e3,
186 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
187 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
188 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
189 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
191 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
192 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length, v->red.offset,
193 v->green.length, v->green.offset, v->blue.length, v->blue.offset,
194 v->transp.length, v->transp.offset);
198 static void fbset_usage(void)
202 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
204 fprintf(stderr, "Usage: fbset [options] [mode]\n");
205 fprintf(stderr, "\tThe following options are recognized:\n");
206 for (i = 0; g_cmdoptions[i].name; i++)
207 fprintf(stderr, "\t\t%s\n", g_cmdoptions[i].name);
212 int main(int argc, char **argv)
214 extern int fbset_main(int argc, char **argv)
217 struct fb_var_screeninfo var, varset;
218 struct fb_fix_screeninfo fix;
220 char *fbdev = DEFAULTFBDEV;
221 char *modefile = DEFAULTFBMODE;
222 char *thisarg, *mode = NULL;
224 memset(&varset, 0xFF, sizeof(varset));
226 /* parse cmd args.... why do they have to make things so difficult? */
228 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
229 for (i = 0; g_cmdoptions[i].name; i++) {
230 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
231 if (argc - 1 < g_cmdoptions[i].param_count) fbset_usage();
232 switch (g_cmdoptions[i].code) {
233 case CMD_HELP: fbset_usage();
234 case CMD_FB: fbdev = argv[1]; break;
235 case CMD_DB: modefile = argv[1]; break;
237 varset.xres = strtoul(argv[1],0,0);
238 varset.yres = strtoul(argv[2],0,0);
239 varset.xres_virtual = strtoul(argv[3],0,0);
240 varset.yres_virtual = strtoul(argv[4],0,0);
241 varset.bits_per_pixel = strtoul(argv[5],0,0);
244 varset.pixclock = strtoul(argv[1],0,0);
245 varset.left_margin = strtoul(argv[2],0,0);
246 varset.right_margin = strtoul(argv[3],0,0);
247 varset.upper_margin = strtoul(argv[4],0,0);
248 varset.lower_margin = strtoul(argv[5],0,0);
249 varset.hsync_len = strtoul(argv[6],0,0);
250 varset.vsync_len = strtoul(argv[7],0,0);
252 #ifdef BB_FBSET_FANCY
253 case CMD_XRES: varset.xres = strtoul(argv[1],0,0); break;
254 case CMD_YRES: varset.yres = strtoul(argv[1],0,0); break;
257 argc -= g_cmdoptions[i].param_count;
258 argv += g_cmdoptions[i].param_count;
262 if (!g_cmdoptions[i].name) {
265 g_options |= OPT_READMODE;
272 if ((fh = open(fbdev, O_RDONLY)) < 0) PERROR("fbset(open)");
273 if (ioctl(fh, FBIOGET_VSCREENINFO, &var)) PERROR("fbset(ioctl)");
274 if (g_options & OPT_READMODE) {
275 if (!readmode(&var, modefile, mode)) {
276 fprintf(stderr, "Unknown video mode `%s'\n", mode);
281 setmode(&var, &varset);
282 if (g_options & OPT_CHANGE)
283 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var)) PERROR("fbset(ioctl)");