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>
35 #include <linux/version.h>
37 #define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
39 #ifndef KERNEL_VERSION
40 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
43 #define DEFAULTFBDEV "/dev/fb0"
44 #define DEFAULTFBMODE "/etc/fb.modes"
47 #define OPT_INFO (1 << 1)
48 #define OPT_READMODE (1 << 2)
53 #define CMD_GEOMETRY 3
60 /* #define CMD_XCOMPAT 10 */
71 #define CMD_PIXCLOCK 19
80 #define CMD_EXTSYNC 28
87 static unsigned int g_options = 0;
91 unsigned char param_count;
94 { "-h", 0, CMD_HELP },
98 { "-i", 0, CMD_INFO },
99 { "-g", 5, CMD_GEOMETRY },
100 { "-t", 7, CMD_TIMING },
101 { "-accel", 1, CMD_ACCEL },
102 { "-hsync", 1, CMD_HSYNC },
103 { "-vsync", 1, CMD_VSYNC },
104 { "-laced", 1, CMD_LACED },
105 { "-double", 1, CMD_DOUBLE },
107 #ifdef BB_FBSET_FANCY
108 { "--help", 0, CMD_HELP },
109 { "-all", 0, CMD_ALL },
110 { "-xres", 1, CMD_XRES },
111 { "-yres", 1, CMD_YRES },
112 { "-vxres", 1, CMD_VXRES },
113 { "-vyres", 1, CMD_VYRES },
114 { "-depth", 1, CMD_DEPTH },
115 { "-match", 0, CMD_MATCH },
116 { "--geometry", 5, CMD_GEOMETRY },
118 { "-pixclock", 1, CMD_PIXCLOCK },
119 { "-left", 1, CMD_LEFT },
120 { "-right", 1, CMD_RIGHT },
121 { "-upper", 1, CMD_UPPER },
122 { "-lower", 1, CMD_LOWER },
123 { "-hslen", 1, CMD_HSLEN },
124 { "-vslen", 1, CMD_VSLEN },
125 { "--timings", 7, CMD_TIMING },
127 { "-csync", 1, CMD_CSYNC },
128 { "-gsync", 1, CMD_GSYNC },
129 { "-extsync", 1, CMD_EXTSYNC },
130 { "-bcast", 1, CMD_BCAST },
131 { "-rgba", 1, CMD_RGBA },
132 { "-step", 1, CMD_STEP },
133 { "-move", 1, CMD_MOVE },
138 static int readmode(struct fb_var_screeninfo *base, const char *fn,
141 #ifdef BB_FBSET_READMODE
146 if ((f = fopen(fn, "r")) == NULL) PERROR("readmode(fopen)");
148 fgets(buf, sizeof(buf), f);
149 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
151 if ((p = strstr(buf, mode))) {
153 if (!isspace(*p) && (*p != 0) && (*p != '"') && (*p != '\r')
154 && (*p != '\n')) continue; /* almost, but not quite */
156 fgets(buf, sizeof(buf), f);
157 if (!strstr(buf, "endmode")) return 1;
163 fprintf(stderr, "W: mode reading was disabled on this copy of fbset; ignoring request\n");
168 static void setmode(struct fb_var_screeninfo *base,
169 struct fb_var_screeninfo *set)
171 if ((int)set->xres > 0) base->xres = set->xres;
172 if ((int)set->yres > 0) base->yres = set->yres;
173 if ((int)set->xres_virtual > 0) base->xres_virtual = set->xres_virtual;
174 if ((int)set->yres_virtual > 0) base->yres_virtual = set->yres_virtual;
175 if ((int)set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
178 static void showmode(struct fb_var_screeninfo *v)
180 double drate = 0, hrate = 0, vrate = 0;
182 drate = 1e12 / v->pixclock;
183 hrate = drate / (v->left_margin+v->xres+v->right_margin+v->hsync_len);
184 vrate = hrate / (v->upper_margin+v->yres+v->lower_margin+v->vsync_len);
186 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int)(vrate+0.5));
187 #ifdef BB_FBSET_FANCY
188 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate/1e6, hrate/1e3,
191 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
192 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
193 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
194 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
195 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
197 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
199 printf("\ttimings %lu %lu %lu %lu %lu %lu %lu\n", v->pixclock, v->left_margin,
200 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
203 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length, v->red.offset,
204 v->green.length, v->green.offset, v->blue.length, v->blue.offset,
205 v->transp.length, v->transp.offset);
209 static void fbset_usage(void)
213 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
215 fprintf(stderr, "Usage: fbset [options] [mode]\n");
216 fprintf(stderr, "\tThe following options are recognized:\n");
217 for (i = 0; g_cmdoptions[i].name; i++)
218 fprintf(stderr, "\t\t%s\n", g_cmdoptions[i].name);
223 int main(int argc, char **argv)
225 extern int fbset_main(int argc, char **argv)
228 struct fb_var_screeninfo var, varset;
230 char *fbdev = DEFAULTFBDEV;
231 char *modefile = DEFAULTFBMODE;
232 char *thisarg, *mode = NULL;
234 memset(&varset, 0xFF, sizeof(varset));
236 /* parse cmd args.... why do they have to make things so difficult? */
238 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
239 for (i = 0; g_cmdoptions[i].name; i++) {
240 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
241 if (argc - 1 < g_cmdoptions[i].param_count) fbset_usage();
242 switch (g_cmdoptions[i].code) {
243 case CMD_HELP: fbset_usage();
244 case CMD_FB: fbdev = argv[1]; break;
245 case CMD_DB: modefile = argv[1]; break;
247 varset.xres = strtoul(argv[1],0,0);
248 varset.yres = strtoul(argv[2],0,0);
249 varset.xres_virtual = strtoul(argv[3],0,0);
250 varset.yres_virtual = strtoul(argv[4],0,0);
251 varset.bits_per_pixel = strtoul(argv[5],0,0);
254 varset.pixclock = strtoul(argv[1],0,0);
255 varset.left_margin = strtoul(argv[2],0,0);
256 varset.right_margin = strtoul(argv[3],0,0);
257 varset.upper_margin = strtoul(argv[4],0,0);
258 varset.lower_margin = strtoul(argv[5],0,0);
259 varset.hsync_len = strtoul(argv[6],0,0);
260 varset.vsync_len = strtoul(argv[7],0,0);
262 #ifdef BB_FBSET_FANCY
263 case CMD_XRES: varset.xres = strtoul(argv[1],0,0); break;
264 case CMD_YRES: varset.yres = strtoul(argv[1],0,0); break;
267 argc -= g_cmdoptions[i].param_count;
268 argv += g_cmdoptions[i].param_count;
272 if (!g_cmdoptions[i].name) {
275 g_options |= OPT_READMODE;
282 if ((fh = open(fbdev, O_RDONLY)) < 0) PERROR("fbset(open)");
283 if (ioctl(fh, FBIOGET_VSCREENINFO, &var)) PERROR("fbset(ioctl)");
284 if (g_options & OPT_READMODE) {
285 if (!readmode(&var, modefile, mode)) {
286 fprintf(stderr, "Unknown video mode `%s'\n", mode);
291 setmode(&var, &varset);
292 if (g_options & OPT_CHANGE)
293 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var)) PERROR("fbset(ioctl)");