Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added SDL_GetDisplayName(), with implementation for Mac OS X
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Dec 31, 2012
1 parent de64420 commit d38c22c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
9 changes: 9 additions & 0 deletions include/SDL_video.h
Expand Up @@ -269,6 +269,15 @@ extern DECLSPEC const char *SDLCALL SDL_GetCurrentVideoDriver(void);
*/ */
extern DECLSPEC int SDLCALL SDL_GetNumVideoDisplays(void); 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 * \brief Get the desktop area represented by a display, with the primary
* display located at 0,0 * display located at 0,0
Expand Down
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -109,6 +109,7 @@ struct SDL_Window
*/ */
struct SDL_VideoDisplay struct SDL_VideoDisplay
{ {
char *name;
int max_display_modes; int max_display_modes;
int num_display_modes; int num_display_modes;
SDL_DisplayMode *display_modes; SDL_DisplayMode *display_modes;
Expand Down
21 changes: 21 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -581,6 +581,15 @@ SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
displays[index] = *display; displays[index] = *display;
displays[index].device = _this; displays[index].device = _this;
_this->displays = displays; _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 { } else {
SDL_OutOfMemory(); SDL_OutOfMemory();
} }
Expand Down Expand Up @@ -612,6 +621,14 @@ SDL_GetIndexOfDisplay(SDL_VideoDisplay *display)
return 0; return 0;
} }


const char *
SDL_GetDisplayName(int displayIndex)
{
CHECK_DISPLAY_INDEX(displayIndex, NULL);

return _this->displays[displayIndex].name;
}

int int
SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect) SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
{ {
Expand Down Expand Up @@ -2195,8 +2212,12 @@ SDL_VideoQuit(void)
} }
} }
if (_this->displays) { if (_this->displays) {
for (i = 0; i < _this->num_displays; ++i) {
SDL_free(_this->displays[i].name);
}
SDL_free(_this->displays); SDL_free(_this->displays);
_this->displays = NULL; _this->displays = NULL;
_this->num_displays = 0;
} }
if (_this->clipboard_text) { if (_this->clipboard_text) {
SDL_free(_this->clipboard_text); SDL_free(_this->clipboard_text);
Expand Down
16 changes: 16 additions & 0 deletions src/video/cocoa/SDL_cocoamodes.m
Expand Up @@ -24,6 +24,9 @@


#include "SDL_cocoavideo.h" #include "SDL_cocoavideo.h"


/* We need this for IODisplayCreateInfoDictionary and kIODisplayOnlyPreferredName */
#include <IOKit/graphics/IOGraphicsLib.h>

/* we need this for ShowMenuBar() and HideMenuBar(). */ /* we need this for ShowMenuBar() and HideMenuBar(). */
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>


Expand Down Expand Up @@ -217,6 +220,18 @@ - (void) setFrame:(NSRect)frame;
#endif #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 void
Cocoa_InitModes(_THIS) Cocoa_InitModes(_THIS)
{ {
Expand Down Expand Up @@ -284,6 +299,7 @@ - (void) setFrame:(NSRect)frame;
displaydata->display = displays[i]; displaydata->display = displays[i];


SDL_zero(display); SDL_zero(display);
display.name = Cocoa_GetDisplayName(displays[i]);
if (!GetDisplayMode (_this, moderef, &mode)) { if (!GetDisplayMode (_this, moderef, &mode)) {
Cocoa_ReleaseDisplayMode(_this, moderef); Cocoa_ReleaseDisplayMode(_this, moderef);
SDL_free(displaydata); SDL_free(displaydata);
Expand Down
4 changes: 2 additions & 2 deletions test/testwm2.c
Expand Up @@ -83,11 +83,11 @@ main(int argc, char *argv[])
if (event.window.event == SDL_WINDOWEVENT_MOVED) { if (event.window.event == SDL_WINDOWEVENT_MOVED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID); SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
if (window) { 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.windowID,
event.window.data1, event.window.data1,
event.window.data2, event.window.data2,
SDL_GetWindowDisplayIndex(window)); SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
} }
} }
} }
Expand Down

0 comments on commit d38c22c

Please sign in to comment.