~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to gfxmenu/view.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* view.c - Graphical menu interface MVC view. */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2008  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB is free software: you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation, either version 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/types.h>
21
 
#include <grub/file.h>
22
 
#include <grub/misc.h>
23
 
#include <grub/mm.h>
24
 
#include <grub/err.h>
25
 
#include <grub/dl.h>
26
 
#include <grub/normal.h>
27
 
#include <grub/video.h>
28
 
#include <grub/gfxterm.h>
29
 
#include <grub/bitmap.h>
30
 
#include <grub/bitmap_scale.h>
31
 
#include <grub/term.h>
32
 
#include <grub/gfxwidgets.h>
33
 
#include <grub/time.h>
34
 
#include <grub/menu.h>
35
 
#include <grub/menu_viewer.h>
36
 
#include <grub/gfxmenu_view.h>
37
 
#include <grub/gui_string_util.h>
38
 
#include <grub/icon_manager.h>
39
 
 
40
 
/* The component ID identifying GUI components to be updated as the timeout
41
 
   status changes.  */
42
 
#define TIMEOUT_COMPONENT_ID "__timeout__"
43
 
 
44
 
static void
45
 
init_terminal (grub_gfxmenu_view_t view);
46
 
static grub_video_rect_t term_rect;
47
 
static grub_gfxmenu_view_t term_view;
48
 
 
49
 
/* Create a new view object, loading the theme specified by THEME_PATH and
50
 
   associating MODEL with the view.  */
51
 
grub_gfxmenu_view_t
52
 
grub_gfxmenu_view_new (const char *theme_path,
53
 
                       int width, int height)
54
 
{
55
 
  grub_gfxmenu_view_t view;
56
 
  grub_font_t default_font;
57
 
  grub_gui_color_t default_fg_color;
58
 
  grub_gui_color_t default_bg_color;
59
 
 
60
 
  view = grub_malloc (sizeof (*view));
61
 
  if (! view)
62
 
    return 0;
63
 
 
64
 
  view->screen.x = 0;
65
 
  view->screen.y = 0;
66
 
  view->screen.width = width;
67
 
  view->screen.height = height;
68
 
 
69
 
  default_font = grub_font_get ("Helvetica 12");
70
 
  default_fg_color = grub_gui_color_rgb (0, 0, 0);
71
 
  default_bg_color = grub_gui_color_rgb (255, 255, 255);
72
 
 
73
 
  view->canvas = 0;
74
 
 
75
 
  view->title_font = default_font;
76
 
  view->message_font = default_font;
77
 
  view->terminal_font_name = grub_strdup ("Fixed 10");
78
 
  view->title_color = default_fg_color;
79
 
  view->message_color = default_bg_color;
80
 
  view->message_bg_color = default_fg_color;
81
 
  view->desktop_image = 0;
82
 
  view->desktop_color = default_bg_color;
83
 
  view->terminal_box = grub_gfxmenu_create_box (0, 0);
84
 
  view->title_text = grub_strdup ("GRUB Boot Menu");
85
 
  view->progress_message_text = 0;
86
 
  view->theme_path = 0;
87
 
 
88
 
  /* Set the timeout bar's frame.  */
89
 
  view->progress_message_frame.width = view->screen.width * 4 / 5;
90
 
  view->progress_message_frame.height = 50;
91
 
  view->progress_message_frame.x = view->screen.x
92
 
    + (view->screen.width - view->progress_message_frame.width) / 2;
93
 
  view->progress_message_frame.y = view->screen.y
94
 
    + view->screen.height - 90 - 20 - view->progress_message_frame.height;
95
 
 
96
 
  if (grub_gfxmenu_view_load_theme (view, theme_path) != 0)
97
 
    {
98
 
      grub_gfxmenu_view_destroy (view);
99
 
      return 0;
100
 
    }
101
 
 
102
 
  return view;
103
 
}
104
 
 
105
 
/* Destroy the view object.  All used memory is freed.  */
106
 
void
107
 
grub_gfxmenu_view_destroy (grub_gfxmenu_view_t view)
108
 
{
109
 
  if (!view)
110
 
    return;
111
 
  grub_video_bitmap_destroy (view->desktop_image);
112
 
  if (view->terminal_box)
113
 
    view->terminal_box->destroy (view->terminal_box);
114
 
  grub_free (view->terminal_font_name);
115
 
  grub_free (view->title_text);
116
 
  grub_free (view->progress_message_text);
117
 
  grub_free (view->theme_path);
118
 
  if (view->canvas)
119
 
    view->canvas->component.ops->destroy (view->canvas);
120
 
  grub_free (view);
121
 
}
122
 
 
123
 
static void
124
 
redraw_background (grub_gfxmenu_view_t view,
125
 
                   const grub_video_rect_t *bounds)
126
 
{
127
 
  if (view->desktop_image)
128
 
    {
129
 
      struct grub_video_bitmap *img = view->desktop_image;
130
 
      grub_video_blit_bitmap (img, GRUB_VIDEO_BLIT_REPLACE,
131
 
                              bounds->x, bounds->y,
132
 
                              bounds->x - view->screen.x,
133
 
                              bounds->y - view->screen.y,
134
 
                              bounds->width, bounds->height);
135
 
    }
136
 
  else
137
 
    {
138
 
      grub_video_fill_rect (grub_gui_map_color (view->desktop_color),
139
 
                            bounds->x, bounds->y,
140
 
                            bounds->width, bounds->height);
141
 
    }
142
 
}
143
 
 
144
 
static void
145
 
draw_title (grub_gfxmenu_view_t view)
146
 
{
147
 
  if (! view->title_text)
148
 
    return;
149
 
 
150
 
  /* Center the title. */
151
 
  int title_width = grub_font_get_string_width (view->title_font,
152
 
                                                view->title_text);
153
 
  int x = (view->screen.width - title_width) / 2;
154
 
  int y = 40 + grub_font_get_ascent (view->title_font);
155
 
  grub_font_draw_string (view->title_text,
156
 
                         view->title_font,
157
 
                         grub_gui_map_color (view->title_color),
158
 
                         x, y);
159
 
}
160
 
 
161
 
struct progress_value_data
162
 
{
163
 
  int visible;
164
 
  int start;
165
 
  int end;
166
 
  int value;
167
 
};
168
 
 
169
 
static void
170
 
update_timeout_visit (grub_gui_component_t component,
171
 
                      void *userdata)
172
 
{
173
 
  struct progress_value_data *pv;
174
 
  pv = (struct progress_value_data *) userdata;
175
 
 
176
 
  ((struct grub_gui_progress *) component)->ops
177
 
    ->set_state ((struct grub_gui_progress *) component,
178
 
                 pv->visible, pv->start, pv->value, pv->end);
179
 
}
180
 
 
181
 
void 
182
 
grub_gfxmenu_print_timeout (int timeout, void *data)
183
 
{
184
 
  struct grub_gfxmenu_view *view = data;
185
 
 
186
 
  struct progress_value_data pv;
187
 
 
188
 
  auto void redraw_timeout_visit (grub_gui_component_t component,
189
 
                                  void *userdata __attribute__ ((unused)));
190
 
 
191
 
  auto void redraw_timeout_visit (grub_gui_component_t component,
192
 
                                  void *userdata __attribute__ ((unused)))
193
 
  {
194
 
    grub_video_rect_t bounds;
195
 
    component->ops->get_bounds (component, &bounds);
196
 
    grub_gfxmenu_view_redraw (view, &bounds);
197
 
  }
198
 
 
199
 
  if (view->first_timeout == -1)
200
 
    view->first_timeout = timeout;
201
 
 
202
 
  pv.visible = 1;
203
 
  pv.start = -(view->first_timeout + 1);
204
 
  pv.end = 0;
205
 
  pv.value = -timeout;
206
 
 
207
 
  grub_gui_find_by_id ((grub_gui_component_t) view->canvas,
208
 
                       TIMEOUT_COMPONENT_ID, update_timeout_visit, &pv);
209
 
  grub_gui_find_by_id ((grub_gui_component_t) view->canvas,
210
 
                       TIMEOUT_COMPONENT_ID, redraw_timeout_visit, &pv);
211
 
  grub_video_swap_buffers ();
212
 
  if (view->double_repaint)
213
 
    grub_gui_find_by_id ((grub_gui_component_t) view->canvas,
214
 
                         TIMEOUT_COMPONENT_ID, redraw_timeout_visit, &pv);
215
 
}
216
 
 
217
 
void 
218
 
grub_gfxmenu_clear_timeout (void *data)
219
 
{
220
 
  struct progress_value_data pv;
221
 
  struct grub_gfxmenu_view *view = data;
222
 
 
223
 
  auto void redraw_timeout_visit (grub_gui_component_t component,
224
 
                                  void *userdata __attribute__ ((unused)));
225
 
 
226
 
  auto void redraw_timeout_visit (grub_gui_component_t component,
227
 
                                  void *userdata __attribute__ ((unused)))
228
 
  {
229
 
    grub_video_rect_t bounds;
230
 
    component->ops->get_bounds (component, &bounds);
231
 
    grub_gfxmenu_view_redraw (view, &bounds);
232
 
  }
233
 
 
234
 
  pv.visible = 0;
235
 
  pv.start = 1;
236
 
  pv.end = 0;
237
 
  pv.value = 0;
238
 
 
239
 
  grub_gui_find_by_id ((grub_gui_component_t) view->canvas,
240
 
                       TIMEOUT_COMPONENT_ID, update_timeout_visit, &pv);
241
 
  grub_gui_find_by_id ((grub_gui_component_t) view->canvas,
242
 
                       TIMEOUT_COMPONENT_ID, redraw_timeout_visit, &pv);
243
 
  grub_video_swap_buffers ();
244
 
  if (view->double_repaint)
245
 
    grub_gui_find_by_id ((grub_gui_component_t) view->canvas,
246
 
                         TIMEOUT_COMPONENT_ID, redraw_timeout_visit, &pv);
247
 
}
248
 
 
249
 
static void
250
 
update_menu_visit (grub_gui_component_t component,
251
 
                   void *userdata)
252
 
{
253
 
  grub_gfxmenu_view_t view;
254
 
  view = userdata;
255
 
  if (component->ops->is_instance (component, "list"))
256
 
    {
257
 
      grub_gui_list_t list = (grub_gui_list_t) component;
258
 
      list->ops->set_view_info (list, view);
259
 
    }
260
 
}
261
 
 
262
 
/* Update any boot menu components with the current menu model and
263
 
   theme path.  */
264
 
static void
265
 
update_menu_components (grub_gfxmenu_view_t view)
266
 
{
267
 
  grub_gui_iterate_recursively ((grub_gui_component_t) view->canvas,
268
 
                                update_menu_visit, view);
269
 
}
270
 
 
271
 
static void
272
 
draw_message (grub_gfxmenu_view_t view)
273
 
{
274
 
  char *text = view->progress_message_text;
275
 
  grub_video_rect_t f = view->progress_message_frame;
276
 
  if (! text)
277
 
    return;
278
 
 
279
 
  grub_font_t font = view->message_font;
280
 
  grub_video_color_t color = grub_gui_map_color (view->message_color);
281
 
 
282
 
  /* Border.  */
283
 
  grub_video_fill_rect (color,
284
 
                        f.x-1, f.y-1, f.width+2, f.height+2);
285
 
  /* Fill.  */
286
 
  grub_video_fill_rect (grub_gui_map_color (view->message_bg_color),
287
 
                        f.x, f.y, f.width, f.height);
288
 
 
289
 
  /* Center the text. */
290
 
  int text_width = grub_font_get_string_width (font, text);
291
 
  int x = f.x + (f.width - text_width) / 2;
292
 
  int y = (f.y + (f.height - grub_font_get_descent (font)) / 2
293
 
           + grub_font_get_ascent (font) / 2);
294
 
  grub_font_draw_string (text, font, color, x, y);
295
 
}
296
 
 
297
 
void
298
 
grub_gfxmenu_view_redraw (grub_gfxmenu_view_t view,
299
 
                          const grub_video_rect_t *region)
300
 
{
301
 
  if (grub_video_have_common_points (&term_rect, region))
302
 
    grub_gfxterm_schedule_repaint ();
303
 
 
304
 
  grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY);
305
 
 
306
 
  redraw_background (view, region);
307
 
  if (view->canvas)
308
 
    view->canvas->component.ops->paint (view->canvas, region);
309
 
  draw_title (view);
310
 
  if (grub_video_have_common_points (&view->progress_message_frame, region))
311
 
    draw_message (view);
312
 
}
313
 
 
314
 
void
315
 
grub_gfxmenu_view_draw (grub_gfxmenu_view_t view)
316
 
{
317
 
  init_terminal (view);
318
 
 
319
 
  /* Clear the screen; there may be garbage left over in video memory. */
320
 
  grub_video_fill_rect (grub_video_map_rgb (0, 0, 0),
321
 
                        view->screen.x, view->screen.y,
322
 
                        view->screen.width, view->screen.height);
323
 
  grub_video_swap_buffers ();
324
 
  if (view->double_repaint)
325
 
    grub_video_fill_rect (grub_video_map_rgb (0, 0, 0),
326
 
                          view->screen.x, view->screen.y,
327
 
                          view->screen.width, view->screen.height);
328
 
 
329
 
  update_menu_components (view);
330
 
 
331
 
  grub_gfxmenu_view_redraw (view, &view->screen);
332
 
  grub_video_swap_buffers ();
333
 
  if (view->double_repaint)
334
 
    grub_gfxmenu_view_redraw (view, &view->screen);
335
 
}
336
 
 
337
 
static void
338
 
redraw_menu_visit (grub_gui_component_t component,
339
 
                   void *userdata)
340
 
{
341
 
  grub_gfxmenu_view_t view;
342
 
  view = userdata;
343
 
  if (component->ops->is_instance (component, "list"))
344
 
    {
345
 
      grub_gui_list_t list;
346
 
      grub_video_rect_t bounds;
347
 
 
348
 
      list = (grub_gui_list_t) component;
349
 
      component->ops->get_bounds (component, &bounds);
350
 
      grub_gfxmenu_view_redraw (view, &bounds);
351
 
    }
352
 
}
353
 
 
354
 
void
355
 
grub_gfxmenu_redraw_menu (grub_gfxmenu_view_t view)
356
 
{
357
 
  update_menu_components (view);
358
 
 
359
 
  grub_gui_iterate_recursively ((grub_gui_component_t) view->canvas,
360
 
                                redraw_menu_visit, view);
361
 
  grub_video_swap_buffers ();
362
 
  if (view->double_repaint)
363
 
    {
364
 
      grub_gui_iterate_recursively ((grub_gui_component_t) view->canvas,
365
 
                                    redraw_menu_visit, view);
366
 
    }
367
 
}
368
 
 
369
 
void 
370
 
grub_gfxmenu_set_chosen_entry (int entry, void *data)
371
 
{
372
 
  grub_gfxmenu_view_t view = data;
373
 
 
374
 
  view->selected = entry;
375
 
  grub_gfxmenu_redraw_menu (view);
376
 
}
377
 
 
378
 
static void
379
 
grub_gfxmenu_draw_terminal_box (void)
380
 
{
381
 
  grub_gfxmenu_box_t term_box;
382
 
 
383
 
  term_box = term_view->terminal_box;
384
 
  if (!term_box)
385
 
    return;
386
 
  
387
 
  term_box->set_content_size (term_box, term_rect.width,
388
 
                              term_rect.height);
389
 
  
390
 
  term_box->draw (term_box,
391
 
                  term_rect.x - term_box->get_left_pad (term_box),
392
 
                  term_rect.y - term_box->get_top_pad (term_box));
393
 
  grub_video_swap_buffers ();
394
 
  if (term_view->double_repaint)
395
 
    term_box->draw (term_box,
396
 
                    term_rect.x - term_box->get_left_pad (term_box),
397
 
                    term_rect.y - term_box->get_top_pad (term_box));
398
 
}
399
 
 
400
 
static void
401
 
init_terminal (grub_gfxmenu_view_t view)
402
 
{
403
 
  term_rect.width = view->screen.width * 7 / 10;
404
 
  term_rect.height = view->screen.height * 7 / 10;
405
 
 
406
 
  term_rect.x = view->screen.x + view->screen.width * (10 - 7) / 10 / 2;
407
 
  term_rect.y = view->screen.y + view->screen.height * (10 - 7) / 10 / 2;
408
 
 
409
 
  term_view = view;
410
 
 
411
 
  /* Note: currently there is no API for changing the gfxterm font
412
 
     on the fly, so whatever font the initially loaded theme specifies
413
 
     will be permanent.  */
414
 
  grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY, term_rect.x,
415
 
                           term_rect.y,
416
 
                           term_rect.width, term_rect.height,
417
 
                           view->double_repaint, view->terminal_font_name, 3);
418
 
  grub_gfxterm_decorator_hook = grub_gfxmenu_draw_terminal_box;
419
 
}
420
 
 
421
 
/* FIXME: previously notifications were displayed in special case.
422
 
   Is it necessary?
423
 
 */
424
 
#if 0
425
 
/* Sets MESSAGE as the progress message for the view.
426
 
   MESSAGE can be 0, in which case no message is displayed.  */
427
 
static void
428
 
set_progress_message (grub_gfxmenu_view_t view, const char *message)
429
 
{
430
 
  grub_free (view->progress_message_text);
431
 
  if (message)
432
 
    view->progress_message_text = grub_strdup (message);
433
 
  else
434
 
    view->progress_message_text = 0;
435
 
}
436
 
 
437
 
static void
438
 
notify_booting (grub_menu_entry_t entry, void *userdata)
439
 
{
440
 
  grub_gfxmenu_view_t view = (grub_gfxmenu_view_t) userdata;
441
 
 
442
 
  char *s = grub_malloc (100 + grub_strlen (entry->title));
443
 
  if (!s)
444
 
    return;
445
 
 
446
 
  grub_sprintf (s, "Booting '%s'", entry->title);
447
 
  set_progress_message (view, s);
448
 
  grub_free (s);
449
 
  grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
450
 
  grub_video_swap_buffers ();
451
 
  if (view->double_repaint)
452
 
    grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
453
 
}
454
 
 
455
 
static void
456
 
notify_fallback (grub_menu_entry_t entry, void *userdata)
457
 
{
458
 
  grub_gfxmenu_view_t view = (grub_gfxmenu_view_t) userdata;
459
 
 
460
 
  char *s = grub_malloc (100 + grub_strlen (entry->title));
461
 
  if (!s)
462
 
    return;
463
 
 
464
 
  grub_sprintf (s, "Falling back to '%s'", entry->title);
465
 
  set_progress_message (view, s);
466
 
  grub_free (s);
467
 
  grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
468
 
  grub_video_swap_buffers ();
469
 
  if (view->double_repaint)
470
 
    grub_gfxmenu_view_redraw (view, &view->progress_message_frame);
471
 
}
472
 
 
473
 
static void
474
 
notify_execution_failure (void *userdata __attribute__ ((unused)))
475
 
{
476
 
}
477
 
 
478
 
 
479
 
static struct grub_menu_execute_callback execute_callback =
480
 
{
481
 
  .notify_booting = notify_booting,
482
 
  .notify_fallback = notify_fallback,
483
 
  .notify_failure = notify_execution_failure
484
 
};
485
 
 
486
 
#endif