]> git.ozlabs.org Git - petitboot/blobdiff - petitboot.c
Unify boot messages
[petitboot] / petitboot.c
index ea7dc57619d1df9f540f6a8642182f3a621f5777..ee6893032e6d033be6ddd9d6e8cac16c68a2695c 100644 (file)
@@ -1,3 +1,6 @@
+
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -14,6 +17,7 @@
 
 #include <libtwin/twin.h>
 #include <libtwin/twin_linux_mouse.h>
+#include <libtwin/twin_linux_js.h>
 #include <libtwin/twin_png.h>
 #include <libtwin/twin_jpeg.h>
 
@@ -31,7 +35,7 @@ static twin_fbdev_t *pboot_fbdev;
 static twin_screen_t *pboot_screen;
 
 #define PBOOT_INITIAL_MESSAGE          \
-       "video hack: 0=default 1=720p 2=1080i 3=1080p"  
+       "keys: 0=safe 1=720p 2=1080i 3=1080p del=GameOS"
 
 #define PBOOT_LEFT_PANE_SIZE           160
 #define PBOOT_LEFT_PANE_COLOR          0x80000000
@@ -141,6 +145,30 @@ static pboot_lpane_t       *pboot_lpane;
 static pboot_rpane_t   *pboot_rpane;
 static pboot_spane_t   *pboot_spane;
 
+/* control to keyboard mappings for the sixaxis controller */
+uint8_t sixaxis_map[] = {
+       0,              /*   0  Select          */
+       0,              /*   1  L3              */
+       0,              /*   2  R3              */
+       0,              /*   3  Start           */
+       KEY_UP,         /*   4  Dpad Up         */
+       KEY_RIGHT,      /*   5  Dpad Right      */
+       KEY_DOWN,       /*   6  Dpad Down       */
+       KEY_LEFT,       /*   7  Dpad Left       */
+       0,              /*   8  L2              */
+       0,              /*   9  R2              */
+       0,              /*  10  L1              */
+       0,              /*  11  R1              */
+       0,              /*  12  Triangle        */
+       KEY_ENTER,      /*  13  Circle          */
+       0,              /*  14  Cross           */
+       KEY_DELETE,     /*  15  Square          */
+       0,              /*  16  PS Button       */
+       0,              /*  17  nothing      */
+       0,              /*  18  nothing      */
+};
+
+
 static int pboot_vmode_change = -1;
 
 /* XXX move to twin */
@@ -290,7 +318,7 @@ static void pboot_rpane_draw(twin_window_t *window)
 static twin_time_t pboot_rfocus_timeout (twin_time_t now, void *closure)
 {
        int dir = 1, dist, pos;
-       const int accel[11] = { 7, 4, 2, 1, 1, 1, 1, 2, 3, 4, 5 };
+       const int accel[11] = { 7, 4, 2, 1, 1, 1, 1, 1, 2, 2, 3 };
 
        dist = abs(pboot_rpane->focus_target - pboot_rpane->focus_start);
        dir = dist > 5 ? 5 : dist;
@@ -432,6 +460,7 @@ static void pboot_choose_option(void)
        pboot_option_t *opt = &dev->options[pboot_rpane->focus_curindex];
 
        LOG("Selected device %s\n", opt->title);
+       pboot_message("booting %s...", opt->title);
 
        /* Give user feedback, make sure errors and panics will be seen */
        pboot_exec_option(opt->data);
@@ -521,12 +550,30 @@ int pboot_add_option(int devindex, const char *title,
 }
 
 
-static void pboot_set_device_select(int sel)
+static void pboot_set_device_select(int sel, int force)
 {
        LOG("%s: %d -> %d\n", __FUNCTION__, pboot_dev_sel, sel);
-       if (sel == pboot_dev_sel || sel >= pboot_dev_count)
+       if (!force && sel == pboot_dev_sel)
+               return;
+       if (sel >= pboot_dev_count)
                return;
        pboot_dev_sel = sel;
+       if (force) {
+               pboot_lpane->focus_curindex = sel;
+               if (sel < 0)
+                       pboot_lpane->focus_target = 0 - PBOOT_LEFT_FOCUS_HEIGHT;
+               else
+                       pboot_lpane->focus_target = PBOOT_LEFT_FOCUS_YOFF +
+                               PBOOT_LEFT_ICON_STRIDE * sel;
+               pboot_rpane->focus_box.bottom = pboot_lpane->focus_target;
+               pboot_rpane->focus_box.bottom = pboot_rpane->focus_box.top +
+                       PBOOT_RIGHT_FOCUS_HEIGHT;
+               twin_window_damage(pboot_lpane->window,
+                                  0, 0,
+                                  pboot_lpane->window->pixmap->width,
+                                  pboot_lpane->window->pixmap->height);
+               twin_window_queue_paint(pboot_lpane->window);
+       }
        pboot_rpane->focus_curindex = -1;
        pboot_rpane->mouse_target = -1;
        pboot_rpane->focus_box.top = -2*PBOOT_RIGHT_FOCUS_HEIGHT;
@@ -571,12 +618,13 @@ static void pboot_create_rpane(void)
 static twin_time_t pboot_lfocus_timeout (twin_time_t now, void *closure)
 {
        int dir = 1, dist, pos;
-       const int accel[11] = { 7, 4, 2, 1, 1, 1, 1, 2, 3, 4, 5 };
+       const int accel[11] = { 7, 4, 2, 1, 1, 1, 1, 1, 2, 2, 3 };
 
        dist = abs(pboot_lpane->focus_target - pboot_lpane->focus_start);
+       dir = dist > 2 ? 2 : dist;
        pos = pboot_lpane->focus_target - (int)pboot_lpane->focus_box.top;
        if (pos == 0) {
-               pboot_set_device_select(pboot_lpane->focus_curindex);
+               pboot_set_device_select(pboot_lpane->focus_curindex, 0);
                return -1;
        }
        if (pos < 0) {
@@ -710,9 +758,23 @@ twin_bool_t pboot_event_filter(twin_screen_t           *screen,
                                               pboot_cursor_hx,
                                               pboot_cursor_hy);
                break;
+       case TwinEventJoyButton:
+               /* map joystick events into key events */
+               if (event->u.js.control >= sizeof(sixaxis_map))
+                       break;
+
+               event->u.key.key = sixaxis_map[event->u.js.control];
+               if (event->u.js.value == 0) {
+                       event->kind = TwinEventKeyUp;
+                       break;
+               } else {
+                       event->kind = TwinEventKeyDown;
+               }
+
+               /* fall through.. */
        case TwinEventKeyDown:
-               /* Gross hack for video modes, need something better ! */
                switch(event->u.key.key) {
+               /* Gross hack for video modes, need something better ! */
                case KEY_0:
                        pboot_vmode_change = 0; /* auto */
                        pboot_quit();
@@ -729,6 +791,12 @@ twin_bool_t pboot_event_filter(twin_screen_t           *screen,
                        pboot_vmode_change = 5; /* 1080p */
                        pboot_quit();
                        return TWIN_TRUE;
+
+               /* Another gross hack for booting back to gameos */
+               case KEY_BACKSPACE:
+               case KEY_DELETE:
+                       pboot_message("booting to GameOS...");
+                       system(BOOT_GAMEOS_BIN);
                }
        case TwinEventKeyUp:
                twin_screen_set_cursor(pboot_screen, NULL, 0, 0);
@@ -856,16 +924,24 @@ static void pboot_spane_draw(twin_window_t *window)
        twin_path_destroy(path);
 }
 
-void pboot_message(const char *message)
+void pboot_message(const char *fmt, ...)
 {
+       va_list ap;
+       char *msg;
+
        if (pboot_spane->text)
                free(pboot_spane->text);
-       pboot_spane->text = strdup(message);
+
+       va_start(ap, fmt);
+       vasprintf(&msg, fmt, ap);
+       va_end(ap);
+
+       pboot_spane->text = msg;
        twin_window_damage(pboot_spane->window,
                           0, 0,
                           pboot_spane->window->pixmap->width,
                           pboot_spane->window->pixmap->height);
-       twin_window_queue_paint(pboot_spane->window);
+       twin_window_draw(pboot_spane->window);
 }
 
 static void pboot_create_spane(void)
@@ -926,8 +1002,8 @@ int pboot_add_device(const char *dev_id, const char *name,
 
 int pboot_remove_device(const char *dev_id)
 {
-       int             i, new_dev_index;
        pboot_device_t  *dev = NULL;
+       int             i, newsel = pboot_dev_sel;
 
        /* find the matching device */
        for (i = 0; i < pboot_dev_count; i++) {
@@ -940,22 +1016,16 @@ int pboot_remove_device(const char *dev_id)
        if (!dev)
                return TWIN_FALSE;
 
-       /* select the newly-focussed device */
-       if (i == pboot_dev_count - 1)
-               new_dev_index = i - 1;
-       else
-               new_dev_index = i + 1;
-
        memmove(pboot_devices + i, pboot_devices + i + 1,
                        sizeof(*pboot_devices) * (pboot_dev_count + i - 1));
-
        pboot_devices[--pboot_dev_count] = NULL;
 
-       pboot_set_device_select(new_dev_index);
-       twin_window_damage(pboot_lpane->window,
-                          dev->box.left, dev->box.top,
-                          dev->box.right, dev->box.bottom);
-       twin_window_queue_paint(pboot_lpane->window);
+       /* select the newly-focussed device */
+       if (pboot_dev_sel > i)
+               newsel = pboot_dev_sel - 1;
+       else if (pboot_dev_sel == i && i >= pboot_dev_count)
+                       newsel = pboot_dev_count - 1;
+       pboot_set_device_select(newsel, 1);
 
        /* todo: free device & options */
 
@@ -1080,6 +1150,7 @@ int main(int argc, char **argv)
        }
        pboot_screen = pboot_fbdev->screen;
        twin_linux_mouse_create(NULL, pboot_screen);
+       twin_linux_js_create(pboot_screen);
 
        if (pboot_fbdev != NULL) {
                char *cursor_path = artwork_pathname("cursor.gz");