]> git.ozlabs.org Git - petitboot/blobdiff - petitboot.c
Move path maniuplation functions to devices/paths.c
[petitboot] / petitboot.c
index 478b294e7ec0f8d07dd9f643e2a5e299027b1678..fc72fd8103c95f2c59bc235ccf36fa49a394597f 100644 (file)
@@ -5,6 +5,8 @@
 #include <unistd.h>
 #include <syscall.h>
 #include <assert.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
 
 #include <linux/input.h>
 
@@ -12,6 +14,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>
 
@@ -28,7 +31,8 @@ static twin_fbdev_t *pboot_fbdev;
 
 static twin_screen_t *pboot_screen;
 
-#define PBOOT_INITIAL_MESSAGE          "Petitboot v0.0.1"
+#define PBOOT_INITIAL_MESSAGE          \
+       "keys: 0=safe 1=720p 2=1080i 3=1080p del=GameOS"
 
 #define PBOOT_LEFT_PANE_SIZE           160
 #define PBOOT_LEFT_PANE_COLOR          0x80000000
@@ -138,6 +142,32 @@ 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 */
 static inline twin_bool_t twin_rect_intersect(twin_rect_t r1,
                                              twin_rect_t r2)
@@ -285,7 +315,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;
@@ -516,12 +546,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;
@@ -566,12 +614,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) {
@@ -686,6 +735,11 @@ static twin_bool_t pboot_lpane_event (twin_window_t            *window,
        return TWIN_FALSE;
 }
 
+static void pboot_quit(void)
+{
+       kill(0, SIGINT);
+}
+
 twin_bool_t pboot_event_filter(twin_screen_t       *screen,
                               twin_event_t         *event)
 {
@@ -700,7 +754,46 @@ 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:
+               switch(event->u.key.key) {
+               /* Gross hack for video modes, need something better ! */
+               case KEY_0:
+                       pboot_vmode_change = 0; /* auto */
+                       pboot_quit();
+                       return TWIN_TRUE;
+               case KEY_1:
+                       pboot_vmode_change = 3; /* 720p */
+                       pboot_quit();
+                       return TWIN_TRUE;
+               case KEY_2:
+                       pboot_vmode_change = 4; /* 1080i */
+                       pboot_quit();
+                       return TWIN_TRUE;
+               case KEY_3:
+                       pboot_vmode_change = 5; /* 1080p */
+                       pboot_quit();
+                       return TWIN_TRUE;
+
+               /* Another gross hack for booting back to gameos */
+               case KEY_BACKSPACE:
+               case KEY_DELETE:
+                       system("boot-game-os");
+                       pboot_quit();
+               }
        case TwinEventKeyUp:
                twin_screen_set_cursor(pboot_screen, NULL, 0, 0);
                break;
@@ -827,6 +920,18 @@ static void pboot_spane_draw(twin_window_t *window)
        twin_path_destroy(path);
 }
 
+void pboot_message(const char *message)
+{
+       if (pboot_spane->text)
+               free(pboot_spane->text);
+       pboot_spane->text = strdup(message);
+       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);
+}
+
 static void pboot_create_spane(void)
 {
        pboot_spane = calloc(1, sizeof(pboot_spane_t));
@@ -846,7 +951,7 @@ static void pboot_create_spane(void)
 
        pboot_spane->window->draw = pboot_spane_draw;
        pboot_spane->window->client_data = pboot_spane;
-       pboot_spane->text = PBOOT_INITIAL_MESSAGE;
+       pboot_spane->text = strdup(PBOOT_INITIAL_MESSAGE);
        twin_window_show(pboot_spane->window);
        twin_window_queue_paint(pboot_spane->window);
 }
@@ -885,8 +990,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++) {
@@ -899,22 +1004,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 */
 
@@ -966,12 +1065,22 @@ static void pboot_make_background(void)
        twin_screen_set_background(pboot_screen, scaledpic);
 }
 
+#define PS3FB_IOCTL_SETMODE          _IOW('r',  1, int)
+#define PS3FB_IOCTL_GETMODE          _IOR('r',  2, int)
+
 static void exitfunc(void)
 {
 #ifndef _USE_X11
        if (pboot_fbdev)
                twin_fbdev_destroy(pboot_fbdev);
        pboot_fbdev = NULL;
+       if (pboot_vmode_change != -1) {
+               int fd = open("/dev/fb0", O_RDWR);
+               if (fd >= 0)
+                       ioctl(fd, PS3FB_IOCTL_SETMODE,
+                             (unsigned long)&pboot_vmode_change);
+               close(fd);
+       }
 #endif
 }
 
@@ -981,8 +1090,35 @@ static void sigint(int sig)
        syscall(__NR_exit);
 }
 
+static void usage(const char *progname)
+{
+       fprintf(stderr, "Usage: %s [-u] [-h]\n", progname);
+}
+
 int main(int argc, char **argv)
 {
+       int c;
+       int udev_trigger = 0;
+
+       for (;;) {
+               c = getopt(argc, argv, "u::h");
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'u':
+                       udev_trigger = 1;
+                       break;
+               case 'h':
+                       usage(argv[0]);
+                       return EXIT_SUCCESS;
+               default:
+                       fprintf(stderr, "Unknown option '%c'\n", c);
+                       usage(argv[0]);
+                       return EXIT_FAILURE;
+               }
+       }
+
        atexit(exitfunc);
        signal(SIGINT, sigint);
 
@@ -1002,9 +1138,10 @@ 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");
+               char *cursor_path = artwork_pathname("cursor.gz");
                pboot_cursor = twin_load_X_cursor(cursor_path, 2,
                                                  &pboot_cursor_hx,
                                                  &pboot_cursor_hy);
@@ -1023,7 +1160,7 @@ int main(int argc, char **argv)
        pboot_create_rpane();
        pboot_create_spane();
 
-       if (!pboot_start_device_discovery()) {
+       if (!pboot_start_device_discovery(udev_trigger)) {
                LOG("Couldn't start device discovery!\n");
                return 1;
        }