2013-07-22 35 views
4

的刷新率在Windows中,WINAPI規定報告有關監控信息的功能:C++的Linux:獲取監視器

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

EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm); 

int FPS = dm.dmDisplayFrequency; 

什麼是這個Linux上的相同呢? Linux手冊頁指引我使用allegro庫函數,但不僅我沒有使用allegro,而且函數來自所述庫的過時版本,並且據報道僅適用於Windows。

+0

sudo ddcprobe | grep monitorrange – Stefan

+0

那麼基本上execlp grep?這是否適用於所有發行版,或者至少是大多數發行版?據我所知,大部分可用的發行版都是 – NmdMystery

+0

。如果你在debian基礎上嘗試:sudo apt-get install xresprobe – Stefan

回答

3

使用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 
+0

如果您有兩臺帶擴展桌面的顯示器,該怎麼辦?在我的情況下,它只打印我的筆記本電腦刷新率,而不是我的外部顯示器。 – user3111525