ragb/io/joypad.c

78 lines
1.7 KiB
C

#include <stdio.h>
#include <gb.h>
#include <r_io.h>
#include <r_util.h>
#include <SDL2/SDL.h>
static bool __check(RIO *io, const char *pathname, bool many) {
return r_str_startswith (pathname, "gb_joypad://");
}
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
if (!r_str_startswith (pathname, "gb_joypad://")) {
return NULL;
}
GBJoypad *joypad = NULL;
sscanf (pathname, "gb_joybad://%p", &joypad);
RIODesc *desc = r_io_desc_new (io, &r_io_plugin_gb_joypad, pathname,
R_PERM_RWX, mode, timers);
return desc;
}
static int __read(RIO *io, RIODesc *desc, ut8 *buf, int len) {
GBJoypad *joypad = (GBJoypad *)desc->data;
if (!len || (joypad->odata & 0x10)) {
return 0;
}
joypad->data = buf[0] & 0xf;
joypad->odata |= 0x10;
return 1;
}
static int __write(RIO *io, RIODesc *desc, const ut8 *buf, int len) {
GBJoypad *joypad = (GBJoypad *)desc->data;
if (!len || (joypad->odata & 0x10)) {
return 0;
}
joypad->odata |= 0x10;
return 1;
}
static bool __close(RIODesc *desc) {
return true;
}
RIOPlugin r_io_plugin_gb_joypad = {
.meta = {
.name = "gb_joypad",
.desc = "gb_joypad",
.license = "LGPL",
},
.uris = "gb_joypad://",
.open = __open,
.close = __close,
.read = __read,
.check = __check,
.seek = __lseek,
.write = __write,
};
GBJoypad *gb_joypad_open (RIO *io) {
joypad = R_NEW0 (GBJoypad);
if (!joypad) {
return NULL;
}
char uri[64];
memset (uri, 0x00, sizeof (char) * 64);
sprintf (uri, "gb_joypad://%p", joypad);
RIODesc *desc = r_io_desc_open_plugin (io, &r_io_plugin_gb_joypad,
uri, R_PERM_RWX, 0);
if (!desc) {
free (joypad);
return NULL;
}
joypad->fd = desc->fd;
joypad->keys = SDL_GetKeyboardState (NULL);
return joypad;
}