Files
fastfetch/src/modules/resolution.c
T

87 lines
2.6 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-11 22:15:48 +01:00
static short getCurrentRate(FFinstance* instance, Display* display)
2021-02-27 14:40:03 +01:00
{
2021-03-11 22:15:48 +01:00
void* xrandr;
if(instance->config.resolutionLibXrandr[0] != '\0')
xrandr = dlopen(instance->config.resolutionLibXrandr, RTLD_LAZY);
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);
return currentRate;
}
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;
if(instance->config.resolutionLibX11[0] != '\0')
x11 = dlopen(instance->config.resolutionLibX11, RTLD_LAZY);
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-11 22:15:48 +01:00
short currentRate = instance->config.resolutionShowRefreshRate ? getCurrentRate(instance, display) : 0;
2021-02-27 14:40:03 +01:00
dlclose(x11);
2021-02-18 22:25:36 +01:00
2021-02-22 16:52:51 +01:00
char resolution[1024];
2021-02-27 14:40:03 +01:00
2021-03-11 22:15:48 +01:00
if(instance->config.resolutionFormat[0] != '\0')
sprintf(resolution, instance->config.resolutionFormat, screen->width, screen->height, currentRate);
else if(currentRate == 0)
2021-02-27 14:40:03 +01:00
sprintf(resolution, "%ix%i", screen->width, screen->height);
else
sprintf(resolution, "%ix%i @ %dHz", screen->width, screen->height, currentRate);
2021-02-22 16:52:51 +01:00
2021-02-27 18:30:05 +01:00
ffPrintAndSaveCachedValue(instance, "Resolution", resolution);
2021-02-18 22:25:36 +01:00
}