11#include <winpr/assert.h>
13#include "ios_freerdp_events.h"
14#include "ios_cliprdr.h"
18#pragma mark Sending compacted input events (from main thread)
22BOOL ios_events_send(mfInfo *mfi, NSDictionary *event_description)
25 NSData *encoded_description = [NSKeyedArchiver archivedDataWithRootObject:event_description
26 requiringSecureCoding:YES
29 if (!encoded_description)
31 NSLog(
@"%s: Failed to archive event (type: %@): %@", __func__,
32 [event_description objectForKey:
@"type"], error);
38 if ([encoded_description length] > 32000 || (mfi->event_pipe_producer == -1))
41 uint32_t archived_data_len = (uint32_t)[encoded_description length];
45 if (write(mfi->event_pipe_producer, &archived_data_len, 4) == -1)
47 NSLog(
@"%s: Failed to write length descriptor to pipe.", __func__);
51 if (write(mfi->event_pipe_producer, [encoded_description bytes], archived_data_len) == -1)
53 NSLog(
@"%s: Failed to write %d bytes into the event queue (event type: %@).", __func__,
54 (
int)[encoded_description length], [event_description objectForKey:
@"type"]);
62#pragma mark Processing compacted input events (from connection thread runloop)
64static BOOL ios_events_handle_event(mfInfo *mfi, NSDictionary *event_description)
66 NSString *event_type = [event_description objectForKey:@"type"];
67 BOOL should_continue = TRUE;
72 freerdp *instance = mfi->instance;
73 WINPR_ASSERT(instance);
74 WINPR_ASSERT(instance->context);
76 input = instance->context->input;
79 if ([event_type isEqualToString:
@"mouse"])
81 if (!input->MouseEvent(input,
82 [[event_description objectForKey:
@"flags"] unsignedShortValue],
83 [[event_description objectForKey:
@"coord_x"] unsignedShortValue],
84 [[event_description objectForKey:
@"coord_y"] unsignedShortValue]))
87 NSLog(
@"%s: MouseEvent failed.", __func__);
90 else if ([event_type isEqualToString:
@"keyboard"])
92 if ([[event_description objectForKey:
@"subtype"] isEqualToString:
@"scancode"])
93 freerdp_input_send_keyboard_event(
94 input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
95 [[event_description objectForKey:
@"scancode"] unsignedShortValue]);
96 else if ([[event_description objectForKey:
@"subtype"] isEqualToString:
@"unicode"])
97 freerdp_input_send_unicode_keyboard_event(
98 input, [[event_description objectForKey:
@"flags"] unsignedShortValue],
99 [[event_description objectForKey:
@"unicode_char"] unsignedShortValue]);
101 NSLog(
@"%s: doesn't know how to send keyboard input with subtype %@", __func__,
102 [event_description objectForKey:
@"subtype"]);
104 else if ([event_type isEqualToString:
@"clipboard"])
107 afc->clipboardHasText = [[event_description objectForKey:@"has_text"] boolValue];
110 if (afc->cliprdr && afc->clipboardSync &&
111 (ios_cliprdr_send_client_format_list(afc->cliprdr) != CHANNEL_RC_OK))
112 NSLog(
@"%s: failed to send the clipboard format list.", __func__);
114 else if ([event_type isEqualToString:
@"resize"])
115 ios_disp_send_monitor_layout(mfi->context,
116 [[event_description objectForKey:
@"width"] unsignedIntValue],
117 [[event_description objectForKey:
@"height"] unsignedIntValue]);
118 else if ([event_type isEqualToString:
@"disconnect"])
119 should_continue = FALSE;
121 NSLog(
@"%s: unrecognized event type: %@", __func__, event_type);
123 return should_continue;
126BOOL ios_events_check_handle(mfInfo *mfi)
130 if (WaitForSingleObject(mfi->handle, 0) != WAIT_OBJECT_0)
133 if (mfi->event_pipe_consumer == -1)
136 uint32_t archived_data_length = 0;
140 bytes_read = read(mfi->event_pipe_consumer, &archived_data_length, 4);
142 if (bytes_read == -1 || archived_data_length < 1 || archived_data_length > 32000)
144 NSLog(
@"%s: just read length descriptor. bytes_read=%ld, archived_data_length=%u", __func__,
145 bytes_read, archived_data_length);
151 NSMutableData *archived_object_data =
152 [[NSMutableData alloc] initWithLength:archived_data_length];
154 read(mfi->event_pipe_consumer, [archived_object_data mutableBytes], archived_data_length);
156 if (bytes_read != archived_data_length)
158 NSLog(
@"%s: attempted to read data; read %ld bytes but wanted %d bytes.", __func__,
159 bytes_read, archived_data_length);
160 [archived_object_data release];
164 NSError *error = nil;
165 NSDictionary *unarchived_object_data = [NSKeyedUnarchiver
166 unarchivedObjectOfClasses:[NSSet setWithObjects:[NSDictionary class], [NSString class],
167 [NSNumber class], nil]
168 fromData:archived_object_data
170 [archived_object_data release];
172 if (!unarchived_object_data)
175 NSLog(
@"%s: Failed to unarchive input event: %@", __func__, error);
179 return ios_events_handle_event(mfi, unarchived_object_data);
182HANDLE ios_events_get_handle(mfInfo *mfi)
189BOOL ios_events_create_pipe(mfInfo *mfi)
195 if (pipe(pipe_fds) == -1)
197 NSLog(
@"%s: pipe failed.", __func__);
201 mfi->event_pipe_consumer = pipe_fds[0];
202 mfi->event_pipe_producer = pipe_fds[1];
203 mfi->handle = CreateFileDescriptorEvent(
nullptr, FALSE, FALSE, mfi->event_pipe_consumer,
204 WINPR_FD_READ | WINPR_FD_WRITE);
208void ios_events_free_pipe(mfInfo *mfi)
211 int consumer_fd = mfi->event_pipe_consumer, producer_fd = mfi->event_pipe_producer;
213 mfi->event_pipe_consumer = mfi->event_pipe_producer = -1;
216 (void)CloseHandle(mfi->handle);