Shutdown sending on the socket when stdin closes.
[oweals/busybox.git] / fbset.c
diff --git a/fbset.c b/fbset.c
index 0a84dce236b42e0b51d30181b24798c4e47b9e33..86f7733c9afbc160967c53ec61019eaa8a134b0a 100644 (file)
--- a/fbset.c
+++ b/fbset.c
@@ -24,7 +24,7 @@
  *     Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
  */
 
-#include "internal.h"
+#include "busybox.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
 #include <ctype.h>
 #include <sys/ioctl.h>
-#include <linux/fb.h>
-#include <linux/version.h>
 
 #define PERROR(ctx)   do { perror(ctx); exit(1); } while(0)
 
-#ifndef KERNEL_VERSION
-#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
-#endif
-
 #define DEFAULTFBDEV  "/dev/fb0"
 #define DEFAULTFBMODE "/etc/fb.modes"
 
 /* #define CMD_XCOMPAT     10 */
 #define CMD_ALL         11
 #define CMD_INFO        12
+#define CMD_CHANGE      13
 
 #ifdef BB_FEATURE_FBSET_FANCY
-#define CMD_XRES       13
-#define CMD_YRES       14
-#define CMD_VXRES      15
-#define CMD_VYRES      16
-#define CMD_DEPTH      17
-#define CMD_MATCH      18
-#define CMD_PIXCLOCK   19
-#define CMD_LEFT       20
-#define CMD_RIGHT      21
-#define CMD_UPPER      22
-#define CMD_LOWER      23
-#define CMD_HSLEN      24
-#define CMD_VSLEN      25
-#define CMD_CSYNC      26
-#define CMD_GSYNC      27
-#define CMD_EXTSYNC    28
-#define CMD_BCAST      29
-#define CMD_RGBA       30
-#define CMD_STEP       31
-#define CMD_MOVE       32
+#define CMD_XRES       100
+#define CMD_YRES       101
+#define CMD_VXRES      102
+#define CMD_VYRES      103
+#define CMD_DEPTH      104
+#define CMD_MATCH      105
+#define CMD_PIXCLOCK   106
+#define CMD_LEFT       107
+#define CMD_RIGHT      108
+#define CMD_UPPER      109
+#define CMD_LOWER      110
+#define CMD_HSLEN      111
+#define CMD_VSLEN      112
+#define CMD_CSYNC      113
+#define CMD_GSYNC      114
+#define CMD_EXTSYNC    115
+#define CMD_BCAST      116
+#define CMD_RGBA       117
+#define CMD_STEP       118
+#define CMD_MOVE       119
 #endif
 
 static unsigned int g_options = 0;
 
+/* Stuff stolen from the kernel's fb.h */
+#define FBIOGET_VSCREENINFO     0x4600
+#define FBIOPUT_VSCREENINFO     0x4601
+#define __u32                  unsigned int
+struct fb_bitfield {
+       __u32 offset;                   /* beginning of bitfield        */
+       __u32 length;                   /* length of bitfield           */
+       __u32 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                   */
+
+       __u32 bits_per_pixel;           /* guess what                   */
+       __u32 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                 */      
+
+       __u32 nonstd;                   /* != 0 Non standard pixel format */
+
+       __u32 activate;                 /* see FB_ACTIVATE_*            */
+
+       __u32 height;                   /* height of picture in mm    */
+       __u32 width;                    /* width of picture in mm     */
+
+       __u32 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 */
+};
+
+
 struct cmdoptions_t {
        char *name;
        unsigned char param_count;
@@ -106,6 +150,7 @@ struct cmdoptions_t {
        "-laced", 1, CMD_LACED}, {
        "-double", 1, CMD_DOUBLE}, {
        "-help", 0, CMD_HELP}, {
+       "-n", 0, CMD_CHANGE}, {
 #ifdef BB_FEATURE_FBSET_FANCY
        "-help", 0, CMD_HELP}, {
        "-all", 0, CMD_ALL}, {
@@ -135,10 +180,19 @@ struct cmdoptions_t {
        0, 0, 0}
 };
 
+#ifdef BB_FEATURE_FBSET_READMODE
+/* taken from linux/fb.h */
+#define FB_VMODE_INTERLACED    1       /* interlaced   */
+#define FB_VMODE_DOUBLE                2       /* double scan */
+#define FB_SYNC_HOR_HIGH_ACT   1       /* horizontal sync high active  */
+#define FB_SYNC_VERT_HIGH_ACT  2       /* vertical sync high active    */
+#define FB_SYNC_EXT            4       /* external sync                */
+#define FB_SYNC_COMP_HIGH_ACT  8       /* composite sync high active   */
+#endif
 static int readmode(struct fb_var_screeninfo *base, const char *fn,
                                        const char *mode)
 {
-#ifdef BB_FBSET_READMODE
+#ifdef BB_FEATURE_FBSET_READMODE
        FILE *f;
        char buf[256];
        char *p = buf;
@@ -156,14 +210,80 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
                                        continue;       /* almost, but not quite */
                                while (!feof(f)) {
                                        fgets(buf, sizeof(buf), f);
-                                       if (!strstr(buf, "endmode"))
+
+                    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;
                                }
                        }
                }
        }
 #else
-       errorMsg( "mode reading not compiled in\n");
+       error_msg( "mode reading not compiled in\n");
 #endif
        return 0;
 }
@@ -203,16 +323,10 @@ static void showmode(struct fb_var_screeninfo *v)
 #endif
        printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
                   v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
        printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
                   v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
                   v->vsync_len);
        printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
-#else
-       printf("\ttimings %lu %lu %lu %lu %lu %lu %lu\n", v->pixclock,
-                  v->left_margin, v->right_margin, v->upper_margin,
-                  v->lower_margin, v->hsync_len, v->vsync_len);
-#endif
        printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length,
                   v->red.offset, v->green.length, v->green.offset, v->blue.length,
                   v->blue.offset, v->transp.length, v->transp.offset);
@@ -286,6 +400,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;
 #ifdef BB_FEATURE_FBSET_FANCY
                                case CMD_XRES:
                                        varset.xres = strtoul(argv[1], 0, 0);
@@ -316,8 +433,8 @@ extern int fbset_main(int argc, char **argv)
                PERROR("fbset(ioctl)");
        if (g_options & OPT_READMODE) {
                if (!readmode(&var, modefile, mode)) {
-                       fprintf(stderr, "Unknown video mode `%s'\n", mode);
-                       exit(1);
+                       error_msg("Unknown video mode `%s'\n", mode);
+                       return EXIT_FAILURE;
                }
        }
 
@@ -329,5 +446,5 @@ extern int fbset_main(int argc, char **argv)
        /* Don't close the file, as exiting will take care of that */
        /* close(fh); */
 
-       return (TRUE);
+       return EXIT_SUCCESS;
 }