Added SDL_GetDisplayName(), with implementation for Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 11:07:46 -0800
changeset 678795a4c5a5464c
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
     1.1 --- a/include/SDL_video.h	Mon Dec 31 10:29:17 2012 -0800
     1.2 +++ b/include/SDL_video.h	Mon Dec 31 11:07:46 2012 -0800
     1.3 @@ -270,6 +270,15 @@
     1.4  extern DECLSPEC int SDLCALL SDL_GetNumVideoDisplays(void);
     1.5  
     1.6  /**
     1.7 + *  \brief Get the name of a display in UTF-8 encoding
     1.8 + *  
     1.9 + *  \return The name of a display, or NULL for an invalid display index.
    1.10 + *  
    1.11 + *  \sa SDL_GetNumVideoDisplays()
    1.12 + */
    1.13 +extern DECLSPEC const char * SDLCALL SDL_GetDisplayName(int displayIndex);
    1.14 +
    1.15 +/**
    1.16   *  \brief Get the desktop area represented by a display, with the primary
    1.17   *         display located at 0,0
    1.18   *  
     2.1 --- a/src/video/SDL_sysvideo.h	Mon Dec 31 10:29:17 2012 -0800
     2.2 +++ b/src/video/SDL_sysvideo.h	Mon Dec 31 11:07:46 2012 -0800
     2.3 @@ -109,6 +109,7 @@
     2.4   */
     2.5  struct SDL_VideoDisplay
     2.6  {
     2.7 +    char *name;
     2.8      int max_display_modes;
     2.9      int num_display_modes;
    2.10      SDL_DisplayMode *display_modes;
     3.1 --- a/src/video/SDL_video.c	Mon Dec 31 10:29:17 2012 -0800
     3.2 +++ b/src/video/SDL_video.c	Mon Dec 31 11:07:46 2012 -0800
     3.3 @@ -581,6 +581,15 @@
     3.4          displays[index] = *display;
     3.5          displays[index].device = _this;
     3.6          _this->displays = displays;
     3.7 +
     3.8 +        if (display->name) {
     3.9 +            displays[index].name = SDL_strdup(display->name);
    3.10 +        } else {
    3.11 +            char name[32];
    3.12 +
    3.13 +            SDL_itoa(index, name, 10);
    3.14 +            displays[index].name = SDL_strdup(name);
    3.15 +        }
    3.16      } else {
    3.17          SDL_OutOfMemory();
    3.18      }
    3.19 @@ -612,6 +621,14 @@
    3.20      return 0;
    3.21  }
    3.22  
    3.23 +const char *
    3.24 +SDL_GetDisplayName(int displayIndex)
    3.25 +{
    3.26 +    CHECK_DISPLAY_INDEX(displayIndex, NULL);
    3.27 +
    3.28 +    return _this->displays[displayIndex].name;
    3.29 +}
    3.30 +
    3.31  int
    3.32  SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
    3.33  {
    3.34 @@ -2195,8 +2212,12 @@
    3.35          }
    3.36      }
    3.37      if (_this->displays) {
    3.38 +        for (i = 0; i < _this->num_displays; ++i) {
    3.39 +            SDL_free(_this->displays[i].name);
    3.40 +        }
    3.41          SDL_free(_this->displays);
    3.42          _this->displays = NULL;
    3.43 +        _this->num_displays = 0;
    3.44      }
    3.45      if (_this->clipboard_text) {
    3.46          SDL_free(_this->clipboard_text);
     4.1 --- a/src/video/cocoa/SDL_cocoamodes.m	Mon Dec 31 10:29:17 2012 -0800
     4.2 +++ b/src/video/cocoa/SDL_cocoamodes.m	Mon Dec 31 11:07:46 2012 -0800
     4.3 @@ -24,6 +24,9 @@
     4.4  
     4.5  #include "SDL_cocoavideo.h"
     4.6  
     4.7 +/* We need this for IODisplayCreateInfoDictionary and kIODisplayOnlyPreferredName */
     4.8 +#include <IOKit/graphics/IOGraphicsLib.h>
     4.9 +
    4.10  /* we need this for ShowMenuBar() and HideMenuBar(). */
    4.11  #include <Carbon/Carbon.h>
    4.12  
    4.13 @@ -217,6 +220,18 @@
    4.14      #endif
    4.15  }
    4.16  
    4.17 +static char *
    4.18 +Cocoa_GetDisplayName(CGDirectDisplayID displayID)
    4.19 +{
    4.20 +    NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName);
    4.21 +    NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];
    4.22 +
    4.23 +    if ([localizedNames count] > 0) {
    4.24 +        return [[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] UTF8String];
    4.25 +    }
    4.26 +    return NULL;
    4.27 +}
    4.28 +
    4.29  void
    4.30  Cocoa_InitModes(_THIS)
    4.31  {
    4.32 @@ -284,6 +299,7 @@
    4.33              displaydata->display = displays[i];
    4.34  
    4.35              SDL_zero(display);
    4.36 +            display.name = Cocoa_GetDisplayName(displays[i]);
    4.37              if (!GetDisplayMode (_this, moderef, &mode)) {
    4.38                  Cocoa_ReleaseDisplayMode(_this, moderef);
    4.39                  SDL_free(displaydata);
     5.1 --- a/test/testwm2.c	Mon Dec 31 10:29:17 2012 -0800
     5.2 +++ b/test/testwm2.c	Mon Dec 31 11:07:46 2012 -0800
     5.3 @@ -83,11 +83,11 @@
     5.4                  if (event.window.event == SDL_WINDOWEVENT_MOVED) {
     5.5                      SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
     5.6                      if (window) {
     5.7 -                        printf("Window %d moved to %d,%d (display %d)\n",
     5.8 +                        printf("Window %d moved to %d,%d (display %s)\n",
     5.9                              event.window.windowID,
    5.10                              event.window.data1,
    5.11                              event.window.data2,
    5.12 -                            SDL_GetWindowDisplayIndex(window));
    5.13 +                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
    5.14                      }
    5.15                  }
    5.16              }