]> git.proxmox.com Git - grub2.git/blob - gfxmenu/model.c
gfxmenu import
[grub2.git] / gfxmenu / model.c
1 /* model.c - Graphical menu interface MVC model. */
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/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/normal.h>
25 #include <grub/menu.h>
26 #include <grub/time.h>
27 #include <grub/gfxmenu_model.h>
28
29 /* Model type definition. */
30 struct grub_gfxmenu_model
31 {
32 grub_menu_t menu;
33 int num_entries;
34 grub_menu_entry_t *entries;
35 int selected_entry_index;
36 int timeout_set;
37 grub_uint64_t timeout_start;
38 grub_uint64_t timeout_at;
39 };
40
41
42 grub_gfxmenu_model_t
43 grub_gfxmenu_model_new (grub_menu_t menu)
44 {
45 grub_gfxmenu_model_t model;
46
47 model = grub_malloc (sizeof (*model));
48 if (! model)
49 return 0;
50
51 model->menu = menu;
52 model->num_entries = menu->size;
53 model->entries = 0;
54 model->selected_entry_index = 0;
55 model->timeout_set = 0;
56 model->timeout_at = 0;
57 if (model->num_entries > 0)
58 {
59 model->entries = grub_malloc (model->num_entries
60 * sizeof (*model->entries));
61 if (! model->entries)
62 goto fail_and_free;
63
64 int i;
65 grub_menu_entry_t cur;
66 for (i = 0, cur = menu->entry_list;
67 i < model->num_entries;
68 i++, cur = cur->next)
69 {
70 model->entries[i] = cur;
71 }
72 }
73
74 return model;
75
76 fail_and_free:
77 grub_free (model->entries);
78 grub_free (model);
79 return 0;
80 }
81
82 void
83 grub_gfxmenu_model_destroy (grub_gfxmenu_model_t model)
84 {
85 if (! model)
86 return;
87
88 grub_free (model->entries);
89 model->entries = 0;
90
91 grub_free (model);
92 }
93
94 grub_menu_t
95 grub_gfxmenu_model_get_menu (grub_gfxmenu_model_t model)
96 {
97 return model->menu;
98 }
99
100 void
101 grub_gfxmenu_model_set_timeout (grub_gfxmenu_model_t model)
102 {
103 int timeout_sec = grub_menu_get_timeout ();
104 if (timeout_sec >= 0)
105 {
106 model->timeout_start = grub_get_time_ms ();
107 model->timeout_at = model->timeout_start + timeout_sec * 1000;
108 model->timeout_set = 1;
109 }
110 else
111 {
112 model->timeout_set = 0;
113 }
114 }
115
116 void
117 grub_gfxmenu_model_clear_timeout (grub_gfxmenu_model_t model)
118 {
119 model->timeout_set = 0;
120 grub_menu_set_timeout (-1);
121 }
122
123 int
124 grub_gfxmenu_model_get_timeout_ms (grub_gfxmenu_model_t model)
125 {
126 if (!model->timeout_set)
127 return -1;
128
129 return model->timeout_at - model->timeout_start;
130 }
131
132 int
133 grub_gfxmenu_model_get_timeout_remaining_ms (grub_gfxmenu_model_t model)
134 {
135 if (!model->timeout_set)
136 return -1;
137
138 return model->timeout_at - grub_get_time_ms ();
139 }
140
141 int
142 grub_gfxmenu_model_timeout_expired (grub_gfxmenu_model_t model)
143 {
144 if (model->timeout_set
145 && grub_get_time_ms () >= model->timeout_at)
146 return 1;
147
148 return 0;
149 }
150
151 int
152 grub_gfxmenu_model_get_num_entries (grub_gfxmenu_model_t model)
153 {
154 return model->num_entries;
155 }
156
157 int
158 grub_gfxmenu_model_get_selected_index (grub_gfxmenu_model_t model)
159 {
160 return model->selected_entry_index;
161 }
162
163 void
164 grub_gfxmenu_model_set_selected_index (grub_gfxmenu_model_t model, int index)
165 {
166 model->selected_entry_index = index;
167 }
168
169 const char *
170 grub_gfxmenu_model_get_entry_title (grub_gfxmenu_model_t model, int index)
171 {
172 if (index < 0 || index >= model->num_entries)
173 {
174 grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid menu index");
175 return 0;
176 }
177
178 return model->entries[index]->title;
179 }
180
181 grub_menu_entry_t
182 grub_gfxmenu_model_get_entry (grub_gfxmenu_model_t model, int index)
183 {
184 if (index < 0 || index >= model->num_entries)
185 {
186 grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid menu index");
187 return 0;
188 }
189
190 return model->entries[index];
191 }