Getting information about nearest monitor to mouse cursor in X11

我是研究僧i 提交于 2019-12-12 03:44:52

问题


I need to get information such as size and coordinates about nearest monitor to the mouse cursor position in multi-monitor systems. I've done it before in Windows and I want to know how to do it in Linux X11.

Using the code below I can measure the sum of whole screens size but cannot measure each monitor separately.

Screen *screen = DefaultScreenOfDisplay(DisplayHandle);
int xx = screen->width / 2 - Settings::WindowWidth / 2;
int yy = screen->height / 2 - Settings::WindowHeight / 2;

My previous code:

POINT mouse_position;
GetCursorPos(&mouse_position);
HMONITOR hMonitor = MonitorFromPoint(mouse_position, MONITOR_DEFAULTTOPRIMARY);
MONITORINFOEX monitor_info;
memset(&monitor_info, 0, sizeof(MONITORINFOEX));
monitor_info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, &monitor_info);

// CREATE WINDOW IN CENTER OF MONITOR //
int edge = GetSystemMetrics(SM_CXEDGE);
int fixed_frame = GetSystemMetrics(SM_CXFIXEDFRAME);
int monitor_width =  monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
int monitor_height =  monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
int xx = monitor_width / 2 - Settings::WindowWidth / 2;
int yy = monitor_height / 2 - Settings::WindowHeight / 2;
int win_x = xx - edge + monitor_info.rcMonitor.left;
int win_y = yy - fixed_frame + monitor_info.rcMonitor.top;

Thanks


回答1:


If you have two monitors that form a single desktop, use the Xinerama extension. The code below picks the largest screen of the available monitors, but you'll get the idea.

#include <X11/extensions/Xinerama.h>

// By default go fullscreen
m_winWidth = DisplayWidth (m_display, m_screenNo);
m_winHeight = DisplayHeight (m_display, m_screenNo);
// But, with Xinerama, use the largest physical screen
if (XineramaIsActive (m_display))
{
  int m = 0;
  int pixels = 0;

  XineramaScreenInfo *xs = XineramaQueryScreens (m_display, &m);

  if (0 != xs && m > 0)
  {
    for (int i = 0; i < m; i++)
    {
      //printf ("%dx%d, [%d, %d] %d\n", xs[i].width, xs[i].height, xs[i].x_org, xs[i].y_org, xs[i].screen_number);
      if (xs[i].width * xs[i].height > pixels)
      {
        m_xineramaScreen = xs[i].screen_number; // pick screen
        pixels = xs[i].width * xs[i].height;
        m_winWidth = xs[i].width;
        m_winHeight = xs[i].height;
      }
    }

    XFree (xs);
  }
}


来源:https://stackoverflow.com/questions/42873901/getting-information-about-nearest-monitor-to-mouse-cursor-in-x11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!