Commit 416d046663b6674ba9f0abc076988840dc0c3407

Ryan C. Gordon 2016-01-07T14:02:37

Mac: Implemented SDL_GetDisplayDPI (thanks, Kirill!). Fixes Bugzilla #3223.

diff --git a/src/video/cocoa/SDL_cocoamodes.h b/src/video/cocoa/SDL_cocoamodes.h
index 38bf079..13a9620 100644
--- a/src/video/cocoa/SDL_cocoamodes.h
+++ b/src/video/cocoa/SDL_cocoamodes.h
@@ -37,6 +37,7 @@ extern void Cocoa_InitModes(_THIS);
 extern int Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
 extern int Cocoa_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
 extern void Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
+extern int Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hpdi, float * vdpi);
 extern int Cocoa_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
 extern void Cocoa_QuitModes(_THIS);
 
diff --git a/src/video/cocoa/SDL_cocoamodes.m b/src/video/cocoa/SDL_cocoamodes.m
index 6df0fe9..2fa619d 100644
--- a/src/video/cocoa/SDL_cocoamodes.m
+++ b/src/video/cocoa/SDL_cocoamodes.m
@@ -374,6 +374,24 @@ Cocoa_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
     return 0;
 }
 
+int
+Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
+{
+    const float MM_IN_INCH = 25.4f;
+
+    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
+
+    CGSize displaySize = CGDisplayScreenSize(data->display);
+    size_t pixelWidth = CGDisplayPixelsWide(data->display);
+    size_t pixelHeight = CGDisplayPixelsHigh(data->display);
+
+    *ddpi = SDL_ComputeDiagonalDPI(pixelWidth, pixelHeight, displaySize.width / MM_IN_INCH, displaySize.height / MM_IN_INCH);
+    *hdpi = pixelWidth * MM_IN_INCH / displaySize.width;
+    *vdpi = pixelHeight * MM_IN_INCH / displaySize.height;
+
+    return 0;
+}
+
 void
 Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
 {
diff --git a/src/video/cocoa/SDL_cocoavideo.m b/src/video/cocoa/SDL_cocoavideo.m
index dd3cf6b..ddf10e2 100644
--- a/src/video/cocoa/SDL_cocoavideo.m
+++ b/src/video/cocoa/SDL_cocoavideo.m
@@ -74,6 +74,7 @@ Cocoa_CreateDevice(int devindex)
     device->VideoQuit = Cocoa_VideoQuit;
     device->GetDisplayBounds = Cocoa_GetDisplayBounds;
     device->GetDisplayUsableBounds = Cocoa_GetDisplayUsableBounds;
+    device->GetDisplayDPI = Cocoa_GetDisplayDPI;
     device->GetDisplayModes = Cocoa_GetDisplayModes;
     device->SetDisplayMode = Cocoa_SetDisplayMode;
     device->PumpEvents = Cocoa_PumpEvents;
diff --git a/test/testdisplayinfo.c b/test/testdisplayinfo.c
index c228eb6..f06722e 100644
--- a/test/testdisplayinfo.c
+++ b/test/testdisplayinfo.c
@@ -51,11 +51,18 @@ main(int argc, char *argv[])
     for (dpy = 0; dpy < num_displays; dpy++) {
         const int num_modes = SDL_GetNumDisplayModes(dpy);
         SDL_Rect rect = { 0, 0, 0, 0 };
+        float ddpi, hdpi, vdpi;
         int m;
 
         SDL_GetDisplayBounds(dpy, &rect);
         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);
 
+        if (SDL_GetDisplayDPI(dpy, &ddpi, &hdpi, &vdpi) == -1) {
+            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "    DPI: failed to query (%s)\n", SDL_GetError());
+        } else {
+            SDL_Log("    DPI: ddpi=%f; hdpi=%f; vdpi=%f\n", ddpi, hdpi, vdpi);
+        }
+
         if (SDL_GetCurrentDisplayMode(dpy, &mode) == -1) {
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "    CURRENT: failed to query (%s)\n", SDL_GetError());
         } else {