gfxmenu.c 3.96 KB
Newer Older
Colin D Bennett's avatar
Colin D Bennett committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* gfxmenu.c - Graphical menu interface controller. */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2008  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/command.h>
#include <grub/video.h>
#include <grub/gfxterm.h>
#include <grub/bitmap.h>
#include <grub/bitmap_scale.h>
#include <grub/term.h>
#include <grub/env.h>
#include <grub/normal.h>
#include <grub/gfxwidgets.h>
#include <grub/menu.h>
#include <grub/menu_viewer.h>
#include <grub/gfxmenu_model.h>
#include <grub/gfxmenu_view.h>
38
#include <grub/time.h>
39
#include <grub/i18n.h>
Colin D Bennett's avatar
Colin D Bennett committed
40

41 42
GRUB_MOD_LICENSE ("GPLv3+");

43
static grub_gfxmenu_view_t cached_view;
44

45
static void 
46 47
grub_gfxmenu_viewer_fini (void *data __attribute__ ((unused)))
{
Colin D Bennett's avatar
Colin D Bennett committed
48 49
}

50
/* FIXME: Previously 't' changed to text menu is it necessary?  */
51
static grub_err_t
52
grub_gfxmenu_try (int entry, grub_menu_t menu, int nested)
Colin D Bennett's avatar
Colin D Bennett committed
53
{
54
  grub_gfxmenu_view_t view = NULL;
55
  const char *theme_path;
56
  char *full_theme_path = 0;
57
  struct grub_menu_viewer *instance;
58 59
  grub_err_t err;
  struct grub_video_mode_info mode_info;
60 61 62

  theme_path = grub_env_get ("theme");
  if (! theme_path)
63 64
    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"),
		       "theme");
Colin D Bennett's avatar
Colin D Bennett committed
65

66 67
  err = grub_video_get_info (&mode_info);
  if (err)
68
    return err;
69

70 71 72 73
  instance = grub_zalloc (sizeof (*instance));
  if (!instance)
    return grub_errno;

74 75 76 77 78 79 80 81 82 83 84
  if (theme_path[0] != '/' && theme_path[0] != '(')
    {
      const char *prefix;
      prefix = grub_env_get ("prefix");
      full_theme_path = grub_xasprintf ("%s/themes/%s",
					prefix,
					theme_path);
    }

  if (!cached_view || grub_strcmp (cached_view->theme_path,
				   full_theme_path ? : theme_path) != 0
85 86
      || cached_view->screen.width != mode_info.width
      || cached_view->screen.height != mode_info.height)
87
    {
88
      grub_gfxmenu_view_destroy (cached_view);
89
      /* Create the view.  */
90 91
      cached_view = grub_gfxmenu_view_new (full_theme_path ? : theme_path,
					   mode_info.width,
92 93
					   mode_info.height);
    }
94
  grub_free (full_theme_path);
95 96

  if (! cached_view)
Colin D Bennett's avatar
Colin D Bennett committed
97
    {
98
      grub_free (instance);
Colin D Bennett's avatar
Colin D Bennett committed
99 100 101
      return grub_errno;
    }

102 103 104 105 106 107 108 109 110 111
  view = cached_view;

  view->double_repaint = (mode_info.mode_type
			  & GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED)
    && !(mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_UPDATING_SWAP);
  view->selected = entry;
  view->menu = menu;
  view->nested = nested;
  view->first_timeout = -1;

112 113 114 115 116 117 118
  grub_video_set_viewport (0, 0, mode_info.width, mode_info.height);
  if (view->double_repaint)
    {
      grub_video_swap_buffers ();
      grub_video_set_viewport (0, 0, mode_info.width, mode_info.height);
    }

119
  grub_gfxmenu_view_draw (view);
Colin D Bennett's avatar
Colin D Bennett committed
120

121 122 123 124 125
  instance->data = view;
  instance->set_chosen_entry = grub_gfxmenu_set_chosen_entry;
  instance->fini = grub_gfxmenu_viewer_fini;
  instance->print_timeout = grub_gfxmenu_print_timeout;
  instance->clear_timeout = grub_gfxmenu_clear_timeout;
Colin D Bennett's avatar
Colin D Bennett committed
126

127
  grub_menu_register_viewer (instance);
Colin D Bennett's avatar
Colin D Bennett committed
128

129
  return GRUB_ERR_NONE;
Colin D Bennett's avatar
Colin D Bennett committed
130 131 132 133
}

GRUB_MOD_INIT (gfxmenu)
{
134 135 136
  struct grub_term_output *term;

  FOR_ACTIVE_TERM_OUTPUTS(term)
137
    if (grub_gfxmenu_try_hook && term->fullscreen)
138
      {
139
	term->fullscreen ();
140 141 142
	break;
      }

143
  grub_gfxmenu_try_hook = grub_gfxmenu_try;
Colin D Bennett's avatar
Colin D Bennett committed
144 145 146 147
}

GRUB_MOD_FINI (gfxmenu)
{
148
  grub_gfxmenu_view_destroy (cached_view);
149
  grub_gfxmenu_try_hook = NULL;
Colin D Bennett's avatar
Colin D Bennett committed
150
}