Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/util-linux/fbset.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 815 by niro, Sat Sep 1 22:45:15 2007 UTC revision 816 by niro, Fri Apr 24 18:33:46 2009 UTC
# Line 12  Line 12 
12   *     Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)   *     Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
13   */   */
14    
15  #include "busybox.h"  #include "libbb.h"
16    
17  #define DEFAULTFBDEV  FB_0  #define DEFAULTFBDEV  FB_0
18  #define DEFAULTFBMODE "/etc/fb.modes"  #define DEFAULTFBMODE "/etc/fb.modes"
19    
20  enum {  enum {
  OPT_CHANGE   = (1 << 0),  
  OPT_INFO     = (1 << 1),  
  OPT_READMODE = (1 << 2),  
  OPT_ALL      = (1 << 9),  
   
21   CMD_FB = 1,   CMD_FB = 1,
22   CMD_DB = 2,   CMD_DB = 2,
23   CMD_GEOMETRY = 3,   CMD_GEOMETRY = 3,
# Line 37  enum { Line 32  enum {
32   CMD_INFO = 12,   CMD_INFO = 12,
33   CMD_CHANGE = 13,   CMD_CHANGE = 13,
34    
35  #ifdef CONFIG_FEATURE_FBSET_FANCY  #if ENABLE_FEATURE_FBSET_FANCY
36   CMD_XRES = 100,   CMD_XRES = 100,
37   CMD_YRES = 101,   CMD_YRES = 101,
38   CMD_VXRES = 102,   CMD_VXRES = 102,
# Line 61  enum { Line 56  enum {
56  #endif  #endif
57  };  };
58    
 static unsigned g_options;  
   
59  /* Stuff stolen from the kernel's fb.h */  /* Stuff stolen from the kernel's fb.h */
60  #define FB_ACTIVATE_ALL 64  #define FB_ACTIVATE_ALL 64
61  enum {  enum {
# Line 114  struct fb_var_screeninfo { Line 107  struct fb_var_screeninfo {
107    
108    
109  static const struct cmdoptions_t {  static const struct cmdoptions_t {
110   const char name[10];   const char name[9];
111   const unsigned char param_count;   const unsigned char param_count;
112   const unsigned char code;   const unsigned char code;
113  } g_cmdoptions[] = {  } g_cmdoptions[] = {
114   { "-fb", 1, CMD_FB },   /*"12345678" + NUL */
115   { "-db", 1, CMD_DB },   { "fb"      , 1, CMD_FB       },
116   { "-a", 0, CMD_ALL },   { "db"      , 1, CMD_DB       },
117   { "-i", 0, CMD_INFO },   { "a"       , 0, CMD_ALL      },
118   { "-g", 5, CMD_GEOMETRY },   { "i"       , 0, CMD_INFO     },
119   { "-t", 7, CMD_TIMING },   { "g"       , 5, CMD_GEOMETRY },
120   { "-accel", 1, CMD_ACCEL },   { "t"       , 7, CMD_TIMING   },
121   { "-hsync", 1, CMD_HSYNC },   { "accel"   , 1, CMD_ACCEL    },
122   { "-vsync", 1, CMD_VSYNC },   { "hsync"   , 1, CMD_HSYNC    },
123   { "-laced", 1, CMD_LACED },   { "vsync"   , 1, CMD_VSYNC    },
124   { "-double", 1, CMD_DOUBLE },   { "laced"   , 1, CMD_LACED    },
125   { "-n", 0, CMD_CHANGE },   { "double"  , 1, CMD_DOUBLE   },
126  #ifdef CONFIG_FEATURE_FBSET_FANCY   { "n"       , 0, CMD_CHANGE   },
127   { "-all", 0, CMD_ALL },  #if ENABLE_FEATURE_FBSET_FANCY
128   { "-xres", 1, CMD_XRES },   { "all"     , 0, CMD_ALL      },
129   { "-yres", 1, CMD_YRES },   { "xres"    , 1, CMD_XRES     },
130   { "-vxres", 1, CMD_VXRES },   { "yres"    , 1, CMD_YRES     },
131   { "-vyres", 1, CMD_VYRES },   { "vxres"   , 1, CMD_VXRES    },
132   { "-depth", 1, CMD_DEPTH },   { "vyres"   , 1, CMD_VYRES    },
133   { "-match", 0, CMD_MATCH },   { "depth"   , 1, CMD_DEPTH    },
134   { "-geometry", 5, CMD_GEOMETRY },   { "match"   , 0, CMD_MATCH    },
135   { "-pixclock", 1, CMD_PIXCLOCK },   { "geometry", 5, CMD_GEOMETRY },
136   { "-left", 1, CMD_LEFT },   { "pixclock", 1, CMD_PIXCLOCK },
137   { "-right", 1, CMD_RIGHT },   { "left"    , 1, CMD_LEFT     },
138   { "-upper", 1, CMD_UPPER },   { "right"   , 1, CMD_RIGHT    },
139   { "-lower", 1, CMD_LOWER },   { "upper"   , 1, CMD_UPPER    },
140   { "-hslen", 1, CMD_HSLEN },   { "lower"   , 1, CMD_LOWER    },
141   { "-vslen", 1, CMD_VSLEN },   { "hslen"   , 1, CMD_HSLEN    },
142   { "-timings", 7, CMD_TIMING },   { "vslen"   , 1, CMD_VSLEN    },
143   { "-csync", 1, CMD_CSYNC },   { "timings" , 7, CMD_TIMING   },
144   { "-gsync", 1, CMD_GSYNC },   { "csync"   , 1, CMD_CSYNC    },
145   { "-extsync", 1, CMD_EXTSYNC },   { "gsync"   , 1, CMD_GSYNC    },
146   { "-bcast", 1, CMD_BCAST },   { "extsync" , 1, CMD_EXTSYNC  },
147   { "-rgba", 1, CMD_RGBA },   { "bcast"   , 1, CMD_BCAST    },
148   { "-step", 1, CMD_STEP },   { "rgba"    , 1, CMD_RGBA     },
149   { "-move", 1, CMD_MOVE },   { "step"    , 1, CMD_STEP     },
150     { "move"    , 1, CMD_MOVE     },
151  #endif  #endif
  { "", 0, 0 }  
152  };  };
153    
154  #ifdef CONFIG_FEATURE_FBSET_READMODE  #if ENABLE_FEATURE_FBSET_READMODE
155  /* taken from linux/fb.h */  /* taken from linux/fb.h */
156  enum {  enum {
157   FB_VMODE_INTERLACED = 1, /* interlaced */   FB_VMODE_INTERLACED = 1,        /* interlaced */
158   FB_VMODE_DOUBLE = 2, /* double scan */   FB_VMODE_DOUBLE = 2,            /* double scan */
159   FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */   FB_SYNC_HOR_HIGH_ACT = 1,       /* horizontal sync high active */
160   FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */   FB_SYNC_VERT_HIGH_ACT = 2,      /* vertical sync high active */
161   FB_SYNC_EXT = 4, /* external sync */   FB_SYNC_EXT = 4,                /* external sync */
162   FB_SYNC_COMP_HIGH_ACT = 8 /* composite sync high active   */   FB_SYNC_COMP_HIGH_ACT = 8,      /* composite sync high active */
163  };  };
164  #endif  #endif
165    
166    #if ENABLE_FEATURE_FBSET_READMODE
167    static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
168    {
169     if (strcmp(buf, what) == 0)
170     *x &= ~flag;
171     else
172     *x |= flag;
173    }
174    
175  static int readmode(struct fb_var_screeninfo *base, const char *fn,  static int readmode(struct fb_var_screeninfo *base, const char *fn,
176   const char *mode)   const char *mode)
177  {  {
178  #ifdef CONFIG_FEATURE_FBSET_READMODE   char *token[2], *p, *s;
179   FILE *f;   parser_t *parser = config_open(fn);
180   char buf[256];  
181   char *p = buf;   while (config_read(parser, token, 2, 1, "# \t\r", PARSE_NORMAL)) {
182     if (strcmp(token[0], "mode") != 0 || !token[1])
  f = xfopen(fn, "r");  
  while (!feof(f)) {  
  fgets(buf, sizeof(buf), f);  
  if (!(p = strstr(buf, "mode ")) && !(p = strstr(buf, "mode\t")))  
183   continue;   continue;
184   p += 5;   p = strstr(token[1], mode);
185   if (!(p = strstr(buf, mode)))   if (!p)
186   continue;   continue;
187   p += strlen(mode);   s = p + strlen(mode);
188   if (!isspace(*p) && (*p != 0) && (*p != '"')   //bb_info_msg("CHECK[%s][%s][%d]", mode, p-1, *s);
189   && (*p != '\r') && (*p != '\n'))   /* exact match? */
190   continue; /* almost, but not quite */   if (((!*s || isspace(*s)) && '"' != s[-1]) /* end-of-token */
191     || ('"' == *s && '"' == p[-1]) /* ends with " but starts with " too! */
192   while (!feof(f)) {   ) {
193   fgets(buf, sizeof(buf), f);   //bb_info_msg("FOUND[%s][%s][%s][%d]", token[1], p, mode, isspace(*s));
194   if ((p = strstr(buf, "geometry "))) {   break;
195   p += 9;   }
196   /* FIXME: catastrophic on arches with 64bit ints */   }
197   sscanf(p, "%d %d %d %d %d",  
198   &(base->xres), &(base->yres),   if (!token[0])
199   &(base->xres_virtual), &(base->yres_virtual),   return 0;
200   &(base->bits_per_pixel));  
201   } else if ((p = strstr(buf, "timings "))) {   while (config_read(parser, token, 2, 1, "# \t", PARSE_NORMAL)) {
202   p += 8;   int i;
  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;  
  }  
  }  
203    
204   if (strstr(buf, "endmode"))  //bb_info_msg("???[%s][%s]", token[0], token[1]);
205   return 1;   if (strcmp(token[0], "endmode") == 0) {
206    //bb_info_msg("OK[%s]", mode);
207     return 1;
208     }
209     p = token[1];
210     i = index_in_strings(
211     "geometry\0timings\0interlaced\0double\0vsync\0hsync\0csync\0extsync\0",
212     token[0]);
213     switch (i) {
214     case 0:
215     /* FIXME: catastrophic on arches with 64bit ints */
216     sscanf(p, "%d %d %d %d %d",
217     &(base->xres), &(base->yres),
218     &(base->xres_virtual), &(base->yres_virtual),
219     &(base->bits_per_pixel));
220    //bb_info_msg("GEO[%s]", p);
221     break;
222     case 1:
223     sscanf(p, "%d %d %d %d %d %d %d",
224     &(base->pixclock),
225     &(base->left_margin), &(base->right_margin),
226     &(base->upper_margin), &(base->lower_margin),
227     &(base->hsync_len), &(base->vsync_len));
228    //bb_info_msg("TIM[%s]", p);
229     break;
230     case 2:
231     case 3: {
232     static const uint32_t syncs[] = {FB_VMODE_INTERLACED, FB_VMODE_DOUBLE};
233     ss(&base->vmode, syncs[i-2], p, "false");
234    //bb_info_msg("VMODE[%s]", p);
235     break;
236     }
237     case 4:
238     case 5:
239     case 6: {
240     static const uint32_t syncs[] = {FB_SYNC_VERT_HIGH_ACT, FB_SYNC_HOR_HIGH_ACT, FB_SYNC_COMP_HIGH_ACT};
241     ss(&base->sync, syncs[i-4], p, "low");
242    //bb_info_msg("SYNC[%s]", p);
243     break;
244     }
245     case 7:
246     ss(&base->sync, FB_SYNC_EXT, p, "false");
247    //bb_info_msg("EXTSYNC[%s]", p);
248     break;
249   }   }
250   }   }
 #else  
  bb_error_msg("mode reading not compiled in");  
 #endif  
251   return 0;   return 0;
252  }  }
253    #endif
254    
255  static inline void setmode(struct fb_var_screeninfo *base,  static void setmode(struct fb_var_screeninfo *base,
256   struct fb_var_screeninfo *set)   struct fb_var_screeninfo *set)
257  {  {
258   if ((int) set->xres > 0)   if ((int32_t) set->xres > 0)
259   base->xres = set->xres;   base->xres = set->xres;
260   if ((int) set->yres > 0)   if ((int32_t) set->yres > 0)
261   base->yres = set->yres;   base->yres = set->yres;
262   if ((int) set->xres_virtual > 0)   if ((int32_t) set->xres_virtual > 0)
263   base->xres_virtual = set->xres_virtual;   base->xres_virtual = set->xres_virtual;
264   if ((int) set->yres_virtual > 0)   if ((int32_t) set->yres_virtual > 0)
265   base->yres_virtual = set->yres_virtual;   base->yres_virtual = set->yres_virtual;
266   if ((int) set->bits_per_pixel > 0)   if ((int32_t) set->bits_per_pixel > 0)
267   base->bits_per_pixel = set->bits_per_pixel;   base->bits_per_pixel = set->bits_per_pixel;
268  }  }
269    
270  static inline void showmode(struct fb_var_screeninfo *v)  static void showmode(struct fb_var_screeninfo *v)
271  {  {
272   double drate = 0, hrate = 0, vrate = 0;   double drate = 0, hrate = 0, vrate = 0;
273    
# Line 286  static inline void showmode(struct fb_va Line 277  static inline void showmode(struct fb_va
277   vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);   vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
278   }   }
279   printf("\nmode \"%ux%u-%u\"\n"   printf("\nmode \"%ux%u-%u\"\n"
280  #ifdef CONFIG_FEATURE_FBSET_FANCY  #if ENABLE_FEATURE_FBSET_FANCY
281   "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"   "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
282  #endif  #endif
283   "\tgeometry %u %u %u %u %u\n"   "\tgeometry %u %u %u %u %u\n"
# Line 295  static inline void showmode(struct fb_va Line 286  static inline void showmode(struct fb_va
286   "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"   "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
287   "endmode\n\n",   "endmode\n\n",
288   v->xres, v->yres, (int) (vrate + 0.5),   v->xres, v->yres, (int) (vrate + 0.5),
289  #ifdef CONFIG_FEATURE_FBSET_FANCY  #if ENABLE_FEATURE_FBSET_FANCY
290   drate / 1e6, hrate / 1e3, vrate,   drate / 1e6, hrate / 1e3, vrate,
291  #endif  #endif
292   v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,   v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
# Line 306  static inline void showmode(struct fb_va Line 297  static inline void showmode(struct fb_va
297   v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);   v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
298  }  }
299    
300  #ifdef STANDALONE  int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int main(int argc, char **argv)  
 #else  
301  int fbset_main(int argc, char **argv)  int fbset_main(int argc, char **argv)
 #endif  
302  {  {
303     enum {
304     OPT_CHANGE   = (1 << 0),
305     /*OPT_INFO     = (1 << 1), ??*/
306     OPT_READMODE = (1 << 2),
307     OPT_ALL      = (1 << 9),
308     };
309   struct fb_var_screeninfo var, varset;   struct fb_var_screeninfo var, varset;
310   int fh, i;   int fh, i;
311   char *fbdev = DEFAULTFBDEV;   unsigned options = 0;
312   char *modefile = DEFAULTFBMODE;  
313     const char *fbdev = DEFAULTFBDEV;
314     const char *modefile = DEFAULTFBMODE;
315   char *thisarg, *mode = NULL;   char *thisarg, *mode = NULL;
316    
317   memset(&varset, 0xFF, sizeof(varset));   memset(&varset, 0xff, sizeof(varset));
318    
319   /* parse cmd args.... why do they have to make things so difficult? */   /* parse cmd args.... why do they have to make things so difficult? */
320   argv++;   argv++;
321   argc--;   argc--;
322   for (; argc > 0 && (thisarg = *argv); argc--, argv++) {   for (; argc > 0 && (thisarg = *argv) != NULL; argc--, argv++) {
323   for (i = 0; g_cmdoptions[i].name[0]; i++) {   if (thisarg[0] == '-') for (i = 0; i < ARRAY_SIZE(g_cmdoptions); i++) {
324   if (strcmp(thisarg, g_cmdoptions[i].name))   if (strcmp(thisarg + 1, g_cmdoptions[i].name) != 0)
325   continue;   continue;
326   if (argc-1 < g_cmdoptions[i].param_count)   if (argc <= g_cmdoptions[i].param_count)
327   bb_show_usage();   bb_show_usage();
328    
329   switch (g_cmdoptions[i].code) {   switch (g_cmdoptions[i].code) {
# Line 354  int fbset_main(int argc, char **argv) Line 350  int fbset_main(int argc, char **argv)
350   varset.vsync_len = xatou32(argv[7]);   varset.vsync_len = xatou32(argv[7]);
351   break;   break;
352   case CMD_ALL:   case CMD_ALL:
353   g_options |= OPT_ALL;   options |= OPT_ALL;
354   break;   break;
355   case CMD_CHANGE:   case CMD_CHANGE:
356   g_options |= OPT_CHANGE;   options |= OPT_CHANGE;
357   break;   break;
358  #ifdef CONFIG_FEATURE_FBSET_FANCY  #if ENABLE_FEATURE_FBSET_FANCY
359   case CMD_XRES:   case CMD_XRES:
360   varset.xres = xatou32(argv[1]);   varset.xres = xatou32(argv[1]);
361   break;   break;
# Line 373  int fbset_main(int argc, char **argv) Line 369  int fbset_main(int argc, char **argv)
369   }   }
370   argc -= g_cmdoptions[i].param_count;   argc -= g_cmdoptions[i].param_count;
371   argv += g_cmdoptions[i].param_count;   argv += g_cmdoptions[i].param_count;
372   break;   goto contin;
  }  
  if (!g_cmdoptions[i].name[0]) {  
  if (argc != 1)  
  bb_show_usage();  
  mode = *argv;  
  g_options |= OPT_READMODE;  
373   }   }
374     if (argc != 1)
375     bb_show_usage();
376     mode = *argv;
377     options |= OPT_READMODE;
378     contin: ;
379   }   }
380    
381   fh = xopen(fbdev, O_RDONLY);   fh = xopen(fbdev, O_RDONLY);
382   if (ioctl(fh, FBIOGET_VSCREENINFO, &var))   xioctl(fh, FBIOGET_VSCREENINFO, &var);
383   bb_perror_msg_and_die("ioctl(%sT_VSCREENINFO)", "GE");   if (options & OPT_READMODE) {
384   if (g_options & OPT_READMODE) {  #if !ENABLE_FEATURE_FBSET_READMODE
385     bb_show_usage();
386    #else
387   if (!readmode(&var, modefile, mode)) {   if (!readmode(&var, modefile, mode)) {
388   bb_error_msg_and_die("unknown video mode '%s'", mode);   bb_error_msg_and_die("unknown video mode '%s'", mode);
389   }   }
390    #endif
391   }   }
392    
393   setmode(&var, &varset);   setmode(&var, &varset);
394   if (g_options & OPT_CHANGE) {   if (options & OPT_CHANGE) {
395   if (g_options & OPT_ALL)   if (options & OPT_ALL)
396   var.activate = FB_ACTIVATE_ALL;   var.activate = FB_ACTIVATE_ALL;
397   if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))   xioctl(fh, FBIOPUT_VSCREENINFO, &var);
  bb_perror_msg_and_die("ioctl(%sT_VSCREENINFO)", "PU");  
398   }   }
399   showmode(&var);   showmode(&var);
400   /* Don't close the file, as exiting will take care of that */   /* Don't close the file, as exiting will take care of that */

Legend:
Removed from v.815  
changed lines
  Added in v.816