22 #include "sdl_connection_dialog.hpp"
23 #include "../sdl_utils.hpp"
24 #include "../sdl_freerdp.hpp"
25 #include "res/sdl2_resource_manager.hpp"
27 static const SDL_Color backgroundcolor = { 0x38, 0x36, 0x35, 0xff };
28 static const SDL_Color textcolor = { 0xd1, 0xcf, 0xcd, 0xff };
29 static const SDL_Color infocolor = { 0x43, 0xe0, 0x0f, 0x60 };
30 static const SDL_Color warncolor = { 0xcd, 0xca, 0x35, 0x60 };
31 static const SDL_Color errorcolor = { 0xf7, 0x22, 0x30, 0x60 };
33 static const Uint32 vpadding = 5;
34 static const Uint32 hpadding = 5;
36 SDLConnectionDialog::SDLConnectionDialog(rdpContext* context) : _context(context)
38 SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
42 SDLConnectionDialog::~SDLConnectionDialog()
49 bool SDLConnectionDialog::visible()
const
51 if (!_window || !_renderer)
54 auto flags = SDL_GetWindowFlags(_window);
55 return (flags & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) == 0;
58 bool SDLConnectionDialog::setTitle(
const char* fmt, ...)
60 std::lock_guard lock(_mux);
63 _title = print(fmt, ap);
66 return show(MSG_NONE);
69 bool SDLConnectionDialog::showInfo(
const char* fmt, ...)
73 auto rc = show(MSG_INFO, fmt, ap);
78 bool SDLConnectionDialog::showWarn(
const char* fmt, ...)
82 auto rc = show(MSG_WARN, fmt, ap);
87 bool SDLConnectionDialog::showError(
const char* fmt, ...)
91 auto rc = show(MSG_ERROR, fmt, ap);
98 bool SDLConnectionDialog::show()
100 std::lock_guard lock(_mux);
101 return show(_type_active);
104 bool SDLConnectionDialog::hide()
106 std::lock_guard lock(_mux);
107 return show(MSG_DISCARD);
110 bool SDLConnectionDialog::running()
const
112 std::lock_guard lock(_mux);
116 bool SDLConnectionDialog::update()
118 std::lock_guard lock(_mux);
124 _type_active = _type;
134 SDL_SetWindowTitle(_window, _title.c_str());
142 bool SDLConnectionDialog::setModal()
146 auto sdl = get_context(_context);
147 if (sdl->windows.empty())
150 auto parent = sdl->windows.begin()->second.window();
151 SDL_SetWindowModalFor(_window, parent);
152 SDL_RaiseWindow(_window);
157 bool SDLConnectionDialog::clearWindow(SDL_Renderer* renderer)
161 const int drc = SDL_SetRenderDrawColor(renderer, backgroundcolor.r, backgroundcolor.g,
162 backgroundcolor.b, backgroundcolor.a);
163 if (widget_log_error(drc,
"SDL_SetRenderDrawColor"))
166 const int rcls = SDL_RenderClear(renderer);
167 return !widget_log_error(rcls,
"SDL_RenderClear");
170 bool SDLConnectionDialog::update(SDL_Renderer* renderer)
172 std::lock_guard lock(_mux);
176 if (!clearWindow(renderer))
179 for (
auto& btn : _list)
181 if (!btn.widget.update_text(renderer, _msg, btn.fgcolor, btn.bgcolor))
185 if (!_buttons.update(renderer))
188 SDL_RenderPresent(renderer);
192 bool SDLConnectionDialog::wait(
bool ignoreRdpContext)
196 if (!ignoreRdpContext)
198 if (freerdp_shall_disconnect_context(_context))
201 std::this_thread::yield();
206 bool SDLConnectionDialog::handle(
const SDL_Event& event)
211 windowID = SDL_GetWindowID(_window);
216 case SDL_USEREVENT_RETRY_DIALOG:
226 auto& ev =
reinterpret_cast<const SDL_KeyboardEvent&
>(event);
228 switch (event.key.keysym.sym)
234 if (event.type == SDL_KEYUP)
236 freerdp_abort_event(_context);
241 _buttons.set_highlight_next();
247 return windowID == ev.windowID;
250 case SDL_MOUSEMOTION:
253 auto& ev =
reinterpret_cast<const SDL_MouseMotionEvent&
>(event);
255 _buttons.set_mouseover(event.button.x, event.button.y);
257 return windowID == ev.windowID;
260 case SDL_MOUSEBUTTONDOWN:
261 case SDL_MOUSEBUTTONUP:
264 auto& ev =
reinterpret_cast<const SDL_MouseButtonEvent&
>(event);
267 auto button = _buttons.get_selected(event.button);
270 if (event.type == SDL_MOUSEBUTTONUP)
272 freerdp_abort_event(_context);
277 return windowID == ev.windowID;
283 auto& ev =
reinterpret_cast<const SDL_MouseWheelEvent&
>(event);
285 return windowID == ev.windowID;
292 auto& ev =
reinterpret_cast<const SDL_TouchFingerEvent&
>(event);
294 #if SDL_VERSION_ATLEAST(2, 0, 18)
295 return windowID == ev.windowID;
301 case SDL_WINDOWEVENT:
303 auto& ev =
reinterpret_cast<const SDL_WindowEvent&
>(event);
306 case SDL_WINDOWEVENT_CLOSE:
307 if (windowID == ev.windowID)
309 freerdp_abort_event(_context);
319 return windowID == ev.windowID;
326 bool SDLConnectionDialog::createWindow()
330 const int widget_height = 50;
331 const int widget_width = 600;
332 const int total_height = 300;
334 auto flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_INPUT_FOCUS;
335 auto rc = SDL_CreateWindowAndRenderer(widget_width, total_height, flags, &_window, &_renderer);
338 widget_log_error(rc,
"SDL_CreateWindowAndRenderer");
341 SDL_SetWindowTitle(_window, _title.c_str());
344 SDL_Color res_bgcolor;
345 switch (_type_active)
348 res_bgcolor = infocolor;
351 res_bgcolor = warncolor;
354 res_bgcolor = errorcolor;
358 res_bgcolor = backgroundcolor;
362 #if defined(WITH_SDL_IMAGE_DIALOGS)
363 std::string res_name;
364 switch (_type_active)
367 res_name =
"icon_info.svg";
370 res_name =
"icon_warning.svg";
373 res_name =
"icon_error.svg";
381 int height = (total_height - 3ul * vpadding) / 2ul;
382 SDL_Rect iconRect{ hpadding, vpadding, widget_width / 4ul - 2ul * hpadding, height };
383 widget_cfg_t icon{ textcolor,
385 { _renderer, iconRect,
387 _list.emplace_back(std::move(icon));
389 iconRect.y += height;
391 widget_cfg_t logo{ textcolor,
393 { _renderer, iconRect,
395 "FreeRDP_Icon.svg") } };
396 _list.emplace_back(std::move(logo));
398 SDL_Rect rect = { widget_width / 4ul, vpadding, widget_width * 3ul / 4ul,
399 total_height - 3ul * vpadding - widget_height };
401 SDL_Rect rect = { hpadding, vpadding, widget_width - 2ul * hpadding,
402 total_height - 2ul * vpadding };
405 widget_cfg_t w{ textcolor, backgroundcolor, { _renderer, rect,
false } };
406 w.widget.set_wrap(
true, widget_width);
407 _list.emplace_back(std::move(w));
408 rect.y += widget_height + vpadding;
410 const std::vector<int> buttonids = { 1 };
411 const std::vector<std::string> buttonlabels = {
"cancel" };
412 _buttons.populate(_renderer, buttonlabels, buttonids, widget_width,
413 total_height - widget_height - vpadding,
414 static_cast<Sint32
>(widget_width / 2),
static_cast<Sint32
>(widget_height));
415 _buttons.set_highlight(0);
417 SDL_ShowWindow(_window);
418 SDL_RaiseWindow(_window);
423 void SDLConnectionDialog::destroyWindow()
427 SDL_DestroyRenderer(_renderer);
428 SDL_DestroyWindow(_window);
433 bool SDLConnectionDialog::show(MsgType type,
const char* fmt, va_list ap)
435 std::lock_guard lock(_mux);
436 _msg = print(fmt, ap);
440 bool SDLConnectionDialog::show(MsgType type)
443 return sdl_push_user_event(SDL_USEREVENT_RETRY_DIALOG);
446 std::string SDLConnectionDialog::print(
const char* fmt, va_list ap)
459 WINPR_PRAGMA_DIAG_PUSH
460 WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
461 size = vsnprintf(res.data(), res.size(), fmt, copy);
462 WINPR_PRAGMA_DIAG_POP
465 }
while ((size > 0) && (
static_cast<size_t>(size) > res.size()));
470 bool SDLConnectionDialog::setTimer(Uint32 timeoutMS)
472 std::lock_guard lock(_mux);
475 _timer = SDL_AddTimer(timeoutMS, &SDLConnectionDialog::timeout,
this);
480 void SDLConnectionDialog::resetTimer()
483 SDL_RemoveTimer(_timer);
487 Uint32 SDLConnectionDialog::timeout(Uint32 intervalMS,
void* pvthis)
491 self->_running =
false;
495 SDLConnectionDialogHider::SDLConnectionDialogHider(freerdp* instance)
500 SDLConnectionDialogHider::SDLConnectionDialogHider(rdpContext* context)
505 SDLConnectionDialogHider::SDLConnectionDialogHider(
SDLConnectionDialog* dialog) : _dialog(dialog)
509 _visible = _dialog->visible();
517 SDLConnectionDialogHider::~SDLConnectionDialogHider()
519 if (_dialog && _visible)
529 return get(instance->context);
534 auto sdl = get_context(context);
537 return sdl->connection_dialog.get();
static SDL_RWops * get(const std::string &type, const std::string &id)