aboutsummaryrefslogtreecommitdiffstats
path: root/user-events-ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'user-events-ui.c')
-rw-r--r--user-events-ui.c134
1 files changed, 115 insertions, 19 deletions
diff --git a/user-events-ui.c b/user-events-ui.c
index f295ade..cef3689 100644
--- a/user-events-ui.c
+++ b/user-events-ui.c
@@ -11,9 +11,110 @@
*/
#include "user-events.h"
#include "android/utils/debug.h"
+#include "android/user-events-common.h"
#include "console.h"
#include <stdio.h>
+#include "android/looper.h"
+#include "android/async-utils.h"
+#include "android/core-connection.h"
+
+/* Descriptor for the user events client. */
+typedef struct ClientUserEvents {
+ /* Core connection instance for the user events client. */
+ CoreConnection* core_connection;
+
+ /* Socket for the client. */
+ int sock;
+
+ /* Socket wrapper for sync I/O. */
+ SyncSocket* sync_socket;
+} ClientUserEvents;
+
+/* One and only one user events client instance. */
+static ClientUserEvents _client_ue = { 0 };
+
+int
+clientue_create(SockAddress* console_socket)
+{
+ char* connect_message = NULL;
+ char switch_cmd[256];
+
+ // Connect to the framebuffer service.
+ _client_ue.core_connection = core_connection_create(console_socket);
+ if (_client_ue.core_connection == NULL) {
+ derror("User events client is unable to connect to the console: %s\n",
+ errno_str);
+ return -1;
+ }
+ if (core_connection_open(_client_ue.core_connection)) {
+ core_connection_free(_client_ue.core_connection);
+ _client_ue.core_connection = NULL;
+ derror("User events client is unable to open the console: %s\n",
+ errno_str);
+ return -1;
+ }
+ snprintf(switch_cmd, sizeof(switch_cmd), "user events");
+ if (core_connection_switch_stream(_client_ue.core_connection, switch_cmd,
+ &connect_message)) {
+ derror("Unable to connect to the user events service: %s\n",
+ connect_message ? connect_message : "");
+ if (connect_message != NULL) {
+ free(connect_message);
+ }
+ core_connection_close(_client_ue.core_connection);
+ core_connection_free(_client_ue.core_connection);
+ _client_ue.core_connection = NULL;
+ return -1;
+ }
+
+ // Now that we're connected lets initialize the descriptor.
+ _client_ue.sock = core_connection_get_socket(_client_ue.core_connection);
+ _client_ue.sync_socket = syncsocket_init(_client_ue.sock);
+ if (connect_message != NULL) {
+ free(connect_message);
+ }
+
+ fprintf(stdout, "User events client is now attached to the core %s\n",
+ sock_address_to_string(console_socket));
+
+ return 0;
+}
+
+/* Sends an event to the core.
+ * Parameters:
+ * ue - User events client instance.
+ * event - Event type. Must be one of the AUSER_EVENT_XXX.
+ * event_param - Event parameters.
+ * size - Byte size of the event parameters buffer.
+ * Return:
+ * 0 on success, or -1 on failure.
+ */
+static int
+clientue_send(ClientUserEvents* ue,
+ uint8_t event,
+ const void* event_param,
+ size_t size)
+{
+ int res;
+ UserEventHeader header;
+
+ header.event_type = event;
+ syncsocket_start_write(ue->sync_socket);
+ // Send event type first (event header)
+ res = syncsocket_write(ue->sync_socket, &header, sizeof(header), 500);
+ if (res < 0) {
+ return -1;
+ }
+ // Send event param next.
+ res = syncsocket_write(ue->sync_socket, event_param, size, 500);
+ if (res < 0) {
+ return -1;
+ }
+ syncsocket_stop_write(ue->sync_socket);
+ return 0;
+}
+
void
user_event_keycodes(int *kcodes, int count)
{
@@ -25,9 +126,9 @@ user_event_keycodes(int *kcodes, int count)
void
user_event_keycode(int kcode)
{
-#if 0 /* TODO */
- kbd_put_keycode(kcode);
-#endif
+ UserEventKeycode message;
+ message.keycode = kcode;
+ clientue_send(&_client_ue, AUSER_EVENT_KEYCODE, &message, sizeof(message));
}
void
@@ -46,29 +147,24 @@ user_event_key(unsigned code, unsigned down)
void
user_event_mouse(int dx, int dy, int dz, unsigned buttons_state)
{
-#if 0 /* TODO */
- kbd_mouse_event(dx, dy, dz, buttons_state);
-#endif
+ UserEventMouse message;
+ message.dx = dx;
+ message.dy = dy;
+ message.dz = dz;
+ message.buttons_state = buttons_state;
+ clientue_send(&_client_ue, AUSER_EVENT_MOUSE, &message, sizeof(message));
}
-#if 0
-static QEMUPutGenericEvent *generic_event_callback;
-static void* generic_event_opaque;
-#endif
-
void user_event_register_generic(void* opaque, QEMUPutGenericEvent *callback)
{
-#if 0 /* TODO */
- generic_event_callback = callback;
- generic_event_opaque = opaque;
-#endif
}
void
user_event_generic(int type, int code, int value)
{
-#if 0 /* TODO */
- if (generic_event_callback)
- generic_event_callback(generic_event_opaque, type, code, value);
-#endif
+ UserEventGeneric message;
+ message.type = type;
+ message.code = code;
+ message.value = value;
+ clientue_send(&_client_ue, AUSER_EVENT_GENERIC, &message, sizeof(message));
}