3
0

fbset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini fbset implementation for busybox
  4. *
  5. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. *
  9. * This is a from-scratch implementation of fbset; but the de facto fbset
  10. * implementation was a good reference. fbset (original) is released under
  11. * the GPL, and is (c) 1995-1999 by:
  12. * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
  13. */
  14. #include "libbb.h"
  15. #define DEFAULTFBDEV FB_0
  16. #define DEFAULTFBMODE "/etc/fb.modes"
  17. enum {
  18. OPT_CHANGE = (1 << 0),
  19. OPT_INFO = (1 << 1),
  20. OPT_READMODE = (1 << 2),
  21. OPT_ALL = (1 << 9),
  22. CMD_FB = 1,
  23. CMD_DB = 2,
  24. CMD_GEOMETRY = 3,
  25. CMD_TIMING = 4,
  26. CMD_ACCEL = 5,
  27. CMD_HSYNC = 6,
  28. CMD_VSYNC = 7,
  29. CMD_LACED = 8,
  30. CMD_DOUBLE = 9,
  31. /* CMD_XCOMPAT = 10, */
  32. CMD_ALL = 11,
  33. CMD_INFO = 12,
  34. CMD_CHANGE = 13,
  35. #if ENABLE_FEATURE_FBSET_FANCY
  36. CMD_XRES = 100,
  37. CMD_YRES = 101,
  38. CMD_VXRES = 102,
  39. CMD_VYRES = 103,
  40. CMD_DEPTH = 104,
  41. CMD_MATCH = 105,
  42. CMD_PIXCLOCK = 106,
  43. CMD_LEFT = 107,
  44. CMD_RIGHT = 108,
  45. CMD_UPPER = 109,
  46. CMD_LOWER = 110,
  47. CMD_HSLEN = 111,
  48. CMD_VSLEN = 112,
  49. CMD_CSYNC = 113,
  50. CMD_GSYNC = 114,
  51. CMD_EXTSYNC = 115,
  52. CMD_BCAST = 116,
  53. CMD_RGBA = 117,
  54. CMD_STEP = 118,
  55. CMD_MOVE = 119,
  56. #endif
  57. };
  58. static unsigned g_options;
  59. /* Stuff stolen from the kernel's fb.h */
  60. #define FB_ACTIVATE_ALL 64
  61. enum {
  62. FBIOGET_VSCREENINFO = 0x4600,
  63. FBIOPUT_VSCREENINFO = 0x4601
  64. };
  65. struct fb_bitfield {
  66. uint32_t offset; /* beginning of bitfield */
  67. uint32_t length; /* length of bitfield */
  68. uint32_t msb_right; /* !=0: Most significant bit is right */
  69. };
  70. struct fb_var_screeninfo {
  71. uint32_t xres; /* visible resolution */
  72. uint32_t yres;
  73. uint32_t xres_virtual; /* virtual resolution */
  74. uint32_t yres_virtual;
  75. uint32_t xoffset; /* offset from virtual to visible */
  76. uint32_t yoffset; /* resolution */
  77. uint32_t bits_per_pixel;
  78. uint32_t grayscale; /* !=0 Graylevels instead of colors */
  79. struct fb_bitfield red; /* bitfield in fb mem if true color, */
  80. struct fb_bitfield green; /* else only length is significant */
  81. struct fb_bitfield blue;
  82. struct fb_bitfield transp; /* transparency */
  83. uint32_t nonstd; /* !=0 Non standard pixel format */
  84. uint32_t activate; /* see FB_ACTIVATE_x */
  85. uint32_t height; /* height of picture in mm */
  86. uint32_t width; /* width of picture in mm */
  87. uint32_t accel_flags; /* acceleration flags (hints) */
  88. /* Timing: All values in pixclocks, except pixclock (of course) */
  89. uint32_t pixclock; /* pixel clock in ps (pico seconds) */
  90. uint32_t left_margin; /* time from sync to picture */
  91. uint32_t right_margin; /* time from picture to sync */
  92. uint32_t upper_margin; /* time from sync to picture */
  93. uint32_t lower_margin;
  94. uint32_t hsync_len; /* length of horizontal sync */
  95. uint32_t vsync_len; /* length of vertical sync */
  96. uint32_t sync; /* see FB_SYNC_x */
  97. uint32_t vmode; /* see FB_VMODE_x */
  98. uint32_t reserved[6]; /* Reserved for future compatibility */
  99. };
  100. static const struct cmdoptions_t {
  101. const char name[10];
  102. const unsigned char param_count;
  103. const unsigned char code;
  104. } g_cmdoptions[] = {
  105. { "-fb", 1, CMD_FB },
  106. { "-db", 1, CMD_DB },
  107. { "-a", 0, CMD_ALL },
  108. { "-i", 0, CMD_INFO },
  109. { "-g", 5, CMD_GEOMETRY },
  110. { "-t", 7, CMD_TIMING },
  111. { "-accel", 1, CMD_ACCEL },
  112. { "-hsync", 1, CMD_HSYNC },
  113. { "-vsync", 1, CMD_VSYNC },
  114. { "-laced", 1, CMD_LACED },
  115. { "-double", 1, CMD_DOUBLE },
  116. { "-n", 0, CMD_CHANGE },
  117. #if ENABLE_FEATURE_FBSET_FANCY
  118. { "-all", 0, CMD_ALL },
  119. { "-xres", 1, CMD_XRES },
  120. { "-yres", 1, CMD_YRES },
  121. { "-vxres", 1, CMD_VXRES },
  122. { "-vyres", 1, CMD_VYRES },
  123. { "-depth", 1, CMD_DEPTH },
  124. { "-match", 0, CMD_MATCH },
  125. { "-geometry", 5, CMD_GEOMETRY },
  126. { "-pixclock", 1, CMD_PIXCLOCK },
  127. { "-left", 1, CMD_LEFT },
  128. { "-right", 1, CMD_RIGHT },
  129. { "-upper", 1, CMD_UPPER },
  130. { "-lower", 1, CMD_LOWER },
  131. { "-hslen", 1, CMD_HSLEN },
  132. { "-vslen", 1, CMD_VSLEN },
  133. { "-timings", 7, CMD_TIMING },
  134. { "-csync", 1, CMD_CSYNC },
  135. { "-gsync", 1, CMD_GSYNC },
  136. { "-extsync", 1, CMD_EXTSYNC },
  137. { "-bcast", 1, CMD_BCAST },
  138. { "-rgba", 1, CMD_RGBA },
  139. { "-step", 1, CMD_STEP },
  140. { "-move", 1, CMD_MOVE },
  141. #endif
  142. { "", 0, 0 }
  143. };
  144. #if ENABLE_FEATURE_FBSET_READMODE
  145. /* taken from linux/fb.h */
  146. enum {
  147. FB_VMODE_INTERLACED = 1, /* interlaced */
  148. FB_VMODE_DOUBLE = 2, /* double scan */
  149. FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
  150. FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
  151. FB_SYNC_EXT = 4, /* external sync */
  152. FB_SYNC_COMP_HIGH_ACT = 8 /* composite sync high active */
  153. };
  154. #endif
  155. #if ENABLE_FEATURE_FBSET_READMODE
  156. static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
  157. {
  158. if (strstr(buf, what))
  159. *x &= ~flag;
  160. else
  161. *x |= flag;
  162. }
  163. static int readmode(struct fb_var_screeninfo *base, const char *fn,
  164. const char *mode)
  165. {
  166. FILE *f;
  167. char buf[256];
  168. char *p = buf;
  169. f = xfopen_for_read(fn);
  170. while (fgets(buf, sizeof(buf), f)) {
  171. p = strstr(buf, "mode ");
  172. if (!p && !(p = strstr(buf, "mode\t")))
  173. continue;
  174. p = strstr(p + 5, mode);
  175. if (!p)
  176. continue;
  177. p += strlen(mode);
  178. if (!isspace(*p) && (*p != 0) && (*p != '"')
  179. && (*p != '\r') && (*p != '\n')
  180. ) {
  181. continue; /* almost, but not quite */
  182. }
  183. while (fgets(buf, sizeof(buf), f)) {
  184. if ((p = strstr(buf, "geometry "))) {
  185. p += 9;
  186. /* FIXME: catastrophic on arches with 64bit ints */
  187. sscanf(p, "%d %d %d %d %d",
  188. &(base->xres), &(base->yres),
  189. &(base->xres_virtual), &(base->yres_virtual),
  190. &(base->bits_per_pixel));
  191. } else if ((p = strstr(buf, "timings "))) {
  192. p += 8;
  193. sscanf(p, "%d %d %d %d %d %d %d",
  194. &(base->pixclock),
  195. &(base->left_margin), &(base->right_margin),
  196. &(base->upper_margin), &(base->lower_margin),
  197. &(base->hsync_len), &(base->vsync_len));
  198. } else if ((p = strstr(buf, "laced "))) {
  199. //p += 6;
  200. ss(&base->vmode, FB_VMODE_INTERLACED, buf, "false");
  201. } else if ((p = strstr(buf, "double "))) {
  202. //p += 7;
  203. ss(&base->vmode, FB_VMODE_DOUBLE, buf, "false");
  204. } else if ((p = strstr(buf, "vsync "))) {
  205. //p += 6;
  206. ss(&base->sync, FB_SYNC_VERT_HIGH_ACT, buf, "low");
  207. } else if ((p = strstr(buf, "hsync "))) {
  208. //p += 6;
  209. ss(&base->sync, FB_SYNC_HOR_HIGH_ACT, buf, "low");
  210. } else if ((p = strstr(buf, "csync "))) {
  211. //p += 6;
  212. ss(&base->sync, FB_SYNC_COMP_HIGH_ACT, buf, "low");
  213. } else if ((p = strstr(buf, "extsync "))) {
  214. //p += 8;
  215. ss(&base->sync, FB_SYNC_EXT, buf, "false");
  216. }
  217. if (strstr(buf, "endmode"))
  218. return 1;
  219. }
  220. }
  221. return 0;
  222. }
  223. #endif
  224. static void setmode(struct fb_var_screeninfo *base,
  225. struct fb_var_screeninfo *set)
  226. {
  227. if ((int32_t) set->xres > 0)
  228. base->xres = set->xres;
  229. if ((int32_t) set->yres > 0)
  230. base->yres = set->yres;
  231. if ((int32_t) set->xres_virtual > 0)
  232. base->xres_virtual = set->xres_virtual;
  233. if ((int32_t) set->yres_virtual > 0)
  234. base->yres_virtual = set->yres_virtual;
  235. if ((int32_t) set->bits_per_pixel > 0)
  236. base->bits_per_pixel = set->bits_per_pixel;
  237. }
  238. static void showmode(struct fb_var_screeninfo *v)
  239. {
  240. double drate = 0, hrate = 0, vrate = 0;
  241. if (v->pixclock) {
  242. drate = 1e12 / v->pixclock;
  243. hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
  244. vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
  245. }
  246. printf("\nmode \"%ux%u-%u\"\n"
  247. #if ENABLE_FEATURE_FBSET_FANCY
  248. "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
  249. #endif
  250. "\tgeometry %u %u %u %u %u\n"
  251. "\ttimings %u %u %u %u %u %u %u\n"
  252. "\taccel %s\n"
  253. "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
  254. "endmode\n\n",
  255. v->xres, v->yres, (int) (vrate + 0.5),
  256. #if ENABLE_FEATURE_FBSET_FANCY
  257. drate / 1e6, hrate / 1e3, vrate,
  258. #endif
  259. v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
  260. v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
  261. v->hsync_len, v->vsync_len,
  262. (v->accel_flags > 0 ? "true" : "false"),
  263. v->red.length, v->red.offset, v->green.length, v->green.offset,
  264. v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
  265. }
  266. int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  267. int fbset_main(int argc, char **argv)
  268. {
  269. struct fb_var_screeninfo var, varset;
  270. int fh, i;
  271. const char *fbdev = DEFAULTFBDEV;
  272. const char *modefile = DEFAULTFBMODE;
  273. char *thisarg, *mode = NULL;
  274. memset(&varset, 0xFF, sizeof(varset));
  275. /* parse cmd args.... why do they have to make things so difficult? */
  276. argv++;
  277. argc--;
  278. for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
  279. for (i = 0; g_cmdoptions[i].name[0]; i++) {
  280. if (strcmp(thisarg, g_cmdoptions[i].name))
  281. continue;
  282. if (argc-1 < g_cmdoptions[i].param_count)
  283. bb_show_usage();
  284. switch (g_cmdoptions[i].code) {
  285. case CMD_FB:
  286. fbdev = argv[1];
  287. break;
  288. case CMD_DB:
  289. modefile = argv[1];
  290. break;
  291. case CMD_GEOMETRY:
  292. varset.xres = xatou32(argv[1]);
  293. varset.yres = xatou32(argv[2]);
  294. varset.xres_virtual = xatou32(argv[3]);
  295. varset.yres_virtual = xatou32(argv[4]);
  296. varset.bits_per_pixel = xatou32(argv[5]);
  297. break;
  298. case CMD_TIMING:
  299. varset.pixclock = xatou32(argv[1]);
  300. varset.left_margin = xatou32(argv[2]);
  301. varset.right_margin = xatou32(argv[3]);
  302. varset.upper_margin = xatou32(argv[4]);
  303. varset.lower_margin = xatou32(argv[5]);
  304. varset.hsync_len = xatou32(argv[6]);
  305. varset.vsync_len = xatou32(argv[7]);
  306. break;
  307. case CMD_ALL:
  308. g_options |= OPT_ALL;
  309. break;
  310. case CMD_CHANGE:
  311. g_options |= OPT_CHANGE;
  312. break;
  313. #if ENABLE_FEATURE_FBSET_FANCY
  314. case CMD_XRES:
  315. varset.xres = xatou32(argv[1]);
  316. break;
  317. case CMD_YRES:
  318. varset.yres = xatou32(argv[1]);
  319. break;
  320. case CMD_DEPTH:
  321. varset.bits_per_pixel = xatou32(argv[1]);
  322. break;
  323. #endif
  324. }
  325. argc -= g_cmdoptions[i].param_count;
  326. argv += g_cmdoptions[i].param_count;
  327. break;
  328. }
  329. if (!g_cmdoptions[i].name[0]) {
  330. if (argc != 1)
  331. bb_show_usage();
  332. mode = *argv;
  333. g_options |= OPT_READMODE;
  334. }
  335. }
  336. fh = xopen(fbdev, O_RDONLY);
  337. xioctl(fh, FBIOGET_VSCREENINFO, &var);
  338. if (g_options & OPT_READMODE) {
  339. #if !ENABLE_FEATURE_FBSET_READMODE
  340. bb_show_usage();
  341. #else
  342. if (!readmode(&var, modefile, mode)) {
  343. bb_error_msg_and_die("unknown video mode '%s'", mode);
  344. }
  345. #endif
  346. }
  347. setmode(&var, &varset);
  348. if (g_options & OPT_CHANGE) {
  349. if (g_options & OPT_ALL)
  350. var.activate = FB_ACTIVATE_ALL;
  351. xioctl(fh, FBIOPUT_VSCREENINFO, &var);
  352. }
  353. showmode(&var);
  354. /* Don't close the file, as exiting will take care of that */
  355. /* close(fh); */
  356. return EXIT_SUCCESS;
  357. }