Skip to content

Commit

Permalink
testwm.c: show all modes of all displays in the on-screen list (#7323)
Browse files Browse the repository at this point in the history
* testwm.c: show all modes of all displays in the on-screen list

To allow testing #7317
  • Loading branch information
ericwa committed Feb 15, 2023
1 parent beb6a2a commit 1f46986
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions test/testwm.c
Expand Up @@ -40,7 +40,7 @@ static const char *cursorNames[] = {
int system_cursor = -1;
SDL_Cursor *cursor = NULL;
SDL_bool relative_mode = SDL_FALSE;
int highlighted_mode = -1;
const SDL_DisplayMode *highlighted_mode = NULL;

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
Expand All @@ -57,13 +57,13 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
const SDL_DisplayMode **modes;
char text[1024];
const int lineHeight = 10;
const SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
int i;
int i, j;
int column_chars = 0;
int text_length;
float x, y;
float table_top;
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
SDL_DisplayID *display_ids;

/* Get mouse position */
if (SDL_GetMouseFocus() == window) {
Expand Down Expand Up @@ -96,49 +96,58 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)

/* Clear the cached mode under the mouse */
if (window == SDL_GetMouseFocus()) {
highlighted_mode = -1;
highlighted_mode = NULL;
}

modes = SDL_GetFullscreenDisplayModes(displayID, NULL);
for (i = 0; modes[i]; ++i) {
SDL_FRect cell_rect;
const SDL_DisplayMode *mode = modes[i];

(void)SDL_snprintf(text, sizeof text, "%d: %dx%d@%gHz",
i, mode->pixel_w, mode->pixel_h, mode->refresh_rate);

/* Update column width */
text_length = (int)SDL_strlen(text);
column_chars = SDL_max(column_chars, text_length);

/* Check if under mouse */
cell_rect.x = x;
cell_rect.y = y;
cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
cell_rect.h = (float)lineHeight;
display_ids = SDL_GetDisplays(NULL);

if (display_ids) {
for (i = 0; display_ids[i]; ++i) {
const SDL_DisplayID display_id = display_ids[i];
modes = SDL_GetFullscreenDisplayModes(display_id, NULL);
for (j = 0; modes[j]; ++j) {
SDL_FRect cell_rect;
const SDL_DisplayMode *mode = modes[j];

(void)SDL_snprintf(text, sizeof text, "%s mode %d: %dx%d@%gHz",
SDL_GetDisplayName(display_id),
j, mode->pixel_w, mode->pixel_h, mode->refresh_rate);

/* Update column width */
text_length = (int)SDL_strlen(text);
column_chars = SDL_max(column_chars, text_length);

/* Check if under mouse */
cell_rect.x = x;
cell_rect.y = y;
cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
cell_rect.h = (float)lineHeight;

if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

/* Update cached mode under the mouse */
if (window == SDL_GetMouseFocus()) {
highlighted_mode = mode;
}
} else {
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
}

if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDLTest_DrawString(renderer, x, y, text);
y += lineHeight;

/* Update cached mode under the mouse */
if (window == SDL_GetMouseFocus()) {
highlighted_mode = i;
if ((y + lineHeight) > (viewport.y + viewport.h)) {
/* Advance to next column */
x += (column_chars + 1) * FONT_CHARACTER_SIZE;
y = table_top;
column_chars = 0;
}
}
} else {
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
}

SDLTest_DrawString(renderer, x, y, text);
y += lineHeight;

if ((y + lineHeight) > (viewport.y + viewport.h)) {
/* Advance to next column */
x += (column_chars + 1) * FONT_CHARACTER_SIZE;
y = table_top;
column_chars = 0;
SDL_free((void *)modes);
}
SDL_free(display_ids);
}
SDL_free((void *)modes);
}

void loop()
Expand Down Expand Up @@ -206,15 +215,9 @@ void loop()
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
SDL_Window *window = SDL_GetMouseFocus();
if (highlighted_mode != -1 && window != NULL) {
SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
int num_modes;
const SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(displayID, &num_modes);
if (highlighted_mode < num_modes) {
SDL_memcpy(&state->fullscreen_mode, modes[highlighted_mode], sizeof(state->fullscreen_mode));
SDL_SetWindowFullscreenMode(window, modes[highlighted_mode]);
}
SDL_free((void *)modes);
if (highlighted_mode != NULL && window != NULL) {
SDL_memcpy(&state->fullscreen_mode, highlighted_mode, sizeof(state->fullscreen_mode));
SDL_SetWindowFullscreenMode(window, highlighted_mode);
}
}
}
Expand Down

0 comments on commit 1f46986

Please sign in to comment.