Merge pull request #5416 from m4ntis/proxy/multimon
[platform/upstream/freerdp.git] / server / proxy / pf_context.c
1 /**
2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * FreeRDP Proxy Server
4 *
5 * Copyright 2019 Mati Shabtay <matishabtay@gmail.com>
6 * Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.com>
7 * Copyright 2019 Idan Freiberg <speidy@gmail.com>
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *     http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 #include "pf_client.h"
23 #include "pf_context.h"
24 #include "pf_common.h"
25
26 /* Proxy context initialization callback */
27 static BOOL client_to_proxy_context_new(freerdp_peer* client,
28                                         pServerContext* context)
29 {
30         context->vcm = WTSOpenServerA((LPSTR) client->context);
31
32         if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
33                 goto fail_open_server;
34
35         return TRUE;
36 fail_open_server:
37         context->vcm = NULL;
38         return FALSE;
39 }
40
41 /* Proxy context free callback */
42 static void client_to_proxy_context_free(freerdp_peer* client,
43         pServerContext* context)
44 {
45         WINPR_UNUSED(client);
46
47         if (context)
48                 WTSCloseServer((HANDLE) context->vcm);
49 }
50
51 BOOL init_p_server_context(freerdp_peer* client)
52 {
53         client->ContextSize = sizeof(pServerContext);
54         client->ContextNew = (psPeerContextNew) client_to_proxy_context_new;
55         client->ContextFree = (psPeerContextFree) client_to_proxy_context_free;
56         return freerdp_peer_context_new(client);
57 }
58
59 rdpContext* p_client_context_create(rdpSettings* clientSettings,
60                                     char* host, DWORD port)
61 {
62         RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
63         rdpContext* context;
64         rdpSettings* settings;
65         RdpClientEntry(&clientEntryPoints);
66         context = freerdp_client_context_new(&clientEntryPoints);
67
68         if (!context)
69                 return NULL;
70
71         settings = context->settings;
72         pf_common_copy_settings(settings, clientSettings);
73         settings->Username = _strdup(clientSettings->Username);
74         settings->Password = _strdup(clientSettings->Password);
75         settings->Domain = _strdup(clientSettings->Domain);
76         settings->ServerHostname = host;
77         settings->ServerPort = port;
78         settings->SoftwareGdi = FALSE;
79         settings->RedirectClipboard = FALSE;
80         /* Client Monitor Data */
81         settings->MonitorCount = clientSettings->MonitorCount;
82         settings->SpanMonitors = clientSettings->SpanMonitors;
83         settings->UseMultimon = clientSettings->UseMultimon;
84         settings->ForceMultimon = clientSettings->ForceMultimon;
85         settings->DesktopPosX = clientSettings->DesktopPosX;
86         settings->DesktopPosY = clientSettings->DesktopPosY;
87         settings->ListMonitors = clientSettings->ListMonitors;
88         settings->NumMonitorIds = clientSettings->NumMonitorIds;
89         settings->MonitorLocalShiftX = clientSettings->MonitorLocalShiftX;
90         settings->MonitorLocalShiftY = clientSettings->MonitorLocalShiftY;
91         settings->HasMonitorAttributes = clientSettings->HasMonitorAttributes;
92         settings->MonitorCount = clientSettings->MonitorCount;
93         settings->MonitorDefArraySize = clientSettings->MonitorDefArraySize;
94
95         if (clientSettings->MonitorDefArraySize > 0)
96         {
97                 settings->MonitorDefArray = (rdpMonitor*) calloc(clientSettings->MonitorDefArraySize,
98                                             sizeof(rdpMonitor));
99
100                 if (!settings->MonitorDefArray)
101                 {
102                         goto error;
103                 }
104
105                 CopyMemory(settings->MonitorDefArray, clientSettings->MonitorDefArray,
106                            sizeof(rdpMonitor) * clientSettings->MonitorDefArraySize);
107         }
108         else
109                 settings->MonitorDefArray = NULL;
110
111         settings->MonitorIds = (UINT32*) calloc(16, sizeof(UINT32));
112
113         if (!settings->MonitorIds)
114                 goto error;
115
116         CopyMemory(settings->MonitorIds, clientSettings->MonitorIds, 16 * sizeof(UINT32));
117         return context;
118 error:
119         freerdp_client_context_free(context);
120         return NULL;
121 }