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