3

目的のモニターの基になる dpi に応じて、アプリケーション ウィンドウがあるモニターから別のモニターに移動するときに、フォント サイズを変更したいと考えています。

xrandr、xdpyinfo、xlib で遊んだ。ソースコードを見ましたが、ウィンドウ(ウィンドウID)が配置されているモニターを関連付ける方法が見つかりませんでした。

Qt には QDesktopWidget があり、これは physicalDpiX/Y を提供しますが、プライマリ モニターにのみ (そう思われます)。

xrandr.h には mm_width と mm_height を提供する XRROutputInfo が含まれていますが、どうすればウィンドウ ID に接続できますか?

この質問は注目を集めたので、私の研究を共有したいと思います。私は完璧な解決策を見つけていません。それは不可能のようです。

しかし、次のコード スニップを試してみると、おそらく役に立ちます。アイデアは、ウィンドウの位置を比較することによって、基になるディスプレイを計算することです。位置が最初の画面の解像度よりも大きい場合は、2 番目のモニターである必要があります。かなり簡単です。

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

// compile: g++ screen_dimension.cpp -lX11 -lXrandr

int main()
{
  int wid = atoi( getenv( "WINDOWID" ) );

  printf("window id: %i\n", wid);

  Display * dpy = XOpenDisplay(NULL);
  int screen  = DefaultScreen(dpy);
  Window root = DefaultRootWindow(dpy);
  
  XRRScreenResources * res = XRRGetScreenResourcesCurrent(dpy, root);
  
  XRROutputInfo * output_info;

  for (int i = 0; i < res->noutput; i++)
  {
      output_info = XRRGetOutputInfo (dpy, res, res->outputs[i]);
 
      if( output_info->connection ) continue; // No connection no crtcs
          printf(" (%lu %lu) mm Name: %s connection: %i ncrtc: %i \n",  output_info->mm_width
            , output_info->mm_height
            , output_info->name
            , output_info->connection
            , output_info->ncrtc
          );
  }
 
   printf("crtcs:\n");
  for( int j = 0; j < output_info->ncrtc; j++ ) {
    XRRCrtcInfo * crtc_info = XRRGetCrtcInfo( dpy, res, res->crtcs[ j ] );
    if( not crtc_info->noutput ) continue;
    printf("%i w: %5i   h: %5i     x: %5i    y: %i\n", j
    , crtc_info->width
    , crtc_info->height
    , crtc_info->x
    , crtc_info->y
    );
  }
}

実際には、画面に関するリソースを照会する関数が 2 つあります。XRRGetScreenResourcesCurrent と XRRGetScreenResources です。最初のものはキャッシュされた値を返しますが、後者はポーリングを導入する可能性があるサーバーに問い合わせます。説明 (RRGetScreenResources を検索): https://www.x.org/releases/X11R7.6/doc/randrproto/randrproto.txt

誰かがタイミングを合わせるのに苦労しました: https://github.com/glfw/glfw/issues/347

XRRGetScreenResourcesCurrent: 通常は 20 から 100 us です。h XRRGetScreenResources: 通常は 13600 ~ 13700 us です。

4

2 に答える 2