shrink the code a bit
[oweals/busybox.git] / util-linux / fbset.c
index 9423abc0949447c1e8fae6657f09a5b6e83d1a87..49435c22e8a856041ec828f9fea7b269c0f67bf7 100644 (file)
@@ -20,7 +20,7 @@
  *
  * This is a from-scratch implementation of fbset; but the de facto fbset
  * implementation was a good reference. fbset (original) is released under
- * the GPL, and is (c) 1995-1999 by: 
+ * the GPL, and is (c) 1995-1999 by:
  *     Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
  */
 
 #include <errno.h>
 #include <ctype.h>
 #include <string.h>
+#include <stdint.h>
 #include <sys/ioctl.h>
 #include "busybox.h"
 
-#define DEFAULTFBDEV  "/dev/fb0"
+#define DEFAULTFBDEV  FB_0
 #define DEFAULTFBMODE "/etc/fb.modes"
 
 static const int OPT_CHANGE   = (1 << 0);
@@ -51,7 +52,7 @@ enum {
        CMD_VSYNC = 7,
        CMD_LACED = 8,
        CMD_DOUBLE = 9,
-/*     CMD_XCOMPAT =     10, */
+/*     CMD_XCOMPAT =     10, */
        CMD_ALL = 11,
        CMD_INFO = 12,
        CMD_CHANGE = 13,
@@ -85,53 +86,52 @@ static unsigned int g_options = 0;
 /* Stuff stolen from the kernel's fb.h */
 static const int FBIOGET_VSCREENINFO = 0x4600;
 static const int FBIOPUT_VSCREENINFO = 0x4601;
-#define __u32                  u_int32_t
 struct fb_bitfield {
-       __u32 offset;                   /* beginning of bitfield        */
-       __u32 length;                   /* length of bitfield           */
-       __u32 msb_right;                /* != 0 : Most significant bit is */ 
-                                       /* right */ 
+       uint32_t offset;                        /* beginning of bitfield        */
+       uint32_t length;                        /* length of bitfield           */
+       uint32_t msb_right;             /* != 0 : Most significant bit is */
+                                       /* right */
 };
 struct fb_var_screeninfo {
-       __u32 xres;                     /* visible resolution           */
-       __u32 yres;
-       __u32 xres_virtual;             /* virtual resolution           */
-       __u32 yres_virtual;
-       __u32 xoffset;                  /* offset from virtual to visible */
-       __u32 yoffset;                  /* resolution                   */
+       uint32_t xres;                  /* visible resolution           */
+       uint32_t yres;
+       uint32_t xres_virtual;          /* virtual resolution           */
+       uint32_t yres_virtual;
+       uint32_t xoffset;                       /* offset from virtual to visible */
+       uint32_t yoffset;                       /* resolution                   */
 
-       __u32 bits_per_pixel;           /* guess what                   */
-       __u32 grayscale;                /* != 0 Graylevels instead of colors */
+       uint32_t bits_per_pixel;                /* guess what                   */
+       uint32_t grayscale;             /* != 0 Graylevels instead of colors */
 
        struct fb_bitfield red;         /* bitfield in fb mem if true color, */
        struct fb_bitfield green;       /* else only length is significant */
        struct fb_bitfield blue;
-       struct fb_bitfield transp;      /* transparency                 */      
+       struct fb_bitfield transp;      /* transparency                 */
 
-       __u32 nonstd;                   /* != 0 Non standard pixel format */
+       uint32_t nonstd;                        /* != 0 Non standard pixel format */
 
-       __u32 activate;                 /* see FB_ACTIVATE_*            */
+       uint32_t activate;                      /* see FB_ACTIVATE_*            */
 
-       __u32 height;                   /* height of picture in mm    */
-       __u32 width;                    /* width of picture in mm     */
+       uint32_t height;                        /* height of picture in mm    */
+       uint32_t width;                 /* width of picture in mm     */
 
-       __u32 accel_flags;              /* acceleration flags (hints)   */
+       uint32_t accel_flags;           /* acceleration flags (hints)   */
 
        /* Timing: All values in pixclocks, except pixclock (of course) */
-       __u32 pixclock;                 /* pixel clock in ps (pico seconds) */
-       __u32 left_margin;              /* time from sync to picture    */
-       __u32 right_margin;             /* time from picture to sync    */
-       __u32 upper_margin;             /* time from sync to picture    */
-       __u32 lower_margin;
-       __u32 hsync_len;                /* length of horizontal sync    */
-       __u32 vsync_len;                /* length of vertical sync      */
-       __u32 sync;                     /* see FB_SYNC_*                */
-       __u32 vmode;                    /* see FB_VMODE_*               */
-       __u32 reserved[6];              /* Reserved for future compatibility */
+       uint32_t pixclock;                      /* pixel clock in ps (pico seconds) */
+       uint32_t left_margin;           /* time from sync to picture    */
+       uint32_t right_margin;          /* time from picture to sync    */
+       uint32_t upper_margin;          /* time from sync to picture    */
+       uint32_t lower_margin;
+       uint32_t hsync_len;             /* length of horizontal sync    */
+       uint32_t vsync_len;             /* length of vertical sync      */
+       uint32_t sync;                  /* see FB_SYNC_*                */
+       uint32_t vmode;                 /* see FB_VMODE_*               */
+       uint32_t reserved[6];           /* Reserved for future compatibility */
 };
 
 
-const static struct cmdoptions_t {
+static const struct cmdoptions_t {
        const char *name;
        const unsigned char param_count;
        const unsigned char code;
@@ -194,7 +194,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
        char buf[256];
        char *p = buf;
 
-       f = xfopen(fn, "r");
+       f = bb_xfopen(fn, "r");
        while (!feof(f)) {
                fgets(buf, sizeof(buf), f);
                if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
@@ -207,71 +207,71 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
                                while (!feof(f)) {
                                        fgets(buf, sizeof(buf), f);
 
-                    if ((p = strstr(buf, "geometry "))) {
-                        p += 9;
-
-                        sscanf(p, "%d %d %d %d %d", 
-                                &(base->xres), &(base->yres), 
-                                &(base->xres_virtual), &(base->yres_virtual), 
-                                &(base->bits_per_pixel));
-                    } else if ((p = strstr(buf, "timings "))) {
-                        p += 8;
-                        
-                        sscanf(p, "%d %d %d %d %d %d %d",
-                                &(base->pixclock),
-                                &(base->left_margin), &(base->right_margin),
-                                &(base->upper_margin), &(base->lower_margin),
-                                &(base->hsync_len), &(base->vsync_len));
-                    } else if ((p = strstr(buf, "laced "))) {
-                        p += 6;
-
-                        if (strstr(buf, "false")) {
-                            base->vmode &= ~FB_VMODE_INTERLACED;
-                        } else {
-                            base->vmode |= FB_VMODE_INTERLACED;
-                        }
-                    } else if ((p = strstr(buf, "double "))) {
-                        p += 7;
-
-                        if (strstr(buf, "false")) {
-                            base->vmode &= ~FB_VMODE_DOUBLE;
-                        } else {
-                            base->vmode |= FB_VMODE_DOUBLE;
-                        }
-                    } else if ((p = strstr(buf, "vsync "))) {
-                        p += 6;
-
-                        if (strstr(buf, "low")) {
-                            base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
-                        } else {
-                            base->sync |= FB_SYNC_VERT_HIGH_ACT;
-                        }
-                    } else if ((p = strstr(buf, "hsync "))) {
-                        p += 6;
-
-                        if (strstr(buf, "low")) {
-                            base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
-                        } else {
-                            base->sync |= FB_SYNC_HOR_HIGH_ACT;
-                        }
-                    } else if ((p = strstr(buf, "csync "))) {
-                        p += 6;
-
-                        if (strstr(buf, "low")) {
-                            base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
-                        } else {
-                            base->sync |= FB_SYNC_COMP_HIGH_ACT;
-                        }
-                    } else if ((p = strstr(buf, "extsync "))) {
-                        p += 8;
-
-                        if (strstr(buf, "false")) {
-                            base->sync &= ~FB_SYNC_EXT;
-                        } else {
-                            base->sync |= FB_SYNC_EXT;
-                        }
-                    }
-                    
+                   if ((p = strstr(buf, "geometry "))) {
+                       p += 9;
+
+                       sscanf(p, "%d %d %d %d %d",
+                               &(base->xres), &(base->yres),
+                               &(base->xres_virtual), &(base->yres_virtual),
+                               &(base->bits_per_pixel));
+                   } else if ((p = strstr(buf, "timings "))) {
+                       p += 8;
+
+                       sscanf(p, "%d %d %d %d %d %d %d",
+                               &(base->pixclock),
+                               &(base->left_margin), &(base->right_margin),
+                               &(base->upper_margin), &(base->lower_margin),
+                               &(base->hsync_len), &(base->vsync_len));
+                   } else if ((p = strstr(buf, "laced "))) {
+                       p += 6;
+
+                       if (strstr(buf, "false")) {
+                           base->vmode &= ~FB_VMODE_INTERLACED;
+                       } else {
+                           base->vmode |= FB_VMODE_INTERLACED;
+                       }
+                   } else if ((p = strstr(buf, "double "))) {
+                       p += 7;
+
+                       if (strstr(buf, "false")) {
+                           base->vmode &= ~FB_VMODE_DOUBLE;
+                       } else {
+                           base->vmode |= FB_VMODE_DOUBLE;
+                       }
+                   } else if ((p = strstr(buf, "vsync "))) {
+                       p += 6;
+
+                       if (strstr(buf, "low")) {
+                           base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
+                       } else {
+                           base->sync |= FB_SYNC_VERT_HIGH_ACT;
+                       }
+                   } else if ((p = strstr(buf, "hsync "))) {
+                       p += 6;
+
+                       if (strstr(buf, "low")) {
+                           base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
+                       } else {
+                           base->sync |= FB_SYNC_HOR_HIGH_ACT;
+                       }
+                   } else if ((p = strstr(buf, "csync "))) {
+                       p += 6;
+
+                       if (strstr(buf, "low")) {
+                           base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
+                       } else {
+                           base->sync |= FB_SYNC_COMP_HIGH_ACT;
+                       }
+                   } else if ((p = strstr(buf, "extsync "))) {
+                       p += 8;
+
+                       if (strstr(buf, "false")) {
+                           base->sync &= ~FB_SYNC_EXT;
+                       } else {
+                           base->sync |= FB_SYNC_EXT;
+                       }
+                   }
+
                                        if (strstr(buf, "endmode"))
                                                return 1;
                                }
@@ -279,7 +279,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
                }
        }
 #else
-       error_msg( "mode reading not compiled in");
+       bb_error_msg( "mode reading not compiled in");
 #endif
        return 0;
 }
@@ -350,7 +350,7 @@ extern int fbset_main(int argc, char **argv)
                for (i = 0; g_cmdoptions[i].name; i++) {
                        if (!strcmp(thisarg, g_cmdoptions[i].name)) {
                                if (argc - 1 < g_cmdoptions[i].param_count)
-                                       show_usage();
+                                       bb_show_usage();
                                switch (g_cmdoptions[i].code) {
                                case CMD_FB:
                                        fbdev = argv[1];
@@ -374,9 +374,9 @@ extern int fbset_main(int argc, char **argv)
                                        varset.hsync_len = strtoul(argv[6], 0, 0);
                                        varset.vsync_len = strtoul(argv[7], 0, 0);
                                        break;
-                case CMD_CHANGE:
-                    g_options |= OPT_CHANGE;
-                    break;
+               case CMD_CHANGE:
+                   g_options |= OPT_CHANGE;
+                   break;
 #ifdef CONFIG_FEATURE_FBSET_FANCY
                                case CMD_XRES:
                                        varset.xres = strtoul(argv[1], 0, 0);
@@ -399,18 +399,18 @@ extern int fbset_main(int argc, char **argv)
                                mode = *argv;
                                g_options |= OPT_READMODE;
                        } else {
-                               show_usage();
+                               bb_show_usage();
                        }
                }
        }
 
        if ((fh = open(fbdev, O_RDONLY)) < 0)
-               perror_msg_and_die("fbset(open)");
+               bb_perror_msg_and_die("fbset(open)");
        if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
-               perror_msg_and_die("fbset(ioctl)");
+               bb_perror_msg_and_die("fbset(ioctl)");
        if (g_options & OPT_READMODE) {
                if (!readmode(&var, modefile, mode)) {
-                       error_msg("Unknown video mode `%s'", mode);
+                       bb_error_msg("Unknown video mode `%s'", mode);
                        return EXIT_FAILURE;
                }
        }
@@ -418,7 +418,7 @@ extern int fbset_main(int argc, char **argv)
        setmode(&var, &varset);
        if (g_options & OPT_CHANGE)
                if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
-                       perror_msg_and_die("fbset(ioctl)");
+                       bb_perror_msg_and_die("fbset(ioctl)");
        showmode(&var);
        /* Don't close the file, as exiting will take care of that */
        /* close(fh); */