11#include <winpr/assert.h>
13#include "ios_freerdp_events.h"
16#pragma mark Sending compacted input events (from main thread)
20BOOL ios_events_send(mfInfo *mfi, NSDictionary *event_description)
22 NSData *encoded_description = [NSKeyedArchiver archivedDataWithRootObject:event_description];
26 if ([encoded_description length] > 32000 || (mfi->event_pipe_producer == -1))
29 uint32_t archived_data_len = (uint32_t)[encoded_description length];
33 if (write(mfi->event_pipe_producer, &archived_data_len, 4) == -1)
35 NSLog(
@"%s: Failed to write length descriptor to pipe.", __func__);
39 if (write(mfi->event_pipe_producer, [encoded_description bytes], archived_data_len) == -1)
41 NSLog(
@"%s: Failed to write %d bytes into the event queue (event type: %@).", __func__,
42 (
int)[encoded_description length], [event_description objectForKey:
@"type"]);
50#pragma mark Processing compacted input events (from connection thread runloop)
52static BOOL ios_events_handle_event(mfInfo *mfi, NSDictionary *event_description)
54 NSString *event_type = [event_description objectForKey:@"type"];
55 BOOL should_continue = TRUE;
60 freerdp *instance = mfi->instance;
61 WINPR_ASSERT(instance);
62 WINPR_ASSERT(instance->context);
64 input = instance->context->input;
67 if ([event_type isEqualToString:
@"mouse"])
69 if (!input->MouseEvent(input,
70 [[event_description objectForKey:
@"flags"] unsignedShortValue],
71 [[event_description objectForKey:
@"coord_x"] unsignedShortValue],
72 [[event_description objectForKey:
@"coord_y"] unsignedShortValue]))
75 NSLog(
@"%s: MouseEvent failed.", __func__);
78 else if ([event_type isEqualToString:
@"keyboard"])
80 if ([[event_description objectForKey:
@"subtype"] isEqualToString:
@"scancode"])
81 freerdp_input_send_keyboard_event(
82 input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
83 [[event_description objectForKey:
@"scancode"] unsignedShortValue]);
84 else if ([[event_description objectForKey:
@"subtype"] isEqualToString:
@"unicode"])
85 freerdp_input_send_unicode_keyboard_event(
86 input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
87 [[event_description objectForKey:
@"unicode_char"] unsignedShortValue]);
89 NSLog(
@"%s: doesn't know how to send keyboard input with subtype %@", __func__,
90 [event_description objectForKey:
@"subtype"]);
92 else if ([event_type isEqualToString:
@"disconnect"])
93 should_continue = FALSE;
95 NSLog(
@"%s: unrecognized event type: %@", __func__, event_type);
97 return should_continue;
100BOOL ios_events_check_handle(mfInfo *mfi)
104 if (WaitForSingleObject(mfi->handle, 0) != WAIT_OBJECT_0)
107 if (mfi->event_pipe_consumer == -1)
110 uint32_t archived_data_length = 0;
114 bytes_read = read(mfi->event_pipe_consumer, &archived_data_length, 4);
116 if (bytes_read == -1 || archived_data_length < 1 || archived_data_length > 32000)
118 NSLog(
@"%s: just read length descriptor. bytes_read=%ld, archived_data_length=%u", __func__,
119 bytes_read, archived_data_length);
125 NSMutableData *archived_object_data =
126 [[NSMutableData alloc] initWithLength:archived_data_length];
128 read(mfi->event_pipe_consumer, [archived_object_data mutableBytes], archived_data_length);
130 if (bytes_read != archived_data_length)
132 NSLog(
@"%s: attempted to read data; read %ld bytes but wanted %d bytes.", __func__,
133 bytes_read, archived_data_length);
134 [archived_object_data release];
138 id unarchived_object_data = [NSKeyedUnarchiver unarchiveObjectWithData:archived_object_data];
139 [archived_object_data release];
141 return ios_events_handle_event(mfi, unarchived_object_data);
144HANDLE ios_events_get_handle(mfInfo *mfi)
151BOOL ios_events_create_pipe(mfInfo *mfi)
157 if (pipe(pipe_fds) == -1)
159 NSLog(
@"%s: pipe failed.", __func__);
163 mfi->event_pipe_consumer = pipe_fds[0];
164 mfi->event_pipe_producer = pipe_fds[1];
165 mfi->handle = CreateFileDescriptorEvent(
nullptr, FALSE, FALSE, mfi->event_pipe_consumer,
166 WINPR_FD_READ | WINPR_FD_WRITE);
170void ios_events_free_pipe(mfInfo *mfi)
173 int consumer_fd = mfi->event_pipe_consumer, producer_fd = mfi->event_pipe_producer;
175 mfi->event_pipe_consumer = mfi->event_pipe_producer = -1;
178 (void)CloseHandle(mfi->handle);