gpt4 book ai didi

C++ Linux : Get the refresh rate of a monitor

转载 作者:IT王子 更新时间:2023-10-29 01:27:05 25 4
gpt4 key购买 nike

在Windows中,winapi提供了一个上报监视器信息的函数:

DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);

EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);

int FPS = dm.dmDisplayFrequency;

这在 Linux 上相当于什么? Linux 手册页将我引导至 allegro 库函数,但不仅我没有使用 allegro,而且该函数来自该库的一个非常过时的版本,据报道仅适用于 Windows。

最佳答案

使用 XRandr API (man 3 Xrandr)。示例见此处:

您还可以查看 xrandr(1) 的代码。


Edit1:为了后代:

稍微调整了示例代码,使其更像是一个演示:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

int main()
{
int num_sizes;
Rotation current_rotation;

Display *dpy = XOpenDisplay(NULL);
Window root = RootWindow(dpy, 0);
XRRScreenSize *xrrs = XRRSizes(dpy, 0, &num_sizes);
//
// GET CURRENT RESOLUTION AND FREQUENCY
//
XRRScreenConfiguration *conf = XRRGetScreenInfo(dpy, root);
short current_rate = XRRConfigCurrentRate(conf);
SizeID current_size_id = XRRConfigCurrentConfiguration(conf, &current_rotation);

int current_width = xrrs[current_size_id].width;
int current_height = xrrs[current_size_id].height;
std::cout << "current_rate = " << current_rate << std::endl;
std::cout << "current_width = " << current_width << std::endl;
std::cout << "current_height = " << current_height << std::endl;

XCloseDisplay(dpy);
}

编译:

g++ 17797636.cpp -o 17797636 -lX11 -lXrandr

输出:

$ ./17797636 
current_rate = 50
current_width = 1920
current_height = 1080

关于C++ Linux : Get the refresh rate of a monitor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17797636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com