[retro-gtk] retro-core: Add set_default_controller()
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [retro-gtk] retro-core: Add set_default_controller()
- Date: Sun, 29 Oct 2017 21:38:12 +0000 (UTC)
commit c551864d248b818c167cf284288a94bf76c47608
Author: Adrien Plazas <kekun plazas laposte net>
Date: Sun Oct 29 14:42:25 2017 +0100
retro-core: Add set_default_controller()
retro-gtk/retro-core-private.h | 1 +
retro-gtk/retro-core.c | 54 +++++++++++++++++++++++++++++++++------
retro-gtk/retro-core.h | 2 +
3 files changed, 48 insertions(+), 9 deletions(-)
---
diff --git a/retro-gtk/retro-core-private.h b/retro-gtk/retro-core-private.h
index 21c71ac..5a1a73b 100644
--- a/retro-gtk/retro-core-private.h
+++ b/retro-gtk/retro-core-private.h
@@ -46,6 +46,7 @@ struct _RetroCore
gdouble sample_rate;
RetroKeyboardCallback keyboard_callback;
+ RetroController *default_controllers[RETRO_CONTROLLER_TYPE_COUNT];
GHashTable *controllers;
GtkWidget *keyboard_widget;
gulong key_press_event_id;
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index 28f5600..b9fdfc5 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -130,6 +130,7 @@ retro_core_finalize (GObject *object)
RetroCore *self = RETRO_CORE (object);
RetroUnloadGame unload_game;
RetroDeinit deinit;
+ gsize i;
g_return_if_fail (RETRO_IS_CORE (self));
@@ -146,6 +147,9 @@ retro_core_finalize (GObject *object)
g_strfreev (self->media_uris);
g_object_unref (self->module);
+ for (i = 0; i < RETRO_CONTROLLER_TYPE_COUNT; i++)
+ if (self->default_controllers[i] != NULL)
+ g_object_unref (self->default_controllers[i]);
g_hash_table_unref (self->controllers);
g_object_unref (self->options);
@@ -1747,22 +1751,24 @@ retro_core_get_controller_input_state (RetroCore *self,
RetroInput *input)
{
RetroController *controller;
+ RetroControllerType controller_type;
g_return_val_if_fail (RETRO_IS_CORE (self), 0);
- if (!g_hash_table_contains (self->controllers, &port))
- return 0;
+ controller_type = retro_input_get_controller_type (input) &
+ RETRO_CONTROLLER_TYPE_TYPE_MASK;
controller = g_hash_table_lookup (self->controllers, &port);
+ if (controller != NULL &&
+ retro_controller_has_capability (controller, controller_type))
+ return retro_controller_get_input_state (controller, input);
- if (controller == NULL)
- return 0;
-
- if ((retro_controller_get_capabilities (controller) &
- (1 << retro_input_get_controller_type (input))) == 0)
- return 0;
+ controller = self->default_controllers[controller_type];
+ if (controller != NULL &&
+ retro_controller_has_capability (controller, controller_type))
+ return retro_controller_get_input_state (controller, input);
- return retro_controller_get_input_state (controller, input);
+ return 0;
}
// FIXME documentation
@@ -1788,6 +1794,36 @@ retro_core_get_controller_capabilities (RetroCore *self)
}
/**
+ * retro_core_set_default_controller:
+ * @self: a #RetroCore
+ * @controller: (nullable): a #RetroController
+ *
+ * Uses @controller as the default controller for its type. When a port has no
+ * controller plugged plugged into it, the core will use the default controllers
+ * instead.
+ */
+void
+retro_core_set_default_controller (RetroCore *self,
+ RetroController *controller)
+{
+ RetroControllerType controller_type;
+ RetroControllerType masked_controller_type;
+
+ g_return_if_fail (RETRO_IS_CORE (self));
+
+ controller_type = retro_controller_get_controller_type (controller);
+ masked_controller_type = controller_type & RETRO_CONTROLLER_TYPE_TYPE_MASK;
+
+ if (masked_controller_type >= RETRO_CONTROLLER_TYPE_COUNT) {
+ g_critical ("Unexpected controller type: %d.", controller_type);
+
+ return;
+ }
+
+ g_set_object (&self->default_controllers[masked_controller_type], controller);
+}
+
+/**
* retro_core_set_controller:
* @self: a #RetroCore
* @port: the port number
diff --git a/retro-gtk/retro-core.h b/retro-gtk/retro-core.h
index 03f069e..215ac09 100644
--- a/retro-gtk/retro-core.h
+++ b/retro-gtk/retro-core.h
@@ -55,6 +55,8 @@ GBytes *retro_core_get_memory (RetroCore *self,
void retro_core_set_memory (RetroCore *self,
RetroMemoryType memory_type,
GBytes *bytes);
+void retro_core_set_default_controller (RetroCore *self,
+ RetroController *controller);
void retro_core_set_controller (RetroCore *self,
guint port,
RetroController *controller);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]