@@ -153,8 +153,28 @@ size_t bitDepth(CGDisplayModeRef mode) {
153153 return depth;
154154}
155155
156+ static bool isHiDPIMode (CGDisplayModeRef mode) {
157+ // Check if the mode is HiDPI by comparing pixel width to width
158+ // If pixel width is greater than width, it's a HiDPI mode
159+ return CGDisplayModeGetPixelWidth (mode) > CGDisplayModeGetWidth (mode);
160+ }
161+
162+ CFArrayRef getAllModes (CGDirectDisplayID display) {
163+ // Create options dictionary to include HiDPI modes
164+ CFMutableDictionaryRef options = CFDictionaryCreateMutable (
165+ kCFAllocatorDefault ,
166+ 0 ,
167+ &kCFTypeDictionaryKeyCallBacks ,
168+ &kCFTypeDictionaryValueCallBacks );
169+ // Include HiDPI modes
170+ CFDictionarySetValue (options, kCGDisplayShowDuplicateLowResolutionModes , kCFBooleanTrue );
171+ CFArrayRef allModes = CGDisplayCopyAllDisplayModes (display, options);
172+ CFRelease (options);
173+ return allModes;
174+ }
175+
156176extern " C" bool MacGetModeNum (CGDirectDisplayID display, uint32_t *numModes) {
157- CFArrayRef allModes = CGDisplayCopyAllDisplayModes (display, NULL );
177+ CFArrayRef allModes = getAllModes (display);
158178 if (allModes == NULL ) {
159179 return false ;
160180 }
@@ -163,12 +183,12 @@ size_t bitDepth(CGDisplayModeRef mode) {
163183 return true ;
164184}
165185
166- extern " C" bool MacGetModes (CGDirectDisplayID display, uint32_t *widths, uint32_t *heights, uint32_t max, uint32_t *numModes) {
186+ extern " C" bool MacGetModes (CGDirectDisplayID display, uint32_t *widths, uint32_t *heights, bool *hidpis, uint32_t max, uint32_t *numModes) {
167187 CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode (display);
168188 if (currentMode == NULL ) {
169189 return false ;
170190 }
171- CFArrayRef allModes = CGDisplayCopyAllDisplayModes (display, NULL );
191+ CFArrayRef allModes = getAllModes (display);
172192 if (allModes == NULL ) {
173193 CGDisplayModeRelease (currentMode);
174194 return false ;
@@ -181,6 +201,7 @@ size_t bitDepth(CGDisplayModeRef mode) {
181201 bitDepth (currentMode) == bitDepth (mode)) {
182202 widths[realNum] = (uint32_t )CGDisplayModeGetWidth (mode);
183203 heights[realNum] = (uint32_t )CGDisplayModeGetHeight (mode);
204+ hidpis[realNum] = isHiDPIMode (mode);
184205 realNum++;
185206 }
186207 }
@@ -201,7 +222,6 @@ size_t bitDepth(CGDisplayModeRef mode) {
201222 return true ;
202223}
203224
204-
205225static bool setDisplayToMode (CGDirectDisplayID display, CGDisplayModeRef mode) {
206226 CGError rc;
207227 CGDisplayConfigRef config;
@@ -220,30 +240,48 @@ static bool setDisplayToMode(CGDirectDisplayID display, CGDisplayModeRef mode) {
220240 return true ;
221241}
222242
223- extern " C" bool MacSetMode (CGDirectDisplayID display, uint32_t width, uint32_t height)
243+ extern " C" bool MacSetMode (CGDirectDisplayID display, uint32_t width, uint32_t height, bool tryHiDPI )
224244{
225245 bool ret = false ;
226246 CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode (display);
227247 if (currentMode == NULL ) {
228248 return ret;
229249 }
230- CFArrayRef allModes = CGDisplayCopyAllDisplayModes (display, NULL );
250+ CFArrayRef allModes = getAllModes (display);
251+
231252 if (allModes == NULL ) {
232253 CGDisplayModeRelease (currentMode);
233254 return ret;
234255 }
235256 int numModes = CFArrayGetCount (allModes);
257+ CGDisplayModeRef preferredHiDPIMode = NULL ;
258+ CGDisplayModeRef fallbackMode = NULL ;
236259 for (int i = 0 ; i < numModes; i++) {
237260 CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (allModes, i);
238261 if (width == CGDisplayModeGetWidth (mode) &&
239262 height == CGDisplayModeGetHeight (mode) &&
240263 CGDisplayModeGetRefreshRate (currentMode) == CGDisplayModeGetRefreshRate (mode) &&
241264 bitDepth (currentMode) == bitDepth (mode)) {
242- ret = setDisplayToMode (display, mode);
243- break ;
265+
266+ if (isHiDPIMode (mode)) {
267+ preferredHiDPIMode = mode;
268+ break ;
269+ } else {
270+ fallbackMode = mode;
271+ if (!tryHiDPI) {
272+ break ;
273+ }
274+ }
244275 }
245276 }
277+
278+ if (preferredHiDPIMode) {
279+ ret = setDisplayToMode (display, preferredHiDPIMode);
280+ } else if (fallbackMode) {
281+ ret = setDisplayToMode (display, fallbackMode);
282+ }
283+
246284 CGDisplayModeRelease (currentMode);
247285 CFRelease (allModes);
248286 return ret;
249- }
287+ }
0 commit comments