Compare commits

..

1 Commits

Author SHA1 Message Date
7ce63499d9 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.
2020-05-07 18:20:27 +02:00
4 changed files with 107 additions and 121 deletions

View File

@ -210,6 +210,7 @@ To disable mirroring while recording:
scrcpy --no-display --record file.mp4
scrcpy -Nr file.mkv
# interrupt recording with Ctrl+C
# Ctrl+C does not terminate properly on Windows, so disconnect the device
```
"Skipped frames" are recorded, even if they are not displayed in real time (for

View File

@ -7,10 +7,6 @@
#include <sys/time.h>
#include <SDL2/SDL.h>
#ifdef _WIN32
# include <windows.h>
#endif
#include "config.h"
#include "command.h"
#include "common.h"
@ -49,18 +45,6 @@ static struct input_manager input_manager = {
.prefer_text = false, // initialized later
};
#ifdef _WIN32
BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) {
if (ctrl_type == CTRL_C_EVENT) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
return TRUE;
}
return FALSE;
}
#endif // _WIN32
// init SDL and set appropriate hints
static bool
sdl_init_and_configure(bool display, const char *render_driver) {
@ -72,14 +56,6 @@ sdl_init_and_configure(bool display, const char *render_driver) {
atexit(SDL_Quit);
#ifdef _WIN32
// Clean up properly on Ctrl+C on Windows
bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE);
if (!ok) {
LOGW("Could not set Ctrl+C handler");
}
#endif // _WIN32
if (!display) {
return true;
}
@ -133,7 +109,7 @@ static int
event_watcher(void *data, SDL_Event *event) {
(void) data;
if (event->type == SDL_WINDOWEVENT
&& event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
&& event->window.event == SDL_WINDOWEVENT_RESIZED) {
// In practice, it seems to always be called from the same thread in
// that specific case. Anyway, it's just a workaround.
screen_render(&screen);

View File

@ -15,6 +15,18 @@
#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
get_rotated_size(struct size size, int rotation) {
struct size rotated_size;
@ -30,10 +42,10 @@ get_rotated_size(struct size size, int rotation) {
// get the window size in a struct size
static struct size
get_window_size(const struct screen *screen) {
get_window_size(SDL_Window *window) {
int width;
int height;
SDL_GetWindowSize(screen->window, &width, &height);
SDL_GetWindowSize(window, &width, &height);
struct size size;
size.width = width;
@ -41,12 +53,31 @@ get_window_size(const struct screen *screen) {
return size;
}
// get the windowed window size
static struct size
get_windowed_window_size(const struct screen *screen) {
if (is_fullscreen(screen) || is_maximized(screen)) {
return screen->windowed_window_size;
}
return get_window_size(screen->window);
}
// apply the windowed window size if fullscreen and maximized are disabled
static void
apply_windowed_size(struct screen *screen) {
if (!is_fullscreen(screen) && !is_maximized(screen)) {
SDL_SetWindowSize(screen->window, screen->windowed_window_size.width,
screen->windowed_window_size.height);
}
}
// set the window size to be applied when fullscreen is disabled
static void
set_window_size(struct screen *screen, struct size new_size) {
assert(!screen->fullscreen);
assert(!screen->maximized);
SDL_SetWindowSize(screen->window, new_size.width, new_size.height);
// setting the window size during fullscreen is implementation defined,
// so apply the resize only after fullscreen is disabled
screen->windowed_window_size = new_size;
apply_windowed_size(screen);
}
// get the preferred display bounds (i.e. the screen bounds with some margins)
@ -119,8 +150,8 @@ get_optimal_size(struct size current_size, struct size content_size) {
// same as get_optimal_size(), but read the current size from the window
static inline struct size
get_optimal_window_size(const struct screen *screen, struct size content_size) {
struct size window_size = get_window_size(screen);
return get_optimal_size(window_size, content_size);
struct size windowed_size = get_windowed_window_size(screen);
return get_optimal_size(windowed_size, content_size);
}
// initially, there is no current size, so use the frame size as current size
@ -289,6 +320,8 @@ screen_init_rendering(struct screen *screen, const char *window_title,
return false;
}
screen->windowed_window_size = window_size;
return true;
}
@ -310,45 +343,6 @@ screen_destroy(struct screen *screen) {
}
}
static void
resize_for_content(struct screen *screen, struct size old_content_size,
struct size new_content_size) {
struct size window_size = get_window_size(screen);
struct size target_size = {
.width = (uint32_t) window_size.width * new_content_size.width
/ old_content_size.width,
.height = (uint32_t) window_size.height * new_content_size.height
/ old_content_size.height,
};
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);
}
static void
set_content_size(struct screen *screen, struct size new_content_size) {
if (!screen->fullscreen && !screen->maximized) {
resize_for_content(screen, screen->content_size, new_content_size);
} else if (!screen->resize_pending) {
// Store the windowed size to be able to compute the optimal size once
// fullscreen and maximized are disabled
screen->windowed_content_size = screen->content_size;
screen->resize_pending = true;
}
screen->content_size = new_content_size;
}
static void
apply_pending_resize(struct screen *screen) {
assert(!screen->fullscreen);
assert(!screen->maximized);
if (screen->resize_pending) {
resize_for_content(screen, screen->windowed_content_size,
screen->content_size);
screen->resize_pending = false;
}
}
void
screen_set_rotation(struct screen *screen, unsigned rotation) {
assert(rotation < 4);
@ -356,6 +350,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
return;
}
struct size old_content_size = screen->content_size;
struct size new_content_size =
get_rotated_size(screen->frame_size, rotation);
@ -366,7 +361,17 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
return;
}
set_content_size(screen, new_content_size);
struct size windowed_size = get_windowed_window_size(screen);
struct size target_size = {
.width = (uint32_t) windowed_size.width * new_content_size.width
/ old_content_size.width,
.height = (uint32_t) windowed_size.height * new_content_size.height
/ old_content_size.height,
};
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);
screen->content_size = new_content_size;
screen->rotation = rotation;
LOGI("Display rotation set to %u", rotation);
@ -390,8 +395,19 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
// frame dimension changed, destroy texture
SDL_DestroyTexture(screen->texture);
set_content_size(screen, new_content_size);
struct size content_size = screen->content_size;
struct size windowed_size = get_windowed_window_size(screen);
struct size target_size = {
(uint32_t) windowed_size.width * new_content_size.width
/ content_size.width,
(uint32_t) windowed_size.height * new_content_size.height
/ content_size.height,
};
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);
screen->frame_size = new_frame_size;
screen->content_size = new_content_size;
LOGI("New texture: %" PRIu16 "x%" PRIu16,
screen->frame_size.width, screen->frame_size.height);
@ -467,30 +483,27 @@ screen_render(struct screen *screen) {
void
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)) {
LOGW("Could not switch fullscreen mode: %s", SDL_GetError());
return;
}
screen->fullscreen = !screen->fullscreen;
if (!screen->fullscreen && !screen->maximized) {
apply_pending_resize(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);
}
void
screen_resize_to_fit(struct screen *screen) {
if (screen->fullscreen) {
if (is_fullscreen(screen)) {
return;
}
if (screen->maximized) {
if (is_maximized(screen)) {
SDL_RestoreWindow(screen->window);
screen->maximized = false;
}
struct size optimal_size =
@ -502,13 +515,12 @@ screen_resize_to_fit(struct screen *screen) {
void
screen_resize_to_pixel_perfect(struct screen *screen) {
if (screen->fullscreen) {
if (is_fullscreen(screen)) {
return;
}
if (screen->maximized) {
if (is_maximized(screen)) {
SDL_RestoreWindow(screen->window);
screen->maximized = false;
}
struct size content_size = screen->content_size;
@ -517,26 +529,6 @@ screen_resize_to_pixel_perfect(struct screen *screen) {
content_size.height);
}
static inline bool
is_fullscreen(const struct screen *screen) {
uint32_t flags = SDL_GetWindowFlags(screen->window);
return !!(flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP));
}
static void
update_fullscreen_state(struct screen *screen) {
// There is no SDL event to detect fullscreen changes, so store the
// state in a field and compare on every "size changed" event
bool fullscreen = is_fullscreen(screen);
if (fullscreen != screen->fullscreen) {
// Fullscreen state have changed
screen->fullscreen = fullscreen;
if (!fullscreen && !screen->maximized) {
apply_pending_resize(screen);
}
}
}
void
screen_handle_window_event(struct screen *screen,
const SDL_WindowEvent *event) {
@ -545,15 +537,34 @@ screen_handle_window_event(struct screen *screen,
screen_render(screen);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
update_fullscreen_state(screen);
if (!is_fullscreen(screen) && !is_maximized(screen)) {
// Backup the previous size: if we receive the MAXIMIZED event,
// then the new size must be ignored (it's the maximized size).
// We could not rely on the window flags due to race conditions
// (they could be updated asynchronously, at least on X11).
screen->windowed_window_size_backup =
screen->windowed_window_size;
// Save the windowed size, so that it is available once the
// window is maximized or fullscreen is enabled.
screen->windowed_window_size = get_window_size(screen->window);
}
screen_render(screen);
break;
case SDL_WINDOWEVENT_MAXIMIZED:
screen->maximized = true;
// The backup size must be non-nul.
assert(screen->windowed_window_size_backup.width);
assert(screen->windowed_window_size_backup.height);
// Revert the last size, it was updated while screen was maximized.
screen->windowed_window_size = screen->windowed_window_size_backup;
#ifdef DEBUG
// Reset the backup to invalid values to detect unexpected usage
screen->windowed_window_size_backup.width = 0;
screen->windowed_window_size_backup.height = 0;
#endif
break;
case SDL_WINDOWEVENT_RESTORED:
screen->maximized = false;
apply_pending_resize(screen);
apply_windowed_size(screen);
break;
}
}

View File

@ -21,17 +21,14 @@ struct screen {
struct sc_opengl gl;
struct size frame_size;
struct size content_size; // rotated frame_size
bool resize_pending; // resize requested while fullscreen or maximized
// The content size the last time the window was not maximized or
// fullscreen (meaningful only when resize_pending is true)
struct size windowed_content_size;
// The window size the last time it was not maximized or fullscreen.
struct size windowed_window_size;
// Since we receive the event SIZE_CHANGED before MAXIMIZED, we must be
// able to revert the size to its non-maximized value.
struct size windowed_window_size_backup;
// client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise)
unsigned rotation;
bool has_frame;
bool fullscreen;
bool maximized;
bool no_window;
bool mipmaps;
};
@ -50,15 +47,16 @@ struct screen {
.width = 0, \
.height = 0, \
}, \
.resize_pending = false, \
.windowed_content_size = { \
.windowed_window_size = { \
.width = 0, \
.height = 0, \
}, \
.windowed_window_size_backup = { \
.width = 0, \
.height = 0, \
}, \
.rotation = 0, \
.has_frame = false, \
.fullscreen = false, \
.maximized = false, \
.no_window = false, \
.mipmaps = false, \
}