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

标签 c++ linux frame-rate xorg

在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/

相关文章:

c++ - 三元运算符使用时的奇怪错误

Android 前置摄像头正在录制 7.5 和 8.0 之间的视频 - setVideoFrameRate 不起作用 - Samsung galaxy s2

JAVA 2D 游戏 Linux 糟糕的 FameRate

c++ - 使用 auto 推导的 lambda 成员函数模板

c++ - 使用构造函数打印特定数字时遇到问题

c++ - 在 C++ 中使用临时对象作为默认参数是否安全?

java - FPS控制游戏速度

c - 警告 : passing argument 1 of ‘uart1_sendchar’ makes pointer from integer without a cast [enabled by default]

c++ - 编译程序 linux 32 位 vs 64 位

linux - Ubuntu 中的 Buildozer 启动问题为 'command not found'