1 /* vi: set sw=4 ts=4: */
3 * Mini fbset implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This is a from-scratch implementation of fbset; but the de facto fbset
22 * implementation was a good reference. fbset (original) is released under
23 * the GPL, and is (c) 1995-1999 by:
24 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
34 #include <sys/ioctl.h>
36 #include <linux/version.h>
38 #define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
40 #ifndef KERNEL_VERSION
41 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
44 #define DEFAULTFBDEV "/dev/fb0"
45 #define DEFAULTFBMODE "/etc/fb.modes"
48 #define OPT_INFO (1 << 1)
49 #define OPT_READMODE (1 << 2)
54 #define CMD_GEOMETRY 3
61 /* #define CMD_XCOMPAT 10 */
65 #ifdef BB_FEATURE_FBSET_FANCY
72 #define CMD_PIXCLOCK 19
81 #define CMD_EXTSYNC 28
88 static unsigned int g_options = 0;
92 unsigned char param_count;
100 "-i", 0, CMD_INFO}, {
101 "-g", 5, CMD_GEOMETRY}, {
102 "-t", 7, CMD_TIMING}, {
103 "-accel", 1, CMD_ACCEL}, {
104 "-hsync", 1, CMD_HSYNC}, {
105 "-vsync", 1, CMD_VSYNC}, {
106 "-laced", 1, CMD_LACED}, {
107 "-double", 1, CMD_DOUBLE}, {
108 "-help", 0, CMD_HELP}, {
109 #ifdef BB_FEATURE_FBSET_FANCY
110 "-help", 0, CMD_HELP}, {
111 "-all", 0, CMD_ALL}, {
112 "-xres", 1, CMD_XRES}, {
113 "-yres", 1, CMD_YRES}, {
114 "-vxres", 1, CMD_VXRES}, {
115 "-vyres", 1, CMD_VYRES}, {
116 "-depth", 1, CMD_DEPTH}, {
117 "-match", 0, CMD_MATCH}, {
118 "-geometry", 5, CMD_GEOMETRY}, {
119 "-pixclock", 1, CMD_PIXCLOCK}, {
120 "-left", 1, CMD_LEFT}, {
121 "-right", 1, CMD_RIGHT}, {
122 "-upper", 1, CMD_UPPER}, {
123 "-lower", 1, CMD_LOWER}, {
124 "-hslen", 1, CMD_HSLEN}, {
125 "-vslen", 1, CMD_VSLEN}, {
126 "-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)
147 PERROR("readmode(fopen)");
149 fgets(buf, sizeof(buf), f);
150 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
152 if ((p = strstr(buf, mode))) {
154 if (!isspace(*p) && (*p != 0) && (*p != '"')
155 && (*p != '\r') && (*p != '\n'))
156 continue; /* almost, but not quite */
158 fgets(buf, sizeof(buf), f);
159 if (!strstr(buf, "endmode"))
166 errorMsg( "mode reading not compiled in\n");
171 static void setmode(struct fb_var_screeninfo *base,
172 struct fb_var_screeninfo *set)
174 if ((int) set->xres > 0)
175 base->xres = set->xres;
176 if ((int) set->yres > 0)
177 base->yres = set->yres;
178 if ((int) set->xres_virtual > 0)
179 base->xres_virtual = set->xres_virtual;
180 if ((int) set->yres_virtual > 0)
181 base->yres_virtual = set->yres_virtual;
182 if ((int) set->bits_per_pixel > 0)
183 base->bits_per_pixel = set->bits_per_pixel;
186 static void showmode(struct fb_var_screeninfo *v)
188 double drate = 0, hrate = 0, vrate = 0;
191 drate = 1e12 / v->pixclock;
193 drate / (v->left_margin + v->xres + v->right_margin +
196 hrate / (v->upper_margin + v->yres + v->lower_margin +
199 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int) (vrate + 0.5));
200 #ifdef BB_FEATURE_FBSET_FANCY
201 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate / 1e6,
204 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
205 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
206 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
207 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
208 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
210 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
212 printf("\ttimings %lu %lu %lu %lu %lu %lu %lu\n", v->pixclock,
213 v->left_margin, v->right_margin, v->upper_margin,
214 v->lower_margin, v->hsync_len, v->vsync_len);
216 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length,
217 v->red.offset, v->green.length, v->green.offset, v->blue.length,
218 v->blue.offset, v->transp.length, v->transp.offset);
219 printf("endmode\n\n");
222 static void fbset_usage(void)
224 #ifndef BB_FEATURE_TRIVIAL_HELP
229 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
232 fprintf(stderr, "Usage: fbset [options] [mode]\n");
233 #ifndef BB_FEATURE_TRIVIAL_HELP
234 fprintf(stderr, "\nShows and modifies frame buffer device settings\n\n");
235 fprintf(stderr, "The following options are recognized:\n");
236 for (i = 0; g_cmdoptions[i].name; i++)
237 fprintf(stderr, "\t%s\n", g_cmdoptions[i].name);
243 int main(int argc, char **argv)
245 extern int fbset_main(int argc, char **argv)
248 struct fb_var_screeninfo var, varset;
250 char *fbdev = DEFAULTFBDEV;
251 char *modefile = DEFAULTFBMODE;
252 char *thisarg, *mode = NULL;
254 memset(&varset, 0xFF, sizeof(varset));
256 /* parse cmd args.... why do they have to make things so difficult? */
259 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
260 for (i = 0; g_cmdoptions[i].name; i++) {
261 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
262 if (argc - 1 < g_cmdoptions[i].param_count)
264 switch (g_cmdoptions[i].code) {
274 varset.xres = strtoul(argv[1], 0, 0);
275 varset.yres = strtoul(argv[2], 0, 0);
276 varset.xres_virtual = strtoul(argv[3], 0, 0);
277 varset.yres_virtual = strtoul(argv[4], 0, 0);
278 varset.bits_per_pixel = strtoul(argv[5], 0, 0);
281 varset.pixclock = strtoul(argv[1], 0, 0);
282 varset.left_margin = strtoul(argv[2], 0, 0);
283 varset.right_margin = strtoul(argv[3], 0, 0);
284 varset.upper_margin = strtoul(argv[4], 0, 0);
285 varset.lower_margin = strtoul(argv[5], 0, 0);
286 varset.hsync_len = strtoul(argv[6], 0, 0);
287 varset.vsync_len = strtoul(argv[7], 0, 0);
289 #ifdef BB_FEATURE_FBSET_FANCY
291 varset.xres = strtoul(argv[1], 0, 0);
294 varset.yres = strtoul(argv[1], 0, 0);
298 argc -= g_cmdoptions[i].param_count;
299 argv += g_cmdoptions[i].param_count;
303 if (!g_cmdoptions[i].name) {
306 g_options |= OPT_READMODE;
313 if ((fh = open(fbdev, O_RDONLY)) < 0)
314 PERROR("fbset(open)");
315 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
316 PERROR("fbset(ioctl)");
317 if (g_options & OPT_READMODE) {
318 if (!readmode(&var, modefile, mode)) {
319 fprintf(stderr, "Unknown video mode `%s'\n", mode);
324 setmode(&var, &varset);
325 if (g_options & OPT_CHANGE)
326 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
327 PERROR("fbset(ioctl)");
329 /* Don't close the file, as exiting will take care of that */