FreeRDP
mf_client.m
1 
20 #include <freerdp/config.h>
21 
22 #include "mfreerdp.h"
23 
24 #include <winpr/assert.h>
25 
26 #include <freerdp/constants.h>
27 #include <freerdp/utils/signal.h>
28 #include <freerdp/client/cmdline.h>
29 
30 #include "MRDPView.h"
31 
36 static BOOL mfreerdp_client_global_init(void)
37 {
38  freerdp_handle_signals();
39  return TRUE;
40 }
41 
42 static void mfreerdp_client_global_uninit(void)
43 {
44 }
45 
46 static int mfreerdp_client_start(rdpContext *context)
47 {
48  MRDPView *view;
49  mfContext *mfc = (mfContext *)context;
50 
51  if (mfc->view == NULL)
52  {
53  // view not specified beforehand. Create view dynamically
54  mfc->view = [[MRDPView alloc]
55  initWithFrame:NSMakeRect(
56  0, 0,
57  freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopWidth),
58  freerdp_settings_get_uint32(context->settings,
59  FreeRDP_DesktopHeight))];
60  mfc->view_ownership = TRUE;
61  }
62 
63  view = (MRDPView *)mfc->view;
64  return [view rdpStart:context];
65 }
66 
67 static int mfreerdp_client_stop(rdpContext *context)
68 {
69  mfContext *mfc = (mfContext *)context;
70 
71  freerdp_client_common_stop(context);
72 
73  if (mfc->view_ownership)
74  {
75  MRDPView *view = (MRDPView *)mfc->view;
76  [view releaseResources];
77  [view release];
78  mfc->view = nil;
79  }
80 
81  return 0;
82 }
83 
84 static BOOL mfreerdp_client_new(freerdp *instance, rdpContext *context)
85 {
86  mfContext *mfc;
87 
88  WINPR_ASSERT(instance);
89 
90  mfc = (mfContext *)instance->context;
91  WINPR_ASSERT(mfc);
92 
93  mfc->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
94  if (!mfc->stopEvent)
95  return FALSE;
96  context->instance->PreConnect = mac_pre_connect;
97  context->instance->PostConnect = mac_post_connect;
98  context->instance->PostDisconnect = mac_post_disconnect;
99  context->instance->AuthenticateEx = mac_authenticate_ex;
100  context->instance->VerifyCertificateEx = mac_verify_certificate_ex;
101  context->instance->VerifyChangedCertificateEx = mac_verify_changed_certificate_ex;
102  context->instance->LogonErrorInfo = mac_logon_error_info;
103  return TRUE;
104 }
105 
106 static void mfreerdp_client_free(freerdp *instance, rdpContext *context)
107 {
108  mfContext *mfc;
109 
110  if (!instance || !context)
111  return;
112 
113  mfc = (mfContext *)instance->context;
114  (void)CloseHandle(mfc->stopEvent);
115 }
116 
117 static void mf_scale_mouse_coordinates(mfContext *mfc, UINT16 *px, UINT16 *py)
118 {
119  UINT16 x = *px;
120  UINT16 y = *py;
121  UINT32 ww = mfc->client_width;
122  UINT32 wh = mfc->client_height;
123  UINT32 dw = freerdp_settings_get_uint32(mfc->common.context.settings, FreeRDP_DesktopWidth);
124  UINT32 dh = freerdp_settings_get_uint32(mfc->common.context.settings, FreeRDP_DesktopHeight);
125 
126  if (!freerdp_settings_get_bool(mfc->common.context.settings, FreeRDP_SmartSizing) ||
127  ((ww == dw) && (wh == dh)))
128  {
129  y = y + mfc->yCurrentScroll;
130  x = x + mfc->xCurrentScroll;
131 
132  y -= (dh - wh);
133  x -= (dw - ww);
134  }
135  else
136  {
137  y = y * dh / wh + mfc->yCurrentScroll;
138  x = x * dw / ww + mfc->xCurrentScroll;
139  }
140 
141  *px = x;
142  *py = y;
143 }
144 
145 void mf_scale_mouse_event(void *context, UINT16 flags, UINT16 x, UINT16 y)
146 {
147  mfContext *mfc = (mfContext *)context;
148  MRDPView *view = (MRDPView *)mfc->view;
149  // Convert to windows coordinates
150  y = [view frame].size.height - y;
151 
152  if ((flags & (PTR_FLAGS_WHEEL | PTR_FLAGS_HWHEEL)) == 0)
153  mf_scale_mouse_coordinates(mfc, &x, &y);
154  freerdp_client_send_button_event(&mfc->common, FALSE, flags, x, y);
155 }
156 
157 void mf_scale_mouse_event_ex(void *context, UINT16 flags, UINT16 x, UINT16 y)
158 {
159  mfContext *mfc = (mfContext *)context;
160  MRDPView *view = (MRDPView *)mfc->view;
161  // Convert to windows coordinates
162  y = [view frame].size.height - y;
163 
164  mf_scale_mouse_coordinates(mfc, &x, &y);
165  freerdp_client_send_extended_button_event(&mfc->common, FALSE, flags, x, y);
166 }
167 
168 void mf_press_mouse_button(void *context, int button, int x, int y, BOOL down)
169 {
170  UINT16 flags = 0;
171  UINT16 xflags = 0;
172 
173  if (down)
174  {
175  flags |= PTR_FLAGS_DOWN;
176  xflags |= PTR_XFLAGS_DOWN;
177  }
178 
179  switch (button)
180  {
181  case 0:
182  mf_scale_mouse_event(context, flags | PTR_FLAGS_BUTTON1, x, y);
183  break;
184 
185  case 1:
186  mf_scale_mouse_event(context, flags | PTR_FLAGS_BUTTON2, x, y);
187  break;
188 
189  case 2:
190  mf_scale_mouse_event(context, flags | PTR_FLAGS_BUTTON3, x, y);
191  break;
192 
193  case 3:
194  mf_scale_mouse_event_ex(context, xflags | PTR_XFLAGS_BUTTON1, x, y);
195  break;
196 
197  case 4:
198  mf_scale_mouse_event_ex(context, xflags | PTR_XFLAGS_BUTTON2, x, y);
199  break;
200 
201  default:
202  break;
203  }
204 }
205 
206 int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS *pEntryPoints)
207 {
208  WINPR_ASSERT(pEntryPoints);
209 
210  pEntryPoints->Version = 1;
211  pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
212  pEntryPoints->GlobalInit = mfreerdp_client_global_init;
213  pEntryPoints->GlobalUninit = mfreerdp_client_global_uninit;
214  pEntryPoints->ContextSize = sizeof(mfContext);
215  pEntryPoints->ClientNew = mfreerdp_client_new;
216  pEntryPoints->ClientFree = mfreerdp_client_free;
217  pEntryPoints->ClientStart = mfreerdp_client_start;
218  pEntryPoints->ClientStop = mfreerdp_client_stop;
219  return 0;
220 }
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.