gfxterm_background.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2009,2013 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/term.h>
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/font.h>
  23. #include <grub/mm.h>
  24. #include <grub/env.h>
  25. #include <grub/video.h>
  26. #include <grub/gfxterm.h>
  27. #include <grub/bitmap.h>
  28. #include <grub/command.h>
  29. #include <grub/extcmd.h>
  30. #include <grub/bitmap_scale.h>
  31. #include <grub/i18n.h>
  32. #include <grub/color.h>
  33. GRUB_MOD_LICENSE ("GPLv3+");
  34. /* Option array indices. */
  35. enum
  36. {
  37. BACKGROUND_CMD_ARGINDEX_MODE = 0
  38. };
  39. static const struct grub_arg_option background_image_cmd_options[] =
  40. {
  41. {"mode", 'm', 0, N_("Background image mode."),
  42. /* TRANSLATORS: This refers to background image mode (stretched or
  43. in left-top corner). Note that GRUB will accept only original
  44. keywords stretch and normal, not the translated ones.
  45. So please put both in translation
  46. e.g. stretch(=%STRETCH%)|normal(=%NORMAL%).
  47. The percents mark the translated version. Since many people
  48. may not know the word stretch or normal I recommend
  49. putting the translation either here or in "Background image mode."
  50. string. */
  51. N_("stretch|normal"),
  52. ARG_TYPE_STRING},
  53. {0, 0, 0, 0, 0, 0}
  54. };
  55. static grub_err_t
  56. grub_gfxterm_background_image_cmd (grub_extcmd_context_t ctxt,
  57. int argc, char **args)
  58. {
  59. struct grub_arg_list *state = ctxt->state;
  60. /* Check that we have video adapter active. */
  61. if (grub_video_get_info(NULL) != GRUB_ERR_NONE)
  62. return grub_errno;
  63. /* Destroy existing background bitmap if loaded. */
  64. if (grub_gfxterm_background.bitmap)
  65. {
  66. grub_video_bitmap_destroy (grub_gfxterm_background.bitmap);
  67. grub_gfxterm_background.bitmap = 0;
  68. grub_gfxterm_background.blend_text_bg = 0;
  69. /* Mark whole screen as dirty. */
  70. grub_gfxterm_schedule_repaint ();
  71. }
  72. /* If filename was provided, try to load that. */
  73. if (argc >= 1)
  74. {
  75. /* Try to load new one. */
  76. grub_video_bitmap_load (&grub_gfxterm_background.bitmap, args[0]);
  77. if (grub_errno != GRUB_ERR_NONE)
  78. return grub_errno;
  79. /* Determine if the bitmap should be scaled to fit the screen. */
  80. if (!state[BACKGROUND_CMD_ARGINDEX_MODE].set
  81. || grub_strcmp (state[BACKGROUND_CMD_ARGINDEX_MODE].arg,
  82. "stretch") == 0)
  83. {
  84. unsigned int width, height;
  85. grub_gfxterm_get_dimensions (&width, &height);
  86. if (width
  87. != grub_video_bitmap_get_width (grub_gfxterm_background.bitmap)
  88. || height
  89. != grub_video_bitmap_get_height (grub_gfxterm_background.bitmap))
  90. {
  91. struct grub_video_bitmap *scaled_bitmap;
  92. grub_video_bitmap_create_scaled (&scaled_bitmap,
  93. width,
  94. height,
  95. grub_gfxterm_background.bitmap,
  96. GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST);
  97. if (grub_errno == GRUB_ERR_NONE)
  98. {
  99. /* Replace the original bitmap with the scaled one. */
  100. grub_video_bitmap_destroy (grub_gfxterm_background.bitmap);
  101. grub_gfxterm_background.bitmap = scaled_bitmap;
  102. }
  103. }
  104. }
  105. /* If bitmap was loaded correctly, display it. */
  106. if (grub_gfxterm_background.bitmap)
  107. {
  108. grub_gfxterm_background.blend_text_bg = 1;
  109. /* Mark whole screen as dirty. */
  110. grub_gfxterm_schedule_repaint ();
  111. }
  112. }
  113. /* All was ok. */
  114. grub_errno = GRUB_ERR_NONE;
  115. return grub_errno;
  116. }
  117. static grub_err_t
  118. grub_gfxterm_background_color_cmd (grub_command_t cmd __attribute__ ((unused)),
  119. int argc, char **args)
  120. {
  121. if (argc != 1)
  122. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  123. /* Check that we have video adapter active. */
  124. if (grub_video_get_info (NULL) != GRUB_ERR_NONE)
  125. return grub_errno;
  126. if (grub_video_parse_color (args[0],
  127. &grub_gfxterm_background.default_bg_color)
  128. != GRUB_ERR_NONE)
  129. return grub_errno;
  130. /* Destroy existing background bitmap if loaded. */
  131. if (grub_gfxterm_background.bitmap)
  132. {
  133. grub_video_bitmap_destroy (grub_gfxterm_background.bitmap);
  134. grub_gfxterm_background.bitmap = 0;
  135. /* Mark whole screen as dirty. */
  136. grub_gfxterm_schedule_repaint ();
  137. }
  138. /* Set the background and border colors. The background color needs to be
  139. compatible with the text layer. */
  140. grub_gfxterm_video_update_color ();
  141. grub_gfxterm_background.blend_text_bg = 1;
  142. /* Mark whole screen as dirty. */
  143. grub_gfxterm_schedule_repaint ();
  144. return GRUB_ERR_NONE;
  145. }
  146. static grub_extcmd_t background_image_cmd_handle;
  147. static grub_command_t background_color_cmd_handle;
  148. GRUB_MOD_INIT(gfxterm_background)
  149. {
  150. background_image_cmd_handle =
  151. grub_register_extcmd ("background_image",
  152. grub_gfxterm_background_image_cmd, 0,
  153. N_("[-m (stretch|normal)] FILE"),
  154. N_("Load background image for active terminal."),
  155. background_image_cmd_options);
  156. background_color_cmd_handle =
  157. grub_register_command ("background_color",
  158. grub_gfxterm_background_color_cmd,
  159. N_("COLOR"),
  160. N_("Set background color for active terminal."));
  161. }
  162. GRUB_MOD_FINI(gfxterm_background)
  163. {
  164. grub_unregister_command (background_color_cmd_handle);
  165. grub_unregister_extcmd (background_image_cmd_handle);
  166. }