Always request fullscreen and maximized state

The window state may be changed by the window manager without scrcpy
being aware of it (for example the fullscreen mode on macOS).

To avoid problems, request the window state to SDL when needed.
This commit is contained in:
Romain Vimont 2020-05-07 18:20:27 +02:00
parent e2d5f0e7fc
commit 7ce63499d9
2 changed files with 22 additions and 18 deletions

View File

@ -15,6 +15,18 @@
#define DISPLAY_MARGINS 96 #define DISPLAY_MARGINS 96
static bool
is_maximized(const struct screen *screen) {
uint32_t flags = SDL_GetWindowFlags(screen->window);
return !!(flags & SDL_WINDOW_MAXIMIZED);
}
static bool
is_fullscreen(const struct screen *screen) {
uint32_t flags = SDL_GetWindowFlags(screen->window);
return !!(flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP));
}
static inline struct size static inline struct size
get_rotated_size(struct size size, int rotation) { get_rotated_size(struct size size, int rotation) {
struct size rotated_size; struct size rotated_size;
@ -44,7 +56,7 @@ get_window_size(SDL_Window *window) {
// get the windowed window size // get the windowed window size
static struct size static struct size
get_windowed_window_size(const struct screen *screen) { get_windowed_window_size(const struct screen *screen) {
if (screen->fullscreen || screen->maximized) { if (is_fullscreen(screen) || is_maximized(screen)) {
return screen->windowed_window_size; return screen->windowed_window_size;
} }
return get_window_size(screen->window); return get_window_size(screen->window);
@ -53,7 +65,7 @@ get_windowed_window_size(const struct screen *screen) {
// apply the windowed window size if fullscreen and maximized are disabled // apply the windowed window size if fullscreen and maximized are disabled
static void static void
apply_windowed_size(struct screen *screen) { apply_windowed_size(struct screen *screen) {
if (!screen->fullscreen && !screen->maximized) { if (!is_fullscreen(screen) && !is_maximized(screen)) {
SDL_SetWindowSize(screen->window, screen->windowed_window_size.width, SDL_SetWindowSize(screen->window, screen->windowed_window_size.width,
screen->windowed_window_size.height); screen->windowed_window_size.height);
} }
@ -471,28 +483,27 @@ screen_render(struct screen *screen) {
void void
screen_switch_fullscreen(struct screen *screen) { screen_switch_fullscreen(struct screen *screen) {
uint32_t new_mode = screen->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP; bool was_fullscreen = is_fullscreen(screen);
uint32_t new_mode = was_fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP;
if (SDL_SetWindowFullscreen(screen->window, new_mode)) { if (SDL_SetWindowFullscreen(screen->window, new_mode)) {
LOGW("Could not switch fullscreen mode: %s", SDL_GetError()); LOGW("Could not switch fullscreen mode: %s", SDL_GetError());
return; return;
} }
screen->fullscreen = !screen->fullscreen;
apply_windowed_size(screen); apply_windowed_size(screen);
LOGD("Switched to %s mode", screen->fullscreen ? "fullscreen" : "windowed"); LOGD("Switched to %s mode", was_fullscreen ? "windowed" : "fullscreen");
screen_render(screen); screen_render(screen);
} }
void void
screen_resize_to_fit(struct screen *screen) { screen_resize_to_fit(struct screen *screen) {
if (screen->fullscreen) { if (is_fullscreen(screen)) {
return; return;
} }
if (screen->maximized) { if (is_maximized(screen)) {
SDL_RestoreWindow(screen->window); SDL_RestoreWindow(screen->window);
screen->maximized = false;
} }
struct size optimal_size = struct size optimal_size =
@ -504,13 +515,12 @@ screen_resize_to_fit(struct screen *screen) {
void void
screen_resize_to_pixel_perfect(struct screen *screen) { screen_resize_to_pixel_perfect(struct screen *screen) {
if (screen->fullscreen) { if (is_fullscreen(screen)) {
return; return;
} }
if (screen->maximized) { if (is_maximized(screen)) {
SDL_RestoreWindow(screen->window); SDL_RestoreWindow(screen->window);
screen->maximized = false;
} }
struct size content_size = screen->content_size; struct size content_size = screen->content_size;
@ -527,7 +537,7 @@ screen_handle_window_event(struct screen *screen,
screen_render(screen); screen_render(screen);
break; break;
case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_SIZE_CHANGED:
if (!screen->fullscreen && !screen->maximized) { if (!is_fullscreen(screen) && !is_maximized(screen)) {
// Backup the previous size: if we receive the MAXIMIZED event, // Backup the previous size: if we receive the MAXIMIZED event,
// then the new size must be ignored (it's the maximized size). // then the new size must be ignored (it's the maximized size).
// We could not rely on the window flags due to race conditions // We could not rely on the window flags due to race conditions
@ -552,10 +562,8 @@ screen_handle_window_event(struct screen *screen,
screen->windowed_window_size_backup.width = 0; screen->windowed_window_size_backup.width = 0;
screen->windowed_window_size_backup.height = 0; screen->windowed_window_size_backup.height = 0;
#endif #endif
screen->maximized = true;
break; break;
case SDL_WINDOWEVENT_RESTORED: case SDL_WINDOWEVENT_RESTORED:
screen->maximized = false;
apply_windowed_size(screen); apply_windowed_size(screen);
break; break;
} }

View File

@ -29,8 +29,6 @@ struct screen {
// client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise) // client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise)
unsigned rotation; unsigned rotation;
bool has_frame; bool has_frame;
bool fullscreen;
bool maximized;
bool no_window; bool no_window;
bool mipmaps; bool mipmaps;
}; };
@ -59,8 +57,6 @@ struct screen {
}, \ }, \
.rotation = 0, \ .rotation = 0, \
.has_frame = false, \ .has_frame = false, \
.fullscreen = false, \
.maximized = false, \
.no_window = false, \ .no_window = false, \
.mipmaps = false, \ .mipmaps = false, \
} }