Files
fastfetch/src/modules/resolution.c
T

101 lines
2.9 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-02-27 14:40:03 +01:00
#include "X11/extensions/Xrandr.h"
#include "dlfcn.h"
2021-03-19 20:57:07 +01:00
static int getCurrentRate(FFinstance* instance, Display* display)
2021-02-27 14:40:03 +01:00
{
2021-03-11 22:15:48 +01:00
void* xrandr;
2021-03-19 20:57:07 +01:00
if(!ffStrbufIsEmpty(&instance->config.libXrandr))
xrandr = dlopen(instance->config.libXrandr.chars, RTLD_LAZY);
2021-03-11 22:15:48 +01:00
else
xrandr = dlopen("libXrandr.so", RTLD_LAZY);
2021-02-27 14:40:03 +01:00
if(xrandr == NULL)
return 0;
Window root = RootWindow(display, 0);
XRRScreenConfiguration*(*ffXRRGetScreenInfo)(Display*, Window) = dlsym(xrandr, "XRRGetScreenInfo");
if(ffXRRGetScreenInfo == NULL)
return 0;
short(*ffXRRConfigCurrentRate)(XRRScreenConfiguration*) = dlsym(xrandr, "XRRConfigCurrentRate");
if(ffXRRConfigCurrentRate == NULL)
return 0;
XRRScreenConfiguration* xrrscreenconf = ffXRRGetScreenInfo(display, root);
if(xrrscreenconf == NULL)
return 0;
short currentRate = ffXRRConfigCurrentRate(xrrscreenconf);
void(*ffXRRFreeScreenConfigInfo)(XRRScreenConfiguration*) = dlsym(xrandr, "XRRFreeScreenConfigInfo");
if(ffXRRFreeScreenConfigInfo != NULL)
ffXRRFreeScreenConfigInfo(xrrscreenconf);
dlclose(xrandr);
2021-03-19 20:57:07 +01:00
return (int) currentRate;
2021-02-27 14:40:03 +01:00
}
2021-02-18 22:25:36 +01:00
2021-02-27 18:30:05 +01:00
void ffPrintResolution(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2021-02-27 18:30:05 +01:00
if(ffPrintCachedValue(instance, "Resolution"))
2021-02-22 16:52:51 +01:00
return;
2021-02-18 22:25:36 +01:00
2021-03-11 22:15:48 +01:00
void* x11;
2021-03-19 20:57:07 +01:00
if(!ffStrbufIsEmpty(&instance->config.libX11))
x11 = dlopen(instance->config.libX11.chars, RTLD_LAZY);
2021-03-11 22:15:48 +01:00
else
x11 = dlopen("libX11.so", RTLD_LAZY);
2021-02-27 14:40:03 +01:00
if(x11 == NULL)
2021-02-27 14:46:31 +01:00
{
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "Resolution", "dlopen(\"libX11.so\", RTLD_LAZY) == NULL");
2021-02-27 14:40:03 +01:00
return;
2021-02-27 14:46:31 +01:00
}
2021-02-27 14:40:03 +01:00
Display*(*ffXOpenDisplay)(const char*) = dlsym(x11, "XOpenDisplay");
if(ffXOpenDisplay == NULL)
2021-02-27 14:46:31 +01:00
{
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "Resolution", "dlsym(x11, \"XOpenDisplay\") == NULL");
2021-02-27 14:40:03 +01:00
return;
2021-02-27 14:46:31 +01:00
}
2021-02-27 14:40:03 +01:00
Display* display = ffXOpenDisplay(NULL);
if(display == NULL)
2021-02-27 14:46:31 +01:00
{
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "Resolution", "ffXOpenDisplay(NULL) == NULL");
2021-02-27 14:40:03 +01:00
return;
2021-02-27 14:46:31 +01:00
}
2021-02-27 14:40:03 +01:00
Screen* screen = DefaultScreenOfDisplay(display);
2021-03-19 20:57:07 +01:00
int currentRate = getCurrentRate(instance, display);
2021-02-27 14:40:03 +01:00
dlclose(x11);
2021-02-18 22:25:36 +01:00
2021-03-19 20:57:07 +01:00
ffPrintLogoAndKey(instance, "Resolution");
2021-02-27 14:40:03 +01:00
2021-03-19 20:57:07 +01:00
FFstrbuf resolution;
ffStrbufInit(&resolution);
if(!ffStrbufIsEmpty(&instance->config.resolutionFormat))
{
ffParseFormatString(&resolution, &instance->config.resolutionFormat, 3,
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &screen->width},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &screen->height},
(FFformatarg){FF_FORMAT_ARG_TYPE_INT, &currentRate}
);
}
2021-02-27 14:40:03 +01:00
else
2021-03-19 20:57:07 +01:00
{
ffStrbufAppendF(&resolution, "%ix%i", screen->width, screen->height);
if(currentRate > 0)
ffStrbufAppendF(&resolution, " @ %iHz", currentRate);
}
2021-02-22 16:52:51 +01:00
2021-03-19 20:57:07 +01:00
ffPrintAndSaveCachedValue(instance, "Resolution", resolution.chars);
ffStrbufDestroy(&resolution);
2021-02-18 22:25:36 +01:00
}