Commit ae04dc75 authored by Abhinav Singh's avatar Abhinav Singh Committed by Adrien Plazas

Add RetroKeyJoypadMapping

This is required to provide configuration for keyboard.

https://bugzilla.gnome.org/show_bug.cgi?id=780755
parent 162ce030
......@@ -23,6 +23,7 @@ retro_gtk_sources = [
'retro-input.c',
'retro-input-descriptor.c',
'retro-keyboard-key.c',
'retro-key-joypad-mapping.c',
'retro-log.c',
'retro-main-loop.c',
'retro-memory-type.c',
......@@ -50,6 +51,7 @@ retro_gtk_headers = [
'retro-core.h',
'retro-core-view.h',
'retro-input.h',
'retro-key-joypad-mapping.h',
'retro-log.h',
'retro-main-loop.h',
'retro-memory-type.h',
......
......@@ -17,6 +17,7 @@
#include "retro-core-view.h"
#include "retro-gtk-version.h"
#include "retro-input.h"
#include "retro-key-joypad-mapping.h"
#include "retro-log.h"
#include "retro-main-loop.h"
#include "retro-memory-type.h"
......
// This file is part of retro-gtk. License: GPL-3.0+.
#include "retro-key-joypad-mapping.h"
#include <linux/input-event-codes.h>
static guint16 DEFAULT_KEY_JOYPAD_BUTTON_MAPPING[RETRO_JOYPAD_ID_COUNT] = {
KEY_S,
KEY_A,
KEY_BACKSPACE,
KEY_ENTER,
KEY_UP,
KEY_DOWN,
KEY_LEFT,
KEY_RIGHT,
KEY_D,
KEY_W,
KEY_Q,
KEY_E,
KEY_Z,
KEY_C,
KEY_1,
KEY_3,
};
struct _RetroKeyJoypadMapping
{
GObject parent_instance;
guint16 joypad_keys[RETRO_JOYPAD_ID_COUNT];
};
G_DEFINE_TYPE (RetroKeyJoypadMapping, retro_key_joypad_mapping, G_TYPE_OBJECT)
/* Private */
static void
retro_key_joypad_mapping_finalize (GObject *object)
{
G_OBJECT_CLASS (retro_key_joypad_mapping_parent_class)->finalize (object);
}
static void
retro_key_joypad_mapping_class_init (RetroKeyJoypadMappingClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = retro_key_joypad_mapping_finalize;
}
static void
retro_key_joypad_mapping_init (RetroKeyJoypadMapping *self)
{
}
/* Public */
/**
* retro_key_joypad_mapping_set_button_key:
* @self: a #RetroKeyJoypadMapping
* @button: joypad button id
* @hardware_keycode: mapping key code
*
* Maps the joypad button id to mapping key code.
*/
void
retro_key_joypad_mapping_set_button_key (RetroKeyJoypadMapping *self,
RetroJoypadId button,
guint16 hardware_keycode)
{
g_return_if_fail (RETRO_IS_KEY_JOYPAD_MAPPING (self));
g_return_if_fail (button >= 0);
g_return_if_fail (button < RETRO_JOYPAD_ID_COUNT);
self->joypad_keys[button] = hardware_keycode;
}
/**
* retro_key_joypad_mapping_get_button_key:
* @self: a #RetroKeyJoypadMapping
* @button: joypad button id
*
* Gets the mapping key code for the joypad button id.
*
* Returns: mapping key code
*/
guint16
retro_key_joypad_mapping_get_button_key (RetroKeyJoypadMapping *self,
RetroJoypadId button)
{
g_return_val_if_fail (RETRO_IS_KEY_JOYPAD_MAPPING (self), 0);
g_return_val_if_fail (button >= 0, 0);
g_return_val_if_fail (button < RETRO_JOYPAD_ID_COUNT, 0);
return self->joypad_keys[button];
}
/**
* retro_key_joypad_mapping_new:
*
* Creates a new #RetroKeyJoypadMapping.
*
* Returns: (transfer full): a new #RetroKeyJoypadMapping
*/
RetroKeyJoypadMapping *
retro_key_joypad_mapping_new ()
{
RetroKeyJoypadMapping *self;
self = g_object_new (RETRO_TYPE_KEY_JOYPAD_MAPPING, NULL);
return self;
}
/**
* retro_key_joypad_mapping_new_default:
*
* Creates a new #RetroKeyJoypadMapping with the default mapping.
*
* Returns: (transfer full): a new #RetroKeyJoypadMapping
*/
RetroKeyJoypadMapping *
retro_key_joypad_mapping_new_default ()
{
RetroKeyJoypadMapping *self;
RetroJoypadId button;
self = g_object_new (RETRO_TYPE_KEY_JOYPAD_MAPPING, NULL);
for (button = 0; button < RETRO_JOYPAD_ID_COUNT; button ++)
/* GDK adds 8 to the Linux input event codes to create the hardware keycode.
* These codes are the only ones not coming from GDK so lets standardize on
* what GDK does.
*/
self->joypad_keys[button] = DEFAULT_KEY_JOYPAD_BUTTON_MAPPING[button] + 8;
return self;
}
// This file is part of retro-gtk. License: GPL-3.0+.
#ifndef RETRO_KEY_JOYPAD_MAPPING_H
#define RETRO_KEY_JOYPAD_MAPPING_H
#if !defined(__RETRO_GTK_INSIDE__) && !defined(RETRO_GTK_COMPILATION)
# error "Only <retro-gtk.h> can be included directly."
#endif
#include <glib-object.h>
#include "retro-controller-codes.h"
G_BEGIN_DECLS
#define RETRO_TYPE_KEY_JOYPAD_MAPPING (retro_key_joypad_mapping_get_type())
G_DECLARE_FINAL_TYPE (RetroKeyJoypadMapping, retro_key_joypad_mapping, RETRO, KEY_JOYPAD_MAPPING, GObject)
RetroKeyJoypadMapping *retro_key_joypad_mapping_new ();
RetroKeyJoypadMapping *retro_key_joypad_mapping_new_default ();
void retro_key_joypad_mapping_set_button_key (RetroKeyJoypadMapping *self,
RetroJoypadId button,
guint16 hardware_keycode);
guint16 retro_key_joypad_mapping_get_button_key (RetroKeyJoypadMapping *self,
RetroJoypadId button);
G_END_DECLS
#endif /* RETRO_KEY_JOYPAD_MAPPING_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment