video.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/video.h>
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. GRUB_EXPORT(grub_video_adapter_list);
  24. GRUB_EXPORT(grub_video_blit_bitmap);
  25. GRUB_EXPORT(grub_video_blit_render_target);
  26. GRUB_EXPORT(grub_video_create_render_target);
  27. GRUB_EXPORT(grub_video_delete_render_target);
  28. GRUB_EXPORT(grub_video_fill_rect);
  29. GRUB_EXPORT(grub_video_get_active_render_target);
  30. GRUB_EXPORT(grub_video_get_blit_format);
  31. GRUB_EXPORT(grub_video_get_info);
  32. GRUB_EXPORT(grub_video_get_info_and_fini);
  33. GRUB_EXPORT(grub_video_set_viewport);
  34. GRUB_EXPORT(grub_video_get_viewport);
  35. GRUB_EXPORT(grub_video_map_color);
  36. GRUB_EXPORT(grub_video_map_rgb);
  37. GRUB_EXPORT(grub_video_map_rgba);
  38. GRUB_EXPORT(grub_video_restore);
  39. GRUB_EXPORT(grub_video_scroll);
  40. GRUB_EXPORT(grub_video_swap_buffers);
  41. GRUB_EXPORT(grub_video_create_render_target);
  42. GRUB_EXPORT(grub_video_delete_render_target);
  43. GRUB_EXPORT(grub_video_set_active_render_target);
  44. GRUB_EXPORT(grub_video_set_mode);
  45. GRUB_EXPORT(grub_video_unmap_color);
  46. GRUB_EXPORT(grub_video_update_rect);
  47. GRUB_EXPORT(grub_video_get_driver_id);
  48. GRUB_EXPORT(grub_video_get_palette);
  49. /* The list of video adapters registered to system. */
  50. grub_video_adapter_t grub_video_adapter_list;
  51. /* Active video adapter. */
  52. static grub_video_adapter_t grub_video_adapter_active;
  53. /* Restore back to initial mode (where applicable). */
  54. grub_err_t
  55. grub_video_restore (void)
  56. {
  57. if (grub_video_adapter_active)
  58. {
  59. grub_video_adapter_active->fini ();
  60. if (grub_errno != GRUB_ERR_NONE)
  61. return grub_errno;
  62. grub_video_adapter_active = 0;
  63. }
  64. return GRUB_ERR_NONE;
  65. }
  66. /* Get information about active video mode. */
  67. grub_err_t
  68. grub_video_get_info (struct grub_video_mode_info *mode_info)
  69. {
  70. if (! grub_video_adapter_active)
  71. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  72. /* If mode_info is NULL just report that video adapter is active. */
  73. if (! mode_info)
  74. {
  75. grub_errno = GRUB_ERR_NONE;
  76. return grub_errno;
  77. }
  78. return grub_video_adapter_active->get_info (mode_info);
  79. }
  80. grub_video_driver_id_t
  81. grub_video_get_driver_id (void)
  82. {
  83. if (! grub_video_adapter_active)
  84. return GRUB_VIDEO_DRIVER_NONE;
  85. return grub_video_adapter_active->id;
  86. }
  87. /* Get information about active video mode. */
  88. grub_err_t
  89. grub_video_get_info_and_fini (struct grub_video_mode_info *mode_info,
  90. void **framebuffer)
  91. {
  92. grub_err_t err;
  93. if (! grub_video_adapter_active)
  94. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  95. err = grub_video_adapter_active->get_info_and_fini (mode_info, framebuffer);
  96. if (err)
  97. return err;
  98. grub_video_adapter_active = 0;
  99. return GRUB_ERR_NONE;
  100. }
  101. /* Determine optimized blitting formation for specified video mode info. */
  102. enum grub_video_blit_format
  103. grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
  104. {
  105. /* Check if we have any known 32 bit modes. */
  106. if (mode_info->bpp == 32)
  107. {
  108. if ((mode_info->red_mask_size == 8)
  109. && (mode_info->red_field_pos == 16)
  110. && (mode_info->green_mask_size == 8)
  111. && (mode_info->green_field_pos == 8)
  112. && (mode_info->blue_mask_size == 8)
  113. && (mode_info->blue_field_pos == 0))
  114. {
  115. return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
  116. }
  117. else if ((mode_info->red_mask_size == 8)
  118. && (mode_info->red_field_pos == 0)
  119. && (mode_info->green_mask_size == 8)
  120. && (mode_info->green_field_pos == 8)
  121. && (mode_info->blue_mask_size == 8)
  122. && (mode_info->blue_field_pos == 16))
  123. {
  124. return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
  125. }
  126. }
  127. /* Check if we have any known 24 bit modes. */
  128. else if (mode_info->bpp == 24)
  129. {
  130. if ((mode_info->red_mask_size == 8)
  131. && (mode_info->red_field_pos == 16)
  132. && (mode_info->green_mask_size == 8)
  133. && (mode_info->green_field_pos == 8)
  134. && (mode_info->blue_mask_size == 8)
  135. && (mode_info->blue_field_pos == 0))
  136. {
  137. return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
  138. }
  139. else if ((mode_info->red_mask_size == 8)
  140. && (mode_info->red_field_pos == 0)
  141. && (mode_info->green_mask_size == 8)
  142. && (mode_info->green_field_pos == 8)
  143. && (mode_info->blue_mask_size == 8)
  144. && (mode_info->blue_field_pos == 16))
  145. {
  146. return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
  147. }
  148. }
  149. /* Check if we have any known 16 bit modes. */
  150. else if (mode_info->bpp == 16)
  151. {
  152. if ((mode_info->red_mask_size == 5)
  153. && (mode_info->red_field_pos == 11)
  154. && (mode_info->green_mask_size == 6)
  155. && (mode_info->green_field_pos == 5)
  156. && (mode_info->blue_mask_size == 5)
  157. && (mode_info->blue_field_pos == 0))
  158. {
  159. return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
  160. }
  161. else if ((mode_info->red_mask_size == 5)
  162. && (mode_info->red_field_pos == 0)
  163. && (mode_info->green_mask_size == 6)
  164. && (mode_info->green_field_pos == 5)
  165. && (mode_info->blue_mask_size == 5)
  166. && (mode_info->blue_field_pos == 11))
  167. {
  168. return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
  169. }
  170. }
  171. else if (mode_info->bpp == 1)
  172. return GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
  173. /* Backup route. Unknown format. */
  174. /* If there are more than 8 bits per color, assume RGB(A) mode. */
  175. if (mode_info->bpp > 8)
  176. {
  177. if (mode_info->reserved_mask_size > 0)
  178. {
  179. return GRUB_VIDEO_BLIT_FORMAT_RGBA;
  180. }
  181. else
  182. {
  183. return GRUB_VIDEO_BLIT_FORMAT_RGB;
  184. }
  185. }
  186. /* Assume as indexcolor mode. */
  187. return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
  188. }
  189. /* Set new indexed color palette entries. */
  190. grub_err_t
  191. grub_video_set_palette (unsigned int start, unsigned int count,
  192. struct grub_video_palette_data *palette_data)
  193. {
  194. if (! grub_video_adapter_active)
  195. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  196. return grub_video_adapter_active->set_palette (start, count, palette_data);
  197. }
  198. /* Get indexed color palette entries. */
  199. grub_err_t
  200. grub_video_get_palette (unsigned int start, unsigned int count,
  201. struct grub_video_palette_data *palette_data)
  202. {
  203. if (! grub_video_adapter_active)
  204. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  205. return grub_video_adapter_active->get_palette (start, count, palette_data);
  206. }
  207. /* Set viewport dimensions. */
  208. grub_err_t
  209. grub_video_set_viewport (unsigned int x, unsigned int y,
  210. unsigned int width, unsigned int height)
  211. {
  212. if (! grub_video_adapter_active)
  213. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  214. return grub_video_adapter_active->set_viewport (x, y, width, height);
  215. }
  216. /* Get viewport dimensions. */
  217. grub_err_t
  218. grub_video_get_viewport (unsigned int *x, unsigned int *y,
  219. unsigned int *width, unsigned int *height)
  220. {
  221. if (! grub_video_adapter_active)
  222. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  223. return grub_video_adapter_active->get_viewport (x, y, width, height);
  224. }
  225. /* Map color name to adapter specific color. */
  226. grub_video_color_t
  227. grub_video_map_color (grub_uint32_t color_name)
  228. {
  229. if (! grub_video_adapter_active)
  230. return 0;
  231. return grub_video_adapter_active->map_color (color_name);
  232. }
  233. /* Map RGB value to adapter specific color. */
  234. grub_video_color_t
  235. grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
  236. {
  237. if (! grub_video_adapter_active)
  238. return 0;
  239. return grub_video_adapter_active->map_rgb (red, green, blue);
  240. }
  241. /* Map RGBA value to adapter specific color. */
  242. grub_video_color_t
  243. grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
  244. grub_uint8_t alpha)
  245. {
  246. if (! grub_video_adapter_active)
  247. return 0;
  248. return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
  249. }
  250. /* Unmap video color back to RGBA components. */
  251. grub_err_t
  252. grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
  253. grub_uint8_t *green, grub_uint8_t *blue,
  254. grub_uint8_t *alpha)
  255. {
  256. if (! grub_video_adapter_active)
  257. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  258. return grub_video_adapter_active->unmap_color (color,
  259. red,
  260. green,
  261. blue,
  262. alpha);
  263. }
  264. /* Fill rectangle using specified color. */
  265. grub_err_t
  266. grub_video_fill_rect (grub_video_color_t color, int x, int y,
  267. unsigned int width, unsigned int height)
  268. {
  269. if (! grub_video_adapter_active)
  270. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  271. return grub_video_adapter_active->fill_rect (color, x, y, width, height);
  272. }
  273. /* Blit bitmap to screen. */
  274. grub_err_t
  275. grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
  276. enum grub_video_blit_operators oper,
  277. int x, int y, int offset_x, int offset_y,
  278. unsigned int width, unsigned int height)
  279. {
  280. if (! grub_video_adapter_active)
  281. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  282. return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
  283. offset_x, offset_y,
  284. width, height);
  285. }
  286. /* Blit render target to active render target. */
  287. grub_err_t
  288. grub_video_blit_render_target (struct grub_video_render_target *target,
  289. enum grub_video_blit_operators oper,
  290. int x, int y, int offset_x, int offset_y,
  291. unsigned int width, unsigned int height)
  292. {
  293. if (! grub_video_adapter_active)
  294. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  295. return grub_video_adapter_active->blit_render_target (target, oper, x, y,
  296. offset_x, offset_y,
  297. width, height);
  298. }
  299. /* Scroll viewport and fill new areas with specified color. */
  300. grub_err_t
  301. grub_video_scroll (grub_video_color_t color, int dx, int dy)
  302. {
  303. if (! grub_video_adapter_active)
  304. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  305. return grub_video_adapter_active->scroll (color, dx, dy);
  306. }
  307. /* Swap buffers (swap active render target). */
  308. grub_err_t
  309. grub_video_swap_buffers (void)
  310. {
  311. if (! grub_video_adapter_active)
  312. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  313. return grub_video_adapter_active->swap_buffers ();
  314. }
  315. /* Create new render target. */
  316. grub_err_t
  317. grub_video_create_render_target (struct grub_video_render_target **result,
  318. unsigned int width, unsigned int height,
  319. unsigned int mode_type)
  320. {
  321. if (! grub_video_adapter_active)
  322. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  323. return grub_video_adapter_active->create_render_target (result,
  324. width, height,
  325. mode_type);
  326. }
  327. /* Delete render target. */
  328. grub_err_t
  329. grub_video_delete_render_target (struct grub_video_render_target *target)
  330. {
  331. if (! grub_video_adapter_active)
  332. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  333. return grub_video_adapter_active->delete_render_target (target);
  334. }
  335. /* Set active render target. */
  336. grub_err_t
  337. grub_video_set_active_render_target (struct grub_video_render_target *target)
  338. {
  339. if (! grub_video_adapter_active)
  340. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  341. return grub_video_adapter_active->set_active_render_target (target);
  342. }
  343. /* Get active render target. */
  344. grub_err_t
  345. grub_video_get_active_render_target (struct grub_video_render_target **target)
  346. {
  347. if (! grub_video_adapter_active)
  348. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  349. return grub_video_adapter_active->get_active_render_target (target);
  350. }
  351. /* Parse <width>x<height>[x<depth>]*/
  352. static grub_err_t
  353. parse_modespec (const char *current_mode, int *width, int *height, int *depth)
  354. {
  355. const char *value;
  356. const char *param = current_mode;
  357. *width = *height = *depth = -1;
  358. if (grub_strcmp (param, "auto") == 0)
  359. {
  360. *width = *height = 0;
  361. return GRUB_ERR_NONE;
  362. }
  363. /* Find width value. */
  364. value = param;
  365. param = grub_strchr(param, 'x');
  366. if (param == NULL)
  367. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  368. "Invalid mode: %s\n",
  369. current_mode);
  370. param++;
  371. *width = grub_strtoul (value, 0, 0);
  372. if (grub_errno != GRUB_ERR_NONE)
  373. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  374. "Invalid mode: %s\n",
  375. current_mode);
  376. /* Find height value. */
  377. value = param;
  378. param = grub_strchr(param, 'x');
  379. if (param == NULL)
  380. {
  381. *height = grub_strtoul (value, 0, 0);
  382. if (grub_errno != GRUB_ERR_NONE)
  383. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  384. "Invalid mode: %s\n",
  385. current_mode);
  386. }
  387. else
  388. {
  389. /* We have optional color depth value. */
  390. param++;
  391. *height = grub_strtoul (value, 0, 0);
  392. if (grub_errno != GRUB_ERR_NONE)
  393. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  394. "Invalid mode: %s\n",
  395. current_mode);
  396. /* Convert color depth value. */
  397. value = param;
  398. *depth = grub_strtoul (value, 0, 0);
  399. if (grub_errno != GRUB_ERR_NONE)
  400. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  401. "Invalid mode: %s\n",
  402. current_mode);
  403. }
  404. return GRUB_ERR_NONE;
  405. }
  406. grub_err_t
  407. grub_video_set_mode (const char *modestring,
  408. unsigned int modemask,
  409. unsigned int modevalue)
  410. {
  411. char *tmp;
  412. char *next_mode;
  413. char *current_mode;
  414. char *modevar;
  415. modevalue &= modemask;
  416. /* Take copy of env.var. as we don't want to modify that. */
  417. modevar = grub_strdup (modestring);
  418. /* Initialize next mode. */
  419. next_mode = modevar;
  420. if (! modevar)
  421. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  422. "couldn't allocate space for local modevar copy");
  423. if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
  424. || grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
  425. || grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
  426. {
  427. int suitable = 1;
  428. grub_err_t err;
  429. if (grub_video_adapter_active)
  430. {
  431. struct grub_video_mode_info mode_info;
  432. grub_memset (&mode_info, 0, sizeof (mode_info));
  433. err = grub_video_get_info (&mode_info);
  434. if (err)
  435. {
  436. suitable = 0;
  437. grub_errno = GRUB_ERR_NONE;
  438. }
  439. if ((mode_info.mode_type & modemask) != modevalue)
  440. suitable = 0;
  441. }
  442. else if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
  443. && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
  444. suitable = 0;
  445. if (suitable)
  446. {
  447. grub_free (modevar);
  448. return GRUB_ERR_NONE;
  449. }
  450. next_mode += sizeof ("keep") - 1;
  451. if (! *next_mode)
  452. {
  453. grub_free (modevar);
  454. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  455. "no suitable mode found");
  456. }
  457. /* Skip separator. */
  458. next_mode++;
  459. }
  460. /* De-activate last set video adapter. */
  461. if (grub_video_adapter_active)
  462. {
  463. /* Finalize adapter. */
  464. grub_video_adapter_active->fini ();
  465. if (grub_errno != GRUB_ERR_NONE)
  466. grub_errno = GRUB_ERR_NONE;
  467. /* Mark active adapter as not set. */
  468. grub_video_adapter_active = 0;
  469. }
  470. /* Loop until all modes has been tested out. */
  471. while (next_mode != NULL)
  472. {
  473. int width = -1;
  474. int height = -1;
  475. int depth = -1;
  476. grub_err_t err;
  477. unsigned int flags = modevalue;
  478. unsigned int flagmask = modemask;
  479. /* Use last next_mode as current mode. */
  480. tmp = next_mode;
  481. /* Save position of next mode and separate modes. */
  482. for (; *next_mode; next_mode++)
  483. if (*next_mode == ',' || *next_mode == ';')
  484. break;
  485. if (*next_mode)
  486. {
  487. *next_mode = 0;
  488. next_mode++;
  489. }
  490. else
  491. next_mode = 0;
  492. /* Skip whitespace. */
  493. while (grub_isspace (*tmp))
  494. tmp++;
  495. /* Initialize token holders. */
  496. current_mode = tmp;
  497. /* XXX: we assume that we're in pure text mode if
  498. no video mode is initialized. Is it always true? */
  499. if (grub_strcmp (current_mode, "text") == 0)
  500. {
  501. struct grub_video_mode_info mode_info;
  502. grub_memset (&mode_info, 0, sizeof (mode_info));
  503. if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) == 0)
  504. || ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) != 0))
  505. {
  506. /* Valid mode found from adapter, and it has been activated.
  507. Specify it as active adapter. */
  508. grub_video_adapter_active = NULL;
  509. /* Free memory. */
  510. grub_free (modevar);
  511. return GRUB_ERR_NONE;
  512. }
  513. }
  514. err = parse_modespec (current_mode, &width, &height, &depth);
  515. if (err)
  516. {
  517. /* Free memory before returning. */
  518. grub_free (modevar);
  519. return err;
  520. }
  521. /* Try out video mode. */
  522. /* If user requested specific depth check if this depth is supported. */
  523. if (depth != -1 && (flagmask & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  524. &&
  525. (((flags & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  526. != (((unsigned) depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
  527. & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK))))
  528. continue;
  529. if (depth != -1)
  530. {
  531. flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
  532. & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  533. flagmask |= GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  534. }
  535. /* Try to initialize requested mode. Ignore any errors. */
  536. grub_video_adapter_t p;
  537. /* Loop thru all possible video adapter trying to find requested mode. */
  538. for (p = grub_video_adapter_list; p; p = p->next)
  539. {
  540. struct grub_video_mode_info mode_info;
  541. grub_memset (&mode_info, 0, sizeof (mode_info));
  542. /* Try to initialize adapter, if it fails, skip to next adapter. */
  543. err = p->init ();
  544. if (err != GRUB_ERR_NONE)
  545. {
  546. grub_errno = GRUB_ERR_NONE;
  547. continue;
  548. }
  549. /* Try to initialize video mode. */
  550. err = p->setup (width, height, flags, flagmask);
  551. if (err != GRUB_ERR_NONE)
  552. {
  553. p->fini ();
  554. grub_errno = GRUB_ERR_NONE;
  555. continue;
  556. }
  557. err = p->get_info (&mode_info);
  558. if (err != GRUB_ERR_NONE)
  559. {
  560. p->fini ();
  561. grub_errno = GRUB_ERR_NONE;
  562. continue;
  563. }
  564. flags = mode_info.mode_type & ~GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  565. flags |= (mode_info.bpp << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
  566. & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  567. /* Check that mode is suitable for upper layer. */
  568. if ((flags & GRUB_VIDEO_MODE_TYPE_PURE_TEXT)
  569. ? (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
  570. && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
  571. : ((flags & modemask) != modevalue))
  572. {
  573. p->fini ();
  574. grub_errno = GRUB_ERR_NONE;
  575. continue;
  576. }
  577. /* Valid mode found from adapter, and it has been activated.
  578. Specify it as active adapter. */
  579. grub_video_adapter_active = p;
  580. /* Free memory. */
  581. grub_free (modevar);
  582. return GRUB_ERR_NONE;
  583. }
  584. }
  585. /* Free memory. */
  586. grub_free (modevar);
  587. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  588. "no suitable mode found");
  589. }
  590. void
  591. grub_video_update_rect (int x, int y, int width, int height)
  592. {
  593. if ((grub_video_adapter_active) && (grub_video_adapter_active->update_rect))
  594. grub_video_adapter_active->update_rect (x, y, width, height);
  595. }
  596. /* Initialize Video API module. */
  597. GRUB_MOD_INIT(video)
  598. {
  599. }
  600. /* Finalize Video API module. */
  601. GRUB_MOD_FINI(video)
  602. {
  603. }