From patchwork Wed Jul 8 00:11:58 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 29566 Return-Path: X-Original-To: patchwork-incoming@bilbo.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (ozlabs.org [203.10.76.45]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mx.ozlabs.org", Issuer "CA Cert Signing Authority" (verified OK)) by bilbo.ozlabs.org (Postfix) with ESMTPS id E792DB708B for ; Wed, 8 Jul 2009 10:23:47 +1000 (EST) Received: by ozlabs.org (Postfix) id C0646DEEB1; Wed, 8 Jul 2009 10:21:01 +1000 (EST) Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id BAB87DE6C1 for ; Wed, 8 Jul 2009 10:21:01 +1000 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from hera.kernel.org (hera.kernel.org [140.211.167.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 43AADDDEF1; Wed, 8 Jul 2009 10:18:26 +1000 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX19zZ4ieFgyYtsJ90bmE0VKMgZEBhLgYZdc@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n680ILiV026766 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 8 Jul 2009 00:18:21 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n680IKNF026765; Wed, 8 Jul 2009 00:18:20 GMT Message-Id: <20090708001136.567836577@am.sony.com> User-Agent: quilt/0.47-1 Date: Tue, 07 Jul 2009 17:11:58 -0700 From: Geoff Levand To: Jeremy Kerr In-Reply-To: <20090708001134.934244536@am.sony.com> References: <20090708001134.934244536@am.sony.com> Content-Disposition: inline; filename=nc-joystick.diff X-Virus-Scanned: ClamAV 0.93.3/9541/Tue Jul 7 17:31:53 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00, UNPARSEABLE_RELAY autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on hera.kernel.org X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (hera.kernel.org [127.0.0.1]); Wed, 08 Jul 2009 00:18:22 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 24/31] petitboot: Add ncurses joystick support X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Signed-off-by: Geoff Levand --- rules.mk | 9 ++-- ui/common/joystick.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ ui/common/joystick.h | 47 ++++++++++++++++++++++ ui/ncurses/nc-cui.c | 34 +++++++++++++++- ui/ncurses/nc-cui.h | 5 +- ui/ncurses/nc-ked.h | 1 ui/ncurses/nc-menu.c | 2 ui/ncurses/nc-menu.h | 1 ui/ncurses/nc-scr.h | 1 ui/ncurses/pb-cui.c | 2 ui/ncurses/ps3-cui.c | 79 +++++++++++++++++++++++++++++++++++++- 11 files changed, 278 insertions(+), 9 deletions(-) --- a/rules.mk +++ b/rules.mk @@ -48,10 +48,11 @@ discover_objs = discover/event.o discove discover/parser-utils.o # client objs -ui_common_objs = ui/common/discover-client.o ui/common/loader.o \ - ui/common/ui-system.o ui/common/timer.o ui/common/url.o -ncurses_objs = ui/ncurses/nc-scr.o ui/ncurses/nc-menu.o \ - ui/ncurses/nc-ked.o ui/ncurses/nc-cui.o +ui_common_objs = ui/common/discover-client.o ui/common/joystick.o \ + ui/common/loader.o ui/common/ui-system.o ui/common/timer.o \ + ui/common/url.o +ncurses_objs = ui/ncurses/nc-scr.o ui/ncurses/nc-menu.o ui/ncurses/nc-ked.o \ + ui/ncurses/nc-cui.o twin_objs = ui/twin/pb-twin.o # Makefiles --- /dev/null +++ b/ui/common/joystick.c @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2009 Sony Computer Entertainment Inc. + * Copyright 2009 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "log/log.h" +#include "talloc/talloc.h" +#include "joystick.h" + +/** + * pjs_process_event - Read joystick event and map to UI key code. + * + * Returns a map routine UI key code or zero. + */ + +int pjs_process_event(const struct pjs *pjs) +{ + int result; + struct js_event e; + + assert(pjs->fd); + + result = read(pjs->fd, &e, sizeof(e)); + + if (result != sizeof(e)) { + pb_log("%s: read failed: %s\n", __func__, strerror(errno)); + return 0; + } + + return pjs->map(&e); +} + +/** + * pjs_destructor - The talloc destructor for a joystick handler. + */ + +static int pjs_destructor(void *arg) +{ + struct pjs *pjs = pjs_from_arg(arg); + + close(pjs->fd); + pjs->fd = 0; + + return 0; +} + +/** + * pjs_init - Initialize the joystick event handler. + */ + +struct pjs *pjs_init(void *ctx, int (*map)(const struct js_event *)) +{ + static const char dev_name[] = "/dev/input/js0"; + struct pjs *pjs; + + pjs = talloc_zero(ctx, struct pjs); + + if (!pjs) + return NULL; + + pjs->map = map; + pjs->fd = open(dev_name, O_RDONLY | O_NONBLOCK); + + if (pjs->fd < 0) { + pb_log("%s: open %s failed: %s\n", __func__, dev_name, + strerror(errno)); + goto out_err; + } + + talloc_set_destructor(pjs, pjs_destructor); + + pb_log("%s: using %s\n", __func__, dev_name); + + return pjs; + +out_err: + close(pjs->fd); + pjs->fd = 0; + talloc_free(pjs); + return NULL; +} --- /dev/null +++ b/ui/common/joystick.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2009 Sony Computer Entertainment Inc. + * Copyright 2009 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if !defined(_PB_JOYSTICK_H) +#define _PB_JOYSTICK_H + +#include + +/** + * struct pjs - Petitboot joystick event handler. + * @map: Routine to map from a Linux struct js_event to a ui key code. + */ + +struct pjs { + int fd; + int (*map)(const struct js_event *e); +}; + +struct pjs *pjs_init(void *ctx, int (*map)(const struct js_event *)); +int pjs_process_event(const struct pjs *pjs); + +static inline struct pjs *pjs_from_arg(void *arg) +{ + return arg; +} + +static inline int pjs_get_fd(const struct pjs *pjs) +{ + return pjs->fd; +} + +#endif --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -230,6 +230,10 @@ struct nc_scr *cui_set_current(struct cu return old; } +/** + * cui_process_key - Process input on stdin. + */ + static int cui_process_key(void *arg) { struct cui *cui = cui_from_arg(arg); @@ -243,6 +247,24 @@ static int cui_process_key(void *arg) } /** + * cui_process_js - Process joystick events. + */ + +static int cui_process_js(void *arg) +{ + struct cui *cui = cui_from_arg(arg); + int c; + + c = pjs_process_event(cui->pjs); + + if (c) { + ungetch(c); + cui_process_key(arg); + } + + return 0; +} +/** * cui_client_process_socket - Process a socket event from the discover server. */ @@ -511,7 +533,8 @@ static struct discover_client_ops cui_cl */ struct cui *cui_init(void* platform_info, - int (*on_kexec)(struct cui *, struct cui_opt_data *)) + int (*on_kexec)(struct cui *, struct cui_opt_data *), + int (*js_map)(const struct js_event *e)) { struct cui *cui; struct discover_client *client; @@ -557,6 +580,15 @@ struct cui *cui_init(void* platform_info waiter_register(STDIN_FILENO, WAIT_IN, cui_process_key, cui); + if (js_map) { + + cui->pjs = pjs_init(cui, js_map); + + if (cui->pjs) + waiter_register(pjs_get_fd(cui->pjs), WAIT_IN, + cui_process_js, cui); + } + return cui; fail_client_init: --- a/ui/ncurses/nc-cui.h +++ b/ui/ncurses/nc-cui.h @@ -21,6 +21,7 @@ #include +#include "ui/common/joystick.h" #include "ui/common/timer.h" #include "nc-menu.h" #include "nc-ked.h" @@ -53,13 +54,15 @@ struct cui { struct nc_scr *current; struct pmenu *main; struct ui_timer timer; + struct pjs *pjs; void *platform_info; unsigned int default_item; int (*on_kexec)(struct cui *cui, struct cui_opt_data *cod); }; struct cui *cui_init(void* platform_info, - int (*on_kexec)(struct cui *, struct cui_opt_data *)); + int (*on_kexec)(struct cui *, struct cui_opt_data *), + int (*js_map)(const struct js_event *e)); struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr); int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item); int cui_ked_run(struct pmenu_item *item); --- a/ui/ncurses/nc-ked.h +++ b/ui/ncurses/nc-ked.h @@ -20,6 +20,7 @@ #define _PB_NC_KED_H #include +#include /* This must be included before ncurses.h */ #include #include "pb-protocol/pb-protocol.h" --- a/ui/ncurses/nc-menu.c +++ b/ui/ncurses/nc-menu.c @@ -204,7 +204,7 @@ static void pmenu_process_key(struct nc_ if (c == ERR) return; - /* DBGS("%d (%o)\n", c, c); */ + if (1) DBGS("%d (%o)\n", c, c); if (menu->hot_key) c = menu->hot_key(menu, item, c); --- a/ui/ncurses/nc-menu.h +++ b/ui/ncurses/nc-menu.h @@ -20,6 +20,7 @@ #define _PB_NC_MENU_H #include +#include /* This must be included before ncurses.h */ #include #include "log/log.h" --- a/ui/ncurses/nc-scr.h +++ b/ui/ncurses/nc-scr.h @@ -19,6 +19,7 @@ #if !defined(_PB_NC_SCR_H) #define _PB_NC_SCR_H +#include /* This must be included before ncurses.h */ #include #define DBG(fmt, args...) pb_log("DBG: " fmt, ## args) --- a/ui/ncurses/pb-cui.c +++ b/ui/ncurses/pb-cui.c @@ -269,7 +269,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - pb.cui = cui_init(&pb, pb_kexec_cb); + pb.cui = cui_init(&pb, pb_kexec_cb, NULL); if (!pb.cui) return EXIT_FAILURE; --- a/ui/ncurses/ps3-cui.c +++ b/ui/ncurses/ps3-cui.c @@ -157,6 +157,83 @@ static struct ps3_cui *ps3_from_item(str } /** + * ps3_sixaxis_map - Map a Linux joystick event to an ncurses key code. + * + */ + +static int ps3_sixaxis_map(const struct js_event *e) +{ +#if 0 + static const int axis_map[] = { + 0, /* 0 Left thumb X */ + 0, /* 1 Left thumb Y */ + 0, /* 2 Right thumb X */ + 0, /* 3 Right thumb Y */ + 0, /* 4 nothing */ + 0, /* 5 nothing */ + 0, /* 6 nothing */ + 0, /* 7 nothing */ + 0, /* 8 Dpad Up */ + 0, /* 9 Dpad Right */ + 0, /* 10 Dpad Down */ + 0, /* 11 Dpad Left */ + 0, /* 12 L2 */ + 0, /* 13 R2 */ + 0, /* 14 L1 */ + 0, /* 15 R1 */ + 0, /* 16 Triangle */ + 0, /* 17 Circle */ + 0, /* 18 Cross */ + 0, /* 19 Square */ + 0, /* 20 nothing */ + 0, /* 21 nothing */ + 0, /* 22 nothing */ + 0, /* 23 nothing */ + 0, /* 24 nothing */ + 0, /* 25 nothing */ + 0, /* 26 nothing */ + 0, /* 27 nothing */ + }; +#endif + static const int button_map[] = { + 0, /* 0 Select */ + 0, /* 1 L3 */ + 0, /* 2 R3 */ + 0, /* 3 Start */ + KEY_UP, /* 4 Dpad Up */ + 0, /* 5 Dpad Right */ + KEY_DOWN, /* 6 Dpad Down */ + 0, /* 7 Dpad Left */ + KEY_UP, /* 8 L2 */ + KEY_DOWN, /* 9 R2 */ + KEY_HOME, /* 10 L1 */ + KEY_END, /* 11 R1 */ + 0, /* 12 Triangle */ + 0, /* 13 Circle */ + 13, /* 14 Cross */ + 0, /* 15 Square */ + 0, /* 16 PS Button */ + 0, /* 17 nothing */ + 0, /* 18 nothing */ + }; + + if (!e->value) + return 0; + + if (e->type == JS_EVENT_BUTTON + && e->number < sizeof(button_map) / sizeof(button_map[0])) + return button_map[e->number]; + +#if 0 + if (e->type == JS_EVENT_AXIS + && e->number < sizeof(axis_map) / sizeof(axis_map[0])) + return axis_map[e->number]; +#endif + + return 0; +} + +/** * ps3_set_mode - Set video mode helper. * * Runs ps3_set_video_mode(). @@ -550,7 +627,7 @@ int main(int argc, char *argv[]) if (!result && (ps3.values.video_mode != (uint16_t)mode)) ps3_set_video_mode(ps3.values.video_mode); - ps3.cui = cui_init(&ps3, ps3_kexec_cb); + ps3.cui = cui_init(&ps3, ps3_kexec_cb, ps3_sixaxis_map); if (!ps3.cui) return EXIT_FAILURE;