FreeRDP
Loading...
Searching...
No Matches
ios_freerdp_events.m
1/*
2 RDP event queuing
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Dorian Johnson
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9 */
10
11#include <winpr/assert.h>
12
13#include "ios_freerdp_events.h"
14#include "ios_cliprdr.h"
15#include "ios_disp.h"
16
17#pragma mark -
18#pragma mark Sending compacted input events (from main thread)
19
20// While this function may be called from any thread that has an autorelease pool allocated, it is
21// not threadsafe: caller is responsible for synchronization
22BOOL ios_events_send(mfInfo *mfi, NSDictionary *event_description)
23{
24 NSError *error = nil;
25 NSData *encoded_description = [NSKeyedArchiver archivedDataWithRootObject:event_description
26 requiringSecureCoding:YES
27 error:&error];
28
29 if (!encoded_description)
30 {
31 NSLog(@"%s: Failed to archive event (type: %@): %@", __func__,
32 [event_description objectForKey:@"type"], error);
33 return FALSE;
34 }
35
36 WINPR_ASSERT(mfi);
37
38 if ([encoded_description length] > 32000 || (mfi->event_pipe_producer == -1))
39 return FALSE;
40
41 uint32_t archived_data_len = (uint32_t)[encoded_description length];
42
43 // NSLog(@"writing %d bytes to input event pipe", archived_data_len);
44
45 if (write(mfi->event_pipe_producer, &archived_data_len, 4) == -1)
46 {
47 NSLog(@"%s: Failed to write length descriptor to pipe.", __func__);
48 return FALSE;
49 }
50
51 if (write(mfi->event_pipe_producer, [encoded_description bytes], archived_data_len) == -1)
52 {
53 NSLog(@"%s: Failed to write %d bytes into the event queue (event type: %@).", __func__,
54 (int)[encoded_description length], [event_description objectForKey:@"type"]);
55 return FALSE;
56 }
57
58 return TRUE;
59}
60
61#pragma mark -
62#pragma mark Processing compacted input events (from connection thread runloop)
63
64static BOOL ios_events_handle_event(mfInfo *mfi, NSDictionary *event_description)
65{
66 NSString *event_type = [event_description objectForKey:@"type"];
67 BOOL should_continue = TRUE;
68 rdpInput *input;
69
70 WINPR_ASSERT(mfi);
71
72 freerdp *instance = mfi->instance;
73 WINPR_ASSERT(instance);
74 WINPR_ASSERT(instance->context);
75
76 input = instance->context->input;
77 WINPR_ASSERT(input);
78
79 if ([event_type isEqualToString:@"mouse"])
80 {
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]))
85 {
86 // Returning an error here can terminate the connection.
87 NSLog(@"%s: MouseEvent failed.", __func__);
88 }
89 }
90 else if ([event_type isEqualToString:@"keyboard"])
91 {
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]);
100 else
101 NSLog(@"%s: doesn't know how to send keyboard input with subtype %@", __func__,
102 [event_description objectForKey:@"subtype"]);
103 }
104 else if ([event_type isEqualToString:@"clipboard"])
105 {
106 mfContext *afc = mfi->context;
107 afc->clipboardHasText = [[event_description objectForKey:@"has_text"] boolValue];
108
109 // before the channel is ready the monitor ready handler sends the list instead
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__);
113 }
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;
120 else
121 NSLog(@"%s: unrecognized event type: %@", __func__, event_type);
122
123 return should_continue;
124}
125
126BOOL ios_events_check_handle(mfInfo *mfi)
127{
128 WINPR_ASSERT(mfi);
129
130 if (WaitForSingleObject(mfi->handle, 0) != WAIT_OBJECT_0)
131 return TRUE;
132
133 if (mfi->event_pipe_consumer == -1)
134 return TRUE;
135
136 uint32_t archived_data_length = 0;
137 ssize_t bytes_read;
138
139 // First, read the length of the blob
140 bytes_read = read(mfi->event_pipe_consumer, &archived_data_length, 4);
141
142 if (bytes_read == -1 || archived_data_length < 1 || archived_data_length > 32000)
143 {
144 NSLog(@"%s: just read length descriptor. bytes_read=%ld, archived_data_length=%u", __func__,
145 bytes_read, archived_data_length);
146 return FALSE;
147 }
148
149 // NSLog(@"reading %d bytes from input event pipe", archived_data_length);
150
151 NSMutableData *archived_object_data =
152 [[NSMutableData alloc] initWithLength:archived_data_length];
153 bytes_read =
154 read(mfi->event_pipe_consumer, [archived_object_data mutableBytes], archived_data_length);
155
156 if (bytes_read != archived_data_length)
157 {
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];
161 return FALSE;
162 }
163
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
169 error:&error];
170 [archived_object_data release];
171
172 if (!unarchived_object_data)
173 {
174 // just return TRUE and ignore data. (if return FALSE, sesison can be terminated)
175 NSLog(@"%s: Failed to unarchive input event: %@", __func__, error);
176 return TRUE;
177 }
178
179 return ios_events_handle_event(mfi, unarchived_object_data);
180}
181
182HANDLE ios_events_get_handle(mfInfo *mfi)
183{
184 WINPR_ASSERT(mfi);
185 return mfi->handle;
186}
187
188// Sets up the event pipe
189BOOL ios_events_create_pipe(mfInfo *mfi)
190{
191 int pipe_fds[2];
192
193 WINPR_ASSERT(mfi);
194
195 if (pipe(pipe_fds) == -1)
196 {
197 NSLog(@"%s: pipe failed.", __func__);
198 return FALSE;
199 }
200
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);
205 return TRUE;
206}
207
208void ios_events_free_pipe(mfInfo *mfi)
209{
210 WINPR_ASSERT(mfi);
211 int consumer_fd = mfi->event_pipe_consumer, producer_fd = mfi->event_pipe_producer;
212
213 mfi->event_pipe_consumer = mfi->event_pipe_producer = -1;
214 close(producer_fd);
215 close(consumer_fd);
216 (void)CloseHandle(mfi->handle);
217}