Merge branch 'makefile' into haiku
[grub2/phcoder.git] / video / video.c
blobbdd23f8c624f5ef046079bba7376d32429c9c9c9
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>
25 /* The list of video adapters registered to system. */
26 static grub_video_adapter_t grub_video_adapter_list;
28 /* Active video adapter. */
29 static grub_video_adapter_t grub_video_adapter_active;
31 /* Register video driver. */
32 void
33 grub_video_register (grub_video_adapter_t adapter)
35 adapter->next = grub_video_adapter_list;
36 grub_video_adapter_list = adapter;
39 /* Unregister video driver. */
40 void
41 grub_video_unregister (grub_video_adapter_t adapter)
43 grub_video_adapter_t *p, q;
45 for (p = &grub_video_adapter_list, q = *p; q; p = &(q->next), q = q->next)
46 if (q == adapter)
48 *p = q->next;
49 break;
53 /* Iterate thru all registered video drivers. */
54 void
55 grub_video_iterate (int (*hook) (grub_video_adapter_t adapter))
57 grub_video_adapter_t p;
59 for (p = grub_video_adapter_list; p; p = p->next)
60 if (hook (p))
61 break;
64 /* Restore back to initial mode (where applicable). */
65 grub_err_t
66 grub_video_restore (void)
68 if (grub_video_adapter_active)
70 grub_video_adapter_active->fini ();
71 if (grub_errno != GRUB_ERR_NONE)
72 return grub_errno;
74 grub_video_adapter_active = 0;
76 return GRUB_ERR_NONE;
79 /* Get information about active video mode. */
80 grub_err_t
81 grub_video_get_info (struct grub_video_mode_info *mode_info)
83 if (! grub_video_adapter_active)
84 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
86 /* If mode_info is NULL just report that video adapter is active. */
87 if (! mode_info)
89 grub_errno = GRUB_ERR_NONE;
90 return grub_errno;
93 return grub_video_adapter_active->get_info (mode_info);
96 /* Determine optimized blitting formation for specified video mode info. */
97 enum grub_video_blit_format
98 grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
100 /* Check if we have any known 32 bit modes. */
101 if (mode_info->bpp == 32)
103 if ((mode_info->red_mask_size == 8)
104 && (mode_info->red_field_pos == 16)
105 && (mode_info->green_mask_size == 8)
106 && (mode_info->green_field_pos == 8)
107 && (mode_info->blue_mask_size == 8)
108 && (mode_info->blue_field_pos == 0))
110 return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
112 else if ((mode_info->red_mask_size == 8)
113 && (mode_info->red_field_pos == 0)
114 && (mode_info->green_mask_size == 8)
115 && (mode_info->green_field_pos == 8)
116 && (mode_info->blue_mask_size == 8)
117 && (mode_info->blue_field_pos == 16))
119 return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
122 /* Check if we have any known 24 bit modes. */
123 else if (mode_info->bpp == 24)
125 if ((mode_info->red_mask_size == 8)
126 && (mode_info->red_field_pos == 16)
127 && (mode_info->green_mask_size == 8)
128 && (mode_info->green_field_pos == 8)
129 && (mode_info->blue_mask_size == 8)
130 && (mode_info->blue_field_pos == 0))
132 return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
134 else if ((mode_info->red_mask_size == 8)
135 && (mode_info->red_field_pos == 0)
136 && (mode_info->green_mask_size == 8)
137 && (mode_info->green_field_pos == 8)
138 && (mode_info->blue_mask_size == 8)
139 && (mode_info->blue_field_pos == 16))
141 return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
144 /* Check if we have any known 16 bit modes. */
145 else if (mode_info->bpp == 16)
147 if ((mode_info->red_mask_size == 5)
148 && (mode_info->red_field_pos == 11)
149 && (mode_info->green_mask_size == 6)
150 && (mode_info->green_field_pos == 5)
151 && (mode_info->blue_mask_size == 5)
152 && (mode_info->blue_field_pos == 0))
154 return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
156 else if ((mode_info->red_mask_size == 5)
157 && (mode_info->red_field_pos == 0)
158 && (mode_info->green_mask_size == 6)
159 && (mode_info->green_field_pos == 5)
160 && (mode_info->blue_mask_size == 5)
161 && (mode_info->blue_field_pos == 11))
163 return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
167 /* Backup route. Unknown format. */
169 /* If there are more than 8 bits per color, assume RGB(A) mode. */
170 if (mode_info->bpp > 8)
172 if (mode_info->reserved_mask_size > 0)
174 return GRUB_VIDEO_BLIT_FORMAT_RGBA;
176 else
178 return GRUB_VIDEO_BLIT_FORMAT_RGB;
182 /* Assume as indexcolor mode. */
183 return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
186 /* Set new indexed color palette entries. */
187 grub_err_t
188 grub_video_set_palette (unsigned int start, unsigned int count,
189 struct grub_video_palette_data *palette_data)
191 if (! grub_video_adapter_active)
192 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
194 return grub_video_adapter_active->set_palette (start, count, palette_data);
197 /* Get indexed color palette entries. */
198 grub_err_t
199 grub_video_get_palette (unsigned int start, unsigned int count,
200 struct grub_video_palette_data *palette_data)
202 if (! grub_video_adapter_active)
203 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
205 return grub_video_adapter_active->get_palette (start, count, palette_data);
208 /* Set viewport dimensions. */
209 grub_err_t
210 grub_video_set_viewport (unsigned int x, unsigned int y,
211 unsigned int width, unsigned int height)
213 if (! grub_video_adapter_active)
214 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
216 return grub_video_adapter_active->set_viewport (x, y, width, height);
219 /* Get viewport dimensions. */
220 grub_err_t
221 grub_video_get_viewport (unsigned int *x, unsigned int *y,
222 unsigned int *width, unsigned int *height)
224 if (! grub_video_adapter_active)
225 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
227 return grub_video_adapter_active->get_viewport (x, y, width, height);
230 /* Map color name to adapter specific color. */
231 grub_video_color_t
232 grub_video_map_color (grub_uint32_t color_name)
234 if (! grub_video_adapter_active)
235 return 0;
237 return grub_video_adapter_active->map_color (color_name);
240 /* Map RGB value to adapter specific color. */
241 grub_video_color_t
242 grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
244 if (! grub_video_adapter_active)
245 return 0;
247 return grub_video_adapter_active->map_rgb (red, green, blue);
250 /* Map RGBA value to adapter specific color. */
251 grub_video_color_t
252 grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
253 grub_uint8_t alpha)
255 if (! grub_video_adapter_active)
256 return 0;
258 return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
261 /* Unmap video color back to RGBA components. */
262 grub_err_t
263 grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
264 grub_uint8_t *green, grub_uint8_t *blue,
265 grub_uint8_t *alpha)
267 if (! grub_video_adapter_active)
268 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
270 return grub_video_adapter_active->unmap_color (color,
271 red,
272 green,
273 blue,
274 alpha);
277 /* Fill rectangle using specified color. */
278 grub_err_t
279 grub_video_fill_rect (grub_video_color_t color, int x, int y,
280 unsigned int width, unsigned int height)
282 if (! grub_video_adapter_active)
283 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
285 return grub_video_adapter_active->fill_rect (color, x, y, width, height);
288 /* Blit bitmap to screen. */
289 grub_err_t
290 grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
291 enum grub_video_blit_operators oper,
292 int x, int y, int offset_x, int offset_y,
293 unsigned int width, unsigned int height)
295 if (! grub_video_adapter_active)
296 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
298 return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
299 offset_x, offset_y,
300 width, height);
303 /* Blit render target to active render target. */
304 grub_err_t
305 grub_video_blit_render_target (struct grub_video_render_target *target,
306 enum grub_video_blit_operators oper,
307 int x, int y, int offset_x, int offset_y,
308 unsigned int width, unsigned int height)
310 if (! grub_video_adapter_active)
311 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
313 return grub_video_adapter_active->blit_render_target (target, oper, x, y,
314 offset_x, offset_y,
315 width, height);
318 /* Scroll viewport and fill new areas with specified color. */
319 grub_err_t
320 grub_video_scroll (grub_video_color_t color, int dx, int dy)
322 if (! grub_video_adapter_active)
323 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
325 return grub_video_adapter_active->scroll (color, dx, dy);
328 /* Swap buffers (swap active render target). */
329 grub_err_t
330 grub_video_swap_buffers (void)
332 if (! grub_video_adapter_active)
333 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
335 return grub_video_adapter_active->swap_buffers ();
338 /* Enable or disable double buffering. */
339 grub_err_t
340 grub_video_enable_double_buffering (int enable)
342 if (! grub_video_adapter_active)
343 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
345 return grub_video_adapter_active->enable_double_buffering (enable);
348 /* Create new render target. */
349 grub_err_t
350 grub_video_create_render_target (struct grub_video_render_target **result,
351 unsigned int width, unsigned int height,
352 unsigned int mode_type)
354 if (! grub_video_adapter_active)
355 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
357 return grub_video_adapter_active->create_render_target (result,
358 width, height,
359 mode_type);
362 /* Delete render target. */
363 grub_err_t
364 grub_video_delete_render_target (struct grub_video_render_target *target)
366 if (! grub_video_adapter_active)
367 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
369 return grub_video_adapter_active->delete_render_target (target);
372 /* Set active render target. */
373 grub_err_t
374 grub_video_set_active_render_target (struct grub_video_render_target *target)
376 if (! grub_video_adapter_active)
377 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
379 return grub_video_adapter_active->set_active_render_target (target);
382 /* Get active render target. */
383 grub_err_t
384 grub_video_get_active_render_target (struct grub_video_render_target **target)
386 if (! grub_video_adapter_active)
387 return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated");
389 return grub_video_adapter_active->get_active_render_target (target);
392 grub_err_t
393 grub_video_set_mode (const char *modestring,
394 int NESTED_FUNC_ATTR (*hook) (grub_video_adapter_t p,
395 struct grub_video_mode_info *mode_info))
397 char *tmp;
398 char *next_mode;
399 char *current_mode;
400 char *param;
401 char *value;
402 char *modevar;
403 int width = -1;
404 int height = -1;
405 int depth = -1;
406 int flags = 0;
408 /* Take copy of env.var. as we don't want to modify that. */
409 modevar = grub_strdup (modestring);
411 /* Initialize next mode. */
412 next_mode = modevar;
414 if (! modevar)
415 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
416 "couldn't allocate space for local modevar copy");
418 if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
419 || grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
420 || grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
422 struct grub_video_mode_info mode_info;
423 int suitable = 1;
424 grub_err_t err;
426 grub_memset (&mode_info, 0, sizeof (mode_info));
428 if (grub_video_adapter_active)
430 err = grub_video_get_info (&mode_info);
431 if (err)
433 suitable = 0;
434 grub_errno = GRUB_ERR_NONE;
437 else
438 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_PURE_TEXT;
440 if (suitable && hook)
441 suitable = hook (grub_video_adapter_active, &mode_info);
442 if (suitable)
444 grub_free (modevar);
445 return GRUB_ERR_NONE;
447 next_mode += sizeof ("keep") - 1;
448 if (! *next_mode)
450 grub_free (modevar);
452 return grub_error (GRUB_ERR_BAD_ARGUMENT,
453 "No suitable mode found.");
456 /* Skip separator. */
457 next_mode++;
460 /* De-activate last set video adapter. */
461 if (grub_video_adapter_active)
463 /* Finalize adapter. */
464 grub_video_adapter_active->fini ();
465 if (grub_errno != GRUB_ERR_NONE)
466 grub_errno = GRUB_ERR_NONE;
468 /* Mark active adapter as not set. */
469 grub_video_adapter_active = 0;
472 /* Loop until all modes has been tested out. */
473 while (next_mode != NULL)
475 grub_video_adapter_t p;
477 /* Use last next_mode as current mode. */
478 tmp = next_mode;
480 /* Reset video mode settings. */
481 width = -1;
482 height = -1;
483 depth = -1;
484 flags = 0;
486 /* Save position of next mode and separate modes. */
487 for (; *next_mode; next_mode++)
488 if (*next_mode == ',' || *next_mode == ';')
489 break;
490 if (*next_mode)
492 *next_mode = 0;
493 next_mode++;
495 else
496 next_mode = 0;
498 /* Skip whitespace. */
499 while (grub_isspace (*tmp))
500 tmp++;
502 /* Initialize token holders. */
503 current_mode = tmp;
504 param = tmp;
505 value = NULL;
507 /* XXX: we assume that we're in pure text mode if
508 no video mode is initialized. Is it always true? */
509 if (grub_strcmp (param, "text") == 0)
511 struct grub_video_mode_info mode_info;
513 grub_memset (&mode_info, 0, sizeof (mode_info));
514 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_PURE_TEXT;
516 if (! hook || hook (0, &mode_info))
518 /* Valid mode found from adapter, and it has been activated.
519 Specify it as active adapter. */
520 grub_video_adapter_active = NULL;
522 /* Free memory. */
523 grub_free (modevar);
525 return GRUB_ERR_NONE;
529 /* Parse <width>x<height>[x<depth>]*/
531 /* Find width value. */
532 value = param;
533 param = grub_strchr(param, 'x');
534 if (param == NULL)
536 grub_err_t rc;
538 /* First setup error message. */
539 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
540 "Invalid mode: %s\n",
541 current_mode);
543 /* Free memory before returning. */
544 grub_free (modevar);
546 return rc;
549 *param = 0;
550 param++;
552 width = grub_strtoul (value, 0, 0);
553 if (grub_errno != GRUB_ERR_NONE)
555 grub_err_t rc;
557 /* First setup error message. */
558 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
559 "Invalid mode: %s\n",
560 current_mode);
562 /* Free memory before returning. */
563 grub_free (modevar);
565 return rc;
568 /* Find height value. */
569 value = param;
570 param = grub_strchr(param, 'x');
571 if (param == NULL)
573 height = grub_strtoul (value, 0, 0);
574 if (grub_errno != GRUB_ERR_NONE)
576 grub_err_t rc;
578 /* First setup error message. */
579 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
580 "Invalid mode: %s\n",
581 current_mode);
583 /* Free memory before returning. */
584 grub_free (modevar);
586 return rc;
589 else
591 /* We have optional color depth value. */
592 *param = 0;
593 param++;
595 height = grub_strtoul (value, 0, 0);
596 if (grub_errno != GRUB_ERR_NONE)
598 grub_err_t rc;
600 /* First setup error message. */
601 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
602 "Invalid mode: %s\n",
603 current_mode);
605 /* Free memory before returning. */
606 grub_free (modevar);
608 return rc;
611 /* Convert color depth value. */
612 value = param;
613 depth = grub_strtoul (value, 0, 0);
614 if (grub_errno != GRUB_ERR_NONE)
616 grub_err_t rc;
618 /* First setup error message. */
619 rc = grub_error (GRUB_ERR_BAD_ARGUMENT,
620 "Invalid mode: %s\n",
621 current_mode);
623 /* Free memory before returning. */
624 grub_free (modevar);
626 return rc;
630 /* Try out video mode. */
632 /* If we have 8 or less bits, then assume that it is indexed color mode. */
633 if ((depth <= 8) && (depth != -1))
634 flags |= GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
636 /* We have more than 8 bits, then assume that it is RGB color mode. */
637 if (depth > 8)
638 flags |= GRUB_VIDEO_MODE_TYPE_RGB;
640 /* If user requested specific depth, forward that information to driver. */
641 if (depth != -1)
642 flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
643 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
645 /* Try to initialize requested mode. Ignore any errors. */
647 /* Loop thru all possible video adapter trying to find requested mode. */
648 for (p = grub_video_adapter_list; p; p = p->next)
650 grub_err_t err;
651 struct grub_video_mode_info mode_info;
653 grub_memset (&mode_info, 0, sizeof (mode_info));
655 /* Try to initialize adapter, if it fails, skip to next adapter. */
656 err = p->init ();
657 if (err != GRUB_ERR_NONE)
659 grub_errno = GRUB_ERR_NONE;
660 continue;
663 /* Try to initialize video mode. */
664 err = p->setup (width, height, flags);
665 if (err != GRUB_ERR_NONE)
667 p->fini ();
668 grub_dprintf ("video", "Failed to setup mode %dx%d (%x) on %s\n",
669 width, height, flags, p->name);
670 grub_errno = GRUB_ERR_NONE;
671 continue;
674 err = p->get_info (&mode_info);
675 if (err != GRUB_ERR_NONE)
677 p->fini ();
678 grub_dprintf ("video", "Failed to get info: %dx%d (%x) on %s\n",
679 width, height, flags, p->name);
680 grub_errno = GRUB_ERR_NONE;
681 continue;
684 if (hook && ! hook (p, &mode_info))
686 p->fini ();
687 grub_errno = GRUB_ERR_NONE;
688 continue;
691 /* Valid mode found from adapter, and it has been activated.
692 Specify it as active adapter. */
693 grub_video_adapter_active = p;
695 /* Free memory. */
696 grub_free (modevar);
698 return GRUB_ERR_NONE;
703 /* Free memory. */
704 grub_free (modevar);
706 return grub_error (GRUB_ERR_BAD_ARGUMENT,
707 "No suitable mode found.");
710 /* Initialize Video API module. */
711 GRUB_MOD_INIT(video_video)
715 /* Finalize Video API module. */
716 GRUB_MOD_FINI(video_video)