Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / video / video.c
blobf252663c108f2117cb8e14019d4774d4abb7b7dd
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/video.h>
20 #include <grub/types.h>
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/i18n.h>
26 GRUB_MOD_LICENSE ("GPLv3+");
28 /* The list of video adapters registered to system. */
29 grub_video_adapter_t grub_video_adapter_list = NULL;
31 /* Active video adapter. */
32 grub_video_adapter_t grub_video_adapter_active;
34 /* Restore back to initial mode (where applicable). */
35 grub_err_t
36 grub_video_restore (void)
38 if (grub_video_adapter_active)
40 grub_video_adapter_active->fini ();
41 if (grub_errno != GRUB_ERR_NONE)
42 return grub_errno;
44 grub_video_adapter_active = 0;
46 return GRUB_ERR_NONE;
49 /* Get information about active video mode. */
50 grub_err_t
51 grub_video_get_info (struct grub_video_mode_info *mode_info)
53 if (! grub_video_adapter_active)
54 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
56 /* If mode_info is NULL just report that video adapter is active. */
57 if (! mode_info)
59 grub_errno = GRUB_ERR_NONE;
60 return grub_errno;
63 return grub_video_adapter_active->get_info (mode_info);
66 grub_video_driver_id_t
67 grub_video_get_driver_id (void)
69 if (! grub_video_adapter_active)
70 return GRUB_VIDEO_DRIVER_NONE;
71 return grub_video_adapter_active->id;
74 /* Get information about active video mode. */
75 grub_err_t
76 grub_video_get_info_and_fini (struct grub_video_mode_info *mode_info,
77 void **framebuffer)
79 grub_err_t err;
81 if (! grub_video_adapter_active)
82 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
84 err = grub_video_adapter_active->get_info_and_fini (mode_info, framebuffer);
85 if (err)
86 return err;
88 grub_video_adapter_active = 0;
89 return GRUB_ERR_NONE;
92 /* Determine optimized blitting formation for specified video mode info. */
93 enum grub_video_blit_format
94 grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
96 /* Check if we have any known 32 bit modes. */
97 if (mode_info->bpp == 32)
99 if ((mode_info->red_mask_size == 8)
100 && (mode_info->red_field_pos == 16)
101 && (mode_info->green_mask_size == 8)
102 && (mode_info->green_field_pos == 8)
103 && (mode_info->blue_mask_size == 8)
104 && (mode_info->blue_field_pos == 0))
106 return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
108 else if ((mode_info->red_mask_size == 8)
109 && (mode_info->red_field_pos == 0)
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 == 16))
115 return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
118 /* Check if we have any known 24 bit modes. */
119 else if (mode_info->bpp == 24)
121 if ((mode_info->red_mask_size == 8)
122 && (mode_info->red_field_pos == 16)
123 && (mode_info->green_mask_size == 8)
124 && (mode_info->green_field_pos == 8)
125 && (mode_info->blue_mask_size == 8)
126 && (mode_info->blue_field_pos == 0))
128 return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
130 else if ((mode_info->red_mask_size == 8)
131 && (mode_info->red_field_pos == 0)
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 == 16))
137 return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
140 /* Check if we have any known 16 bit modes. */
141 else if (mode_info->bpp == 16)
143 if ((mode_info->red_mask_size == 5)
144 && (mode_info->red_field_pos == 11)
145 && (mode_info->green_mask_size == 6)
146 && (mode_info->green_field_pos == 5)
147 && (mode_info->blue_mask_size == 5)
148 && (mode_info->blue_field_pos == 0))
150 return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
152 else if ((mode_info->red_mask_size == 5)
153 && (mode_info->red_field_pos == 0)
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 == 11))
159 return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
162 else if (mode_info->bpp == 1)
163 return GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
165 /* Backup route. Unknown format. */
167 /* If there are more than 8 bits per color, assume RGB(A) mode. */
168 if (mode_info->bpp > 8)
170 if (mode_info->reserved_mask_size > 0)
172 return GRUB_VIDEO_BLIT_FORMAT_RGBA;
174 else
176 return GRUB_VIDEO_BLIT_FORMAT_RGB;
180 /* Assume as indexcolor mode. */
181 return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
184 /* Set new indexed color palette entries. */
185 grub_err_t
186 grub_video_set_palette (unsigned int start, unsigned int count,
187 struct grub_video_palette_data *palette_data)
189 if (! grub_video_adapter_active)
190 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
192 return grub_video_adapter_active->set_palette (start, count, palette_data);
195 /* Get indexed color palette entries. */
196 grub_err_t
197 grub_video_get_palette (unsigned int start, unsigned int count,
198 struct grub_video_palette_data *palette_data)
200 if (! grub_video_adapter_active)
201 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
203 return grub_video_adapter_active->get_palette (start, count, palette_data);
206 /* Set viewport dimensions. */
207 grub_err_t
208 grub_video_set_viewport (unsigned int x, unsigned int y,
209 unsigned int width, unsigned int height)
211 if (! grub_video_adapter_active)
212 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
214 return grub_video_adapter_active->set_viewport (x, y, width, height);
217 /* Get viewport dimensions. */
218 grub_err_t
219 grub_video_get_viewport (unsigned int *x, unsigned int *y,
220 unsigned int *width, unsigned int *height)
222 if (! grub_video_adapter_active)
223 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
225 return grub_video_adapter_active->get_viewport (x, y, width, height);
228 /* Set region dimensions. */
229 grub_err_t
230 grub_video_set_region (unsigned int x, unsigned int y,
231 unsigned int width, unsigned int height)
233 if (! grub_video_adapter_active)
234 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
236 return grub_video_adapter_active->set_region (x, y, width, height);
239 /* Get region dimensions. */
240 grub_err_t
241 grub_video_get_region (unsigned int *x, unsigned int *y,
242 unsigned int *width, unsigned int *height)
244 if (! grub_video_adapter_active)
245 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
247 return grub_video_adapter_active->get_region (x, y, width, height);
250 /* Set status of the intersection of the viewport and the region. */
251 grub_err_t
252 grub_video_set_area_status (grub_video_area_status_t area_status)
254 if (! grub_video_adapter_active)
255 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
257 return grub_video_adapter_active->set_area_status (area_status);
260 /* Get status of the intersection of the viewport and the region. */
261 grub_err_t
262 grub_video_get_area_status (grub_video_area_status_t *area_status)
264 if (! grub_video_adapter_active)
265 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
267 return grub_video_adapter_active->get_area_status (area_status);
270 /* Map color name to adapter specific color. */
271 grub_video_color_t
272 grub_video_map_color (grub_uint32_t color_name)
274 if (! grub_video_adapter_active)
275 return 0;
277 return grub_video_adapter_active->map_color (color_name);
280 /* Map RGB value to adapter specific color. */
281 grub_video_color_t
282 grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
284 if (! grub_video_adapter_active)
285 return 0;
287 return grub_video_adapter_active->map_rgb (red, green, blue);
290 /* Map RGBA value to adapter specific color. */
291 grub_video_color_t
292 grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
293 grub_uint8_t alpha)
295 if (! grub_video_adapter_active)
296 return 0;
298 return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
301 /* Unmap video color back to RGBA components. */
302 grub_err_t
303 grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
304 grub_uint8_t *green, grub_uint8_t *blue,
305 grub_uint8_t *alpha)
307 if (! grub_video_adapter_active)
308 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
310 return grub_video_adapter_active->unmap_color (color,
311 red,
312 green,
313 blue,
314 alpha);
317 /* Fill rectangle using specified color. */
318 grub_err_t
319 grub_video_fill_rect (grub_video_color_t color, int x, int y,
320 unsigned int width, unsigned int height)
322 if (! grub_video_adapter_active)
323 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
325 return grub_video_adapter_active->fill_rect (color, x, y, width, height);
328 /* Blit bitmap to screen. */
329 grub_err_t
330 grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
331 enum grub_video_blit_operators oper,
332 int x, int y, int offset_x, int offset_y,
333 unsigned int width, unsigned int height)
335 if (! grub_video_adapter_active)
336 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
338 return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
339 offset_x, offset_y,
340 width, height);
343 /* Blit render target to active render target. */
344 grub_err_t
345 grub_video_blit_render_target (struct grub_video_render_target *target,
346 enum grub_video_blit_operators oper,
347 int x, int y, int offset_x, int offset_y,
348 unsigned int width, unsigned int height)
350 if (! grub_video_adapter_active)
351 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
353 return grub_video_adapter_active->blit_render_target (target, oper, x, y,
354 offset_x, offset_y,
355 width, height);
358 /* Scroll viewport and fill new areas with specified color. */
359 grub_err_t
360 grub_video_scroll (grub_video_color_t color, int dx, int dy)
362 if (! grub_video_adapter_active)
363 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
365 return grub_video_adapter_active->scroll (color, dx, dy);
368 /* Swap buffers (swap active render target). */
369 grub_err_t
370 grub_video_swap_buffers (void)
372 if (! grub_video_adapter_active)
373 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
375 return grub_video_adapter_active->swap_buffers ();
378 /* Create new render target. */
379 grub_err_t
380 grub_video_create_render_target (struct grub_video_render_target **result,
381 unsigned int width, unsigned int height,
382 unsigned int mode_type)
384 *result = 0;
385 if (! grub_video_adapter_active)
386 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
388 return grub_video_adapter_active->create_render_target (result,
389 width, height,
390 mode_type);
393 /* Delete render target. */
394 grub_err_t
395 grub_video_delete_render_target (struct grub_video_render_target *target)
397 if (!target)
398 return GRUB_ERR_NONE;
399 if (! grub_video_adapter_active)
400 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
402 return grub_video_adapter_active->delete_render_target (target);
405 /* Set active render target. */
406 grub_err_t
407 grub_video_set_active_render_target (struct grub_video_render_target *target)
409 if (! grub_video_adapter_active)
410 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
412 return grub_video_adapter_active->set_active_render_target (target);
415 /* Get active render target. */
416 grub_err_t
417 grub_video_get_active_render_target (struct grub_video_render_target **target)
419 if (! grub_video_adapter_active)
420 return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
422 return grub_video_adapter_active->get_active_render_target (target);
425 grub_err_t
426 grub_video_edid_checksum (struct grub_video_edid_info *edid_info)
428 const char *edid_bytes = (const char *) edid_info;
429 int i;
430 char checksum = 0;
432 /* Check EDID checksum. */
433 for (i = 0; i < 128; ++i)
434 checksum += edid_bytes[i];
436 if (checksum != 0)
437 return grub_error (GRUB_ERR_BAD_DEVICE,
438 "invalid EDID checksum %d", checksum);
440 grub_errno = GRUB_ERR_NONE;
441 return grub_errno;
444 grub_err_t
445 grub_video_edid_preferred_mode (struct grub_video_edid_info *edid_info,
446 unsigned int *width, unsigned int *height)
448 /* Bit 1 in the Feature Support field indicates that the first
449 Detailed Timing Description is the preferred timing mode. */
450 if (edid_info->version == 1 /* we don't understand later versions */
451 && (edid_info->feature_support
452 & GRUB_VIDEO_EDID_FEATURE_PREFERRED_TIMING_MODE)
453 && edid_info->detailed_timings[0].pixel_clock)
455 *width = edid_info->detailed_timings[0].horizontal_active_lo
456 | (((unsigned int)
457 (edid_info->detailed_timings[0].horizontal_hi & 0xf0))
458 << 4);
459 *height = edid_info->detailed_timings[0].vertical_active_lo
460 | (((unsigned int)
461 (edid_info->detailed_timings[0].vertical_hi & 0xf0))
462 << 4);
463 if (*width && *height)
464 return GRUB_ERR_NONE;
467 return grub_error (GRUB_ERR_BAD_DEVICE, "no preferred mode available");
470 /* Parse <width>x<height>[x<depth>]*/
471 static grub_err_t
472 parse_modespec (const char *current_mode, int *width, int *height, int *depth)
474 const char *value;
475 const char *param = current_mode;
477 *width = *height = *depth = -1;
479 if (grub_strcmp (param, "auto") == 0)
481 *width = *height = 0;
482 return GRUB_ERR_NONE;
485 /* Find width value. */
486 value = param;
487 param = grub_strchr(param, 'x');
488 if (param == NULL)
489 return grub_error (GRUB_ERR_BAD_ARGUMENT,
490 N_("invalid video mode specification `%s'"),
491 current_mode);
493 param++;
495 *width = grub_strtoul (value, 0, 0);
496 if (grub_errno != GRUB_ERR_NONE)
497 return grub_error (GRUB_ERR_BAD_ARGUMENT,
498 N_("invalid video mode specification `%s'"),
499 current_mode);
501 /* Find height value. */
502 value = param;
503 param = grub_strchr(param, 'x');
504 if (param == NULL)
506 *height = grub_strtoul (value, 0, 0);
507 if (grub_errno != GRUB_ERR_NONE)
508 return grub_error (GRUB_ERR_BAD_ARGUMENT,
509 N_("invalid video mode specification `%s'"),
510 current_mode);
512 else
514 /* We have optional color depth value. */
515 param++;
517 *height = grub_strtoul (value, 0, 0);
518 if (grub_errno != GRUB_ERR_NONE)
519 return grub_error (GRUB_ERR_BAD_ARGUMENT,
520 N_("invalid video mode specification `%s'"),
521 current_mode);
523 /* Convert color depth value. */
524 value = param;
525 *depth = grub_strtoul (value, 0, 0);
526 if (grub_errno != GRUB_ERR_NONE)
527 return grub_error (GRUB_ERR_BAD_ARGUMENT,
528 N_("invalid video mode specification `%s'"),
529 current_mode);
531 return GRUB_ERR_NONE;
534 grub_err_t
535 grub_video_set_mode (const char *modestring,
536 unsigned int modemask,
537 unsigned int modevalue)
539 char *tmp;
540 char *next_mode;
541 char *current_mode;
542 char *modevar;
544 if (grub_video_adapter_active && grub_video_adapter_active->id == GRUB_VIDEO_ADAPTER_CAPTURE)
545 return GRUB_ERR_NONE;
547 modevalue &= modemask;
549 /* Take copy of env.var. as we don't want to modify that. */
550 modevar = grub_strdup (modestring);
552 /* Initialize next mode. */
553 next_mode = modevar;
555 if (! modevar)
556 return grub_errno;
558 if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
559 || grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
560 || grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
562 int suitable = 1;
563 grub_err_t err;
565 if (grub_video_adapter_active)
567 struct grub_video_mode_info mode_info;
568 grub_memset (&mode_info, 0, sizeof (mode_info));
569 err = grub_video_get_info (&mode_info);
570 if (err)
572 suitable = 0;
573 grub_errno = GRUB_ERR_NONE;
575 if ((mode_info.mode_type & modemask) != modevalue)
576 suitable = 0;
578 else if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
579 && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
580 suitable = 0;
582 if (suitable)
584 grub_free (modevar);
585 return GRUB_ERR_NONE;
587 next_mode += sizeof ("keep") - 1;
588 if (! *next_mode)
590 grub_free (modevar);
592 /* TRANSLATORS: This doesn't imply that there is no available video
593 mode at all. All modes may have been filtered out by some criteria.
595 return grub_error (GRUB_ERR_BAD_ARGUMENT,
596 N_("no suitable video mode found"));
599 /* Skip separator. */
600 next_mode++;
603 /* De-activate last set video adapter. */
604 if (grub_video_adapter_active)
606 /* Finalize adapter. */
607 grub_video_adapter_active->fini ();
608 if (grub_errno != GRUB_ERR_NONE)
609 grub_errno = GRUB_ERR_NONE;
611 /* Mark active adapter as not set. */
612 grub_video_adapter_active = 0;
615 /* Loop until all modes has been tested out. */
616 while (next_mode != NULL)
618 int width = -1;
619 int height = -1;
620 int depth = -1;
621 grub_err_t err;
622 unsigned int flags = modevalue;
623 unsigned int flagmask = modemask;
625 /* Use last next_mode as current mode. */
626 tmp = next_mode;
628 /* Save position of next mode and separate modes. */
629 for (; *next_mode; next_mode++)
630 if (*next_mode == ',' || *next_mode == ';')
631 break;
632 if (*next_mode)
634 *next_mode = 0;
635 next_mode++;
637 else
638 next_mode = 0;
640 /* Skip whitespace. */
641 while (grub_isspace (*tmp))
642 tmp++;
644 /* Initialize token holders. */
645 current_mode = tmp;
647 /* XXX: we assume that we're in pure text mode if
648 no video mode is initialized. Is it always true? */
649 if (grub_strcmp (current_mode, "text") == 0)
651 struct grub_video_mode_info mode_info;
653 grub_memset (&mode_info, 0, sizeof (mode_info));
654 if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) == 0)
655 || ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) != 0))
657 /* Valid mode found from adapter, and it has been activated.
658 Specify it as active adapter. */
659 grub_video_adapter_active = NULL;
661 /* Free memory. */
662 grub_free (modevar);
664 return GRUB_ERR_NONE;
668 err = parse_modespec (current_mode, &width, &height, &depth);
669 if (err)
671 /* Free memory before returning. */
672 grub_free (modevar);
674 return err;
677 /* Try out video mode. */
679 /* If user requested specific depth check if this depth is supported. */
680 if (depth != -1 && (flagmask & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
682 (((flags & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
683 != ((depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
684 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK))))
685 continue;
687 if (depth != -1)
689 flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
690 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
691 flagmask |= GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
694 /* Try to initialize requested mode. Ignore any errors. */
695 grub_video_adapter_t p;
697 /* Loop thru all possible video adapter trying to find requested mode. */
698 for (p = grub_video_adapter_list; p; p = p->next)
700 struct grub_video_mode_info mode_info;
702 grub_memset (&mode_info, 0, sizeof (mode_info));
704 /* Try to initialize adapter, if it fails, skip to next adapter. */
705 err = p->init ();
706 if (err != GRUB_ERR_NONE)
708 grub_errno = GRUB_ERR_NONE;
709 continue;
712 /* Try to initialize video mode. */
713 err = p->setup (width, height, flags, flagmask);
714 if (err != GRUB_ERR_NONE)
716 p->fini ();
717 grub_errno = GRUB_ERR_NONE;
718 continue;
721 err = p->get_info (&mode_info);
722 if (err != GRUB_ERR_NONE)
724 p->fini ();
725 grub_errno = GRUB_ERR_NONE;
726 continue;
729 flags = mode_info.mode_type & ~GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
730 flags |= (mode_info.bpp << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
731 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
733 /* Check that mode is suitable for upper layer. */
734 if ((flags & GRUB_VIDEO_MODE_TYPE_PURE_TEXT)
735 ? (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
736 && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
737 : ((flags & modemask) != modevalue))
739 p->fini ();
740 grub_errno = GRUB_ERR_NONE;
741 continue;
744 /* Valid mode found from adapter, and it has been activated.
745 Specify it as active adapter. */
746 grub_video_adapter_active = p;
748 /* Free memory. */
749 grub_free (modevar);
751 return GRUB_ERR_NONE;
756 /* Free memory. */
757 grub_free (modevar);
759 return grub_error (GRUB_ERR_BAD_ARGUMENT,
760 N_("no suitable video mode found"));