1 /* model.c - Graphical menu interface MVC model. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/types.h>
21 #include <grub/misc.h>
24 #include <grub/normal.h>
25 #include <grub/menu.h>
26 #include <grub/time.h>
27 #include <grub/gfxmenu_model.h>
29 /* Model type definition. */
30 struct grub_gfxmenu_model
34 grub_menu_entry_t
*entries
;
35 int selected_entry_index
;
37 grub_uint64_t timeout_start
;
38 grub_uint64_t timeout_at
;
43 grub_gfxmenu_model_new (grub_menu_t menu
)
45 grub_gfxmenu_model_t model
;
47 model
= grub_malloc (sizeof (*model
));
52 model
->num_entries
= menu
->size
;
54 model
->selected_entry_index
= 0;
55 model
->timeout_set
= 0;
56 model
->timeout_at
= 0;
57 if (model
->num_entries
> 0)
59 model
->entries
= grub_malloc (model
->num_entries
60 * sizeof (*model
->entries
));
65 grub_menu_entry_t cur
;
66 for (i
= 0, cur
= menu
->entry_list
;
67 i
< model
->num_entries
;
70 model
->entries
[i
] = cur
;
77 grub_free (model
->entries
);
83 grub_gfxmenu_model_destroy (grub_gfxmenu_model_t model
)
88 grub_free (model
->entries
);
95 grub_gfxmenu_model_get_menu (grub_gfxmenu_model_t model
)
101 grub_gfxmenu_model_set_timeout (grub_gfxmenu_model_t model
)
103 int timeout_sec
= grub_menu_get_timeout ();
104 if (timeout_sec
>= 0)
106 model
->timeout_start
= grub_get_time_ms ();
107 model
->timeout_at
= model
->timeout_start
+ timeout_sec
* 1000;
108 model
->timeout_set
= 1;
112 model
->timeout_set
= 0;
117 grub_gfxmenu_model_clear_timeout (grub_gfxmenu_model_t model
)
119 model
->timeout_set
= 0;
120 grub_menu_set_timeout (-1);
124 grub_gfxmenu_model_get_timeout_ms (grub_gfxmenu_model_t model
)
126 if (!model
->timeout_set
)
129 return model
->timeout_at
- model
->timeout_start
;
133 grub_gfxmenu_model_get_timeout_remaining_ms (grub_gfxmenu_model_t model
)
135 if (!model
->timeout_set
)
138 return model
->timeout_at
- grub_get_time_ms ();
142 grub_gfxmenu_model_timeout_expired (grub_gfxmenu_model_t model
)
144 if (model
->timeout_set
145 && grub_get_time_ms () >= model
->timeout_at
)
152 grub_gfxmenu_model_get_num_entries (grub_gfxmenu_model_t model
)
154 return model
->num_entries
;
158 grub_gfxmenu_model_get_selected_index (grub_gfxmenu_model_t model
)
160 return model
->selected_entry_index
;
164 grub_gfxmenu_model_set_selected_index (grub_gfxmenu_model_t model
, int index
)
166 model
->selected_entry_index
= index
;
170 grub_gfxmenu_model_get_entry_title (grub_gfxmenu_model_t model
, int index
)
172 if (index
< 0 || index
>= model
->num_entries
)
174 grub_error (GRUB_ERR_OUT_OF_RANGE
, "invalid menu index");
178 return model
->entries
[index
]->title
;
182 grub_gfxmenu_model_get_entry (grub_gfxmenu_model_t model
, int index
)
184 if (index
< 0 || index
>= model
->num_entries
)
186 grub_error (GRUB_ERR_OUT_OF_RANGE
, "invalid menu index");
190 return model
->entries
[index
];