summaryrefslogtreecommitdiffstats
path: root/ui/common
diff options
context:
space:
mode:
authorGeoff Levand <geoffrey.levand@am.sony.com>2009-07-09 10:40:44 -0700
committerGeoff Levand <geoffrey.levand@am.sony.com>2009-07-09 10:40:44 -0700
commitd3e5eadbdcce508c48258b15664baa753ce6fc81 (patch)
treed6dd61bcb46c00d0ef21216c8463eacca3dcc092 /ui/common
parent4341aeae5ace4f8137334d6e8fa885d546fd3216 (diff)
downloadtalos-petitboot-d3e5eadbdcce508c48258b15664baa753ce6fc81.tar.gz
talos-petitboot-d3e5eadbdcce508c48258b15664baa753ce6fc81.zip
Add ncurses joystick support
Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
Diffstat (limited to 'ui/common')
-rw-r--r--ui/common/joystick.c106
-rw-r--r--ui/common/joystick.h47
2 files changed, 153 insertions, 0 deletions
diff --git a/ui/common/joystick.c b/ui/common/joystick.c
new file mode 100644
index 0000000..94c85fe
--- /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 <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#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;
+}
diff --git a/ui/common/joystick.h b/ui/common/joystick.h
new file mode 100644
index 0000000..cf3fc45
--- /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 <linux/joystick.h>
+
+/**
+ * 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
OpenPOWER on IntegriCloud