Skip to content

Commit d93c934

Browse files
MacOS Resolution support
1 parent 5c1ac33 commit d93c934

File tree

5 files changed

+102
-36
lines changed

5 files changed

+102
-36
lines changed

src/detection/displayserver/displayserver.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
#include "displayserver.h"
22
#include "detection/internal.h"
33

4+
uint32_t ffdsParseRefreshRate(int32_t refreshRate)
5+
{
6+
if(refreshRate <= 0)
7+
return 0;
8+
9+
int remainder = refreshRate % 5;
10+
if(remainder >= 3)
11+
refreshRate += (5 - remainder);
12+
else
13+
refreshRate -= remainder;
14+
15+
//All other typicall refresh rates are dividable by 5
16+
if(refreshRate == 145)
17+
refreshRate = 144;
18+
19+
return (uint32_t) refreshRate;
20+
}
21+
22+
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate)
23+
{
24+
if(width == 0 || height == 0)
25+
return false;
26+
27+
FFResolutionResult* resolution = ffListAdd(&result->resolutions);
28+
resolution->width = width;
29+
resolution->height = height;
30+
resolution->refreshRate = refreshRate;
31+
32+
return true;
33+
}
34+
435
void ffConnectDisplayServerImpl(FFDisplayServerResult* ds, const FFinstance* instance);
536

637
const FFDisplayServerResult* ffConnectDisplayServer(const FFinstance* instance)

src/detection/displayserver/displayserver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ typedef struct FFDisplayServerResult
2525

2626
const FFDisplayServerResult* ffConnectDisplayServer(const FFinstance* instance);
2727

28-
#endif
28+
//Used internal
29+
uint32_t ffdsParseRefreshRate(int32_t refreshRate);
30+
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate);
2931

32+
#endif

src/detection/displayserver/displayserver_apple.c

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,68 @@
11
#include "displayserver.h"
2+
#include "common/library.h"
3+
4+
#include <stdlib.h>
5+
#include <ApplicationServices/ApplicationServices.h>
6+
7+
//Resolution code heavily inspired by displayplacer <3
8+
9+
typedef union
10+
{
11+
uint8_t rawData[0xDC];
12+
struct
13+
{
14+
uint32_t mode;
15+
uint32_t flags; // 0x4
16+
uint32_t width; // 0x8
17+
uint32_t height; // 0xC
18+
uint32_t depth; // 0x10
19+
uint32_t dc2[42];
20+
uint16_t dc3;
21+
uint16_t freq; // 0xBC
22+
uint32_t dc4[4];
23+
float density; // 0xD0
24+
} derived;
25+
} modes_D4;
26+
27+
void CGSGetCurrentDisplayMode(CGDirectDisplayID display, int* modeNum);
28+
void CGSGetDisplayModeDescriptionOfLength(CGDirectDisplayID display, int idx, modes_D4* mode, int length);
29+
30+
static void detectResolution(FFDisplayServerResult* ds)
31+
{
32+
void* iokit = dlopen(FASTFETCH_TARGET_DIR_ROOT"/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics", RTLD_LAZY);
33+
if(iokit == NULL)
34+
return;
35+
36+
FF_LIBRARY_LOAD_SYMBOL(iokit, CGGetOnlineDisplayList, )
37+
FF_LIBRARY_LOAD_SYMBOL(iokit, CGDisplayScreenSize, )
38+
FF_LIBRARY_LOAD_SYMBOL(iokit, CGSGetCurrentDisplayMode, )
39+
FF_LIBRARY_LOAD_SYMBOL(iokit, CGSGetDisplayModeDescriptionOfLength, )
40+
41+
CGDisplayCount screenCount;
42+
ffCGGetOnlineDisplayList(INT_MAX, NULL, &screenCount);
43+
if(screenCount == 0)
44+
{
45+
dlclose(iokit);
46+
return;
47+
}
48+
49+
CGDirectDisplayID* screens = malloc(screenCount * sizeof(CGDirectDisplayID));
50+
ffCGGetOnlineDisplayList(INT_MAX, screens, &screenCount);
51+
52+
for(uint32_t i = 0; i < screenCount; i++)
53+
{
54+
int modeID;
55+
ffCGSGetCurrentDisplayMode(screens[i], &modeID);
56+
modes_D4 mode;
57+
ffCGSGetDisplayModeDescriptionOfLength(screens[i], modeID, &mode, 0xD4);
58+
59+
uint32_t refreshRate = ffdsParseRefreshRate(mode.derived.freq);
60+
ffdsAppendResolution(ds, mode.derived.width, mode.derived.height, refreshRate);
61+
}
62+
63+
free(screens);
64+
dlclose(iokit);
65+
}
266

367
void ffConnectDisplayServerImpl(FFDisplayServerResult* ds, const FFinstance* instance)
468
{
@@ -16,7 +80,9 @@ void ffConnectDisplayServerImpl(FFDisplayServerResult* ds, const FFinstance* ins
1680
ffStrbufInit(&ds->dePrettyName);
1781
ffStrbufAppendS(&ds->dePrettyName, "Aqua");
1882

83+
ffListInitA(&ds->resolutions, sizeof(FFResolutionResult), 4);
84+
detectResolution(ds);
85+
1986
ffStrbufInitA(&ds->deVersion, 0);
2087
ffStrbufInitA(&ds->wmProtocolName, 0);
21-
ffListInitA(&ds->resolutions, sizeof(FFResolutionResult), 0);
2288
}

src/detection/displayserver/linux/displayserver_linux.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,6 @@
22

33
#include <dirent.h>
44

5-
uint32_t ffdsParseRefreshRate(int32_t refreshRate)
6-
{
7-
if(refreshRate <= 0)
8-
return 0;
9-
10-
int remainder = refreshRate % 5;
11-
if(remainder >= 3)
12-
refreshRate += (5 - remainder);
13-
else
14-
refreshRate -= remainder;
15-
16-
//All other typicall refresh rates are dividable by 5
17-
if(refreshRate == 145)
18-
refreshRate = 144;
19-
20-
return (uint32_t) refreshRate;
21-
}
22-
23-
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate)
24-
{
25-
if(width == 0 || height == 0)
26-
return false;
27-
28-
FFResolutionResult* resolution = ffListAdd(&result->resolutions);
29-
resolution->width = width;
30-
resolution->height = height;
31-
resolution->refreshRate = refreshRate;
32-
33-
return true;
34-
}
35-
365
static void parseDRM(FFDisplayServerResult* result)
376
{
387
const char* drmDirPath = "/sys/class/drm/";

src/detection/displayserver/linux/displayserver_linux.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#define FF_DISPLAYSERVER_PROTOCOL_X11 "X11"
1010
#define FF_DISPLAYSERVER_PROTOCOL_TTY "TTY"
1111

12-
uint32_t ffdsParseRefreshRate(int32_t refreshRate);
13-
bool ffdsAppendResolution(FFDisplayServerResult* result, uint32_t width, uint32_t height, uint32_t refreshRate);
14-
1512
void ffdsConnectWayland(const FFinstance* instance, FFDisplayServerResult* result);
1613

1714
void ffdsConnectXcbRandr(const FFinstance* instance, FFDisplayServerResult* result);

0 commit comments

Comments
 (0)