FreeRDP/client/SDL/sdl_utils.cpp

227 lines
5.6 KiB
C++
Raw Normal View History

2022-12-30 13:25:28 +03:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* SDL Client
*
* Copyright 2022 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 <assert.h>
2023-04-14 09:36:05 +03:00
#include "sdl_utils.hpp"
2022-12-30 13:25:28 +03:00
2023-04-14 09:36:05 +03:00
#include "sdl_freerdp.hpp"
2022-12-30 13:25:28 +03:00
#include <SDL.h>
const char* sdl_event_type_str(Uint32 type)
{
#define STR(x) #x
#define EV_CASE_STR(x) \
case x: \
return STR(x)
switch (type)
{
EV_CASE_STR(SDL_FIRSTEVENT);
EV_CASE_STR(SDL_QUIT);
EV_CASE_STR(SDL_APP_TERMINATING);
EV_CASE_STR(SDL_APP_LOWMEMORY);
EV_CASE_STR(SDL_APP_WILLENTERBACKGROUND);
EV_CASE_STR(SDL_APP_DIDENTERBACKGROUND);
EV_CASE_STR(SDL_APP_WILLENTERFOREGROUND);
EV_CASE_STR(SDL_APP_DIDENTERFOREGROUND);
#if SDL_VERSION_ATLEAST(2, 0, 10)
EV_CASE_STR(SDL_DISPLAYEVENT);
#endif
EV_CASE_STR(SDL_WINDOWEVENT);
EV_CASE_STR(SDL_SYSWMEVENT);
EV_CASE_STR(SDL_KEYDOWN);
EV_CASE_STR(SDL_KEYUP);
EV_CASE_STR(SDL_TEXTEDITING);
EV_CASE_STR(SDL_TEXTINPUT);
EV_CASE_STR(SDL_KEYMAPCHANGED);
EV_CASE_STR(SDL_MOUSEMOTION);
EV_CASE_STR(SDL_MOUSEBUTTONDOWN);
EV_CASE_STR(SDL_MOUSEBUTTONUP);
EV_CASE_STR(SDL_MOUSEWHEEL);
EV_CASE_STR(SDL_JOYAXISMOTION);
EV_CASE_STR(SDL_JOYBALLMOTION);
EV_CASE_STR(SDL_JOYHATMOTION);
EV_CASE_STR(SDL_JOYBUTTONDOWN);
EV_CASE_STR(SDL_JOYBUTTONUP);
EV_CASE_STR(SDL_JOYDEVICEADDED);
EV_CASE_STR(SDL_JOYDEVICEREMOVED);
EV_CASE_STR(SDL_CONTROLLERAXISMOTION);
EV_CASE_STR(SDL_CONTROLLERBUTTONDOWN);
EV_CASE_STR(SDL_CONTROLLERBUTTONUP);
EV_CASE_STR(SDL_CONTROLLERDEVICEADDED);
EV_CASE_STR(SDL_CONTROLLERDEVICEREMOVED);
EV_CASE_STR(SDL_CONTROLLERDEVICEREMAPPED);
#if SDL_VERSION_ATLEAST(2, 0, 14)
EV_CASE_STR(SDL_LOCALECHANGED);
EV_CASE_STR(SDL_CONTROLLERTOUCHPADDOWN);
EV_CASE_STR(SDL_CONTROLLERTOUCHPADMOTION);
EV_CASE_STR(SDL_CONTROLLERTOUCHPADUP);
EV_CASE_STR(SDL_CONTROLLERSENSORUPDATE);
#endif
EV_CASE_STR(SDL_FINGERDOWN);
EV_CASE_STR(SDL_FINGERUP);
EV_CASE_STR(SDL_FINGERMOTION);
EV_CASE_STR(SDL_DOLLARGESTURE);
EV_CASE_STR(SDL_DOLLARRECORD);
EV_CASE_STR(SDL_MULTIGESTURE);
EV_CASE_STR(SDL_CLIPBOARDUPDATE);
EV_CASE_STR(SDL_DROPFILE);
EV_CASE_STR(SDL_DROPTEXT);
EV_CASE_STR(SDL_DROPBEGIN);
EV_CASE_STR(SDL_DROPCOMPLETE);
EV_CASE_STR(SDL_AUDIODEVICEADDED);
EV_CASE_STR(SDL_AUDIODEVICEREMOVED);
#if SDL_VERSION_ATLEAST(2, 0, 9)
EV_CASE_STR(SDL_SENSORUPDATE);
#endif
EV_CASE_STR(SDL_RENDER_TARGETS_RESET);
EV_CASE_STR(SDL_RENDER_DEVICE_RESET);
EV_CASE_STR(SDL_USEREVENT);
2023-02-14 13:12:18 +03:00
EV_CASE_STR(SDL_USEREVENT_UPDATE);
EV_CASE_STR(SDL_USEREVENT_CREATE_WINDOWS);
EV_CASE_STR(SDL_USEREVENT_WINDOW_RESIZEABLE);
EV_CASE_STR(SDL_USEREVENT_WINDOW_FULLSCREEN);
EV_CASE_STR(SDL_USEREVENT_POINTER_NULL);
EV_CASE_STR(SDL_USEREVENT_POINTER_DEFAULT);
EV_CASE_STR(SDL_USEREVENT_POINTER_POSITION);
EV_CASE_STR(SDL_USEREVENT_POINTER_SET);
2023-05-30 19:22:57 +03:00
EV_CASE_STR(SDL_USEREVENT_QUIT);
2022-12-30 13:25:28 +03:00
EV_CASE_STR(SDL_LASTEVENT);
default:
return "SDL_UNKNOWNEVENT";
}
#undef EV_CASE_STR
#undef STR
}
2023-01-18 10:50:55 +03:00
const char* sdl_error_string(Uint32 res)
{
2023-03-13 14:53:40 +03:00
if (res == 0)
return nullptr;
2023-01-18 10:50:55 +03:00
return SDL_GetError();
}
BOOL sdl_log_error_ex(Uint32 res, wLog* log, const char* what, const char* file, size_t line,
const char* fkt)
{
const char* msg = sdl_error_string(res);
WINPR_UNUSED(file);
if (!msg)
return FALSE;
WLog_Print(log, WLOG_ERROR, "[%s:%" PRIuz "][%s]: %s", fkt, line, what, msg);
return TRUE;
}
2023-02-14 13:12:18 +03:00
BOOL sdl_push_user_event(Uint32 type, ...)
{
2023-05-23 13:34:47 +03:00
SDL_Event ev = {};
2023-02-14 13:12:18 +03:00
SDL_UserEvent* event = &ev.user;
va_list ap;
va_start(ap, type);
event->type = type;
switch (type)
{
case SDL_USEREVENT_UPDATE:
event->data1 = va_arg(ap, void*);
break;
case SDL_USEREVENT_POINTER_POSITION:
2023-05-23 13:34:47 +03:00
event->data1 = reinterpret_cast<void*>(static_cast<uintptr_t>(va_arg(ap, UINT32)));
event->data2 = reinterpret_cast<void*>(static_cast<uintptr_t>(va_arg(ap, UINT32)));
2023-02-14 13:12:18 +03:00
break;
case SDL_USEREVENT_POINTER_SET:
event->data1 = va_arg(ap, void*);
event->data2 = va_arg(ap, void*);
break;
case SDL_USEREVENT_CREATE_WINDOWS:
2023-05-23 13:34:47 +03:00
event->data1 = reinterpret_cast<void*>(va_arg(ap, void*));
2023-02-14 13:12:18 +03:00
break;
case SDL_USEREVENT_WINDOW_FULLSCREEN:
case SDL_USEREVENT_WINDOW_RESIZEABLE:
event->data1 = va_arg(ap, void*);
2023-04-14 10:16:50 +03:00
event->code = va_arg(ap, int);
2023-02-14 13:12:18 +03:00
break;
2023-05-30 19:22:57 +03:00
case SDL_USEREVENT_QUIT:
2023-02-14 13:12:18 +03:00
case SDL_USEREVENT_POINTER_NULL:
case SDL_USEREVENT_POINTER_DEFAULT:
break;
default:
va_end(ap);
2023-02-14 13:12:18 +03:00
return FALSE;
}
va_end(ap);
return SDL_PushEvent(&ev) == 1;
}
CriticalSection::CriticalSection()
{
InitializeCriticalSection(&_section);
}
CriticalSection::~CriticalSection()
{
DeleteCriticalSection(&_section);
}
void CriticalSection::lock()
{
EnterCriticalSection(&_section);
}
void CriticalSection::unlock()
{
LeaveCriticalSection(&_section);
}
2023-05-30 22:04:53 +03:00
WinPREvent::WinPREvent(bool initial)
: _handle(CreateEventA(nullptr, TRUE, initial ? TRUE : FALSE, nullptr))
{
}
WinPREvent::~WinPREvent()
{
CloseHandle(_handle);
}
void WinPREvent::set()
{
SetEvent(_handle);
}
void WinPREvent::clear()
{
ResetEvent(_handle);
}
bool WinPREvent::isSet() const
{
return WaitForSingleObject(_handle, 0) == WAIT_OBJECT_0;
}
HANDLE WinPREvent::handle() const
{
return _handle;
}