After local X window manager has moved a window to a different location than what...
[platform/upstream/freerdp.git] / client / X11 / xf_rail.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * X11 RAIL
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22
23 #include <freerdp/utils/event.h>
24 #include <freerdp/utils/hexdump.h>
25 #include <freerdp/utils/rail.h>
26 #include <freerdp/rail/rail.h>
27
28 #include "xf_window.h"
29 #include "xf_rail.h"
30
31 void xf_rail_paint(xfInfo* xfi, rdpRail* rail, uint32 uleft, uint32 utop, uint32 uright, uint32 ubottom)
32 {
33         xfWindow* xfw;
34         rdpWindow* window;
35         boolean intersect;
36         uint32 iwidth, iheight;
37         uint32 ileft, itop, iright, ibottom;
38         uint32 wleft, wtop, wright, wbottom;
39
40         window_list_rewind(rail->list);
41
42         while (window_list_has_next(rail->list))
43         {
44                 window = window_list_get_next(rail->list);
45                 xfw = (xfWindow*) window->extra;
46
47                 wleft = window->windowOffsetX;
48                 wtop = window->windowOffsetY;
49                 wright = window->windowOffsetX + window->windowWidth - 1;
50                 wbottom = window->windowOffsetY + window->windowHeight - 1;
51
52                 ileft = MAX(uleft, wleft);
53                 itop = MAX(utop, wtop);
54                 iright = MIN(uright, wright);
55                 ibottom = MIN(ubottom, wbottom);
56
57                 iwidth = iright - ileft + 1;
58                 iheight = ibottom - itop + 1;
59
60                 intersect = ((iright > ileft) && (ibottom > itop)) ? true : false;
61
62                 if (intersect)
63                 {
64                         xf_UpdateWindowArea(xfi, xfw, ileft - wleft, itop - wtop, iwidth, iheight);
65                 }
66         }
67 }
68
69 void xf_rail_CreateWindow(rdpRail* rail, rdpWindow* window)
70 {
71         xfInfo* xfi;
72         xfWindow* xfw;
73
74         xfi = (xfInfo*) rail->extra;
75
76         xfw = xf_CreateWindow((xfInfo*) rail->extra, window,
77                         window->windowOffsetX, window->windowOffsetY,
78                         window->windowWidth, window->windowHeight,
79                         window->windowId);
80
81         xf_SetWindowStyle(xfi, xfw, window->style, window->extendedStyle);
82
83         XStoreName(xfi->display, xfw->handle, window->title);
84
85         window->extra = (void*) xfw;
86         window->extraId = (void*) xfw->handle;
87 }
88
89 void xf_rail_MoveWindow(rdpRail* rail, rdpWindow* window)
90 {
91         xfInfo* xfi;
92         xfWindow* xfw;
93
94         xfi = (xfInfo*) rail->extra;
95         xfw = (xfWindow*) window->extra;
96
97         // Do nothing if window is already in the correct position
98         if ( xfw->left == window->windowOffsetX && 
99                 xfw->top == window->windowOffsetY && 
100                 xfw->width == window->windowWidth && 
101                 xfw->height == window->windowHeight)
102         {
103                 return;
104         }
105
106         xf_MoveWindow((xfInfo*) rail->extra, xfw,
107                         window->windowOffsetX, window->windowOffsetY,
108                         window->windowWidth, window->windowHeight);
109 }
110
111 void xf_rail_ShowWindow(rdpRail* rail, rdpWindow* window, uint8 state)
112 {
113         xfInfo* xfi;
114         xfWindow* xfw;
115
116         xfi = (xfInfo*) rail->extra;
117         xfw = (xfWindow*) window->extra;
118
119         xf_ShowWindow((xfInfo*) rail->extra, xfw, state);
120 }
121
122 void xf_rail_SetWindowText(rdpRail* rail, rdpWindow* window)
123 {
124         xfInfo* xfi;
125         xfWindow* xfw;
126
127         xfi = (xfInfo*) rail->extra;
128         xfw = (xfWindow*) window->extra;
129
130         XStoreName(xfi->display, xfw->handle, window->title);
131 }
132
133 void xf_rail_SetWindowIcon(rdpRail* rail, rdpWindow* window, rdpIcon* icon)
134 {
135         xfInfo* xfi;
136         xfWindow* xfw;
137
138         xfi = (xfInfo*) rail->extra;
139         xfw = (xfWindow*) window->extra;
140
141         icon->extra = freerdp_icon_convert(icon->entry->bitsColor, NULL, icon->entry->bitsMask,
142                         icon->entry->width, icon->entry->height, icon->entry->bpp, rail->clrconv);
143
144         xf_SetWindowIcon(xfi, xfw, icon);
145 }
146
147 void xf_rail_SetWindowRects(rdpRail* rail, rdpWindow* window)
148 {
149         xfInfo* xfi;
150         xfWindow* xfw;
151
152         xfi = (xfInfo*) rail->extra;
153         xfw = (xfWindow*) window->extra;
154
155         xf_SetWindowRects(xfi, xfw, window->windowRects, window->numWindowRects);
156 }
157
158 void xf_rail_SetWindowVisibilityRects(rdpRail* rail, rdpWindow* window)
159 {
160         xfInfo* xfi;
161         xfWindow* xfw;
162
163         xfi = (xfInfo*) rail->extra;
164         xfw = (xfWindow*) window->extra;
165
166         xf_SetWindowVisibilityRects(xfi, xfw, window->windowRects, window->numWindowRects);
167 }
168
169 void xf_rail_DestroyWindow(rdpRail* rail, rdpWindow* window)
170 {
171         xfWindow* xfw;
172         xfw = (xfWindow*) window->extra;
173         xf_DestroyWindow((xfInfo*) rail->extra, xfw);
174 }
175
176 void xf_rail_register_callbacks(xfInfo* xfi, rdpRail* rail)
177 {
178         rail->extra = (void*) xfi;
179         rail->rail_CreateWindow = xf_rail_CreateWindow;
180         rail->rail_MoveWindow = xf_rail_MoveWindow;
181         rail->rail_ShowWindow = xf_rail_ShowWindow;
182         rail->rail_SetWindowText = xf_rail_SetWindowText;
183         rail->rail_SetWindowIcon = xf_rail_SetWindowIcon;
184         rail->rail_SetWindowRects = xf_rail_SetWindowRects;
185         rail->rail_SetWindowVisibilityRects = xf_rail_SetWindowVisibilityRects;
186         rail->rail_DestroyWindow = xf_rail_DestroyWindow;
187 }
188
189 static void xf_on_free_rail_client_event(RDP_EVENT* event)
190 {
191         if (event->event_class == RDP_EVENT_CLASS_RAIL)
192         {
193                 rail_free_cloned_order(event->event_type, event->user_data);
194         }
195 }
196
197 static void xf_send_rail_client_event(rdpChannels* channels, uint16 event_type, void* param)
198 {
199         RDP_EVENT* out_event = NULL;
200         void * payload = NULL;
201
202         payload = rail_clone_order(event_type, param);
203         if (payload != NULL)
204         {
205                 out_event = freerdp_event_new(RDP_EVENT_CLASS_RAIL, event_type,
206                         xf_on_free_rail_client_event, payload);
207                 freerdp_channels_send_event(channels, out_event);
208         }
209 }
210
211 void xf_rail_send_windowmove(xfInfo* xfi, uint32 windowId, uint32 left, uint32 top, uint32 right, uint32 bottom)
212 {
213         rdpChannels* channels;
214         RAIL_WINDOW_MOVE_ORDER window_move;
215
216         channels = xfi->_context->channels;
217
218         window_move.windowId = windowId;
219         window_move.left = left;
220         window_move.top = top;
221         window_move.right = right;
222         window_move.bottom = bottom;
223
224         xf_send_rail_client_event(channels, RDP_EVENT_TYPE_RAIL_CLIENT_WINDOW_MOVE, &window_move);
225 }
226
227 void xf_rail_send_activate(xfInfo* xfi, Window xwindow, boolean enabled)
228 {
229         rdpRail* rail;
230         rdpChannels* channels;
231         rdpWindow* rail_window;
232         RAIL_ACTIVATE_ORDER activate;
233
234         rail = xfi->_context->rail;
235         channels = xfi->_context->channels;
236
237         rail_window = window_list_get_by_extra_id(rail->list, (void*) xwindow);
238
239         if (rail_window == NULL)
240                 return;
241
242         activate.windowId = rail_window->windowId;
243         activate.enabled = enabled;
244
245         xf_send_rail_client_event(channels, RDP_EVENT_TYPE_RAIL_CLIENT_ACTIVATE, &activate);
246 }
247
248 void xf_rail_send_client_system_command(xfInfo* xfi, uint32 windowId, uint16 command)
249 {
250         rdpChannels* channels;
251         RAIL_SYSCOMMAND_ORDER syscommand;
252
253         channels = xfi->_context->channels;
254
255         syscommand.windowId = windowId;
256         syscommand.command = command;
257
258         xf_send_rail_client_event(channels, RDP_EVENT_TYPE_RAIL_CLIENT_SYSCOMMAND, &syscommand);
259 }
260
261 void xf_process_rail_get_sysparams_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
262 {
263         RAIL_SYSPARAM_ORDER* sysparam;
264
265         sysparam = (RAIL_SYSPARAM_ORDER*) event->user_data;
266
267         sysparam->workArea.left = xfi->workArea.x;
268         sysparam->workArea.top = xfi->workArea.y;
269         sysparam->workArea.right = xfi->workArea.x + xfi->workArea.width;
270         sysparam->workArea.bottom = xfi->workArea.y + xfi->workArea.height;
271
272         sysparam->taskbarPos.left = 0;
273         sysparam->taskbarPos.top = 0;
274         sysparam->taskbarPos.right = 0;
275         sysparam->taskbarPos.bottom = 0;
276
277         sysparam->dragFullWindows = false;
278
279         xf_send_rail_client_event(channels, RDP_EVENT_TYPE_RAIL_CLIENT_SET_SYSPARAMS, sysparam);
280 }
281
282 const char* error_code_names[] =
283 {
284                 "RAIL_EXEC_S_OK",
285                 "RAIL_EXEC_E_HOOK_NOT_LOADED",
286                 "RAIL_EXEC_E_DECODE_FAILED",
287                 "RAIL_EXEC_E_NOT_IN_ALLOWLIST",
288                 "RAIL_EXEC_E_FILE_NOT_FOUND",
289                 "RAIL_EXEC_E_FAIL",
290                 "RAIL_EXEC_E_SESSION_LOCKED"
291 };
292
293 void xf_process_rail_exec_result_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
294 {
295         RAIL_EXEC_RESULT_ORDER* exec_result;
296
297         exec_result = (RAIL_EXEC_RESULT_ORDER*) event->user_data;
298
299         if (exec_result->execResult != RAIL_EXEC_S_OK)
300         {
301                 printf("RAIL exec error: execResult=%s NtError=0x%X\n",
302                         error_code_names[exec_result->execResult], exec_result->rawResult);
303         }
304 }
305
306 void xf_process_rail_server_sysparam_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
307 {
308         RAIL_SYSPARAM_ORDER* sysparam = (RAIL_SYSPARAM_ORDER*) event->user_data;
309
310         switch (sysparam->param)
311         {
312                 case SPI_SET_SCREEN_SAVE_ACTIVE:
313                         break;
314
315                 case SPI_SET_SCREEN_SAVE_SECURE:
316                         break;
317         }
318 }
319
320 void xf_process_rail_server_minmaxinfo_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
321 {
322         rdpRail* rail;
323         rdpWindow* rail_window = NULL;
324         RAIL_MINMAXINFO_ORDER* minmax = (RAIL_MINMAXINFO_ORDER*) event->user_data;
325
326         rail = ((rdpContext*) xfi->context)->rail;
327         rail_window = window_list_get_by_id(rail->list, minmax->windowId);
328
329         if (rail_window != NULL)
330         {
331                 xfWindow * window = NULL;
332                 window = (xfWindow *) rail_window->extra;
333
334                 DEBUG_X11_LMS("windowId=0x%X maxWidth=%d maxHeight=%d maxPosX=%d maxPosY=%d "
335                         "minTrackWidth=%d minTrackHeight=%d maxTrackWidth=%d maxTrackHeight=%d",
336                         minmax->windowId, minmax->maxWidth, minmax->maxHeight,
337                         (sint16)minmax->maxPosX, (sint16)minmax->maxPosY,
338                         minmax->minTrackWidth, minmax->minTrackHeight,
339                         minmax->maxTrackWidth, minmax->maxTrackHeight);
340
341                 xf_SetWindowMinMaxInfo(xfi, window, minmax->maxWidth, minmax->maxHeight, minmax->maxPosX, minmax->maxPosY,
342                         minmax->minTrackWidth, minmax->minTrackHeight, minmax->maxTrackWidth, minmax->maxTrackHeight);
343         }
344 }
345
346 const char* movetype_names[] =
347 {
348         "(invalid)",
349         "RAIL_WMSZ_LEFT",
350         "RAIL_WMSZ_RIGHT",
351         "RAIL_WMSZ_TOP",
352         "RAIL_WMSZ_TOPLEFT",
353         "RAIL_WMSZ_TOPRIGHT",
354         "RAIL_WMSZ_BOTTOM",
355         "RAIL_WMSZ_BOTTOMLEFT",
356         "RAIL_WMSZ_BOTTOMRIGHT",
357         "RAIL_WMSZ_MOVE",
358         "RAIL_WMSZ_KEYMOVE",
359         "RAIL_WMSZ_KEYSIZE"
360 };
361
362 void xf_process_rail_server_localmovesize_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
363 {
364         rdpRail* rail;
365         rdpWindow* rail_window = NULL;
366         RAIL_LOCALMOVESIZE_ORDER* movesize = (RAIL_LOCALMOVESIZE_ORDER*) event->user_data;
367
368         rail = ((rdpContext*) xfi->context)->rail;
369         rail_window = window_list_get_by_id(rail->list, movesize->windowId);
370
371         if (rail_window != NULL)
372         {
373                 xfWindow* window = NULL;
374                 window = (xfWindow*) rail_window->extra;
375
376                 DEBUG_X11_LMS("windowId=0x%X isMoveSizeStart=%d moveSizeType=%s PosX=%d PosY=%d",
377                         movesize->windowId, movesize->isMoveSizeStart,
378                         movetype_names[movesize->moveSizeType], (sint16) movesize->posX, (sint16) movesize->posY);
379
380                 if (movesize->isMoveSizeStart)
381                         xf_StartLocalMoveSize(xfi, window, movesize->moveSizeType, (int) movesize->posX, (int) movesize->posY);
382                 else
383                         xf_StopLocalMoveSize(xfi, window, movesize->moveSizeType, (int) movesize->posX, (int) movesize->posY);
384         }
385
386 }
387
388 void xf_process_rail_appid_resp_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
389 {
390         RAIL_GET_APPID_RESP_ORDER* appid_resp =
391                 (RAIL_GET_APPID_RESP_ORDER*)event->user_data;
392
393         printf("Server Application ID Response PDU: windowId=0x%X "
394                 "applicationId=(length=%d dump)\n",
395                 appid_resp->windowId, appid_resp->applicationId.length);
396
397         freerdp_hexdump(appid_resp->applicationId.string, appid_resp->applicationId.length);
398 }
399
400 void xf_process_rail_langbarinfo_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
401 {
402         RAIL_LANGBAR_INFO_ORDER* langbar =
403                 (RAIL_LANGBAR_INFO_ORDER*) event->user_data;
404
405         printf("Language Bar Information PDU: languageBarStatus=0x%X\n",
406                 langbar->languageBarStatus);
407 }
408
409 void xf_process_rail_event(xfInfo* xfi, rdpChannels* channels, RDP_EVENT* event)
410 {
411         switch (event->event_type)
412         {
413                 case RDP_EVENT_TYPE_RAIL_CHANNEL_GET_SYSPARAMS:
414                         xf_process_rail_get_sysparams_event(xfi, channels, event);
415                         break;
416
417                 case RDP_EVENT_TYPE_RAIL_CHANNEL_EXEC_RESULTS:
418                         xf_process_rail_exec_result_event(xfi, channels, event);
419                         break;
420
421                 case RDP_EVENT_TYPE_RAIL_CHANNEL_SERVER_SYSPARAM:
422                         xf_process_rail_server_sysparam_event(xfi, channels, event);
423                         break;
424
425                 case RDP_EVENT_TYPE_RAIL_CHANNEL_SERVER_MINMAXINFO:
426                         xf_process_rail_server_minmaxinfo_event(xfi, channels, event);
427                         break;
428
429                 case RDP_EVENT_TYPE_RAIL_CHANNEL_SERVER_LOCALMOVESIZE:
430                         xf_process_rail_server_localmovesize_event(xfi, channels, event);
431                         break;
432
433                 case RDP_EVENT_TYPE_RAIL_CHANNEL_APPID_RESP:
434                         xf_process_rail_appid_resp_event(xfi, channels, event);
435                         break;
436
437                 case RDP_EVENT_TYPE_RAIL_CHANNEL_LANGBARINFO:
438                         xf_process_rail_langbarinfo_event(xfi, channels, event);
439                         break;
440
441                 default:
442                         break;
443         }
444 }