]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/ps3-main.c
ui/ncurses: Fix warning from unnecessary if statement
[petitboot] / ui / ncurses / ps3-main.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] [-r, --reset-defaults]\n"
56 "              [-t, --timeout] [-V, --version]\n");
57 }
58
59 /**
60  * enum opt_value - Tri-state options variables.
61  */
62
63 enum opt_value {opt_undef = 0, opt_yes, opt_no};
64
65 /**
66  * struct opts - Values from command line options.
67  */
68
69 struct opts {
70         enum opt_value show_help;
71         const char *log_file;
72         enum opt_value reset_defaults;
73         enum opt_value use_timeout;
74         enum opt_value show_version;
75 };
76
77 /**
78  * opts_parse - Parse the command line options.
79  */
80
81 static int opts_parse(struct opts *opts, int argc, char *argv[])
82 {
83         static const struct option long_options[] = {
84                 {"help",           no_argument,       NULL, 'h'},
85                 {"log",            required_argument, NULL, 'l'},
86                 {"reset-defaults", no_argument,       NULL, 'r'},
87                 {"timeout",        no_argument,       NULL, 't'},
88                 {"version",        no_argument,       NULL, 'V'},
89                 { NULL, 0, NULL, 0},
90         };
91         static const char short_options[] = "hl:trV";
92         static const struct opts default_values = {
93                 .log_file = "/var/log/petitboot/petitboot-nc.log",
94         };
95
96         *opts = default_values;
97
98         while (1) {
99                 int c = getopt_long(argc, argv, short_options, long_options,
100                         NULL);
101
102                 if (c == EOF)
103                         break;
104
105                 switch (c) {
106                 case 'h':
107                         opts->show_help = opt_yes;
108                         break;
109                 case 'l':
110                         opts->log_file = optarg;
111                         break;
112                 case 't':
113                         opts->use_timeout = opt_yes;
114                         break;
115                 case 'r':
116                         opts->reset_defaults = opt_yes;
117                         break;
118                 case 'V':
119                         opts->show_version = opt_yes;
120                         break;
121                 default:
122                         opts->show_help = opt_yes;
123                         return -1;
124                 }
125         }
126
127         return optind != argc;
128 }
129
130 /**
131  * struct ps3_cui - Main cui program instance.
132  * @mm: Main menu.
133  * @svm: Set video mode menu.
134  */
135
136 struct ps3_cui {
137         struct pmenu *mm;
138         struct pmenu *svm;
139         struct cui *cui;
140         struct ps3_flash_values values;
141         int dirty_values;
142 };
143
144 static struct ps3_cui *ps3_from_cui(struct cui *cui)
145 {
146         struct ps3_cui *ps3;
147
148         assert(cui->c_sig == pb_cui_sig);
149         ps3 = cui->platform_info;
150         assert(ps3->cui->c_sig == pb_cui_sig);
151         return ps3;
152 }
153
154 static struct ps3_cui *ps3_from_item(struct pmenu_item *item)
155 {
156         return ps3_from_cui(cui_from_item(item));
157 }
158
159 /**
160  * ps3_sixaxis_map - Map a Linux joystick event to an ncurses key code.
161  *
162  */
163
164 static int ps3_sixaxis_map(const struct js_event *e)
165 {
166 #if 0
167         static const int axis_map[] = {
168                 0,              /*   0  Left thumb X    */
169                 0,              /*   1  Left thumb Y    */
170                 0,              /*   2  Right thumb X   */
171                 0,              /*   3  Right thumb Y   */
172                 0,              /*   4  nothing         */
173                 0,              /*   5  nothing         */
174                 0,              /*   6  nothing         */
175                 0,              /*   7  nothing         */
176                 0,              /*   8  Dpad Up         */
177                 0,              /*   9  Dpad Right      */
178                 0,              /*  10  Dpad Down       */
179                 0,              /*  11  Dpad Left       */
180                 0,              /*  12  L2              */
181                 0,              /*  13  R2              */
182                 0,              /*  14  L1              */
183                 0,              /*  15  R1              */
184                 0,              /*  16  Triangle        */
185                 0,              /*  17  Circle          */
186                 0,              /*  18  Cross           */
187                 0,              /*  19  Square          */
188                 0,              /*  20  nothing         */
189                 0,              /*  21  nothing         */
190                 0,              /*  22  nothing         */
191                 0,              /*  23  nothing         */
192                 0,              /*  24  nothing         */
193                 0,              /*  25  nothing         */
194                 0,              /*  26  nothing         */
195                 0,              /*  27  nothing         */
196         };
197 #endif
198         static const int button_map[] = {
199                 0,              /*   0  Select          */
200                 0,              /*   1  L3              */
201                 0,              /*   2  R3              */
202                 0,              /*   3  Start           */
203                 KEY_UP,         /*   4  Dpad Up         */
204                 0,              /*   5  Dpad Right      */
205                 KEY_DOWN,       /*   6  Dpad Down       */
206                 0,              /*   7  Dpad Left       */
207                 KEY_UP,         /*   8  L2              */
208                 KEY_DOWN,       /*   9  R2              */
209                 KEY_HOME,       /*  10  L1              */
210                 KEY_END,        /*  11  R1              */
211                 0,              /*  12  Triangle        */
212                 0,              /*  13  Circle          */
213                 13,             /*  14  Cross           */
214                 0,              /*  15  Square          */
215                 0,              /*  16  PS Button       */
216                 0,              /*  17  nothing         */
217                 0,              /*  18  nothing         */
218         };
219
220         if (!e->value)
221                 return 0;
222
223         if (e->type == JS_EVENT_BUTTON
224                 && e->number < sizeof(button_map) / sizeof(button_map[0]))
225                 return button_map[e->number];
226
227 #if 0
228         if (e->type == JS_EVENT_AXIS
229                 && e->number < sizeof(axis_map) / sizeof(axis_map[0]))
230                 return axis_map[e->number];
231 #endif
232
233         return 0;
234 }
235
236 /**
237  * ps3_set_mode - Set video mode helper.
238  *
239  * Runs ps3_set_video_mode().
240  */
241
242 static void ps3_set_mode(struct ps3_cui *ps3, unsigned int mode)
243 {
244         int result;
245
246         if (ps3->values.video_mode == (uint16_t)mode)
247                 return;
248
249         ps3->values.video_mode = (uint16_t)mode;
250         ps3->dirty_values = 1;
251
252         result = ps3_set_video_mode(mode);
253
254         if (result)
255                 nc_scr_status_printf(ps3->cui->current,
256                         "Failed: set_video_mode(%u)", mode);
257 }
258
259 /**
260  * ps3_svm_cb - The set video mode callback.
261  */
262
263 static int ps3_svm_cb(struct pmenu_item *item)
264 {
265         ps3_set_mode(ps3_from_item(item), (unsigned int)item->data);
266         return 0;
267 }
268
269 /**
270  * ps3_kexec_cb - The kexec callback.
271  *
272  * Writes config data to PS3 flash then calls pb_run_kexec().
273  * Adds a video mode arg to the kernel command line if needed.
274  */
275
276 static int ps3_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
277 {
278         struct ps3_cui *ps3 = ps3_from_cui(cui);
279         int result;
280         int altered_args;
281         char *orig_args;
282
283         pb_log("%s: %s\n", __func__, cod->name);
284
285         assert(ps3->cui->current == &ps3->cui->main->scr);
286
287         /* Save values to flash if needed */
288
289         if ((cod->opt_hash && cod->opt_hash != cui->default_item)
290                 || ps3->dirty_values) {
291                 ps3->values.default_item = cod->opt_hash;
292                 ps3_flash_set_values(&ps3->values);
293         }
294
295         /* Add a default kernel video mode. */
296
297         if (!cod->kd->args) {
298                 altered_args = 1;
299                 orig_args = NULL;
300                 cod->kd->args = talloc_asprintf(NULL, "video=ps3fb:mode:%u",
301                         (unsigned int)ps3->values.video_mode);
302         } else if (!strstr(cod->kd->args, "video=")) {
303                 altered_args = 1;
304                 orig_args = cod->kd->args;
305                 cod->kd->args = talloc_asprintf(NULL, "%s video=ps3fb:mode:%u",
306                         orig_args, (unsigned int)ps3->values.video_mode);
307         } else
308                 altered_args = 0;
309
310         result = pb_run_kexec(cod->kd, ps3->cui->dry_run);
311
312         if (altered_args) {
313                 talloc_free(cod->kd->args);
314                 cod->kd->args = orig_args;
315         }
316
317         return result;
318 }
319
320 /**
321  * ps3_mm_to_svm_cb - Callback to switch to the set video mode menu.
322  */
323
324 static int ps3_mm_to_svm_cb(struct pmenu_item *item)
325 {
326         struct ps3_cui *ps3 = ps3_from_item(item);
327         struct nc_scr *old;
328
329         old = cui_set_current(ps3->cui, &ps3->svm->scr);
330         assert(old == &ps3->mm->scr);
331
332         return 0;
333 }
334
335 /**
336  * ps3_svm_to_mm_cb - Callback to switch back to the main menu.
337  */
338
339 static int ps3_svm_to_mm_cb(struct pmenu_item *item)
340 {
341         struct ps3_cui *ps3 = ps3_from_item(item);
342         struct nc_scr *old;
343
344         old = cui_set_current(ps3->cui, &ps3->mm->scr);
345         assert(old == &ps3->svm->scr);
346
347         return 0;
348 }
349
350 /**
351  * ps3_svm_to_mm_helper - The svm ESC callback.
352  */
353
354 static void ps3_svm_to_mm_helper(struct pmenu *menu)
355 {
356         ps3_svm_to_mm_cb(pmenu_find_selected(menu));
357 }
358
359 /**
360  * ps3_hot_key - PS3 specific hot keys.
361  *
362  * '@' = Set video mode to auto (mode 0)
363  * '$' = Set video mode to safe (480i)
364  * '+' = Cycles through a set of common video modes.
365  * '-' = Cycles through a set of common video modes in reverse.
366  */
367
368 static int ps3_hot_key(struct pmenu __attribute__((unused)) *menu,
369         struct pmenu_item *item, int c)
370 {
371         static const unsigned int modes[] = {0, 1, 6, 3, 11, 12};
372         static const unsigned int *const end = modes
373                 + sizeof(modes) / sizeof(modes[0]) - 1;
374         static const unsigned int *p = modes;
375
376         switch (c) {
377         default:
378                 /* DBGS("%d (%o)\n", c, c); */
379                 break;
380         case '@':
381                 p = modes + 0;
382                 ps3_set_mode(ps3_from_item(item), *p);
383                 break;
384         case '$':
385                 p = modes + 1;
386                 ps3_set_mode(ps3_from_item(item), *p);
387                 break;
388         case '+':
389                 p = (p < end) ? p + 1 : modes;
390                 ps3_set_mode(ps3_from_item(item), *p);
391                 break;
392         case '-':
393                 p = (p > modes) ? p - 1 : end;
394                 ps3_set_mode(ps3_from_item(item), *p);
395                 break;
396         }
397
398         return c;
399 }
400
401 /**
402  * ps3_timer_update - Timer callback.
403  */
404
405 static void ps3_timer_update(struct ui_timer *timer, unsigned int timeout)
406 {
407         struct ps3_cui *ps3 = ps3_from_cui(cui_from_timer(timer));
408
409         //FIXME: make scr:timer.
410         // nc_scr_timer_update(&ps3.mm->scr, timeout);
411
412         nc_scr_status_printf(&ps3->mm->scr,
413                 "Welcome to Petitboot (timeout %u sec)", timeout);
414 }
415
416 /**
417  * ps3_mm_init - Setup the main menu instance.
418  */
419
420 static struct pmenu *ps3_mm_init(struct ps3_cui *ps3_cui)
421 {
422         int result;
423         struct pmenu *m;
424         struct pmenu_item *i;
425         static const char *const bgo[] = {"/usr/sbin/ps3-boot-game-os", NULL};
426
427         m = pmenu_init(ps3_cui->cui, 3, cui_on_exit);
428
429         if (!m) {
430                 pb_log("%s: failed\n", __func__);
431                 return NULL;
432         }
433
434         m->hot_key = ps3_hot_key;
435         m->on_open = cui_on_open;
436
437 #if defined(DEBUG)
438         m->scr.frame.title = talloc_strdup(m,
439                 "Petitboot PS3 (" PACKAGE_VERSION ")");
440 #else
441         m->scr.frame.title = talloc_strdup(m, "Petitboot PS3");
442 #endif
443         m->scr.frame.help = talloc_strdup(m,
444                 "ESC=exit, Enter=accept, e=edit, o=open");
445         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
446
447         i = pmenu_item_init(m, 0, "Boot GameOS");
448         i->on_execute = cui_run_cmd;
449         i->data = (void *)bgo;
450
451         i = pmenu_item_init(m, 1, "Set Video Mode");
452         i->on_execute = ps3_mm_to_svm_cb;
453
454         i = pmenu_item_init(m, 2, "Exit to Shell");
455         i->on_execute = pmenu_exit_cb;
456
457         result = pmenu_setup(m);
458
459         if (result) {
460                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
461                         strerror(errno));
462                 goto fail_setup;
463         }
464
465         menu_opts_off(m->ncm, O_SHOWDESC);
466         set_menu_mark(m->ncm, " *");
467         set_current_item(m->ncm, i->nci);
468
469         return m;
470
471 fail_setup:
472         talloc_free(m);
473         return NULL;
474 }
475
476 /**
477  * ps3_svm_init - Setup the set video mode menu instance.
478  */
479
480 static struct pmenu *ps3_svm_init(struct ps3_cui *ps3_cui)
481 {
482         int result;
483         struct pmenu *m;
484         struct pmenu_item *i;
485
486         m = pmenu_init(ps3_cui->cui, 12, ps3_svm_to_mm_helper);
487
488         if (!m) {
489                 pb_log("%s: failed\n", __func__);
490                 return NULL;
491         }
492
493         m->hot_key = ps3_hot_key;
494         m->scr.frame.title = talloc_strdup(m, "Select PS3 Video Mode");
495         m->scr.frame.help = talloc_strdup(m, "ESC=exit, Enter=accept");
496
497         i = pmenu_item_init(m, 0, "auto detect");
498         i->on_execute = ps3_svm_cb;
499         i->data = (void *)0;
500
501         i = pmenu_item_init(m, 1, "480i    (576 x 384)");
502         i->on_execute = ps3_svm_cb;
503         i->data = (void *)1;
504
505         i = pmenu_item_init(m, 2, "480p    (576 x 384)");
506         i->on_execute = ps3_svm_cb;
507         i->data = (void *)2;
508
509         i = pmenu_item_init(m, 3, "576i    (576 x 460)");
510         i->on_execute = ps3_svm_cb;
511         i->data = (void *)6;
512
513         i = pmenu_item_init(m, 4, "576p    (576 x 460)");
514         i->on_execute = ps3_svm_cb;
515         i->data = (void *)7;
516
517         i = pmenu_item_init(m, 5, "720p   (1124 x 644)");
518         i->on_execute = ps3_svm_cb;
519         i->data = (void *)3;
520
521         i = pmenu_item_init(m, 6, "1080i  (1688 x 964)");
522         i->on_execute = ps3_svm_cb;
523         i->data = (void *)4;
524
525         i = pmenu_item_init(m, 7, "1080p  (1688 x 964)");
526         i->on_execute = ps3_svm_cb;
527         i->data = (void *)5;
528
529         i = pmenu_item_init(m, 8, "wxga   (1280 x 768)");
530         i->on_execute = ps3_svm_cb;
531         i->data = (void *)11;
532
533         i = pmenu_item_init(m, 9, "sxga   (1280 x 1024)");
534         i->on_execute = ps3_svm_cb;
535         i->data = (void *)12;
536
537         i = pmenu_item_init(m, 10, "wuxga  (1920 x 1200)");
538         i->on_execute = ps3_svm_cb;
539         i->data = (void *)13;
540
541         i = pmenu_item_init(m, 11, "Return");
542         i->on_execute = ps3_svm_to_mm_cb;
543
544         result = pmenu_setup(m);
545
546         if (result) {
547                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
548                         strerror(errno));
549                 goto fail_setup;
550         }
551
552         menu_opts_off(m->ncm, O_SHOWDESC);
553         set_menu_mark(m->ncm, " *");
554
555         return m;
556
557 fail_setup:
558         talloc_free(m);
559         return NULL;
560 }
561
562 static struct ps3_cui ps3;
563
564 static void sig_handler(int signum)
565 {
566         DBGS("%d\n", signum);
567
568         switch (signum) {
569         case SIGALRM:
570                 if (ps3.cui)
571                         ui_timer_sigalrm(&ps3.cui->timer);
572                 break;
573         case SIGWINCH:
574                 if (ps3.cui)
575                         cui_resize(ps3.cui);
576                 break;
577         default:
578                 assert(0 && "unknown sig");
579                 /* fall through */
580         case SIGINT:
581         case SIGHUP:
582         case SIGTERM:
583                 if (ps3.cui)
584                         cui_abort(ps3.cui);
585                 break;
586         }
587 }
588
589 /**
590  * main - cui bootloader main routine.
591  */
592
593 int main(int argc, char *argv[])
594 {
595         static struct sigaction sa;
596         static struct opts opts;
597         int result;
598         int cui_result;
599         unsigned int mode;
600
601         result = opts_parse(&opts, argc, argv);
602
603         if (result) {
604                 print_usage();
605                 return EXIT_FAILURE;
606         }
607
608         if (opts.show_help == opt_yes) {
609                 print_usage();
610                 return EXIT_SUCCESS;
611         }
612
613         if (opts.show_version == opt_yes) {
614                 print_version();
615                 return EXIT_SUCCESS;
616         }
617
618         if (strcmp(opts.log_file, "-")) {
619                 FILE *log = fopen(opts.log_file, "a");
620
621                 assert(log);
622                 pb_log_set_stream(log);
623         } else
624                 pb_log_set_stream(stderr);
625
626 #if defined(DEBUG)
627         pb_log_always_flush(1);
628 #endif
629
630         pb_log("--- pb-cui ---\n");
631
632         sa.sa_handler = sig_handler;
633         result = sigaction(SIGALRM, &sa, NULL);
634         result += sigaction(SIGHUP, &sa, NULL);
635         result += sigaction(SIGINT, &sa, NULL);
636         result += sigaction(SIGTERM, &sa, NULL);
637         result += sigaction(SIGWINCH, &sa, NULL);
638
639         if (result) {
640                 pb_log("%s sigaction failed.\n", __func__);
641                 return EXIT_FAILURE;
642         }
643
644         ps3.values = ps3_flash_defaults;
645
646         if (opts.reset_defaults != opt_yes)
647                 ps3.dirty_values = ps3_flash_get_values(&ps3.values);
648
649         result = ps3_get_video_mode(&mode);
650
651         /* Current becomes default if ps3_flash_get_values() failed. */
652
653         if (ps3.dirty_values && !result)
654                 ps3.values.video_mode = mode;
655
656         /* Set mode if not at default. */
657
658         if (!result && (ps3.values.video_mode != (uint16_t)mode))
659                 ps3_set_video_mode(ps3.values.video_mode);
660
661         ps3.cui = cui_init(&ps3, ps3_kexec_cb, ps3_sixaxis_map);
662
663         if (!ps3.cui)
664                 return EXIT_FAILURE;
665
666         ps3.mm = ps3_mm_init(&ps3);
667         ps3.svm = ps3_svm_init(&ps3);
668
669         if (opts.use_timeout != opt_yes
670                 || ps3.values.timeout == ps3_timeout_forever)
671                 ui_timer_disable(&ps3.cui->timer);
672         else {
673                 ps3.cui->timer.update_display = ps3_timer_update;
674                 ui_timer_init(&ps3.cui->timer, ps3.values.timeout);
675         }
676
677         cui_result = cui_run(ps3.cui, ps3.mm, ps3.values.default_item);
678
679         pmenu_delete(ps3.mm);
680         pmenu_delete(ps3.svm);
681
682         if (ps3.dirty_values)
683                 ps3_flash_set_values(&ps3.values);
684
685         talloc_free(ps3.cui);
686
687         pb_log("--- end ---\n");
688
689         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
690 }