11 #include <winpr/assert.h>
13 #include "ios_freerdp_events.h"
16 #pragma mark Sending compacted input events (from main thread)
20 BOOL 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)
52 static 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 input->MouseEvent(input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
70 [[event_description objectForKey:
@"coord_x"] unsignedShortValue],
71 [[event_description objectForKey:
@"coord_y"] unsignedShortValue]);
73 else if ([event_type isEqualToString:
@"keyboard"])
75 if ([[event_description objectForKey:
@"subtype"] isEqualToString:
@"scancode"])
76 freerdp_input_send_keyboard_event(
77 input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
78 [[event_description objectForKey:
@"scancode"] unsignedShortValue]);
79 else if ([[event_description objectForKey:
@"subtype"] isEqualToString:
@"unicode"])
80 freerdp_input_send_unicode_keyboard_event(
81 input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
82 [[event_description objectForKey:
@"unicode_char"] unsignedShortValue]);
84 NSLog(
@"%s: doesn't know how to send keyboard input with subtype %@", __func__,
85 [event_description objectForKey:
@"subtype"]);
87 else if ([event_type isEqualToString:
@"disconnect"])
88 should_continue = FALSE;
90 NSLog(
@"%s: unrecognized event type: %@", __func__, event_type);
92 return should_continue;
95 BOOL ios_events_check_handle(mfInfo *mfi)
99 if (WaitForSingleObject(mfi->handle, 0) != WAIT_OBJECT_0)
102 if (mfi->event_pipe_consumer == -1)
105 uint32_t archived_data_length = 0;
109 bytes_read = read(mfi->event_pipe_consumer, &archived_data_length, 4);
111 if (bytes_read == -1 || archived_data_length < 1 || archived_data_length > 32000)
113 NSLog(
@"%s: just read length descriptor. bytes_read=%ld, archived_data_length=%u", __func__,
114 bytes_read, archived_data_length);
120 NSMutableData *archived_object_data =
121 [[NSMutableData alloc] initWithLength:archived_data_length];
123 read(mfi->event_pipe_consumer, [archived_object_data mutableBytes], archived_data_length);
125 if (bytes_read != archived_data_length)
127 NSLog(
@"%s: attempted to read data; read %ld bytes but wanted %d bytes.", __func__,
128 bytes_read, archived_data_length);
129 [archived_object_data release];
133 id unarchived_object_data = [NSKeyedUnarchiver unarchiveObjectWithData:archived_object_data];
134 [archived_object_data release];
136 return ios_events_handle_event(mfi, unarchived_object_data);
139 HANDLE ios_events_get_handle(mfInfo *mfi)
146 BOOL ios_events_create_pipe(mfInfo *mfi)
152 if (pipe(pipe_fds) == -1)
154 NSLog(
@"%s: pipe failed.", __func__);
158 mfi->event_pipe_consumer = pipe_fds[0];
159 mfi->event_pipe_producer = pipe_fds[1];
160 mfi->handle = CreateFileDescriptorEvent(NULL, FALSE, FALSE, mfi->event_pipe_consumer,
161 WINPR_FD_READ | WINPR_FD_WRITE);
165 void ios_events_free_pipe(mfInfo *mfi)
168 int consumer_fd = mfi->event_pipe_consumer, producer_fd = mfi->event_pipe_producer;
170 mfi->event_pipe_consumer = mfi->event_pipe_producer = -1;
173 (void)CloseHandle(mfi->handle);