FreeRDP/client/SDL/sdl_pointer.cpp

193 lines
4.9 KiB
C++
Raw Normal View History

2022-12-30 13:25:28 +03:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Wayland Mouse Pointer
*
* Copyright 2023 Armin Novak <armin.novak@thincast.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.
*/
#include <freerdp/config.h>
#include <freerdp/gdi/gdi.h>
2023-04-14 09:36:05 +03:00
#include "sdl_pointer.hpp"
#include "sdl_freerdp.hpp"
#include "sdl_touch.hpp"
#include "sdl_utils.hpp"
2022-12-30 13:25:28 +03:00
#include <SDL_mouse.h>
#define TAG CLIENT_TAG("SDL.pointer")
typedef struct
{
rdpPointer pointer;
SDL_Cursor* cursor;
SDL_Surface* image;
size_t size;
void* data;
} sdlPointer;
static BOOL sdl_Pointer_New(rdpContext* context, rdpPointer* pointer)
{
auto ptr = reinterpret_cast<sdlPointer*>(pointer);
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(context);
if (!ptr)
return FALSE;
rdpGdi* gdi = context->gdi;
WINPR_ASSERT(gdi);
ptr->size = pointer->width * pointer->height * 4ULL;
ptr->data = winpr_aligned_malloc(ptr->size, 16);
if (!ptr->data)
return FALSE;
auto data = static_cast<BYTE*>(ptr->data);
2022-12-30 13:25:28 +03:00
if (!freerdp_image_copy_from_pointer_data(
data, gdi->dstFormat, 0, 0, 0, pointer->width, pointer->height, pointer->xorMaskData,
pointer->lengthXorMask, pointer->andMaskData, pointer->lengthAndMask, pointer->xorBpp,
&context->gdi->palette))
2022-12-30 13:25:28 +03:00
{
winpr_aligned_free(ptr->data);
return FALSE;
}
return TRUE;
}
static void sdl_Pointer_Clear(sdlPointer* ptr)
{
WINPR_ASSERT(ptr);
SDL_FreeCursor(ptr->cursor);
SDL_FreeSurface(ptr->image);
ptr->cursor = nullptr;
ptr->image = nullptr;
2022-12-30 13:25:28 +03:00
}
static void sdl_Pointer_Free(rdpContext* context, rdpPointer* pointer)
{
2023-05-23 13:34:47 +03:00
auto ptr = reinterpret_cast<sdlPointer*>(pointer);
2022-12-30 13:25:28 +03:00
WINPR_UNUSED(context);
if (ptr)
{
sdl_Pointer_Clear(ptr);
winpr_aligned_free(ptr->data);
ptr->data = nullptr;
2022-12-30 13:25:28 +03:00
}
}
static BOOL sdl_Pointer_SetDefault(rdpContext* context)
{
WINPR_UNUSED(context);
2023-02-14 13:12:18 +03:00
return sdl_push_user_event(SDL_USEREVENT_POINTER_DEFAULT);
2022-12-30 13:25:28 +03:00
}
static BOOL sdl_Pointer_Set(rdpContext* context, rdpPointer* pointer)
{
auto sdl = get_context(context);
2023-02-14 13:12:18 +03:00
return sdl_push_user_event(SDL_USEREVENT_POINTER_SET, pointer, sdl);
}
BOOL sdl_Pointer_Set_Process(SDL_UserEvent* uptr)
{
2022-12-30 13:25:28 +03:00
INT32 w, h, x, y, sw, sh;
2023-02-14 13:12:18 +03:00
WINPR_ASSERT(uptr);
auto sdl = static_cast<SdlContext*>(uptr->data2);
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(sdl);
2023-02-14 13:12:18 +03:00
auto context = sdl->context();
auto ptr = static_cast<sdlPointer*>(uptr->data1);
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(ptr);
2023-02-14 13:12:18 +03:00
rdpPointer* pointer = &ptr->pointer;
2022-12-30 13:25:28 +03:00
rdpGdi* gdi = context->gdi;
WINPR_ASSERT(gdi);
2023-05-23 13:34:47 +03:00
x = static_cast<INT32>(pointer->xPos);
y = static_cast<INT32>(pointer->yPos);
sw = w = static_cast<INT32>(pointer->width);
sh = h = static_cast<INT32>(pointer->height);
2022-12-30 13:25:28 +03:00
SDL_Window* window = SDL_GetMouseFocus();
if (!window)
return sdl_Pointer_SetDefault(context);
const Uint32 id = SDL_GetWindowID(window);
if (!sdl_scale_coordinates(sdl, id, &x, &y, FALSE, FALSE) ||
!sdl_scale_coordinates(sdl, id, &sw, &sh, FALSE, FALSE))
return FALSE;
sdl_Pointer_Clear(ptr);
const DWORD bpp = FreeRDPGetBitsPerPixel(gdi->dstFormat);
2023-05-23 13:34:47 +03:00
ptr->image =
SDL_CreateRGBSurfaceWithFormat(0, sw, sh, static_cast<int>(bpp), sdl->sdl_pixel_format);
2022-12-30 13:25:28 +03:00
if (!ptr->image)
return FALSE;
SDL_LockSurface(ptr->image);
auto pixels = static_cast<BYTE*>(ptr->image->pixels);
auto data = static_cast<const BYTE*>(ptr->data);
2023-05-23 13:34:47 +03:00
const BOOL rc = freerdp_image_scale(
pixels, gdi->dstFormat, static_cast<UINT32>(ptr->image->pitch), 0, 0,
static_cast<UINT32>(ptr->image->w), static_cast<UINT32>(ptr->image->h), data,
gdi->dstFormat, 0, 0, 0, static_cast<UINT32>(w), static_cast<UINT32>(h));
2022-12-30 13:25:28 +03:00
SDL_UnlockSurface(ptr->image);
if (!rc)
return FALSE;
ptr->cursor = SDL_CreateColorCursor(ptr->image, x, y);
if (!ptr->cursor)
return FALSE;
SDL_SetCursor(ptr->cursor);
SDL_ShowCursor(SDL_ENABLE);
return TRUE;
}
static BOOL sdl_Pointer_SetNull(rdpContext* context)
{
WINPR_UNUSED(context);
2023-02-14 13:12:18 +03:00
return sdl_push_user_event(SDL_USEREVENT_POINTER_NULL);
2022-12-30 13:25:28 +03:00
}
static BOOL sdl_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
{
auto sdl = get_context(context);
2022-12-30 13:25:28 +03:00
WINPR_ASSERT(sdl);
2023-02-14 13:12:18 +03:00
return sdl_push_user_event(SDL_USEREVENT_POINTER_POSITION, x, y);
2022-12-30 13:25:28 +03:00
}
BOOL sdl_register_pointer(rdpGraphics* graphics)
{
2023-08-25 11:32:53 +03:00
const rdpPointer pointer = { sizeof(sdlPointer), sdl_Pointer_New,
sdl_Pointer_Free, sdl_Pointer_Set,
sdl_Pointer_SetNull, sdl_Pointer_SetDefault,
sdl_Pointer_SetPosition, 0 };
2023-05-23 13:34:47 +03:00
graphics_register_pointer(graphics, &pointer);
2022-12-30 13:25:28 +03:00
return TRUE;
}