LoginSignup
0
0

More than 5 years have passed since last update.

[X.org] 接続されているディスプレイ(モニター)の配置と解像度を一覧表示する

Posted at

前提知識

Xでモニターを特定するために以下の概念があります。

  • display ... Xサーバーを特定するための数字のようなもの? /tmp/.X11-unixの中身を見ればわかる。普通は1つのscreenで構成される。
  • screen ... displayに属する。複数のmonitorで構成される。各ウィンドウはscreenの中を自由に移動できる。(2つのmonitorにまたがってもよい)
  • monitor ... 実際のディスプレイ等の機器と1対1に対応する。

なお、デフォルトのディスプレイはDISPLAY環境変数で指定します。

# display, screenを指定する例
export DISPLAY=localhost:0.0

# 最小の例
export DISPLAY=:0

xrandrコマンドを実行することで、monitorの情報などを表示できます。今回はlibXrandrを用いてC言語からこれらの情報を扱ってみます。

コード

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



int main(void) {

    Display *disp = XOpenDisplay(NULL);
    int screen = DefaultScreen(disp);
    Window root = RootWindow(disp, screen);
    XRRScreenResources *res = XRRGetScreenResources(disp, root);

    printf("Screen %d (%dx%d):\n", screen, DisplayWidth(disp, screen), DisplayHeight(disp, screen));

    info->connection == 0int i, j;
    for (i = 0; i < res->noutput; i++) {
        XRROutputInfo *info = XRRGetOutputInfo(disp, res, res->outputs[i]);
        printf("  %s state:%d\n", info->name, info->connection);
        if (info->crtc) {

            XRRCrtcInfo *cinfo = XRRGetCrtcInfo(disp, res, info->crtc);
            printf("    %d %d %d %d\n", cinfo->width, cinfo->height, cinfo->x, cinfo->y);
        }
    }

    return 0;
}

コンパイル

gcc main.c -lXrandr -lX11

出力の例

Screen 0 (1920x2160):
  eDP1 state:0
    1920 1080 0 1080
  DP1 state:1
  DP2 state:1
  HDMI1 state:1
  HDMI2 state:1
    1920 1080 0 0
  VIRTUAL1 state:1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0