]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/ps3-cui.c
Add PS3 countdown timer
[petitboot] / ui / ncurses / ps3-cui.c
1 /*
2  * Petitboot cui bootloader for the PS3 game console
3  *
4  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
5  *  Copyright 2009 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program 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 this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * TODO
23  * removable media event
24  * ncurses mouse support
25  */
26
27 #if defined(HAVE_CONFIG_H)
28 #include "config.h"
29 #endif
30
31 #define _GNU_SOURCE
32 #include <errno.h>
33 #include <getopt.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38
39 #include "log/log.h"
40 #include "talloc/talloc.h"
41 #include "waiter/waiter.h"
42 #include "ui/common/discover-client.h"
43 #include "ui/common/ps3.h"
44 #include "nc-cui.h"
45
46 static void print_version(void)
47 {
48         printf("pb-cui (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
49 }
50
51 static void print_usage(void)
52 {
53         print_version();
54         printf(
55 "Usage: pb-cui [-h, --help] [-l, --log log-file] [-V, --version]\n");
56 }
57
58 /**
59  * enum opt_value - Tri-state options variables.
60  */
61
62 enum opt_value {opt_undef = 0, opt_yes, opt_no};
63
64 /**
65  * struct opts - Values from command line options.
66  */
67
68 struct opts {
69         enum opt_value show_help;
70         const char *log_file;
71         enum opt_value show_version;
72 };
73
74 /**
75  * opts_parse - Parse the command line options.
76  */
77
78 static int opts_parse(struct opts *opts, int argc, char *argv[])
79 {
80         static const struct option long_options[] = {
81                 {"help",    no_argument,       NULL, 'h'},
82                 {"log",     required_argument, NULL, 'l'},
83                 {"version", no_argument,       NULL, 'V'},
84                 { NULL,     0,                 NULL, 0},
85         };
86         static const char short_options[] = "hl:V";
87         static const struct opts default_values = {
88                 .log_file = "pb-cui.log",
89         };
90
91         *opts = default_values;
92
93         while (1) {
94                 int c = getopt_long(argc, argv, short_options, long_options,
95                         NULL);
96
97                 if (c == EOF)
98                         break;
99
100                 switch (c) {
101                 case 'h':
102                         opts->show_help = opt_yes;
103                         break;
104                 case 'l':
105                         opts->log_file = optarg;
106                         break;
107                 case 'V':
108                         opts->show_version = opt_yes;
109                         break;
110                 default:
111                         opts->show_help = opt_yes;
112                         return -1;
113                 }
114         }
115
116         return 0;
117 }
118
119 /**
120  * struct ps3_cui - Main cui program instance.
121  * @mm: Main menu.
122  * @svm: Set video mode menu.
123  */
124
125 struct ps3_cui {
126         struct pmenu *mm;
127         struct pmenu *svm;
128         struct cui *cui;
129         struct ps3_flash_values values;
130         int dirty_values;
131 };
132
133 static struct ps3_cui *ps3_from_cui(struct cui *cui)
134 {
135         struct ps3_cui *ps3;
136
137         assert(cui->c_sig == pb_cui_sig);
138         ps3 = cui->platform_info;
139         assert(ps3->cui->c_sig == pb_cui_sig);
140         return ps3;
141 }
142
143 static struct ps3_cui *ps3_from_item(struct pmenu_item *item)
144 {
145         return ps3_from_cui(cui_from_item(item));
146 }
147
148 /**
149  * ps3_set_mode - Set video mode helper.
150  *
151  * Runs ps3_set_video_mode().
152  */
153
154 static void ps3_set_mode(struct ps3_cui *ps3, unsigned int mode)
155 {
156         int result;
157
158         if (ps3->values.video_mode == (uint16_t)mode)
159                 return;
160
161         ps3->values.video_mode = (uint16_t)mode;
162         ps3->dirty_values = 1;
163
164         result = ps3_set_video_mode(mode);
165
166         if (result)
167                 nc_scr_status_printf(ps3->cui->current,
168                         "Failed: set_video_mode(%u)", mode);
169 }
170
171 /**
172  * ps3_svm_cb - The set video mode callback.
173  */
174
175 static int ps3_svm_cb(struct pmenu_item *item)
176 {
177         ps3_set_mode(ps3_from_item(item), (unsigned int)item->data);
178         return 0;
179 }
180
181 /**
182  * ps3_kexec_cb - The kexec callback.
183  *
184  * Writes config data to PS3 flash then calls pb_run_kexec().
185  */
186
187 static int ps3_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
188 {
189         struct ps3_cui *ps3 = ps3_from_cui(cui);
190
191         pb_log("%s: %s:%s\n", __func__, cod->dev->name, cod->opt->name);
192
193         assert(ps3->cui->current == &ps3->cui->main->scr);
194
195         if (cui->default_item != cod->opt_hash || ps3->dirty_values) {
196                 ps3->values.default_item = cod->opt_hash;
197                 ps3_flash_set_values(&ps3->values);
198         }
199
200         return pb_run_kexec(cod->kd);
201 }
202
203 /**
204  * ps3_mm_to_svm_cb - Callback to switch to the set video mode menu.
205  */
206
207 static int ps3_mm_to_svm_cb(struct pmenu_item *item)
208 {
209         struct ps3_cui *ps3 = ps3_from_item(item);
210         struct nc_scr *old;
211
212         old = cui_set_current(ps3->cui, &ps3->svm->scr);
213         assert(old == &ps3->mm->scr);
214
215         return 0;
216 }
217
218 /**
219  * ps3_svm_to_mm_cb - Callback to switch back to the main menu.
220  */
221
222 static int ps3_svm_to_mm_cb(struct pmenu_item *item)
223 {
224         struct ps3_cui *ps3 = ps3_from_item(item);
225         struct nc_scr *old;
226
227         old = cui_set_current(ps3->cui, &ps3->mm->scr);
228         assert(old == &ps3->svm->scr);
229
230         return 0;
231 }
232
233 /**
234  * ps3_svm_to_mm_helper - The svm ESC callback.
235  */
236
237 static void ps3_svm_to_mm_helper(struct pmenu *menu)
238 {
239         ps3_svm_to_mm_cb(pmenu_find_selected(menu));
240 }
241
242 /**
243  * ps3_hot_key - PS3 specific hot keys.
244  *
245  * '@' = Set video mode to auto (mode 0)
246  * '$' = Set video mode to safe (480i)
247  * '+' = Cycles through a set of common video modes.
248  * '-' = Cycles through a set of common video modes in reverse.
249  */
250
251 static int ps3_hot_key(struct pmenu __attribute__((unused)) *menu,
252         struct pmenu_item *item, int c)
253 {
254         static const unsigned int modes[] = {0, 1, 6, 3, 11, 12};
255         static const unsigned int *const end = modes
256                 + sizeof(modes) / sizeof(modes[0]) - 1;
257         static const unsigned int *p = modes;
258
259         switch (c) {
260         default:
261                 /* DBGS("%d (%o)\n", c, c); */
262                 break;
263         case '@':
264                 p = modes + 0;
265                 ps3_set_mode(ps3_from_item(item), *p);
266                 break;
267         case '$':
268                 p = modes + 1;
269                 ps3_set_mode(ps3_from_item(item), *p);
270                 break;
271         case '+':
272                 p = (p < end) ? p + 1 : modes;
273                 ps3_set_mode(ps3_from_item(item), *p);
274                 break;
275         case '-':
276                 p = (p > modes) ? p - 1 : end;
277                 ps3_set_mode(ps3_from_item(item), *p);
278                 break;
279         }
280
281         return c;
282 }
283
284 /**
285  * ps3_timer_update - Timer callback.
286  */
287
288 static void ps3_timer_update(struct ui_timer *timer, unsigned int timeout)
289 {
290         struct ps3_cui *ps3 = ps3_from_cui(cui_from_timer(timer));
291
292         //FIXME: make scr:timer.
293         // nc_scr_timer_update(&ps3.mm->scr, timeout);
294
295         nc_scr_status_printf(&ps3->mm->scr,
296                 "Welcome to Petitboot (timeout %u sec)", timeout);
297 }
298
299 /**
300  * ps3_mm_init - Setup the main menu instance.
301  */
302
303 static struct pmenu *ps3_mm_init(struct ps3_cui *ps3_cui)
304 {
305         int result;
306         struct pmenu *m;
307         struct pmenu_item *i;
308         static const char *const bgo[] =
309                 {"/usr/sbin/ps3-boot-game-os-NOT", NULL};
310
311         m = pmenu_init(ps3_cui->cui, 3, cui_on_exit);
312
313         if (!m) {
314                 pb_log("%s: failed\n", __func__);
315                 return NULL;
316         }
317
318         m->hot_key = ps3_hot_key;
319         m->scr.frame.title = talloc_strdup(m, "Petitboot PS3");
320         m->scr.frame.help = talloc_strdup(m,
321                 "ESC=exit, Enter=accept, E,e=edit");
322         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
323
324         i = pmenu_item_init(m, 0, "Boot GameOS",
325                 "Reboot the PS3 into the GameOS");
326         i->on_execute = cui_run_cmd;
327         i->data = (void *)bgo;
328
329         i = pmenu_item_init(m, 1, "Set Video Mode",
330                 "Display a video mode selection menu");
331         i->on_execute = ps3_mm_to_svm_cb;
332
333         i = pmenu_item_init(m, 2, "Exit to Shell",
334                 "Exit petitboot and return to a shell prompt");
335         i->on_execute = pmenu_exit_cb;
336
337         result = pmenu_setup(m);
338
339         if (result) {
340                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
341                         strerror(errno));
342                 goto fail_setup;
343         }
344
345         menu_opts_off(m->ncm, O_SHOWDESC);
346         set_menu_mark(m->ncm, " *");
347         set_current_item(m->ncm, i->nci);
348
349         return m;
350
351 fail_setup:
352         talloc_free(m);
353         return NULL;
354 }
355
356 /**
357  * ps3_svm_init - Setup the set video mode menu instance.
358  */
359
360 static struct pmenu *ps3_svm_init(struct ps3_cui *ps3_cui)
361 {
362         int result;
363         struct pmenu *m;
364         struct pmenu_item *i;
365
366         m = pmenu_init(ps3_cui->cui, 12, ps3_svm_to_mm_helper);
367
368         if (!m) {
369                 pb_log("%s: failed\n", __func__);
370                 return NULL;
371         }
372
373         m->hot_key = ps3_hot_key;
374         m->scr.frame.title = talloc_strdup(m, "Select PS3 Video Mode");
375         m->scr.frame.help = talloc_strdup(m, "ESC=exit, Enter=accept");
376
377         i = pmenu_item_init(m, 0, "auto detect",
378                 "Auto detect the best HDMI video mode");
379         i->on_execute = ps3_svm_cb;
380         i->data = (void *)0;
381
382         i = pmenu_item_init(m, 1, "480i    (576 x 384)", NULL);
383         i->on_execute = ps3_svm_cb;
384         i->data = (void *)1;
385
386         i = pmenu_item_init(m, 2, "480p    (576 x 384)", NULL);
387         i->on_execute = ps3_svm_cb;
388         i->data = (void *)2;
389
390         i = pmenu_item_init(m, 3, "576i    (576 x 460)", NULL);
391         i->on_execute = ps3_svm_cb;
392         i->data = (void *)6;
393
394         i = pmenu_item_init(m, 4, "576p    (576 x 460)", NULL);
395         i->on_execute = ps3_svm_cb;
396         i->data = (void *)7;
397
398         i = pmenu_item_init(m, 5, "720p   (1124 x 644)", NULL);
399         i->on_execute = ps3_svm_cb;
400         i->data = (void *)3;
401
402         i = pmenu_item_init(m, 6, "1080i  (1688 x 964)", NULL);
403         i->on_execute = ps3_svm_cb;
404         i->data = (void *)4;
405
406         i = pmenu_item_init(m, 7, "1080p  (1688 x 964)", NULL);
407         i->on_execute = ps3_svm_cb;
408         i->data = (void *)5;
409
410         i = pmenu_item_init(m, 8, "wxga   (1280 x 768)", NULL);
411         i->on_execute = ps3_svm_cb;
412         i->data = (void *)11;
413
414         i = pmenu_item_init(m, 9, "sxga   (1280 x 1024)", NULL);
415         i->on_execute = ps3_svm_cb;
416         i->data = (void *)12;
417
418         i = pmenu_item_init(m, 10, "wuxga  (1920 x 1200)", NULL);
419         i->on_execute = ps3_svm_cb;
420         i->data = (void *)13;
421
422         i = pmenu_item_init(m, 11, "Return",
423                 "Return to the main menu");
424         i->on_execute = ps3_svm_to_mm_cb;
425
426         result = pmenu_setup(m);
427
428         if (result) {
429                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
430                         strerror(errno));
431                 goto fail_setup;
432         }
433
434         menu_opts_off(m->ncm, O_SHOWDESC);
435         set_menu_mark(m->ncm, " *");
436
437         return m;
438
439 fail_setup:
440         talloc_free(m);
441         return NULL;
442 }
443
444 static struct ps3_cui ps3;
445
446 static void sig_handler(int signum)
447 {
448         DBGS("%d\n", signum);
449
450         switch (signum) {
451         case SIGALRM:
452                 if (ps3.cui)
453                         ui_timer_sigalrm(&ps3.cui->timer);
454                 break;
455         case SIGWINCH:
456                 if (ps3.cui)
457                         cui_resize(ps3.cui);
458                 break;
459         default:
460                 assert(0 && "unknown sig");
461                 /* fall through */
462         case SIGINT:
463         case SIGHUP:
464         case SIGTERM:
465                 if (ps3.cui)
466                         cui_abort(ps3.cui);
467                 break;
468         }
469 }
470
471 /**
472  * main - cui bootloader main routine.
473  */
474
475 int main(int argc, char *argv[])
476 {
477         static struct sigaction sa;
478         static struct opts opts;
479         int result;
480         int cui_result;
481         unsigned int mode;
482         FILE *log;
483
484         result = opts_parse(&opts, argc, argv);
485
486         if (result) {
487                 print_usage();
488                 return EXIT_FAILURE;
489         }
490
491         if (opts.show_help == opt_yes) {
492                 print_usage();
493                 return EXIT_SUCCESS;
494         }
495
496         if (opts.show_version == opt_yes) {
497                 print_version();
498                 return EXIT_SUCCESS;
499         }
500
501         log = fopen(opts.log_file, "a");
502         assert(log);
503         pb_log_set_stream(log);
504
505 #if defined(DEBUG)
506         pb_log_always_flush(1);
507 #endif
508
509         pb_log("--- pb-cui ---\n");
510
511         sa.sa_handler = sig_handler;
512         result = sigaction(SIGALRM, &sa, NULL);
513         result += sigaction(SIGHUP, &sa, NULL);
514         result += sigaction(SIGINT, &sa, NULL);
515         result += sigaction(SIGTERM, &sa, NULL);
516         result += sigaction(SIGWINCH, &sa, NULL);
517
518         if (result) {
519                 pb_log("%s sigaction failed.\n", __func__);
520                 return EXIT_FAILURE;
521         }
522
523         ps3.dirty_values = ps3_flash_get_values(&ps3.values);
524
525         result = ps3_get_video_mode(&mode);
526
527         /* Current becomes default if ps3_flash_get_values() failed. */
528
529         if (ps3.dirty_values && !result)
530                 ps3.values.video_mode = mode;
531
532         /* Set mode if not at default. */
533
534         if (!result && (ps3.values.video_mode != (uint16_t)mode))
535                 ps3_set_video_mode(ps3.values.video_mode);
536
537         ps3.cui = cui_init(&ps3, ps3_kexec_cb);
538
539         if (!ps3.cui)
540                 return EXIT_FAILURE;
541
542         ps3.mm = ps3_mm_init(&ps3);
543         ps3.svm = ps3_svm_init(&ps3);
544
545         if (ps3.values.timeout == ps3_timeout_forever)
546                 ui_timer_disable(&ps3.cui->timer);
547         else {
548                 ps3.cui->timer.update_display = ps3_timer_update;
549                 ui_timer_init(&ps3.cui->timer, ps3.values.timeout);
550         }
551
552         cui_result = cui_run(ps3.cui, ps3.mm, ps3.values.default_item);
553
554         pmenu_delete(ps3.mm);
555         pmenu_delete(ps3.svm);
556
557         if (ps3.dirty_values)
558                 ps3_flash_set_values(&ps3.values);
559
560         talloc_free(ps3.cui);
561
562         pb_log("--- end ---\n");
563
564         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
565 }