C Linux:获取监视器的刷新率

Windows中,winapi提供了一个报告监视器信息的功能:

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

EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);

int FPS = dm.dmDisplayFrequency;

在Linux上有什么相同的东西? Linux手册页引导我进入一个快板库函数,但不仅我不使用allegro,该函数来自所述库的一个非常过时的版本,据报道只能在Windows上运行.

解决方法:

使用XRandr API(man 3 Xrandr).请看这里的例子:

> http://www.blitzbasic.com/Community/posts.php?topic=86911

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

编辑1:为了后人的缘故:

示例代码略有调整,因此更多的是演示:

#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
上一篇:云主机选择攻略-稳定易用有服务才是好的云主机(符详细攻略)


下一篇:linux – 挂起后在Xorg环境中恢复键盘设置