From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1HmIhV-0002ce-QQ for qemu-devel@nongnu.org; Thu, 10 May 2007 20:09:37 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1HmIhT-0002al-6v for qemu-devel@nongnu.org; Thu, 10 May 2007 20:09:37 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HmIhT-0002ai-0h for qemu-devel@nongnu.org; Thu, 10 May 2007 20:09:35 -0400 Received: from relais.videotron.ca ([24.201.245.36]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1HmIa5-0001gD-Gd for qemu-devel@nongnu.org; Thu, 10 May 2007 20:01:58 -0400 Received: from [192.168.0.100] ([74.56.228.237]) by VL-MO-MR001.ip.videotron.ca (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTP id <0JHU004MTO37ZR60@VL-MO-MR001.ip.videotron.ca> for qemu-devel@nongnu.org; Thu, 10 May 2007 20:01:56 -0400 (EDT) Date: Thu, 10 May 2007 20:01:55 -0400 From: Jonathan Phenix Message-id: <4643B273.4040907@videotron.ca> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_gxeX35m/1DvFbpWgG+I5XA)" Subject: [Qemu-devel] [PATCH] Joystick API Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This is a multi-part message in MIME format. --Boundary_(ID_gxeX35m/1DvFbpWgG+I5XA) Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: QUOTED-PRINTABLE Hi, this is an attempt to add a simple joystick API to qemu. This API wil= l=20 be required for my upcoming Playstation emulator. It includes a "null= "=20 driver, I have a Linux and SDL driver as well, I will send them when= =20 this patch will be merged. Constructive comments are welcome. Regards, Jonathan Ph=E9nix --Boundary_(ID_gxeX35m/1DvFbpWgG+I5XA) Content-type: text/x-patch; CHARSET=ISO-8859-1; name=qemu-cvs-joystick.patch Content-transfer-encoding: QUOTED-PRINTABLE Content-disposition: inline; filename=qemu-cvs-joystick.patch diff -urN --exclude CVS qemu/joystick/joystick.c qemu-joystick/joysti= ck/joystick.c --- qemu/joystick/joystick.c=091969-12-31 19:00:00.000000000 -0500 +++ qemu-joystick/joystick/joystick.c=092007-05-10 19:21:07.000000000= -0400 @@ -0,0 +1,148 @@ +/* QEMU joystick API + * + * Copyright (C) 2007 Jonathan Ph=C3=A9nix + * + * This program is free software; you can redistribute it and/or mo= dify + * it under the terms of the GNU General Public License as publishe= d by + * the Free Software Foundation; either version 2 of the License, o= r + * (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 Licens= e + * 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[] =3D { +#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 =3D joy_driver_table; + + while (*ptr) { + if ((*ptr)->probe() =3D=3D JOY_OK) + break; + ptr++; + } + + if (!ptr) { + /* This should never happens, the null driver should catch i= t but we are never too careful */ + qemu_joy_log("%s: No suitable joystick driver\n", __func__); + qemu_free(s); + return NULL; + } + + s =3D qemu_mallocz(sizeof(*s) + (*ptr)->size); + if (!s) + return NULL; + + s->driver =3D *ptr; + + s->joy_but_cb =3D but_cb; + s->joy_axis_cb =3D axis_cb; + s->cb_opaque =3D opaque; + + s->driver->init(s); + + return s; +} + +JoyError qemu_joy_open(JoyState *s, unsigned int devno) +{ +#ifdef JOY_SAFE + if (devno >=3D 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 >=3D 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 >=3D 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/joysti= ck/joystick.h --- qemu/joystick/joystick.h=091969-12-31 19:00:00.000000000 -0500 +++ qemu-joystick/joystick/joystick.h=092007-05-10 19:14:08.000000000= -0400 @@ -0,0 +1,84 @@ +/* QEMU joystick API + * + * Copyright (C) 2007 Jonathan Ph=C3=A9nix + * + * This program is free software; you can redistribute it and/or mo= dify + * it under the terms of the GNU General Public License as publishe= d by + * the Free Software Foundation; either version 2 of the License, o= r + * (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 Licens= e + * 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 =3D 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 < JO= Y_MAX". The first joystick on + the system is 0 and not 1, i.e. devno =3D=3D 1 is the second joys= tick */ + +/* Init. the joystick subsystem and define the callback functions fo= r axes and buttons state + changes. The callbacks functions should not be NULL. The opaque p= ointer 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 w= as 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 fun= ctions. */ +void qemu_joy_enable(JoyState *s); + +/*******************************************************************= *********************************/ + +#endif /* __JOYSTICK_H__ */ + diff -urN --exclude CVS qemu/joystick/joystick_int.h qemu-joystick/jo= ystick/joystick_int.h --- qemu/joystick/joystick_int.h=091969-12-31 19:00:00.000000000 -050= 0 +++ qemu-joystick/joystick/joystick_int.h=092007-05-10 19:26:27.00000= 0000 -0400 @@ -0,0 +1,58 @@ +/* QEMU internal joystick API + * + * Copyright (C) 2007 Jonathan Ph=C3=A9nix + * + * This program is free software; you can redistribute it and/or mo= dify + * it under the terms of the GNU General Public License as publishe= d by + * the Free Software Foundation; either version 2 of the License, o= r + * (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 Licens= e + * 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/joystic= k/nulljoy.c --- qemu/joystick/nulljoy.c=091969-12-31 19:00:00.000000000 -0500 +++ qemu-joystick/joystick/nulljoy.c=092007-05-10 19:14:20.000000000 = -0400 @@ -0,0 +1,63 @@ +/* QEMU joystick NULL driver + * + * Copyright (C) 2007 Jonathan Ph=C3=A9nix + * + * This program is free software; you can redistribute it and/or mo= dify + * it under the terms of the GNU General Public License as publishe= d by + * the Free Software Foundation; either version 2 of the License, o= r + * (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 Licens= e + * 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, JoyIn= fo *info) +{ + strncpy(info->name, "NULL", JOY_NAME_MAX - 1); + strncpy(info->driver, "NULL", JOY_DRIVER_MAX - 1); + info->nr_buttons =3D 1; + info->nr_axes =3D 2; + + return JOY_OK; +} + +static unsigned int null_nr(JoyState *s) +{ + return UINT_MAX; +} + +const JoyDriver joy_driver_null =3D { + 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.t= arget --- qemu/Makefile.target=092007-05-08 17:05:55.000000000 -0400 +++ qemu-joystick/Makefile.target=092007-05-10 19:55:17.000000000 -04= 00 @@ -17,7 +17,7 @@ TARGET_BASE_ARCH:=3Dsparc endif TARGET_PATH=3D$(SRC_PATH)/target-$(TARGET_BASE_ARCH) -VPATH=3D$(SRC_PATH):$(TARGET_PATH):$(SRC_PATH)/hw:$(SRC_PATH)/audio +VPATH=3D$(SRC_PATH):$(TARGET_PATH):$(SRC_PATH)/hw:$(SRC_PATH)/audio:= $(SRC_PATH)/joystick CPPFLAGS=3D-I. -I.. -I$(TARGET_PATH) -I$(SRC_PATH) ifdef CONFIG_DARWIN_USER VPATH+=3D:$(SRC_PATH)/darwin-user @@ -416,6 +416,14 @@ VL_OBJS +=3D pcnet.o VL_OBJS +=3D rtl8139.o =20 +JOYDRV =3D joystick.o nulljoy.o +#ifdef CONFIG_JOY_LINUX +#JOYDRV +=3D linuxjoy.o +#endif +#ifdef CONFIG_SDL +#JOYDRV +=3D sdljoy.o +#endif + ifeq ($(TARGET_BASE_ARCH), i386) # Hardware support VL_OBJS+=3D ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o $(AUDIODRV) --Boundary_(ID_gxeX35m/1DvFbpWgG+I5XA)--