2017-09-14 97 views
0

是否有可能檢查顯示器(顯示器)是否正常工作並將該數據導入代碼?我認爲有一些命令行技巧或設備可能會泄露關於它的信息。使用Linux。不斷檢查顯示器是否在Linux上工作

+0

像'xrandr | grep connected'? – rodrigo

+0

非常感謝!這就是訣竅:) – Levi

+0

很高興幫助。我已經添加了更長的答案,所以如果你喜歡,你可以接受它。 ;-) – rodrigo

回答

0

您可以使用X11擴展XRandRX分辨率和旋轉或類似的東西)。

您可以通過命令xrandr查看輸出顯示的狀態。在我的電腦中,例如:

$ xrandr | grep connected 
DVI-I-0 disconnected (normal left inverted right x axis y axis) 
DVI-I-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 531mm x 299mm 
.... 

當然,輸出的名稱是特定於設備的。

如果您想訪問C程序中的數據,Xrandr擴展程序很容易編程。此示例代碼將打印所有的輸出(檢查省略錯誤)連接狀態:

#include <X11/Xlib.h> 
#include <X11/extensions/Xrandr.h> 
#include <stdio.h> 

int main() 
{ 
    Display *dsp = XOpenDisplay(NULL); 
    Window root = XRootWindow(dsp, 0); 
    XRRScreenResources *sres = XRRGetScreenResources(dsp, root); 
    printf("N outputs %d\n", sres->noutput); 
    for (int i = 0; i < sres->noutput; ++i) 
    { 
     XRROutputInfo *info = XRRGetOutputInfo(dsp, sres, sres->outputs[i]); 
     printf(" %d: '%s' %s\n", i, info->name, info->connection == RR_Connected ? "connected" : ""); 
     XRRFreeOutputInfo(info); 

    } 
    XRRFreeScreenResources(sres); 
    XCloseDisplay(dsp); 
    return 0; 
} 

如果你想要得到的,你可以使用XRROutputChangeNotifyEvent X事件的變化實時通知,但是這將是一個有點複雜:你將需要一個事件循環或使用一個小工具工具包,並鉤住X事件處理程序......