13#ifdef WITH_OPENGL_BACKEND
16#ifdef WITH_VULKAN_BACKEND
31 if (!SDL_Init(SDL_INIT_VIDEO)) {
32 throw std::runtime_error(SDL_GetError());
35 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
36 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
37 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
38 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
39 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
47GHOST_IWindow *GHOST_SystemSDL::createWindow(
const char *title,
78 SDL_SetWindowFullscreenMode(sdl_win,
nullptr);
79 SDL_ShowWindow(sdl_win);
80 SDL_SetWindowFullscreen(sdl_win,
true);
113 SDL_DisplayID display_id = SDL_GetPrimaryDisplay();
114 const SDL_DisplayMode *mode = SDL_GetDesktopDisplayMode(display_id);
115 if (mode ==
nullptr) {
124 SDL_DisplayID display_id = SDL_GetPrimaryDisplay();
125 const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display_id);
126 if (mode ==
nullptr) {
136 SDL_DisplayID *displays = SDL_GetDisplays(&count);
138 return uint8_t(count);
147#ifdef WITH_OPENGL_BACKEND
148 case GHOST_kDrawingContextTypeOpenGL: {
149 for (
int minor = 6; minor >= 3; --minor) {
151 context_params_offscreen,
159 if (context->initializeDrawingContext()) {
168#ifdef WITH_VULKAN_BACKEND
169 case GHOST_kDrawingContextTypeVulkan: {
172 if (context->initializeDrawingContext()) {
195 SDL_Keymod
mod = SDL_GetModState();
209#define GXMAP(k, x, y) \
218 if ((key >= SDL_SCANCODE_A) && (key <= SDL_SCANCODE_Z)) {
221 else if ((key >= SDL_SCANCODE_1) && (key <= SDL_SCANCODE_0)) {
225 else if ((key >= SDL_SCANCODE_F1) && (key <= SDL_SCANCODE_F12)) {
228 else if ((key >= SDL_SCANCODE_F13) && (key <= SDL_SCANCODE_F24)) {
328 SDL_Keycode sym = sdl_sub_evt.key;
334 case SDLK_KP_MULTIPLY:
382 if (sdl_sub_evt.mod & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT)) {
384 if (sym >=
'a' && sym <= (
'a' + 32)) {
467 SDL_Window *sdl_win = SDL_GetWindowFromID(
id);
468 if (sdl_win ==
nullptr) {
469 sdl_win = SDL_GL_GetCurrentWindow();
474void GHOST_SystemSDL::processEvent(SDL_Event *sdl_event)
476 std::unique_ptr<GHOST_Event> g_event =
nullptr;
478 switch (sdl_event->type) {
479 case SDL_EVENT_WINDOW_EXPOSED:
480 case SDL_EVENT_WINDOW_RESIZED:
481 case SDL_EVENT_WINDOW_MOVED:
482 case SDL_EVENT_WINDOW_FOCUS_GAINED:
483 case SDL_EVENT_WINDOW_FOCUS_LOST:
484 case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
485 const SDL_WindowEvent &sdl_sub_evt = sdl_event->window;
486 const uint64_t event_ms = SDL_NS_TO_MS(sdl_sub_evt.timestamp);
487 GHOST_WindowSDL *window = findGhostWindow(
491 switch (sdl_event->type) {
492 case SDL_EVENT_WINDOW_EXPOSED:
495 case SDL_EVENT_WINDOW_RESIZED:
498 case SDL_EVENT_WINDOW_MOVED:
501 case SDL_EVENT_WINDOW_FOCUS_GAINED:
504 case SDL_EVENT_WINDOW_FOCUS_LOST:
507 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
515 case SDL_EVENT_QUIT: {
516 const SDL_QuitEvent &sdl_sub_evt = sdl_event->quit;
517 const uint64_t event_ms = SDL_NS_TO_MS(sdl_sub_evt.timestamp);
523 case SDL_EVENT_MOUSE_MOTION: {
524 const SDL_MouseMotionEvent &sdl_sub_evt = sdl_event->motion;
525 const uint64_t event_ms = SDL_NS_TO_MS(sdl_sub_evt.timestamp);
527 GHOST_WindowSDL *window = findGhostWindow(sdl_win);
528 assert(window !=
nullptr);
531 SDL_GetWindowPosition(sdl_win, &x_win, &y_win);
556 if (x_new != x_root || y_new != y_root) {
560 SDL_WarpMouseInWindow(sdl_win, x_new - x_win, y_new - y_win);
566 SDL_WarpMouseInWindow(sdl_win, x_new - x_win, y_new - y_win);
569 g_event = std::make_unique<GHOST_EventCursor>(
573 g_event = std::make_unique<GHOST_EventCursor>(event_ms,
578 GHOST_TABLET_DATA_NONE);
584 g_event = std::make_unique<GHOST_EventCursor>(
589 case SDL_EVENT_MOUSE_BUTTON_UP:
590 case SDL_EVENT_MOUSE_BUTTON_DOWN: {
591 const SDL_MouseButtonEvent &sdl_sub_evt = sdl_event->button;
592 const uint64_t event_ms = SDL_NS_TO_MS(sdl_sub_evt.timestamp);
596 GHOST_WindowSDL *window = findGhostWindow(
598 assert(window !=
nullptr);
601 if (sdl_sub_evt.button == SDL_BUTTON_LEFT) {
604 else if (sdl_sub_evt.button == SDL_BUTTON_MIDDLE) {
607 else if (sdl_sub_evt.button == SDL_BUTTON_RIGHT) {
611 else if (sdl_sub_evt.button == SDL_BUTTON_X1) {
614 else if (sdl_sub_evt.button == SDL_BUTTON_X2) {
621 g_event = std::make_unique<GHOST_EventButton>(
622 event_ms, type, window, gbmask, GHOST_TABLET_DATA_NONE);
625 case SDL_EVENT_MOUSE_WHEEL: {
626 const SDL_MouseWheelEvent &sdl_sub_evt = sdl_event->wheel;
627 const uint64_t event_ms = SDL_NS_TO_MS(sdl_sub_evt.timestamp);
628 GHOST_WindowSDL *window = findGhostWindow(
630 assert(window !=
nullptr);
631 if (sdl_sub_evt.x != 0.0f) {
632 g_event = std::make_unique<GHOST_EventWheel>(
635 else if (sdl_sub_evt.y != 0.0f) {
636 g_event = std::make_unique<GHOST_EventWheel>(
641 case SDL_EVENT_KEY_DOWN:
642 case SDL_EVENT_KEY_UP: {
643 const SDL_KeyboardEvent &sdl_sub_evt = sdl_event->key;
644 const uint64_t event_ms = SDL_NS_TO_MS(sdl_sub_evt.timestamp);
646 const bool is_repeat = sdl_sub_evt.repeat != 0;
648 GHOST_WindowSDL *window = findGhostWindow(
650 assert(window !=
nullptr);
663 g_event = std::make_unique<GHOST_EventKey>(
664 event_ms, type, window, gkey, is_repeat, utf8_buf);
670 switch (g_event->getType()) {
690 SDL_Window *win = SDL_GetMouseFocus();
691 if (win ==
nullptr) {
694 SDL_GetWindowPosition(win, &x_win, &y_win);
697 SDL_GetMouseState(&xf, &yf);
707 SDL_Window *win = SDL_GetMouseFocus();
708 if (win ==
nullptr) {
711 SDL_GetWindowPosition(win, &x_win, &y_win);
713 SDL_WarpMouseInWindow(win,
x - x_win,
y - y_win);
717bool GHOST_SystemSDL::generateWindowExposeEvents()
719 std::vector<GHOST_WindowSDL *>::iterator w_start = dirty_windows_.begin();
720 std::vector<GHOST_WindowSDL *>::const_iterator w_end = dirty_windows_.end();
721 bool anyProcessed =
false;
723 for (; w_start != w_end; ++w_start) {
728 (*w_start)->validate();
737 dirty_windows_.clear();
746 bool anyProcessed =
false;
751 if (waitForEvent && dirty_windows_.empty() && !SDL_HasEvents(SDL_EVENT_FIRST, SDL_EVENT_LAST))
756 SDL_WaitEventTimeout(
nullptr, -1);
774 while (SDL_PollEvent(&sdl_event)) {
775 processEvent(&sdl_event);
779 if (generateWindowExposeEvents()) {
782 }
while (waitForEvent && !anyProcessed);
789 if (sdl_win ==
nullptr) {
799 std::vector<GHOST_IWindow *>::const_iterator win_it = win_vec.begin();
800 std::vector<GHOST_IWindow *>::const_iterator win_end = win_vec.end();
802 for (; win_it != win_end; ++win_it) {
803 GHOST_WindowSDL *window =
static_cast<GHOST_WindowSDL *
>(*win_it);
813 GHOST_ASSERT((bad_wind !=
nullptr),
"addDirtyWindow() nullptr ptr trapped (window)");
815 dirty_windows_.push_back(bad_wind);
820 SDL_MouseButtonFlags
state = SDL_GetMouseState(
nullptr,
nullptr);
857 char *sdl_text = selection ? SDL_GetPrimarySelectionText() : SDL_GetClipboardText();
858 if (sdl_text ==
nullptr) {
861 char *result = strdup(sdl_text);
869 SDL_SetPrimarySelectionText(buffer);
872 SDL_SetClipboardText(buffer);
878 return SDL_GetTicks();
#define GHOST_OPENGL_SDL_RESET_NOTIFICATION_STRATEGY
#define GHOST_OPENGL_SDL_CONTEXT_FLAGS
#define GHOST_ASSERT(x, info)
static char convert_keyboard_event_to_ascii(const SDL_KeyboardEvent &sdl_sub_evt)
static GHOST_TKey convertSDLKey(SDL_Scancode key)
static SDL_Window * SDL_GetWindowFromID_fallback(SDL_WindowID id)
@ GHOST_kEventWheelAxisVertical
@ GHOST_kEventWheelAxisHorizontal
@ GHOST_kWindowStateFullScreen
@ GHOST_kEventWindowClose
@ GHOST_kEventWindowActivate
@ GHOST_kEventWindowUpdate
@ GHOST_kEventWindowDeactivate
@ GHOST_kEventQuitRequest
@ GHOST_kCapabilityKeyboardHyperKey
@ GHOST_kCapabilityCursorRGBA
@ GHOST_kCapabilityInputIME
@ GHOST_kCapabilityCursorGenerator
@ GHOST_kCapabilityWindowPath
@ GHOST_kCapabilityMultiMonitorPlacement
@ GHOST_kCapabilityClipboardImage
@ GHOST_kCapabilityWindowDecorationStyles
@ GHOST_kCapabilityDesktopSample
#define GHOST_CONTEXT_PARAMS_FROM_GPU_SETTINGS(gpu_settings)
#define GHOST_CAPABILITY_FLAG_ALL
@ GHOST_kKeyNumpadAsterisk
@ GHOST_kModifierKeyRightControl
@ GHOST_kModifierKeyLeftControl
@ GHOST_kModifierKeyRightAlt
@ GHOST_kModifierKeyRightShift
@ GHOST_kModifierKeyLeftAlt
@ GHOST_kModifierKeyLeftShift
@ GHOST_kModifierKeyLeftOS
@ GHOST_kModifierKeyRightOS
#define GHOST_CONTEXT_PARAMS_FROM_GPU_SETTINGS_OFFSCREEN(gpu_settings)
@ GHOST_kButtonMaskButton4
@ GHOST_kButtonMaskButton5
@ GHOST_kButtonMaskMiddle
unsigned long long int uint64_t
GHOST_ContextSDL(const GHOST_ContextParams &context_params, SDL_Window *window, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
GHOST_ContextVK(const GHOST_ContextParams &context_params, GHOST_TVulkanPlatformType platform, Window window, Display *display, wl_surface *wayland_surface, wl_display *wayland_display, const GHOST_ContextVK_WindowInfo *wayland_window_info, int contextMajorVersion, int contextMinorVersion, const GHOST_GPUDevice &preferred_device, const GHOST_WindowHDRInfo *hdr_info_=nullptr)
virtual GHOST_TSuccess init()=0
virtual void wrapPoint(int32_t &x, int32_t &y, int32_t ofs, GHOST_TAxisFlag axis)
GHOST_TSuccess getCursorPosition(int32_t &x, int32_t &y) const override
GHOST_TSuccess setCursorPosition(int32_t x, int32_t y) override
char * getClipboard(bool selection) const override
void getMainDisplayDimensions(uint32_t &width, uint32_t &height) const override
GHOST_IContext * createOffscreenContext(GHOST_GPUSettings gpu_settings) override
GHOST_TCapabilityFlag getCapabilities() const override
void addDirtyWindow(GHOST_WindowSDL *bad_wind)
void getAllDisplayDimensions(uint32_t &width, uint32_t &height) const override
uint8_t getNumDisplays() const override
GHOST_TSuccess disposeContext(GHOST_IContext *context) override
void putClipboard(const char *buffer, bool selection) const override
GHOST_TSuccess getButtons(GHOST_Buttons &buttons) const override
GHOST_TSuccess getModifierKeys(GHOST_ModifierKeys &keys) const override
uint64_t getMilliSeconds() const override
bool processEvents(bool waitForEvent) override
GHOST_WindowManager * window_manager_
GHOST_TSuccess init() override
GHOST_TimerManager * getTimerManager() const
GHOST_TSuccess pushEvent(std::unique_ptr< const GHOST_IEvent > event)
bool fireTimers(uint64_t time)
GHOST_TSuccess addWindow(GHOST_IWindow *window)
const std::vector< GHOST_IWindow * > & getWindows() const
GHOST_IWindow * getActiveWindow() const
GHOST_TSuccess setActiveWindow(GHOST_IWindow *window)
void setWindowInactive(const GHOST_IWindow *window)
GHOST_WindowSDL(GHOST_SystemSDL *system, const char *title, int32_t left, int32_t top, uint32_t width, uint32_t height, GHOST_TWindowState state, GHOST_TDrawingContextType type, const GHOST_ContextParams &context_params, const GHOST_GPUDevice &preferred_device, const bool exclusive=false, const GHOST_IWindow *parent_window=nullptr)
SDL_Window * getSDLWindow()
void getClientBounds(GHOST_Rect &bounds) const override
bool getValid() const override
virtual GHOST_TSuccess getCursorGrabBounds(GHOST_Rect &bounds) const
void setCursorGrabAccum(int32_t x, int32_t y)
GHOST_TAxisFlag getCursorGrabAxis() const
GHOST_TGrabCursorMode getCursorGrabMode() const
void getCursorGrabAccum(int32_t &x, int32_t &y) const
#define assert(assertion)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
GHOST_TDrawingContextType context_type
GHOST_GPUDevice preferred_device
void set(GHOST_TModifierKey mask, bool down)