testdisplayinfo.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  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 "SDL.h"
  12. #include "SDL_test.h"
  13. #include <stdio.h>
  14. #include <stdlib.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: fmt=%s w=%d h=%d refresh=%d\n",
  22. prefix, SDL_GetPixelFormatName(mode->format),
  23. mode->w, mode->h, mode->refresh_rate);
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. SDL_DisplayMode mode;
  28. int num_displays, dpy;
  29. SDLTest_CommonState *state;
  30. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  31. if (!state) {
  32. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
  33. return 1;
  34. }
  35. /* Enable standard application logging */
  36. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  37. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  38. return 1;
  39. }
  40. if (!SDLTest_CommonInit(state)) {
  41. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  42. return 1;
  43. }
  44. SDL_Log("Using video target '%s'.\n", SDL_GetCurrentVideoDriver());
  45. num_displays = SDL_GetNumVideoDisplays();
  46. SDL_Log("See %d displays.\n", num_displays);
  47. for (dpy = 0; dpy < num_displays; dpy++) {
  48. const int num_modes = SDL_GetNumDisplayModes(dpy);
  49. SDL_Rect rect = { 0, 0, 0, 0 };
  50. float ddpi, hdpi, vdpi;
  51. int m;
  52. SDL_GetDisplayBounds(dpy, &rect);
  53. SDL_Log("%d: \"%s\" (%dx%d, (%d, %d)), %d modes.\n", dpy, SDL_GetDisplayName(dpy), rect.w, rect.h, rect.x, rect.y, num_modes);
  54. if (SDL_GetDisplayDPI(dpy, &ddpi, &hdpi, &vdpi) == -1) {
  55. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " DPI: failed to query (%s)\n", SDL_GetError());
  56. } else {
  57. SDL_Log(" DPI: ddpi=%f; hdpi=%f; vdpi=%f\n", ddpi, hdpi, vdpi);
  58. }
  59. if (SDL_GetCurrentDisplayMode(dpy, &mode) == -1) {
  60. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " CURRENT: failed to query (%s)\n", SDL_GetError());
  61. } else {
  62. print_mode("CURRENT", &mode);
  63. }
  64. if (SDL_GetDesktopDisplayMode(dpy, &mode) == -1) {
  65. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " DESKTOP: failed to query (%s)\n", SDL_GetError());
  66. } else {
  67. print_mode("DESKTOP", &mode);
  68. }
  69. for (m = 0; m < num_modes; m++) {
  70. if (SDL_GetDisplayMode(dpy, m, &mode) == -1) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, " MODE %d: failed to query (%s)\n", m, SDL_GetError());
  72. } else {
  73. char prefix[64];
  74. (void)SDL_snprintf(prefix, sizeof(prefix), " MODE %d", m);
  75. print_mode(prefix, &mode);
  76. }
  77. }
  78. SDL_Log("\n");
  79. }
  80. SDLTest_CommonQuit(state);
  81. return 0;
  82. }
  83. /* vi: set ts=4 sw=4 expandtab: */