qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] Joystick API


From: Jonathan Phenix
Subject: [Qemu-devel] [PATCH] Joystick API
Date: Thu, 10 May 2007 20:01:55 -0400
User-agent: Thunderbird 1.5.0.9 (X11/20070102)

Hi,

this is an attempt to add a simple joystick API to qemu. This API will be required for my upcoming Playstation emulator. It includes a "null" driver, I have a Linux and SDL driver as well, I will send them when this patch will be merged. Constructive comments are welcome.

Regards,

Jonathan Phénix


diff -urN --exclude CVS qemu/joystick/joystick.c 
qemu-joystick/joystick/joystick.c
--- qemu/joystick/joystick.c    1969-12-31 19:00:00.000000000 -0500
+++ qemu-joystick/joystick/joystick.c   2007-05-10 19:21:07.000000000 -0400
@@ -0,0 +1,148 @@
+/*  QEMU joystick API
+ *
+ *  Copyright (C) 2007 Jonathan Phénix
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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
+ */
+
+#include "joystick_int.h"
+
+static const JoyDriver *joy_driver_table[] = {
+#if 0
+#ifdef CONFIG_JOY_LINUX
+    &joy_driver_linux,
+#endif
+#ifdef CONFIG_SDL
+    &joy_driver_sdl,
+#endif
+#endif
+    &joy_driver_null,
+    NULL,
+};
+
+JoyState *qemu_joy_init(JoyButtonCB but_cb, JoyAxisCB axis_cb, void *opaque)
+{
+    const JoyDriver **ptr;
+    JoyState *s;
+
+#ifdef JOY_SAFE
+    if (!but_cb || !axis_cb)
+        return NULL;
+#endif
+
+    ptr = joy_driver_table;
+
+    while (*ptr) {
+        if ((*ptr)->probe() == JOY_OK)
+            break;
+        ptr++;
+    }
+
+    if (!ptr) {
+        /* This should never happens, the null driver should catch it but we 
are never too careful */
+        qemu_joy_log("%s: No suitable joystick driver\n", __func__);
+        qemu_free(s);
+        return NULL;
+    }
+
+    s = qemu_mallocz(sizeof(*s) + (*ptr)->size);
+    if (!s)
+        return NULL;
+
+    s->driver = *ptr;
+
+    s->joy_but_cb = but_cb;
+    s->joy_axis_cb = axis_cb;
+    s->cb_opaque = opaque;
+
+    s->driver->init(s);
+
+    return s;
+}
+
+JoyError qemu_joy_open(JoyState *s, unsigned int devno)
+{
+#ifdef JOY_SAFE
+    if (devno >= JOY_MAX || !s)
+        return JOY_EINVAL;
+#endif
+    return s->driver->open(s, devno);
+}
+
+JoyError qemu_joy_close(JoyState *s, unsigned int devno)
+{
+#ifdef JOY_SAFE
+    if (devno >= JOY_MAX || !s)
+        return JOY_EINVAL;
+#endif
+
+    return s->driver->close(s, devno);
+}
+
+JoyError qemu_joy_get_info(JoyState *s, unsigned int devno, JoyInfo *info)
+{
+#ifdef JOY_SAFE
+    if (devno >= JOY_MAX || !s || !info)
+        return JOY_EINVAL;
+#endif
+
+    memset(info, 0, sizeof(*info));
+
+    return s->driver->get_info(s, devno, info);
+}
+
+unsigned int qemu_joy_nr(JoyState *s)
+{
+#ifdef JOY_SAFE
+    if (!s)
+        return 0;
+#endif
+
+    return s->driver->nr(s);
+}
+
+void qemu_joy_flush(JoyState *s)
+{
+#ifdef JOY_SAFE
+    if (!s)
+        return;
+#endif
+
+    if (s->driver->flush)
+        s->driver->flush(s);
+}
+
+void qemu_joy_enable(JoyState *s)
+{
+#ifdef JOY_SAFE
+    if (!s)
+        return;
+#endif
+
+    if (s->driver->enable)
+        s->driver->enable(s);
+}
+
+extern FILE *logfile;
+
+void qemu_joy_log(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    vfprintf(logfile ? logfile : stderr, fmt, ap);
+    va_end(ap);
+}
+
diff -urN --exclude CVS qemu/joystick/joystick.h 
qemu-joystick/joystick/joystick.h
--- qemu/joystick/joystick.h    1969-12-31 19:00:00.000000000 -0500
+++ qemu-joystick/joystick/joystick.h   2007-05-10 19:14:08.000000000 -0400
@@ -0,0 +1,84 @@
+/*  QEMU joystick API
+ *
+ *  Copyright (C) 2007 Jonathan Phénix
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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
+ */
+
+#ifndef __JOYSTICK_H__
+#define __JOYSTICK_H__
+
+#include "vl.h"
+
+typedef struct JoyState JoyState;
+
+typedef void (*JoyButtonCB)(void *opaque, unsigned int devno, uint8_t button, 
int down);
+typedef void (*JoyAxisCB)(void *opaque, unsigned devno, uint8_t axis, int16_t 
state);
+
+typedef enum { JOY_OK = 0, JOY_EINVAL, JOY_ENODEV, JOY_ENOMEM, JOY_ERROR } 
JoyError;
+
+#define JOY_NAME_MAX 256
+#define JOY_DRIVER_MAX 256
+#define JOY_VER_MAX 256
+
+/* Maximum number of joystick that can be opened at one time */
+#define JOY_MAX 8
+
+typedef struct {
+    char name[JOY_NAME_MAX];
+    char driver[JOY_DRIVER_MAX];
+    char ver[JOY_VER_MAX];
+    uint8_t nr_axes;
+    uint8_t nr_buttons;
+} JoyInfo;
+
+
+/********************************************* User API 
*********************************************/
+
+/* In all these functions call, it is important that "0 < devno < JOY_MAX". 
The first joystick on
+   the system is 0 and not 1, i.e. devno == 1 is the second joystick */
+
+/* Init. the joystick subsystem and define the callback functions for axes and 
buttons state
+   changes. The callbacks functions should not be NULL. The opaque pointer 
will be passed as is
+   to callback functions */
+JoyState *qemu_joy_init(JoyButtonCB but_cb, JoyAxisCB axis_cb, void *opaque);
+
+/* Open the joystick devno. Should only be called if qemu_joy_init was 
successful. */
+JoyError qemu_joy_open(JoyState *s, unsigned int devno);
+
+/* Close the joystick devno. Should only be called if qemu_joy_init and 
qemu_joy_open(devno)
+   were successful. */
+JoyError qemu_joy_close(JoyState *s, unsigned int devno);
+
+/* Get some information about the joystick. "ver" is the version of the 
joystick driver, this
+   information is not always returned by the driver. Should only be called if 
qemu_joy_init
+   and qemu_joy_open(devno) were successful. */
+JoyError qemu_joy_get_info(JoyState *s, unsigned int devno, JoyInfo *info);
+
+/* Get the number of joystick(s) currently connected to the system. Should 
only be called if
+   qemu_joy_init was successful. */
+unsigned int qemu_joy_nr(JoyState *s);
+
+/* Make sure that all the joystick events were sent to the callback functions, 
use this to
+   make sure that you have a 100% up-to-date information. */
+void qemu_joy_flush(JoyState *s);
+
+/* Call this when you are ready to receive calls to the callback functions. */
+void qemu_joy_enable(JoyState *s);
+
+/****************************************************************************************************/
+
+#endif /* __JOYSTICK_H__ */
+
diff -urN --exclude CVS qemu/joystick/joystick_int.h 
qemu-joystick/joystick/joystick_int.h
--- qemu/joystick/joystick_int.h        1969-12-31 19:00:00.000000000 -0500
+++ qemu-joystick/joystick/joystick_int.h       2007-05-10 19:26:27.000000000 
-0400
@@ -0,0 +1,58 @@
+/*  QEMU internal joystick API
+ *
+ *  Copyright (C) 2007 Jonathan Phénix
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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
+ */
+
+#ifndef __JOYSTICK_INT_H__
+#define __JOYSTICK_INT_H__
+
+#include "vl.h"
+#include "joystick.h"
+
+//#define JOY_SAFE
+//#define JOY_DEBUG
+
+typedef struct JoyDriver JoyDriver;
+
+struct JoyState {
+    const JoyDriver *driver;
+
+    JoyButtonCB joy_but_cb;
+    JoyAxisCB joy_axis_cb;
+    void *cb_opaque;
+};
+
+struct JoyDriver {
+    JoyError (*probe)(void);
+    JoyError (*init)(JoyState *js);
+    JoyError (*open)(JoyState *js, unsigned int devno);
+    JoyError (*close)(JoyState *js, unsigned int devno);
+    JoyError (*get_info)(JoyState *js, unsigned int devno, JoyInfo *info);
+    unsigned int (*nr)(JoyState *js);
+    void (*flush)(JoyState *js);
+    void (*enable)(JoyState *js);
+    int size;
+};
+
+extern const JoyDriver joy_driver_linux;
+extern const JoyDriver joy_driver_sdl;
+extern const JoyDriver joy_driver_null;
+
+void qemu_joy_log(const char *fmt, ...) __attribute__ ((__format__ 
(__printf__, 1, 2)));
+
+#endif /* __JOYSTICK_INT_H__ */
+
diff -urN --exclude CVS qemu/joystick/nulljoy.c qemu-joystick/joystick/nulljoy.c
--- qemu/joystick/nulljoy.c     1969-12-31 19:00:00.000000000 -0500
+++ qemu-joystick/joystick/nulljoy.c    2007-05-10 19:14:20.000000000 -0400
@@ -0,0 +1,63 @@
+/*  QEMU joystick NULL driver
+ *
+ *  Copyright (C) 2007 Jonathan Phénix
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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
+ */
+
+#include "joystick_int.h"
+
+static JoyError null_probe(void)
+{
+    return JOY_OK;
+}
+
+static JoyError null_init(JoyState *s)
+{
+    return JOY_OK;
+}
+
+static JoyError null_open_close(JoyState *s, unsigned int devno)
+{
+    return JOY_OK;
+}
+
+static JoyError null_get_info(JoyState *s, unsigned int devno, JoyInfo *info)
+{
+    strncpy(info->name, "NULL", JOY_NAME_MAX - 1);
+    strncpy(info->driver, "NULL", JOY_DRIVER_MAX - 1);
+    info->nr_buttons = 1;
+    info->nr_axes = 2;
+
+    return JOY_OK;
+}
+
+static unsigned int null_nr(JoyState *s)
+{
+    return UINT_MAX;
+}
+
+const JoyDriver joy_driver_null = {
+    null_probe,
+    null_init,
+    null_open_close,
+    null_open_close,
+    null_get_info,
+    null_nr,
+    NULL, /* flush */
+    NULL, /* enable */
+    0,    /* size of private data */
+};
+
diff -urN --exclude CVS qemu/Makefile.target qemu-joystick/Makefile.target
--- qemu/Makefile.target        2007-05-08 17:05:55.000000000 -0400
+++ qemu-joystick/Makefile.target       2007-05-10 19:55:17.000000000 -0400
@@ -17,7 +17,7 @@
 TARGET_BASE_ARCH:=sparc
 endif
 TARGET_PATH=$(SRC_PATH)/target-$(TARGET_BASE_ARCH)
-VPATH=$(SRC_PATH):$(TARGET_PATH):$(SRC_PATH)/hw:$(SRC_PATH)/audio
+VPATH=$(SRC_PATH):$(TARGET_PATH):$(SRC_PATH)/hw:$(SRC_PATH)/audio:$(SRC_PATH)/joystick
 CPPFLAGS=-I. -I.. -I$(TARGET_PATH) -I$(SRC_PATH)
 ifdef CONFIG_DARWIN_USER
 VPATH+=:$(SRC_PATH)/darwin-user
@@ -416,6 +416,14 @@
 VL_OBJS += pcnet.o
 VL_OBJS += rtl8139.o
 
+JOYDRV = joystick.o nulljoy.o
+#ifdef CONFIG_JOY_LINUX
+#JOYDRV += linuxjoy.o
+#endif
+#ifdef CONFIG_SDL
+#JOYDRV += sdljoy.o
+#endif
+
 ifeq ($(TARGET_BASE_ARCH), i386)
 # Hardware support
 VL_OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o $(AUDIODRV)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]