Added SDL_GetDisplayName(), with implementation for Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 11:07:46 -0800
changeset 6787 95a4c5a5464c
parent 6786 7174fb08017a
child 6788 036f53f2f5aa
Added SDL_GetDisplayName(), with implementation for Mac OS X
include/SDL_video.h
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoamodes.m
test/testwm2.c
--- a/include/SDL_video.h	Mon Dec 31 10:29:17 2012 -0800
+++ b/include/SDL_video.h	Mon Dec 31 11:07:46 2012 -0800
@@ -270,6 +270,15 @@
 extern DECLSPEC int SDLCALL SDL_GetNumVideoDisplays(void);
 
 /**
+ *  \brief Get the name of a display in UTF-8 encoding
+ *  
+ *  \return The name of a display, or NULL for an invalid display index.
+ *  
+ *  \sa SDL_GetNumVideoDisplays()
+ */
+extern DECLSPEC const char * SDLCALL SDL_GetDisplayName(int displayIndex);
+
+/**
  *  \brief Get the desktop area represented by a display, with the primary
  *         display located at 0,0
  *  
--- a/src/video/SDL_sysvideo.h	Mon Dec 31 10:29:17 2012 -0800
+++ b/src/video/SDL_sysvideo.h	Mon Dec 31 11:07:46 2012 -0800
@@ -109,6 +109,7 @@
  */
 struct SDL_VideoDisplay
 {
+    char *name;
     int max_display_modes;
     int num_display_modes;
     SDL_DisplayMode *display_modes;
--- a/src/video/SDL_video.c	Mon Dec 31 10:29:17 2012 -0800
+++ b/src/video/SDL_video.c	Mon Dec 31 11:07:46 2012 -0800
@@ -581,6 +581,15 @@
         displays[index] = *display;
         displays[index].device = _this;
         _this->displays = displays;
+
+        if (display->name) {
+            displays[index].name = SDL_strdup(display->name);
+        } else {
+            char name[32];
+
+            SDL_itoa(index, name, 10);
+            displays[index].name = SDL_strdup(name);
+        }
     } else {
         SDL_OutOfMemory();
     }
@@ -612,6 +621,14 @@
     return 0;
 }
 
+const char *
+SDL_GetDisplayName(int displayIndex)
+{
+    CHECK_DISPLAY_INDEX(displayIndex, NULL);
+
+    return _this->displays[displayIndex].name;
+}
+
 int
 SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
 {
@@ -2195,8 +2212,12 @@
         }
     }
     if (_this->displays) {
+        for (i = 0; i < _this->num_displays; ++i) {
+            SDL_free(_this->displays[i].name);
+        }
         SDL_free(_this->displays);
         _this->displays = NULL;
+        _this->num_displays = 0;
     }
     if (_this->clipboard_text) {
         SDL_free(_this->clipboard_text);
--- a/src/video/cocoa/SDL_cocoamodes.m	Mon Dec 31 10:29:17 2012 -0800
+++ b/src/video/cocoa/SDL_cocoamodes.m	Mon Dec 31 11:07:46 2012 -0800
@@ -24,6 +24,9 @@
 
 #include "SDL_cocoavideo.h"
 
+/* We need this for IODisplayCreateInfoDictionary and kIODisplayOnlyPreferredName */
+#include <IOKit/graphics/IOGraphicsLib.h>
+
 /* we need this for ShowMenuBar() and HideMenuBar(). */
 #include <Carbon/Carbon.h>
 
@@ -217,6 +220,18 @@
     #endif
 }
 
+static char *
+Cocoa_GetDisplayName(CGDirectDisplayID displayID)
+{
+    NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName);
+    NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];
+
+    if ([localizedNames count] > 0) {
+        return [[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] UTF8String];
+    }
+    return NULL;
+}
+
 void
 Cocoa_InitModes(_THIS)
 {
@@ -284,6 +299,7 @@
             displaydata->display = displays[i];
 
             SDL_zero(display);
+            display.name = Cocoa_GetDisplayName(displays[i]);
             if (!GetDisplayMode (_this, moderef, &mode)) {
                 Cocoa_ReleaseDisplayMode(_this, moderef);
                 SDL_free(displaydata);
--- a/test/testwm2.c	Mon Dec 31 10:29:17 2012 -0800
+++ b/test/testwm2.c	Mon Dec 31 11:07:46 2012 -0800
@@ -83,11 +83,11 @@
                 if (event.window.event == SDL_WINDOWEVENT_MOVED) {
                     SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
                     if (window) {
-                        printf("Window %d moved to %d,%d (display %d)\n",
+                        printf("Window %d moved to %d,%d (display %s)\n",
                             event.window.windowID,
                             event.window.data1,
                             event.window.data2,
-                            SDL_GetWindowDisplayIndex(window));
+                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
                     }
                 }
             }