FreeRDP/client/Windows/wf_event.c

968 lines
26 KiB
C
Raw Normal View History

/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* Event Handling
*
* Copyright 2009-2011 Jay Sorg
* Copyright 2010-2011 Vic Lee
* Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-02-16 13:20:38 +03:00
#include <freerdp/config.h>
#include <stdio.h>
#include <winpr/assert.h>
2021-06-02 16:16:49 +03:00
#include <winpr/sysinfo.h>
#include <freerdp/freerdp.h>
#include "wf_client.h"
#include "wf_gdi.h"
#include "wf_event.h"
#include <freerdp/event.h>
#include <windowsx.h>
static HWND g_focus_hWnd = NULL;
static HWND g_main_hWnd = NULL;
static HWND g_parent_hWnd = NULL;
#define RESIZE_MIN_DELAY 200 /* minimum delay in ms between two resizes */
2019-11-06 17:24:51 +03:00
static BOOL wf_scale_blt(wfContext* wfc, HDC hdc, int x, int y, int w, int h, HDC hdcSrc, int x1,
int y1, DWORD rop);
static BOOL wf_scale_mouse_event(wfContext* wfc, UINT16 flags, INT32 x, INT32 y);
2018-03-08 12:58:47 +03:00
#if (_WIN32_WINNT >= 0x0500)
static BOOL wf_scale_mouse_event_ex(wfContext* wfc, UINT16 flags, UINT16 buttonMask, INT32 x,
INT32 y);
2018-03-08 12:58:47 +03:00
#endif
static BOOL g_flipping_in = FALSE;
static BOOL g_flipping_out = FALSE;
static BOOL g_keystates[256] = { 0 };
static BOOL ctrl_down(void)
{
return g_keystates[VK_CONTROL] || g_keystates[VK_LCONTROL] || g_keystates[VK_RCONTROL];
}
static BOOL alt_ctrl_down(void)
{
const BOOL altDown = g_keystates[VK_MENU] || g_keystates[VK_LMENU] || g_keystates[VK_RMENU];
return altDown && ctrl_down();
}
LRESULT CALLBACK wf_ll_kbd_proc(int nCode, WPARAM wParam, LPARAM lParam)
{
DWORD ext_proc_id = 0;
wfContext* wfc = NULL;
DWORD rdp_scancode;
BOOL keystate;
rdpInput* input;
PKBDLLHOOKSTRUCT p;
2019-11-06 17:24:51 +03:00
DEBUG_KBD("Low-level keyboard hook, hWnd %X nCode %X wParam %X", g_focus_hWnd, nCode, wParam);
if (g_flipping_in)
{
if (!alt_ctrl_down())
g_flipping_in = FALSE;
2016-08-03 13:09:24 +03:00
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
if (g_parent_hWnd && g_main_hWnd)
{
wfc = (wfContext*)GetWindowLongPtr(g_main_hWnd, GWLP_USERDATA);
GUITHREADINFO gui_thread_info;
gui_thread_info.cbSize = sizeof(GUITHREADINFO);
HWND fg_win_hwnd = GetForegroundWindow();
DWORD fg_win_thread_id = GetWindowThreadProcessId(fg_win_hwnd, &ext_proc_id);
BOOL result = GetGUIThreadInfo(fg_win_thread_id, &gui_thread_info);
if (gui_thread_info.hwndFocus != wfc->hWndParent)
{
g_focus_hWnd = NULL;
2022-06-23 08:57:38 +03:00
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
2022-06-23 08:57:38 +03:00
g_focus_hWnd = g_main_hWnd;
}
if (g_focus_hWnd && (nCode == HC_ACTION))
{
switch (wParam)
{
2016-10-04 10:00:00 +03:00
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
if (!wfc)
wfc = (wfContext*)GetWindowLongPtr(g_focus_hWnd, GWLP_USERDATA);
2019-11-06 17:24:51 +03:00
p = (PKBDLLHOOKSTRUCT)lParam;
2016-10-04 10:00:00 +03:00
if (!wfc || !p)
2016-08-03 13:37:40 +03:00
return 1;
input = wfc->common.context.input;
2019-11-06 17:24:51 +03:00
rdp_scancode = MAKE_RDP_SCANCODE((BYTE)p->scanCode, p->flags & LLKHF_EXTENDED);
keystate = g_keystates[p->scanCode & 0xFF];
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
g_keystates[p->scanCode & 0xFF] = TRUE;
break;
case WM_KEYUP:
case WM_SYSKEYUP:
default:
g_keystates[p->scanCode & 0xFF] = FALSE;
break;
}
DEBUG_KBD("keydown %d scanCode 0x%08lX flags 0x%08lX vkCode 0x%08lX",
(wParam == WM_KEYDOWN), p->scanCode, p->flags, p->vkCode);
2016-10-04 10:00:00 +03:00
if (wfc->fullscreen_toggle && (p->vkCode == VK_RETURN || p->vkCode == VK_CANCEL))
2016-08-03 13:37:40 +03:00
{
if (alt_ctrl_down())
2016-10-04 10:00:00 +03:00
{
if (wParam == WM_KEYDOWN)
{
wf_toggle_fullscreen(wfc);
return 1;
}
2016-10-04 10:00:00 +03:00
}
}
2016-10-04 10:00:00 +03:00
if (rdp_scancode == RDP_SCANCODE_NUMLOCK_EXTENDED)
{
2016-10-04 10:00:00 +03:00
/* Windows sends NumLock as extended - rdp doesn't */
DEBUG_KBD("hack: NumLock (x45) should not be extended");
rdp_scancode = RDP_SCANCODE_NUMLOCK;
}
2016-10-04 10:00:00 +03:00
else if (rdp_scancode == RDP_SCANCODE_NUMLOCK)
{
/* Windows sends Pause as if it was a RDP NumLock (handled above).
2019-11-06 17:24:51 +03:00
* It must however be sent as a one-shot Ctrl+NumLock */
2016-10-04 10:00:00 +03:00
if (wParam == WM_KEYDOWN)
{
DEBUG_KBD("Pause, sent as Ctrl+NumLock");
freerdp_input_send_keyboard_event_ex(input, TRUE, FALSE,
RDP_SCANCODE_LCONTROL);
freerdp_input_send_keyboard_event_ex(input, TRUE, FALSE,
RDP_SCANCODE_NUMLOCK);
freerdp_input_send_keyboard_event_ex(input, FALSE, FALSE,
RDP_SCANCODE_LCONTROL);
freerdp_input_send_keyboard_event_ex(input, FALSE, FALSE,
RDP_SCANCODE_NUMLOCK);
2016-10-04 10:00:00 +03:00
}
else
{
DEBUG_KBD("Pause up");
}
2016-10-04 10:00:00 +03:00
return 1;
}
else if (rdp_scancode == RDP_SCANCODE_RSHIFT_EXTENDED)
{
DEBUG_KBD("right shift (x36) should not be extended");
rdp_scancode = RDP_SCANCODE_RSHIFT;
}
freerdp_input_send_keyboard_event_ex(input, !(p->flags & LLKHF_UP), keystate,
rdp_scancode);
2019-11-06 17:24:51 +03:00
if (p->vkCode == VK_NUMLOCK || p->vkCode == VK_CAPITAL || p->vkCode == VK_SCROLL ||
p->vkCode == VK_KANA)
DEBUG_KBD(
"lock keys are processed on client side too to toggle their indicators");
2016-10-04 10:00:00 +03:00
else
return 1;
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
break;
default:
break;
}
}
if (g_flipping_out)
{
if (!alt_ctrl_down())
{
g_flipping_out = FALSE;
g_focus_hWnd = NULL;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
void wf_event_focus_in(wfContext* wfc)
{
UINT16 syncFlags;
rdpInput* input;
POINT pt;
RECT rc;
input = wfc->common.context.input;
syncFlags = 0;
if (GetKeyState(VK_NUMLOCK))
syncFlags |= KBD_SYNC_NUM_LOCK;
if (GetKeyState(VK_CAPITAL))
syncFlags |= KBD_SYNC_CAPS_LOCK;
if (GetKeyState(VK_SCROLL))
syncFlags |= KBD_SYNC_SCROLL_LOCK;
if (GetKeyState(VK_KANA))
syncFlags |= KBD_SYNC_KANA_LOCK;
input->FocusInEvent(input, syncFlags);
/* send pointer position if the cursor is currently inside our client area */
GetCursorPos(&pt);
ScreenToClient(wfc->hwnd, &pt);
GetClientRect(wfc->hwnd, &rc);
if (pt.x >= rc.left && pt.x < rc.right && pt.y >= rc.top && pt.y < rc.bottom)
input->MouseEvent(input, PTR_FLAGS_MOVE, (UINT16)pt.x, (UINT16)pt.y);
}
2019-11-06 17:24:51 +03:00
static BOOL wf_event_process_WM_MOUSEWHEEL(wfContext* wfc, HWND hWnd, UINT Msg, WPARAM wParam,
LPARAM lParam, BOOL horizontal, INT32 x, INT32 y)
2012-10-16 20:57:12 +04:00
{
int delta;
2018-03-08 12:58:47 +03:00
UINT16 flags = 0;
2012-10-16 20:57:12 +04:00
rdpInput* input;
WINPR_ASSERT(wfc);
input = wfc->common.context.input;
WINPR_ASSERT(input);
2012-10-16 20:57:12 +04:00
DefWindowProc(hWnd, Msg, wParam, lParam);
2019-11-06 17:24:51 +03:00
delta = ((signed short)HIWORD(wParam)); /* GET_WHEEL_DELTA_WPARAM(wParam); */
2012-10-17 15:48:24 +04:00
2018-03-08 12:58:47 +03:00
if (horizontal)
flags |= PTR_FLAGS_HWHEEL;
2012-10-16 20:57:12 +04:00
else
2018-03-08 12:58:47 +03:00
flags |= PTR_FLAGS_WHEEL;
if (delta < 0)
2012-10-16 20:57:12 +04:00
{
2018-03-08 12:58:47 +03:00
flags |= PTR_FLAGS_WHEEL_NEGATIVE;
/* 9bit twos complement, delta already negative */
delta = 0x100 + delta;
2012-10-16 20:57:12 +04:00
}
2012-10-17 15:48:24 +04:00
flags |= delta;
2022-01-19 15:11:34 +03:00
return wf_scale_mouse_event(wfc, flags, x, y);
2012-10-16 20:57:12 +04:00
}
2016-07-21 16:36:15 +03:00
static void wf_sizing(wfContext* wfc, WPARAM wParam, LPARAM lParam)
{
rdpSettings* settings = wfc->common.context.settings;
// Holding the CTRL key down while resizing the window will force the desktop aspect ratio.
LPRECT rect;
2013-06-16 06:57:21 +04:00
2023-10-13 10:48:44 +03:00
if ((freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate)) &&
ctrl_down())
{
2019-11-06 17:24:51 +03:00
rect = (LPRECT)wParam;
2016-08-03 13:09:24 +03:00
switch (lParam)
{
2016-10-04 10:00:00 +03:00
case WMSZ_LEFT:
case WMSZ_RIGHT:
case WMSZ_BOTTOMRIGHT:
// Adjust height
2023-10-13 10:48:44 +03:00
rect->bottom =
rect->top + freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) *
(rect->right - rect->left) /
freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
2016-10-04 10:00:00 +03:00
break;
2016-10-04 10:00:00 +03:00
case WMSZ_TOP:
case WMSZ_BOTTOM:
case WMSZ_TOPRIGHT:
// Adjust width
2023-10-13 10:48:44 +03:00
rect->right =
rect->left + freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) *
(rect->bottom - rect->top) /
freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2016-10-04 10:00:00 +03:00
break;
2016-10-04 10:00:00 +03:00
case WMSZ_BOTTOMLEFT:
case WMSZ_TOPLEFT:
// adjust width
2023-10-13 10:48:44 +03:00
rect->left =
rect->right - (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) *
(rect->bottom - rect->top) /
freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
2016-10-04 10:00:00 +03:00
break;
}
}
}
2021-06-02 16:16:49 +03:00
static void wf_send_resize(wfContext* wfc)
{
RECT windowRect;
int targetWidth = wfc->client_width;
int targetHeight = wfc->client_height;
rdpSettings* settings = wfc->common.context.settings;
2023-10-13 10:48:44 +03:00
if (freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate) && wfc->disp != NULL)
{
if (GetTickCount64() - wfc->lastSentDate > RESIZE_MIN_DELAY)
{
if (wfc->fullscreen)
{
GetWindowRect(wfc->hwnd, &windowRect);
targetWidth = windowRect.right - windowRect.left;
targetHeight = windowRect.bottom - windowRect.top;
}
2023-10-13 10:48:44 +03:00
if (freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingWidth) != targetWidth ||
freerdp_settings_get_uint32(settings, FreeRDP_SmartSizingHeight) != targetHeight)
{
2021-06-02 16:36:43 +03:00
DISPLAY_CONTROL_MONITOR_LAYOUT layout = { 0 };
layout.Flags = DISPLAY_CONTROL_MONITOR_PRIMARY;
layout.Top = layout.Left = 0;
layout.Width = targetWidth;
layout.Height = targetHeight;
2023-10-13 10:48:44 +03:00
layout.Orientation =
freerdp_settings_get_uint16(settings, FreeRDP_DesktopOrientation);
2023-10-13 10:48:44 +03:00
layout.DesktopScaleFactor =
freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
layout.DeviceScaleFactor =
freerdp_settings_get_uint32(settings, FreeRDP_DeviceScaleFactor);
2021-06-02 16:36:43 +03:00
layout.PhysicalWidth = targetWidth;
layout.PhysicalHeight = targetHeight;
if (IFCALLRESULT(CHANNEL_RC_OK, wfc->disp->SendMonitorLayout, wfc->disp, 1,
2021-06-02 16:36:43 +03:00
&layout) != CHANNEL_RC_OK)
{
WLog_ERR("", "SendMonitorLayout failed.");
}
2024-09-15 09:43:31 +03:00
(void)freerdp_settings_set_uint32(settings, FreeRDP_SmartSizingWidth, targetWidth);
(void)freerdp_settings_set_uint32(settings, FreeRDP_SmartSizingHeight,
targetHeight);
}
wfc->lastSentDate = GetTickCount64();
}
}
}
2019-11-06 17:24:51 +03:00
LRESULT CALLBACK wf_event_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
2024-09-12 13:39:18 +03:00
HDC hdc = { 0 };
PAINTSTRUCT ps = { 0 };
BOOL processed = FALSE;
RECT windowRect = { 0 };
MINMAXINFO* minmax = NULL;
SCROLLINFO si = { 0 };
processed = TRUE;
2024-09-12 13:39:18 +03:00
LONG_PTR ptr = GetWindowLongPtr(hWnd, GWLP_USERDATA);
wfContext* wfc = (wfContext*)ptr;
2013-06-16 06:57:21 +04:00
if (wfc != NULL)
{
rdpInput* input = wfc->common.context.input;
rdpSettings* settings = wfc->common.context.settings;
if (!g_parent_hWnd && wfc->hWndParent)
g_parent_hWnd = wfc->hWndParent;
if (!g_main_hWnd)
g_main_hWnd = wfc->hwnd;
switch (Msg)
{
2016-10-04 10:00:00 +03:00
case WM_MOVE:
if (!wfc->disablewindowtracking)
{
2019-11-06 17:24:51 +03:00
int x = (int)(short)LOWORD(lParam);
int y = (int)(short)HIWORD(lParam);
2016-10-04 10:00:00 +03:00
wfc->client_x = x;
wfc->client_y = y;
}
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
break;
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
case WM_GETMINMAXINFO:
2024-09-12 13:39:18 +03:00
if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
(freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate)))
2016-10-04 10:00:00 +03:00
{
processed = FALSE;
}
else
{
// Set maximum window size for resizing
2019-11-06 17:24:51 +03:00
minmax = (MINMAXINFO*)lParam;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// always use the last determined canvas diff, because it could be
// that the window is minimized when this gets called
// wf_update_canvas_diff(wfc);
2016-10-04 10:00:00 +03:00
if (!wfc->fullscreen)
{
// add window decoration
2023-10-13 10:48:44 +03:00
minmax->ptMaxTrackSize.x =
freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) +
wfc->diff.x;
minmax->ptMaxTrackSize.y =
freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) +
wfc->diff.y;
2016-10-04 10:00:00 +03:00
}
}
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
break;
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
case WM_SIZING:
wf_sizing(wfc, lParam, wParam);
break;
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
case WM_SIZE:
GetWindowRect(wfc->hwnd, &windowRect);
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
if (!wfc->fullscreen)
{
wfc->client_width = LOWORD(lParam);
wfc->client_height = HIWORD(lParam);
wfc->client_x = windowRect.left;
wfc->client_y = windowRect.top;
}
else
{
wfc->wasMaximized = TRUE;
wf_send_resize(wfc);
}
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
if (wfc->client_width && wfc->client_height)
{
wf_size_scrollbars(wfc, LOWORD(lParam), HIWORD(lParam));
2016-08-03 13:37:40 +03:00
2019-11-06 17:24:51 +03:00
// Workaround: when the window is maximized, the call to "ShowScrollBars"
// returns TRUE but has no effect.
2016-10-04 10:00:00 +03:00
if (wParam == SIZE_MAXIMIZED && !wfc->fullscreen)
{
2016-10-04 10:00:00 +03:00
SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, windowRect.right - windowRect.left,
2019-11-06 17:24:51 +03:00
windowRect.bottom - windowRect.top,
SWP_NOMOVE | SWP_FRAMECHANGED);
wfc->wasMaximized = TRUE;
wf_send_resize(wfc);
}
else if (wParam == SIZE_RESTORED && !wfc->fullscreen && wfc->wasMaximized)
{
wfc->wasMaximized = FALSE;
wf_send_resize(wfc);
}
else if (wParam == SIZE_MINIMIZED)
{
g_focus_hWnd = NULL;
}
2016-10-04 10:00:00 +03:00
}
break;
2016-10-04 10:00:00 +03:00
case WM_EXITSIZEMOVE:
wf_size_scrollbars(wfc, wfc->client_width, wfc->client_height);
wf_send_resize(wfc);
break;
2016-10-04 10:00:00 +03:00
case WM_ERASEBKGND:
/* Say we handled it - prevents flickering */
2019-11-06 17:24:51 +03:00
return (LRESULT)1;
2016-10-04 10:00:00 +03:00
case WM_PAINT:
2024-09-12 13:39:18 +03:00
{
2016-10-04 10:00:00 +03:00
hdc = BeginPaint(hWnd, &ps);
2024-09-12 13:39:18 +03:00
const int x = ps.rcPaint.left;
const int y = ps.rcPaint.top;
const int w = ps.rcPaint.right - ps.rcPaint.left + 1;
const int h = ps.rcPaint.bottom - ps.rcPaint.top + 1;
2016-10-04 10:00:00 +03:00
wf_scale_blt(wfc, hdc, x, y, w, h, wfc->primary->hdc,
x - wfc->offset_x + wfc->xCurrentScroll,
y - wfc->offset_y + wfc->yCurrentScroll, SRCCOPY);
EndPaint(hWnd, &ps);
2024-09-12 13:39:18 +03:00
}
break;
2018-03-08 12:58:47 +03:00
#if (_WIN32_WINNT >= 0x0500)
2018-03-08 12:58:47 +03:00
case WM_XBUTTONDOWN:
2022-01-19 15:11:34 +03:00
wf_scale_mouse_event_ex(wfc, PTR_XFLAGS_DOWN, GET_XBUTTON_WPARAM(wParam),
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2018-03-08 12:58:47 +03:00
break;
case WM_XBUTTONUP:
2022-01-19 15:11:34 +03:00
wf_scale_mouse_event_ex(wfc, 0, GET_XBUTTON_WPARAM(wParam),
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2018-03-08 12:58:47 +03:00
break;
#endif
case WM_MBUTTONDOWN:
2022-01-19 15:11:34 +03:00
wf_scale_mouse_event(wfc, PTR_FLAGS_DOWN | PTR_FLAGS_BUTTON3,
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2018-03-08 12:58:47 +03:00
break;
case WM_MBUTTONUP:
wf_scale_mouse_event(wfc, PTR_FLAGS_BUTTON3, GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2018-03-08 12:58:47 +03:00
break;
2016-10-04 10:00:00 +03:00
case WM_LBUTTONDOWN:
2022-01-19 15:11:34 +03:00
wf_scale_mouse_event(wfc, PTR_FLAGS_DOWN | PTR_FLAGS_BUTTON1,
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
SetCapture(wfc->hwnd);
break;
2016-10-04 10:00:00 +03:00
case WM_LBUTTONUP:
wf_scale_mouse_event(wfc, PTR_FLAGS_BUTTON1, GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
ReleaseCapture();
break;
2016-10-04 10:00:00 +03:00
case WM_RBUTTONDOWN:
2022-01-19 15:11:34 +03:00
wf_scale_mouse_event(wfc, PTR_FLAGS_DOWN | PTR_FLAGS_BUTTON2,
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
break;
2016-10-04 10:00:00 +03:00
case WM_RBUTTONUP:
wf_scale_mouse_event(wfc, PTR_FLAGS_BUTTON2, GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2012-10-16 20:57:12 +04:00
break;
2016-10-04 10:00:00 +03:00
case WM_MOUSEMOVE:
wf_scale_mouse_event(wfc, PTR_FLAGS_MOVE, GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2016-08-03 13:37:40 +03:00
break;
2018-03-08 12:58:47 +03:00
#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
2016-10-04 10:00:00 +03:00
case WM_MOUSEWHEEL:
2018-03-08 12:58:47 +03:00
wf_event_process_WM_MOUSEWHEEL(wfc, hWnd, Msg, wParam, lParam, FALSE,
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
break;
2018-03-08 12:58:47 +03:00
#endif
#if (_WIN32_WINNT >= 0x0600)
2018-03-08 12:58:47 +03:00
case WM_MOUSEHWHEEL:
wf_event_process_WM_MOUSEWHEEL(wfc, hWnd, Msg, wParam, lParam, TRUE,
GET_X_LPARAM(lParam) - wfc->offset_x,
GET_Y_LPARAM(lParam) - wfc->offset_y);
2018-03-08 12:58:47 +03:00
break;
#endif
2016-10-04 10:00:00 +03:00
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT)
SetCursor(wfc->cursor);
else
DefWindowProc(hWnd, Msg, wParam, lParam);
2016-08-03 13:09:24 +03:00
break;
2016-10-04 10:00:00 +03:00
case WM_HSCROLL:
2019-11-06 17:24:51 +03:00
{
int xDelta; // xDelta = new_pos - current_pos
int xNewPos; // new position
int yDelta = 0;
switch (LOWORD(wParam))
2016-10-04 10:00:00 +03:00
{
2019-11-06 17:24:51 +03:00
// User clicked the scroll bar shaft left of the scroll box.
case SB_PAGEUP:
xNewPos = wfc->xCurrentScroll - 50;
break;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// User clicked the scroll bar shaft right of the scroll box.
case SB_PAGEDOWN:
xNewPos = wfc->xCurrentScroll + 50;
break;
// User clicked the left arrow.
case SB_LINEUP:
xNewPos = wfc->xCurrentScroll - 5;
break;
// User clicked the right arrow.
case SB_LINEDOWN:
xNewPos = wfc->xCurrentScroll + 5;
break;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// User dragged the scroll box.
case SB_THUMBPOSITION:
xNewPos = HIWORD(wParam);
break;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// user is dragging the scrollbar
case SB_THUMBTRACK:
xNewPos = HIWORD(wParam);
2016-10-04 10:00:00 +03:00
break;
2019-11-06 17:24:51 +03:00
default:
xNewPos = wfc->xCurrentScroll;
2016-10-04 10:00:00 +03:00
}
2019-11-06 17:24:51 +03:00
// New position must be between 0 and the screen width.
xNewPos = MAX(0, xNewPos);
xNewPos = MIN(wfc->xMaxScroll, xNewPos);
// If the current position does not change, do not scroll.
if (xNewPos == wfc->xCurrentScroll)
break;
// Determine the amount scrolled (in pixels).
xDelta = xNewPos - wfc->xCurrentScroll;
// Reset the current scroll position.
wfc->xCurrentScroll = xNewPos;
// Scroll the window. (The system repaints most of the
// client area when ScrollWindowEx is called; however, it is
// necessary to call UpdateWindow in order to repaint the
// rectangle of pixels that were invalidated.)
ScrollWindowEx(wfc->hwnd, -xDelta, -yDelta, (CONST RECT*)NULL, (CONST RECT*)NULL,
(HRGN)NULL, (PRECT)NULL, SW_INVALIDATE);
UpdateWindow(wfc->hwnd);
// Reset the scroll bar.
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = wfc->xCurrentScroll;
SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
}
break;
2016-10-04 10:00:00 +03:00
case WM_VSCROLL:
2019-11-06 17:24:51 +03:00
{
int xDelta = 0;
int yDelta; // yDelta = new_pos - current_pos
int yNewPos; // new position
switch (LOWORD(wParam))
2016-10-04 10:00:00 +03:00
{
2019-11-06 17:24:51 +03:00
// User clicked the scroll bar shaft above the scroll box.
case SB_PAGEUP:
yNewPos = wfc->yCurrentScroll - 50;
break;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// User clicked the scroll bar shaft below the scroll box.
case SB_PAGEDOWN:
yNewPos = wfc->yCurrentScroll + 50;
break;
// User clicked the top arrow.
case SB_LINEUP:
yNewPos = wfc->yCurrentScroll - 5;
break;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// User clicked the bottom arrow.
case SB_LINEDOWN:
yNewPos = wfc->yCurrentScroll + 5;
break;
// User dragged the scroll box.
case SB_THUMBPOSITION:
yNewPos = HIWORD(wParam);
break;
2016-10-04 10:00:00 +03:00
2019-11-06 17:24:51 +03:00
// user is dragging the scrollbar
case SB_THUMBTRACK:
yNewPos = HIWORD(wParam);
2016-10-04 10:00:00 +03:00
break;
2019-11-06 17:24:51 +03:00
default:
yNewPos = wfc->yCurrentScroll;
2016-10-04 10:00:00 +03:00
}
2019-11-06 17:24:51 +03:00
// New position must be between 0 and the screen height.
yNewPos = MAX(0, yNewPos);
yNewPos = MIN(wfc->yMaxScroll, yNewPos);
// If the current position does not change, do not scroll.
if (yNewPos == wfc->yCurrentScroll)
break;
// Determine the amount scrolled (in pixels).
yDelta = yNewPos - wfc->yCurrentScroll;
// Reset the current scroll position.
wfc->yCurrentScroll = yNewPos;
// Scroll the window. (The system repaints most of the
// client area when ScrollWindowEx is called; however, it is
// necessary to call UpdateWindow in order to repaint the
// rectangle of pixels that were invalidated.)
ScrollWindowEx(wfc->hwnd, -xDelta, -yDelta, (CONST RECT*)NULL, (CONST RECT*)NULL,
(HRGN)NULL, (PRECT)NULL, SW_INVALIDATE);
UpdateWindow(wfc->hwnd);
// Reset the scroll bar.
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = wfc->yCurrentScroll;
SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
}
break;
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
case WM_SYSCOMMAND:
2019-11-06 17:24:51 +03:00
{
if (wParam == SYSCOMMAND_ID_SMARTSIZING)
2016-10-04 10:00:00 +03:00
{
2019-11-06 17:24:51 +03:00
HMENU hMenu = GetSystemMenu(wfc->hwnd, FALSE);
2023-10-13 10:48:44 +03:00
const BOOL rc = freerdp_settings_get_bool(settings, FreeRDP_SmartSizing);
2024-09-15 09:43:31 +03:00
(void)freerdp_settings_set_bool(settings, FreeRDP_SmartSizing, !rc);
2019-11-06 17:24:51 +03:00
CheckMenuItem(hMenu, SYSCOMMAND_ID_SMARTSIZING,
2023-10-13 10:48:44 +03:00
freerdp_settings_get_bool(settings, FreeRDP_SmartSizing)
? MF_CHECKED
: MF_UNCHECKED);
if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing))
{
SetWindowPos(wfc->hwnd, HWND_TOP, -1, -1,
2023-10-13 10:48:44 +03:00
freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) +
wfc->diff.x,
freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) +
wfc->diff.y,
SWP_NOMOVE);
}
else
{
wf_size_scrollbars(wfc, wfc->client_width, wfc->client_height);
wf_send_resize(wfc);
}
2016-10-04 10:00:00 +03:00
}
else if (wParam == SYSCOMMAND_ID_REQUEST_CONTROL)
{
freerdp_client_encomsp_set_control(wfc->common.encomsp, TRUE);
}
2019-11-06 17:24:51 +03:00
else
{
processed = FALSE;
}
}
break;
2016-08-03 13:37:40 +03:00
2016-10-04 10:00:00 +03:00
default:
2016-08-03 13:37:40 +03:00
processed = FALSE;
2016-10-04 10:00:00 +03:00
break;
}
}
else
{
processed = FALSE;
}
if (processed)
return 0;
switch (Msg)
{
2016-10-04 10:00:00 +03:00
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
2016-10-04 10:00:00 +03:00
case WM_SETFOCUS:
DEBUG_KBD("getting focus %X", hWnd);
2016-08-03 13:09:24 +03:00
2024-09-15 09:43:31 +03:00
(void)freerdp_settings_set_bool(wfc->common.context.settings, FreeRDP_SuspendInput,
FALSE);
2016-10-04 10:00:00 +03:00
if (alt_ctrl_down())
g_flipping_in = TRUE;
2016-08-03 13:09:24 +03:00
2016-10-04 10:00:00 +03:00
g_focus_hWnd = hWnd;
freerdp_set_focus(wfc->common.context.instance);
2016-10-04 10:00:00 +03:00
break;
2016-10-04 10:00:00 +03:00
case WM_KILLFOCUS:
2024-09-15 09:43:31 +03:00
(void)freerdp_settings_set_bool(wfc->common.context.settings, FreeRDP_SuspendInput,
TRUE);
2016-10-04 10:00:00 +03:00
if (g_focus_hWnd == hWnd && wfc && !wfc->fullscreen)
{
DEBUG_KBD("loosing focus %X", hWnd);
2016-08-03 13:09:24 +03:00
2016-10-04 10:00:00 +03:00
if (alt_ctrl_down())
g_flipping_out = TRUE;
else
g_focus_hWnd = NULL;
}
2016-08-03 13:09:24 +03:00
2016-10-04 10:00:00 +03:00
break;
2016-10-04 10:00:00 +03:00
case WM_ACTIVATE:
2019-11-06 17:24:51 +03:00
{
int activate = (int)(short)LOWORD(wParam);
BOOL minimized_flag = (BOOL)HIWORD(wParam);
2016-08-03 13:09:24 +03:00
if (activate != WA_INACTIVE && !minimized_flag)
2019-11-06 17:24:51 +03:00
{
if (alt_ctrl_down())
g_flipping_in = TRUE;
2016-08-03 13:09:24 +03:00
2019-11-06 17:24:51 +03:00
g_focus_hWnd = hWnd;
}
else
{
if (alt_ctrl_down())
g_flipping_out = TRUE;
2016-10-04 10:00:00 +03:00
else
2019-11-06 17:24:51 +03:00
g_focus_hWnd = NULL;
2016-10-04 10:00:00 +03:00
}
2019-11-06 17:24:51 +03:00
}
2013-05-31 22:20:43 +04:00
2016-10-04 10:00:00 +03:00
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
break;
}
return 0;
}
2019-11-06 17:24:51 +03:00
BOOL wf_scale_blt(wfContext* wfc, HDC hdc, int x, int y, int w, int h, HDC hdcSrc, int x1, int y1,
DWORD rop)
{
2016-08-03 13:09:24 +03:00
UINT32 ww, wh, dw, dh;
WINPR_ASSERT(wfc);
2024-09-12 13:39:18 +03:00
rdpSettings* settings = wfc->common.context.settings;
WINPR_ASSERT(settings);
2013-06-16 06:57:21 +04:00
if (!wfc->client_width)
2023-10-13 10:48:44 +03:00
wfc->client_width = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
2013-06-16 06:57:21 +04:00
if (!wfc->client_height)
2023-10-13 10:48:44 +03:00
wfc->client_height = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2013-06-16 06:57:21 +04:00
ww = wfc->client_width;
wh = wfc->client_height;
2023-10-13 10:48:44 +03:00
dw = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
dh = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
if (!ww)
ww = dw;
if (!wh)
wh = dh;
2023-10-13 10:48:44 +03:00
if (wfc->fullscreen || !freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
(ww == dw && wh == dh))
{
2013-06-16 06:57:21 +04:00
return BitBlt(hdc, x, y, w, h, wfc->primary->hdc, x1, y1, SRCCOPY);
}
else
{
SetStretchBltMode(hdc, HALFTONE);
SetBrushOrgEx(hdc, 0, 0, NULL);
2013-06-16 06:57:21 +04:00
return StretchBlt(hdc, 0, 0, ww, wh, wfc->primary->hdc, 0, 0, dw, dh, SRCCOPY);
}
return TRUE;
}
static BOOL wf_scale_mouse_pos(wfContext* wfc, INT32 x, INT32 y, UINT16* px, UINT16* py)
{
int ww, wh, dw, dh;
2018-03-08 12:58:47 +03:00
rdpSettings* settings;
if (!wfc || !px || !py)
2018-03-08 12:58:47 +03:00
return FALSE;
settings = wfc->common.context.settings;
2018-03-08 12:58:47 +03:00
if (!settings)
return FALSE;
2013-06-16 06:57:21 +04:00
if (!wfc->client_width)
2023-10-13 10:48:44 +03:00
wfc->client_width = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
2013-06-16 06:57:21 +04:00
if (!wfc->client_height)
2023-10-13 10:48:44 +03:00
wfc->client_height = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2013-06-16 06:57:21 +04:00
ww = wfc->client_width;
wh = wfc->client_height;
2023-10-13 10:48:44 +03:00
dw = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
dh = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
2023-10-13 10:48:44 +03:00
if (!freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) || ((ww == dw) && (wh == dh)))
2018-03-08 12:58:47 +03:00
{
x += wfc->xCurrentScroll;
y += wfc->yCurrentScroll;
2018-03-08 12:58:47 +03:00
}
else
2018-03-08 12:58:47 +03:00
{
x = x * dw / ww + wfc->xCurrentScroll;
y = y * dh / wh + wfc->yCurrentScroll;
2018-03-08 12:58:47 +03:00
}
*px = MIN(UINT16_MAX, MAX(0, x));
*py = MIN(UINT16_MAX, MAX(0, y));
return TRUE;
}
static BOOL wf_pub_mouse_event(wfContext* wfc, UINT16 flags, UINT16 x, UINT16 y)
{
MouseEventEventArgs eventArgs = { 0 };
eventArgs.flags = flags;
eventArgs.x = x;
eventArgs.y = y;
PubSub_OnMouseEvent(wfc->common.context.pubSub, &wfc->common.context, &eventArgs);
2018-03-08 12:58:47 +03:00
return TRUE;
}
static BOOL wf_scale_mouse_event(wfContext* wfc, UINT16 flags, INT32 x, INT32 y)
2018-03-08 12:58:47 +03:00
{
UINT16 px, py;
2018-03-08 12:58:47 +03:00
2022-01-19 15:11:34 +03:00
WINPR_ASSERT(wfc);
if (!wf_scale_mouse_pos(wfc, x, y, &px, &py))
2018-03-08 12:58:47 +03:00
return FALSE;
if (freerdp_client_send_button_event(&wfc->common, FALSE, flags, px, py))
2018-03-08 12:58:47 +03:00
return FALSE;
return wf_pub_mouse_event(wfc, flags, px, py);
}
2018-03-08 12:58:47 +03:00
2019-11-06 17:24:51 +03:00
#if (_WIN32_WINNT >= 0x0500)
static BOOL wf_scale_mouse_event_ex(wfContext* wfc, UINT16 flags, UINT16 buttonMask, INT32 x,
INT32 y)
2018-03-08 12:58:47 +03:00
{
UINT16 px, py;
2018-03-08 12:58:47 +03:00
2022-01-19 15:11:34 +03:00
WINPR_ASSERT(wfc);
2018-03-08 12:58:47 +03:00
if (buttonMask & XBUTTON1)
flags |= PTR_XFLAGS_BUTTON1;
2018-03-08 12:58:47 +03:00
if (buttonMask & XBUTTON2)
flags |= PTR_XFLAGS_BUTTON2;
if (!wf_scale_mouse_pos(wfc, x, y, &px, &py))
2018-03-08 12:58:47 +03:00
return FALSE;
if (freerdp_client_send_extended_button_event(&wfc->common, FALSE, flags, px, py))
2018-03-08 12:58:47 +03:00
return FALSE;
return wf_pub_mouse_event(wfc, flags, px, py);
2018-03-08 12:58:47 +03:00
}
#endif