fbset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 int readmode(struct fb_var_screeninfo *base, const char *fn,
  157. const char *mode)
  158. {
  159. FILE *f;
  160. char buf[256];
  161. char *p = buf;
  162. f = xfopen(fn, "r");
  163. while (!feof(f)) {
  164. fgets(buf, sizeof(buf), f);
  165. p = strstr(buf, "mode ");
  166. if (!p && !(p = strstr(buf, "mode\t")))
  167. continue;
  168. p = strstr(p + 5, mode);
  169. if (!p)
  170. continue;
  171. p += strlen(mode);
  172. if (!isspace(*p) && (*p != 0) && (*p != '"')
  173. && (*p != '\r') && (*p != '\n'))
  174. continue; /* almost, but not quite */
  175. while (!feof(f)) {
  176. fgets(buf, sizeof(buf), f);
  177. p = strstr(buf, "geometry ");
  178. if (p) {
  179. p += 9;
  180. /* FIXME: catastrophic on arches with 64bit ints */
  181. sscanf(p, "%d %d %d %d %d",
  182. &(base->xres), &(base->yres),
  183. &(base->xres_virtual), &(base->yres_virtual),
  184. &(base->bits_per_pixel));
  185. } else if ((p = strstr(buf, "timings "))) {
  186. p += 8;
  187. sscanf(p, "%d %d %d %d %d %d %d",
  188. &(base->pixclock),
  189. &(base->left_margin), &(base->right_margin),
  190. &(base->upper_margin), &(base->lower_margin),
  191. &(base->hsync_len), &(base->vsync_len));
  192. } else if ((p = strstr(buf, "laced "))) {
  193. //p += 6;
  194. if (strstr(buf, "false")) {
  195. base->vmode &= ~FB_VMODE_INTERLACED;
  196. } else {
  197. base->vmode |= FB_VMODE_INTERLACED;
  198. }
  199. } else if ((p = strstr(buf, "double "))) {
  200. //p += 7;
  201. if (strstr(buf, "false")) {
  202. base->vmode &= ~FB_VMODE_DOUBLE;
  203. } else {
  204. base->vmode |= FB_VMODE_DOUBLE;
  205. }
  206. } else if ((p = strstr(buf, "vsync "))) {
  207. //p += 6;
  208. if (strstr(buf, "low")) {
  209. base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
  210. } else {
  211. base->sync |= FB_SYNC_VERT_HIGH_ACT;
  212. }
  213. } else if ((p = strstr(buf, "hsync "))) {
  214. //p += 6;
  215. if (strstr(buf, "low")) {
  216. base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
  217. } else {
  218. base->sync |= FB_SYNC_HOR_HIGH_ACT;
  219. }
  220. } else if ((p = strstr(buf, "csync "))) {
  221. //p += 6;
  222. if (strstr(buf, "low")) {
  223. base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
  224. } else {
  225. base->sync |= FB_SYNC_COMP_HIGH_ACT;
  226. }
  227. } else if ((p = strstr(buf, "extsync "))) {
  228. //p += 8;
  229. if (strstr(buf, "false")) {
  230. base->sync &= ~FB_SYNC_EXT;
  231. } else {
  232. base->sync |= FB_SYNC_EXT;
  233. }
  234. }
  235. if (strstr(buf, "endmode"))
  236. return 1;
  237. }
  238. }
  239. return 0;
  240. }
  241. #endif
  242. static inline void setmode(struct fb_var_screeninfo *base,
  243. struct fb_var_screeninfo *set)
  244. {
  245. if ((int) set->xres > 0)
  246. base->xres = set->xres;
  247. if ((int) set->yres > 0)
  248. base->yres = set->yres;
  249. if ((int) set->xres_virtual > 0)
  250. base->xres_virtual = set->xres_virtual;
  251. if ((int) set->yres_virtual > 0)
  252. base->yres_virtual = set->yres_virtual;
  253. if ((int) set->bits_per_pixel > 0)
  254. base->bits_per_pixel = set->bits_per_pixel;
  255. }
  256. static inline void showmode(struct fb_var_screeninfo *v)
  257. {
  258. double drate = 0, hrate = 0, vrate = 0;
  259. if (v->pixclock) {
  260. drate = 1e12 / v->pixclock;
  261. hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
  262. vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
  263. }
  264. printf("\nmode \"%ux%u-%u\"\n"
  265. #if ENABLE_FEATURE_FBSET_FANCY
  266. "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
  267. #endif
  268. "\tgeometry %u %u %u %u %u\n"
  269. "\ttimings %u %u %u %u %u %u %u\n"
  270. "\taccel %s\n"
  271. "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
  272. "endmode\n\n",
  273. v->xres, v->yres, (int) (vrate + 0.5),
  274. #if ENABLE_FEATURE_FBSET_FANCY
  275. drate / 1e6, hrate / 1e3, vrate,
  276. #endif
  277. v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
  278. v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
  279. v->hsync_len, v->vsync_len,
  280. (v->accel_flags > 0 ? "true" : "false"),
  281. v->red.length, v->red.offset, v->green.length, v->green.offset,
  282. v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
  283. }
  284. #ifdef STANDALONE
  285. int main(int argc, char **argv)
  286. #else
  287. int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  288. int fbset_main(int argc, char **argv)
  289. #endif
  290. {
  291. struct fb_var_screeninfo var, varset;
  292. int fh, i;
  293. const char *fbdev = DEFAULTFBDEV;
  294. const char *modefile = DEFAULTFBMODE;
  295. char *thisarg, *mode = NULL;
  296. memset(&varset, 0xFF, sizeof(varset));
  297. /* parse cmd args.... why do they have to make things so difficult? */
  298. argv++;
  299. argc--;
  300. for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
  301. for (i = 0; g_cmdoptions[i].name[0]; i++) {
  302. if (strcmp(thisarg, g_cmdoptions[i].name))
  303. continue;
  304. if (argc-1 < g_cmdoptions[i].param_count)
  305. bb_show_usage();
  306. switch (g_cmdoptions[i].code) {
  307. case CMD_FB:
  308. fbdev = argv[1];
  309. break;
  310. case CMD_DB:
  311. modefile = argv[1];
  312. break;
  313. case CMD_GEOMETRY:
  314. varset.xres = xatou32(argv[1]);
  315. varset.yres = xatou32(argv[2]);
  316. varset.xres_virtual = xatou32(argv[3]);
  317. varset.yres_virtual = xatou32(argv[4]);
  318. varset.bits_per_pixel = xatou32(argv[5]);
  319. break;
  320. case CMD_TIMING:
  321. varset.pixclock = xatou32(argv[1]);
  322. varset.left_margin = xatou32(argv[2]);
  323. varset.right_margin = xatou32(argv[3]);
  324. varset.upper_margin = xatou32(argv[4]);
  325. varset.lower_margin = xatou32(argv[5]);
  326. varset.hsync_len = xatou32(argv[6]);
  327. varset.vsync_len = xatou32(argv[7]);
  328. break;
  329. case CMD_ALL:
  330. g_options |= OPT_ALL;
  331. break;
  332. case CMD_CHANGE:
  333. g_options |= OPT_CHANGE;
  334. break;
  335. #if ENABLE_FEATURE_FBSET_FANCY
  336. case CMD_XRES:
  337. varset.xres = xatou32(argv[1]);
  338. break;
  339. case CMD_YRES:
  340. varset.yres = xatou32(argv[1]);
  341. break;
  342. case CMD_DEPTH:
  343. varset.bits_per_pixel = xatou32(argv[1]);
  344. break;
  345. #endif
  346. }
  347. argc -= g_cmdoptions[i].param_count;
  348. argv += g_cmdoptions[i].param_count;
  349. break;
  350. }
  351. if (!g_cmdoptions[i].name[0]) {
  352. if (argc != 1)
  353. bb_show_usage();
  354. mode = *argv;
  355. g_options |= OPT_READMODE;
  356. }
  357. }
  358. fh = xopen(fbdev, O_RDONLY);
  359. xioctl(fh, FBIOGET_VSCREENINFO, &var);
  360. if (g_options & OPT_READMODE) {
  361. #if !ENABLE_FEATURE_FBSET_READMODE
  362. bb_show_usage();
  363. #else
  364. if (!readmode(&var, modefile, mode)) {
  365. bb_error_msg_and_die("unknown video mode '%s'", mode);
  366. }
  367. #endif
  368. }
  369. setmode(&var, &varset);
  370. if (g_options & OPT_CHANGE) {
  371. if (g_options & OPT_ALL)
  372. var.activate = FB_ACTIVATE_ALL;
  373. xioctl(fh, FBIOPUT_VSCREENINFO, &var);
  374. }
  375. showmode(&var);
  376. /* Don't close the file, as exiting will take care of that */
  377. /* close(fh); */
  378. return EXIT_SUCCESS;
  379. }