Files
fastfetch/src/modules/resolution.c
T

77 lines
2.1 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"
short getCurrentRate(Display* display)
{
void* xrandr = dlopen("libXrandr.so", RTLD_LAZY);
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
void ffPrintResolution(FFstate* state)
{
2021-02-22 16:52:51 +01:00
if(ffPrintCachedValue(state, "Resolution"))
return;
2021-02-18 22:25:36 +01:00
2021-02-27 14:40:03 +01:00
void* x11 = dlopen("libX11.so", RTLD_LAZY);
if(x11 == NULL)
2021-02-27 14:46:31 +01:00
{
ffPrintError(state, "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
{
ffPrintError(state, "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
{
ffPrintError(state, "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);
short currentRate = getCurrentRate(display);
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
if(currentRate == 0)
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-22 20:53:12 +01:00
ffPrintAndSaveCachedValue(state, "Resolution", resolution);
2021-02-18 22:25:36 +01:00
}