Skip to content

Commit

Permalink
Merge pull request #1252 from Ghabry/fix-sdl
Browse files Browse the repository at this point in the history
Fix some SDL annoyances under Linux when using F4 & F5
  • Loading branch information
carstene1ns authored Aug 19, 2017
2 parents 044b697 + b2e3a23 commit 6813ce8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/input_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ namespace Input {
SCROLL_UP,
SCROLL_DOWN,
FAST_FORWARD,
TOGGLE_FULLSCREEN,
TOGGLE_ZOOM,
BUTTON_COUNT
};

Expand Down
2 changes: 2 additions & 0 deletions src/input_buttons_desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void Input::InitButtons() {
buttons[TAKE_SCREENSHOT].push_back(Keys::F10);
buttons[TOGGLE_FPS].push_back(Keys::F2);
buttons[SHOW_LOG].push_back(Keys::F3);
buttons[TOGGLE_FULLSCREEN].push_back(Keys::F4);
buttons[TOGGLE_ZOOM].push_back(Keys::F5);
buttons[PAGE_UP].push_back(Keys::PGUP);
buttons[PAGE_DOWN].push_back(Keys::PGDN);
buttons[RESET].push_back(Keys::F12);
Expand Down
6 changes: 6 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ void Player::Update(bool update_scene) {
if (Input::IsTriggered(Input::RESET)) {
reset_flag = true;
}
if (Input::IsTriggered(Input::TOGGLE_ZOOM)) {
DisplayUi->ToggleZoom();
}
if (Input::IsTriggered(Input::TOGGLE_FULLSCREEN)) {
DisplayUi->ToggleFullscreen();
}

DisplayUi->ProcessEvents();

Expand Down
61 changes: 23 additions & 38 deletions src/sdl_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,31 @@ void SdlUi::Resize(long /*width*/, long /*height*/) {
#endif

void SdlUi::ToggleFullscreen() {
BeginDisplayModeChange();
if (toggle_fs_available && mode_changing) {
if ((current_display_mode.flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
current_display_mode.flags &= ~SDL_WINDOW_FULLSCREEN_DESKTOP;
else
current_display_mode.flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
EndDisplayModeChange();
}

void SdlUi::ToggleZoom() {
BeginDisplayModeChange();
#if SDL_MAJOR_VERSION > 1
// Work around a SDL bug which doesn't demaximize the window when the size
// is changed
int flags = SDL_GetWindowFlags(sdl_window);
if ((flags & SDL_WINDOW_MAXIMIZED) == SDL_WINDOW_MAXIMIZED) {
SDL_RestoreWindow(sdl_window);
}
#endif

if (zoom_available && mode_changing) {
current_display_mode.zoom = !current_display_mode.zoom;
}
EndDisplayModeChange();
}

void SdlUi::ProcessEvents() {
Expand Down Expand Up @@ -753,53 +766,25 @@ void SdlUi::ProcessActiveEvent(SDL_Event &evnt) {

void SdlUi::ProcessKeyDownEvent(SDL_Event &evnt) {
#if defined(USE_KEYBOARD) && defined(SUPPORT_KEYBOARD)
switch (evnt.key.keysym.sym) {
case SDLK_F4:
if (evnt.key.keysym.sym == SDLK_F4 && (evnt.key.keysym.mod & KMOD_LALT)) {
// Close program on LeftAlt+F4
if (evnt.key.keysym.mod & KMOD_LALT) {
Player::exit_flag = true;
return;
}

// Toggle fullscreen on F4 and no alt is pressed
if (!(evnt.key.keysym.mod & KMOD_RALT) && !(evnt.key.keysym.mod & KMOD_LALT)) {
BeginDisplayModeChange();
ToggleFullscreen();
EndDisplayModeChange();
}
return;

case SDLK_F5:
// Toggle zoom on F5
BeginDisplayModeChange();
ToggleZoom();
EndDisplayModeChange();
Player::exit_flag = true;
return;

case SDLK_RETURN:
case SDLK_KP_ENTER:
// Toggle fullscreen on Alt+Enter
} else if (evnt.key.keysym.sym == SDLK_RETURN ||
evnt.key.keysym.sym == SDLK_KP_ENTER) {
if (evnt.key.keysym.mod & KMOD_LALT || (evnt.key.keysym.mod & KMOD_RALT)) {
BeginDisplayModeChange();
ToggleFullscreen();
EndDisplayModeChange();
// Toggle fullscreen on Alt+Enter
ToggleFullscreen();
return;
}
// Continue if return/enter not handled by fullscreen hotkey
# if __GNUC__ >= 7
__attribute__((fallthrough));
# endif
}

default:
// Update key state
// Update key state
# if SDL_MAJOR_VERSION==1
keys[SdlKey2InputKey(evnt.key.keysym.sym)] = true;
keys[SdlKey2InputKey(evnt.key.keysym.sym)] = true;
# else
keys[SdlKey2InputKey(evnt.key.keysym.scancode)] = true;

keys[SdlKey2InputKey(evnt.key.keysym.scancode)] = true;
# endif
return;
}
#else
/* unused */
(void) evnt;
Expand Down

0 comments on commit 6813ce8

Please sign in to comment.