[retro-gtk/wip/aplazas/keyboard-configuration] retro-controller-codes: Add converter for JoypadId
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [retro-gtk/wip/aplazas/keyboard-configuration] retro-controller-codes: Add converter for JoypadId
- Date: Tue, 5 Jun 2018 08:51:18 +0000 (UTC)
commit f3301edd403f3e6345f20ebe9d84b2ba5bc213e9
Author: theawless <theawless gmail com>
Date: Mon Apr 16 02:52:50 2018 +0530
retro-controller-codes: Add converter for JoypadId
This commit adds conversions for linux event codes to/from JoypadId.
This will help in simplification for client code of retro-gtk.
retro-gtk/retro-controller-codes.c | 53 ++++++++++++++++++++++++++++++++++++++
retro-gtk/retro-controller-codes.h | 4 +++
2 files changed, 57 insertions(+)
---
diff --git a/retro-gtk/retro-controller-codes.c b/retro-gtk/retro-controller-codes.c
index 420a003..cf18c5b 100644
--- a/retro-gtk/retro-controller-codes.c
+++ b/retro-gtk/retro-controller-codes.c
@@ -2,6 +2,8 @@
#include "retro-controller-codes.h"
+#include <linux/input-event-codes.h>
+
GType
retro_joypad_id_get_type (void)
{
@@ -26,6 +28,7 @@ retro_joypad_id_get_type (void)
{ RETRO_JOYPAD_ID_L3, "RETRO_JOYPAD_ID_L3", "l3" },
{ RETRO_JOYPAD_ID_R3, "RETRO_JOYPAD_ID_R3", "r3" },
{ RETRO_JOYPAD_ID_COUNT, "RETRO_JOYPAD_ID_COUNT", "count" },
+ { RETRO_JOYPAD_ID_INVALID, "RETRO_JOYPAD_ID_INVALID", "invalid" },
{ 0, NULL, NULL },
};
GType type;
@@ -38,6 +41,56 @@ retro_joypad_id_get_type (void)
return retro_joypad_id_type;
}
+static guint16 RETRO_JOYPAD_ID_EVENT_CODE_MAPPING[RETRO_JOYPAD_ID_COUNT] = {
+ BTN_A,
+ BTN_Y,
+ BTN_SELECT,
+ BTN_START,
+ BTN_DPAD_UP,
+ BTN_DPAD_DOWN,
+ BTN_DPAD_LEFT,
+ BTN_DPAD_RIGHT,
+ BTN_B,
+ BTN_X,
+ BTN_TL,
+ BTN_TR,
+ BTN_TL2,
+ BTN_TR2,
+ BTN_THUMBL,
+ BTN_THUMBR,
+};
+
+/**
+ * retro_joypad_id_to_event_code:
+ * @joypad_id: a #RetroJoypadId
+ *
+ * Returns: the event code for joypad id
+ */
+guint16
+retro_joypad_id_to_event_code (RetroJoypadId joypad_id)
+{
+ g_return_val_if_fail (joypad_id >= 0, EV_MAX);
+ g_return_val_if_fail (joypad_id < RETRO_JOYPAD_ID_COUNT, EV_MAX);
+
+ return RETRO_JOYPAD_ID_EVENT_CODE_MAPPING[joypad_id];
+}
+
+/**
+ * retro_joypad_id_from_event_code:
+ * @event_code: a linux event code
+ *
+ * Returns: the joypad id for event code
+ */
+RetroJoypadId
+retro_joypad_id_from_event_code (guint16 event_code)
+{
+ for (gint i = 0; i < RETRO_JOYPAD_ID_COUNT; ++i)
+ if (event_code == RETRO_JOYPAD_ID_EVENT_CODE_MAPPING[i])
+ return (RetroJoypadId) i;
+
+ return RETRO_JOYPAD_ID_INVALID;
+}
+
GType
retro_mouse_id_get_type (void)
{
diff --git a/retro-gtk/retro-controller-codes.h b/retro-gtk/retro-controller-codes.h
index 39df383..105d032 100644
--- a/retro-gtk/retro-controller-codes.h
+++ b/retro-gtk/retro-controller-codes.h
@@ -61,8 +61,12 @@ typedef enum
RETRO_JOYPAD_ID_L3,
RETRO_JOYPAD_ID_R3,
RETRO_JOYPAD_ID_COUNT,
+ RETRO_JOYPAD_ID_INVALID,
} RetroJoypadId;
+guint16 retro_joypad_id_to_event_code (RetroJoypadId joypad_id);
+RetroJoypadId retro_joypad_id_from_event_code (guint16 event_code);
+
#define RETRO_TYPE_MOUSE_ID (retro_mouse_id_get_type ())
GType retro_mouse_id_get_type (void) G_GNUC_CONST;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]