2022-09-06 15:42:37 +02:00
|
|
|
#include "displayserver_linux.h"
|
2023-01-08 20:46:22 +08:00
|
|
|
#include "common/io.h"
|
2022-06-18 14:25:31 +02:00
|
|
|
|
2022-01-02 16:55:26 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
2023-01-08 20:46:22 +08:00
|
|
|
static void parseBacklight(FFDisplayServerResult* result)
|
|
|
|
|
{
|
|
|
|
|
if(result->resolutions.length != 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-class-backlight
|
|
|
|
|
const char* backlightDirPath = "/sys/class/backlight/";
|
|
|
|
|
|
|
|
|
|
DIR* dirp = opendir(backlightDirPath);
|
|
|
|
|
if(dirp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FFstrbuf backlightDir;
|
|
|
|
|
ffStrbufInitA(&backlightDir, 64);
|
|
|
|
|
ffStrbufAppendS(&backlightDir, backlightDirPath);
|
|
|
|
|
|
|
|
|
|
uint32_t backlightDirLength = backlightDir.length;
|
|
|
|
|
|
|
|
|
|
FFstrbuf buffer;
|
|
|
|
|
ffStrbufInit(&buffer);
|
|
|
|
|
|
|
|
|
|
struct dirent* entry;
|
2023-01-09 10:42:52 +08:00
|
|
|
while((entry = readdir(dirp)) != NULL)
|
2023-01-08 20:46:22 +08:00
|
|
|
{
|
2023-01-09 10:42:52 +08:00
|
|
|
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-01-08 20:46:22 +08:00
|
|
|
ffStrbufAppendS(&backlightDir, entry->d_name);
|
|
|
|
|
ffStrbufAppendS(&backlightDir, "/actual_brightness");
|
|
|
|
|
if(ffReadFileBuffer(backlightDir.chars, &buffer))
|
|
|
|
|
{
|
|
|
|
|
double actualBrightness = ffStrbufToDouble(&buffer);
|
|
|
|
|
ffStrbufSubstrBefore(&backlightDir, backlightDirLength);
|
2023-01-09 10:42:52 +08:00
|
|
|
ffStrbufAppendS(&backlightDir, entry->d_name);
|
2023-01-08 20:46:22 +08:00
|
|
|
ffStrbufAppendS(&backlightDir, "/max_brightness");
|
|
|
|
|
if(ffReadFileBuffer(backlightDir.chars, &buffer))
|
|
|
|
|
{
|
|
|
|
|
double maxBrightness = ffStrbufToDouble(&buffer);
|
|
|
|
|
FF_LIST_FOR_EACH(FFResolutionResult, resolution, result->resolutions)
|
|
|
|
|
{
|
|
|
|
|
resolution->brightness = (int) (actualBrightness * 100 / maxBrightness);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 10:42:52 +08:00
|
|
|
|
|
|
|
|
break;
|
2023-01-08 20:46:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
|
ffStrbufDestroy(&backlightDir);
|
|
|
|
|
ffStrbufDestroy(&buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 16:55:26 +01:00
|
|
|
static void parseDRM(FFDisplayServerResult* result)
|
|
|
|
|
{
|
|
|
|
|
const char* drmDirPath = "/sys/class/drm/";
|
|
|
|
|
|
|
|
|
|
DIR* dirp = opendir(drmDirPath);
|
|
|
|
|
if(dirp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FFstrbuf drmDir;
|
|
|
|
|
ffStrbufInitA(&drmDir, 64);
|
|
|
|
|
ffStrbufAppendS(&drmDir, drmDirPath);
|
|
|
|
|
|
|
|
|
|
uint32_t drmDirLength = drmDir.length;
|
|
|
|
|
|
|
|
|
|
struct dirent* entry;
|
|
|
|
|
while((entry = readdir(dirp)) != NULL)
|
|
|
|
|
{
|
2023-01-09 10:45:51 +08:00
|
|
|
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-01-02 16:55:26 +01:00
|
|
|
ffStrbufAppendS(&drmDir, entry->d_name);
|
|
|
|
|
ffStrbufAppendS(&drmDir, "/modes");
|
|
|
|
|
|
|
|
|
|
FILE* modeFile = fopen(drmDir.chars, "r");
|
|
|
|
|
if(modeFile == NULL)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSubstrBefore(&drmDir, drmDirLength);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FFResolutionResult* resolution = ffListAdd(&result->resolutions);
|
|
|
|
|
resolution->width = 0;
|
|
|
|
|
resolution->height = 0;
|
|
|
|
|
resolution->refreshRate = 0;
|
|
|
|
|
|
2022-09-23 11:51:16 +02:00
|
|
|
int scanned = fscanf(modeFile, "%ux%u", &resolution->width, &resolution->height);
|
2022-01-02 16:55:26 +01:00
|
|
|
if(scanned < 2 || resolution->width == 0 || resolution->height == 0)
|
|
|
|
|
--result->resolutions.length;
|
|
|
|
|
|
|
|
|
|
fclose(modeFile);
|
|
|
|
|
ffStrbufSubstrBefore(&drmDir, drmDirLength);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
|
ffStrbufDestroy(&drmDir);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 15:42:37 +02:00
|
|
|
void ffConnectDisplayServerImpl(FFDisplayServerResult* ds, const FFinstance* instance)
|
2022-01-02 16:55:26 +01:00
|
|
|
{
|
2022-09-06 15:42:37 +02:00
|
|
|
ffStrbufInit(&ds->wmProcessName);
|
|
|
|
|
ffStrbufInit(&ds->wmPrettyName);
|
|
|
|
|
ffStrbufInit(&ds->wmProtocolName);
|
|
|
|
|
ffStrbufInit(&ds->deProcessName);
|
|
|
|
|
ffStrbufInit(&ds->dePrettyName);
|
|
|
|
|
ffStrbufInit(&ds->deVersion);
|
|
|
|
|
ffListInitA(&ds->resolutions, sizeof(FFResolutionResult), 4);
|
2022-01-02 16:55:26 +01:00
|
|
|
|
|
|
|
|
//We try wayland as our prefered display server, as it supports the most features.
|
|
|
|
|
//This method can't detect the name of our WM / DE
|
2022-09-06 15:42:37 +02:00
|
|
|
ffdsConnectWayland(instance, ds);
|
2022-01-02 16:55:26 +01:00
|
|
|
|
|
|
|
|
//Try the x11 libs, from most feature rich to least.
|
|
|
|
|
//We use the resolution list to detect if a connection is needed.
|
|
|
|
|
//They respect wmProtocolName, and only detect resolution if it is set.
|
|
|
|
|
|
2022-09-06 15:42:37 +02:00
|
|
|
if(ds->resolutions.length == 0)
|
|
|
|
|
ffdsConnectXcbRandr(instance, ds);
|
2022-01-05 14:40:57 +01:00
|
|
|
|
2022-09-06 15:42:37 +02:00
|
|
|
if(ds->resolutions.length == 0)
|
|
|
|
|
ffdsConnectXrandr(instance, ds);
|
2022-01-02 16:55:26 +01:00
|
|
|
|
2022-09-06 15:42:37 +02:00
|
|
|
if(ds->resolutions.length == 0)
|
|
|
|
|
ffdsConnectXcb(instance, ds);
|
2022-01-05 14:40:57 +01:00
|
|
|
|
2022-09-06 15:42:37 +02:00
|
|
|
if(ds->resolutions.length == 0)
|
|
|
|
|
ffdsConnectXlib(instance, ds);
|
2022-01-02 16:55:26 +01:00
|
|
|
|
|
|
|
|
//This resolution detection method is display server independent.
|
|
|
|
|
//Use it if all connections failed
|
2022-09-06 15:42:37 +02:00
|
|
|
if(ds->resolutions.length == 0)
|
|
|
|
|
parseDRM(ds);
|
2022-01-02 16:55:26 +01:00
|
|
|
|
|
|
|
|
//This fills in missing information about WM / DE by using env vars and iterating processes
|
2022-09-06 15:42:37 +02:00
|
|
|
ffdsDetectWMDE(instance, ds);
|
2023-01-08 20:46:22 +08:00
|
|
|
|
|
|
|
|
parseBacklight(ds);
|
2022-01-02 16:55:26 +01:00
|
|
|
}
|