2012-12-04 25 views
9

下面是一個例子調用xrandr:xrandr相關,C編程

 
$ xrandr --output LVDS --mode 1680x1050 --pos 0x0 --rotate normal --output S-video --off --output DVI-0 --mode 1024x768 --pos 1680x104 --rotate normal 

回想一下這一呼籲有着成功的系統;有兩個屏幕(LVDS和DVI-0)以不同的分辨率工作。 DVI-0位於右側,位於中間。

如何在C程序中獲取所有這些信息? 我檢查了xrandr源代碼,但是我發現它很難閱讀,並且沒有明顯的方式來查詢--pos值(編輯:它隱藏在明顯的視野中,這要歸功於ernestopheles的答案)。

我知道我可以用XGetWindowProperty問一個_NET_WORKAREA,但據我看到它不能告訴屏幕位置,只是包含它們的理想矩形的大小。

經過對xrandr代碼的一些其他研究之後,此代碼似乎是解決方案的一步。 但我不確定,圍繞2940行的xrandr.c假定crtc_info可能不可用。我仍然想念另一種獲得解決方案和立場的方式。

 

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

    int main() { 
     Display *disp; 
     XRRScreenResources *screen; 
     XRROutputInfo *info; 
     XRRCrtcInfo *crtc_info; 
     int iscres; 
     int icrtc; 

     disp = XOpenDisplay(0); 
     screen = XRRGetScreenResources (disp, DefaultRootWindow(disp)); 
     for (iscres = screen->noutput; iscres > 0;) { 
      --iscres; 

      info = XRRGetOutputInfo (disp, screen, screen->outputs[iscres]); 
      if (info->connection == RR_Connected) { 
       for (icrtc = info->ncrtc; icrtc > 0;) { 
        --icrtc; 

        crtc_info = XRRGetCrtcInfo (disp, screen, screen->crtcs[icrtc]); 
        fprintf(stderr, "==> %dx%d+%dx%d\n", crtc_info->x, crtc_info->y, crtc_info->width, crtc_info->height); 

        XRRFreeCrtcInfo(crtc_info); 
       } 
      } 
      XRRFreeOutputInfo (info); 
     } 
     XRRFreeScreenResources(screen); 

     return 0; 
    } 

+0

您好,如果您確信今天你的代碼,我想知道?或者如果你找到更好的方法? – yatg

+0

我想在xrandr不可用的情況下使用這個和xinerma的combinsation:http://stackoverflow.com/a/836376/5062337 – yatg

回答

1

我不確定是否正確理解問題。假設,你要讀出的X服務器的當前狀態的參數,請使用以下命令:

xrandr -q
,並解析其輸出:

LVDS connected 1680x1050+0+0 (normal left inverted right x axis y axis) 123mm x 123mm 
[...] 

的第一個屏幕和

TV_SVIDEO connected 1024x768+1680x104 (normal left inverted right x axis y axis) 123mm x 123mm 
[...] 

爲第二。運行命令和解析可以用C語言編寫的程序中完成

+0

感謝您的回答,我注意到位置值只是分辨率附近。至少我可以檢查xrandr代碼是如何確定它的。 –

5

你可以這樣做讓每個屏幕分辨率:

Display *dpy; 
XRRScreenResources *screen; 
XRRCrtcInfo *crtc_info; 

dpy = XOpenDisplay(":0"); 
screen = XRRGetScreenResources (dpy, DefaultRootWindow(dpy)); 
//0 to get the first monitor 
crtc_info = XRRGetCrtcInfo (dpy, screen, screen->crtcs[0]);  

crtc_info->width之後將包含顯示器和crtc_info->x的寬度x位置。

不要忘了包括:

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

與-lX11 -lXrandr編譯鏈接庫