testdisplayinfo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Program to test querying of display info */
  11. #include <stdlib.h>
  12. #include <SDL3/SDL.h>
  13. #include <SDL3/SDL_main.h>
  14. #include <SDL3/SDL_test.h>
  15. static void
  16. print_mode(const char *prefix, const SDL_DisplayMode *mode)
  17. {
  18. if (!mode) {
  19. return;
  20. }
  21. SDL_Log("%s: %dx%d@%gx, %gHz, fmt=%s\n",
  22. prefix,
  23. mode->w, mode->h, mode->pixel_density, mode->refresh_rate,
  24. SDL_GetPixelFormatName(mode->format));
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. SDL_DisplayID *displays;
  29. const SDL_DisplayMode **modes;
  30. const SDL_DisplayMode *mode;
  31. int num_displays, i;
  32. SDLTest_CommonState *state;
  33. /* Initialize test framework */
  34. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  35. if (!state) {
  36. return 1;
  37. }
  38. /* Enable standard application logging */
  39. SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  40. /* Parse commandline */
  41. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  42. return 1;
  43. }
  44. /* Load the SDL library */
  45. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  46. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  47. return 1;
  48. }
  49. SDL_Log("Using video target '%s'.\n", SDL_GetCurrentVideoDriver());
  50. displays = SDL_GetDisplays(&num_displays);
  51. SDL_Log("See %d displays.\n", num_displays);
  52. for (i = 0; i < num_displays; i++) {
  53. SDL_DisplayID dpy = displays[i];
  54. SDL_Rect rect = { 0, 0, 0, 0 };
  55. int m, num_modes = 0;
  56. SDL_GetDisplayBounds(dpy, &rect);
  57. modes = SDL_GetFullscreenDisplayModes(dpy, &num_modes);
  58. SDL_Log("%" SDL_PRIu32 ": \"%s\" (%dx%d at %d,%d), content scale %.2f, %d fullscreen modes.\n", dpy, SDL_GetDisplayName(dpy), rect.w, rect.h, rect.x, rect.y, SDL_GetDisplayContentScale(dpy), num_modes);
  59. mode = SDL_GetCurrentDisplayMode(dpy);
  60. if (mode) {
  61. print_mode("CURRENT", mode);
  62. } else {
  63. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " CURRENT: failed to query (%s)\n", SDL_GetError());
  64. }
  65. mode = SDL_GetDesktopDisplayMode(dpy);
  66. if (mode) {
  67. print_mode("DESKTOP", mode);
  68. } else {
  69. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " DESKTOP: failed to query (%s)\n", SDL_GetError());
  70. }
  71. for (m = 0; m < num_modes; m++) {
  72. char prefix[64];
  73. (void)SDL_snprintf(prefix, sizeof(prefix), " MODE %d", m);
  74. print_mode(prefix, modes[m]);
  75. }
  76. SDL_free((void*)modes);
  77. SDL_Log("\n");
  78. }
  79. SDL_free(displays);
  80. SDL_Quit();
  81. SDLTest_CommonDestroyState(state);
  82. return 0;
  83. }