d3d9/tests: Use a separate device for test_vshader_input().
[wine/wine-gecko.git] / dlls / winex11.drv / xinerama.c
blob7e28726f688cf208563fb37525b28f26d7bf8783
1 /*
2 * Xinerama support
4 * Copyright 2006 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <X11/Xlib.h>
27 #ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
28 #include <X11/extensions/Xinerama.h>
29 #endif
30 #include "wine/library.h"
31 #include "x11drv.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
36 static RECT virtual_screen_rect;
38 static MONITORINFOEXW default_monitor =
40 sizeof(default_monitor), /* cbSize */
41 { 0, 0, 0, 0 }, /* rcMonitor */
42 { 0, 0, 0, 0 }, /* rcWork */
43 MONITORINFOF_PRIMARY, /* dwFlags */
44 { '\\','\\','.','\\','D','I','S','P','L','A','Y','1',0 } /* szDevice */
47 static MONITORINFOEXW *monitors;
48 static int nb_monitors;
50 static inline MONITORINFOEXW *get_primary(void)
52 /* default to 0 if specified primary is invalid */
53 int idx = primary_monitor;
54 if (idx >= nb_monitors) idx = 0;
55 return &monitors[idx];
58 static inline HMONITOR index_to_monitor( int index )
60 return (HMONITOR)(UINT_PTR)(index + 1);
63 static inline int monitor_to_index( HMONITOR handle )
65 UINT_PTR index = (UINT_PTR)handle;
66 if (index < 1 || index > nb_monitors) return -1;
67 return index - 1;
70 static void query_work_area( RECT *rc_work )
72 Atom type;
73 int format;
74 unsigned long count, remaining;
75 long *work_area;
77 if (!XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display), x11drv_atom(_NET_WORKAREA), 0,
78 ~0, False, XA_CARDINAL, &type, &format, &count,
79 &remaining, (unsigned char **)&work_area ))
81 if (type == XA_CARDINAL && format == 32 && count >= 4)
83 SetRect( rc_work, work_area[0], work_area[1],
84 work_area[0] + work_area[2], work_area[1] + work_area[3] );
86 XFree( work_area );
90 #ifdef SONAME_LIBXINERAMA
92 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
94 MAKE_FUNCPTR(XineramaQueryExtension);
95 MAKE_FUNCPTR(XineramaQueryScreens);
97 static void load_xinerama(void)
99 void *handle;
101 if (!(handle = wine_dlopen(SONAME_LIBXINERAMA, RTLD_NOW, NULL, 0)))
103 WARN( "failed to open %s\n", SONAME_LIBXINERAMA );
104 return;
106 pXineramaQueryExtension = wine_dlsym( handle, "XineramaQueryExtension", NULL, 0 );
107 if (!pXineramaQueryExtension) WARN( "XineramaQueryScreens not found\n" );
108 pXineramaQueryScreens = wine_dlsym( handle, "XineramaQueryScreens", NULL, 0 );
109 if (!pXineramaQueryScreens) WARN( "XineramaQueryScreens not found\n" );
112 static int query_screens(void)
114 int i, count, event_base, error_base;
115 XineramaScreenInfo *screens;
116 RECT rc_work = {0, 0, 0, 0};
118 if (!monitors) /* first time around */
119 load_xinerama();
121 query_work_area( &rc_work );
123 if (!pXineramaQueryExtension || !pXineramaQueryScreens ||
124 !pXineramaQueryExtension( gdi_display, &event_base, &error_base ) ||
125 !(screens = pXineramaQueryScreens( gdi_display, &count ))) return 0;
127 if (monitors != &default_monitor) HeapFree( GetProcessHeap(), 0, monitors );
128 if ((monitors = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*monitors) )))
130 nb_monitors = count;
131 for (i = 0; i < nb_monitors; i++)
133 monitors[i].cbSize = sizeof( monitors[i] );
134 monitors[i].rcMonitor.left = screens[i].x_org;
135 monitors[i].rcMonitor.top = screens[i].y_org;
136 monitors[i].rcMonitor.right = screens[i].x_org + screens[i].width;
137 monitors[i].rcMonitor.bottom = screens[i].y_org + screens[i].height;
138 monitors[i].dwFlags = 0;
139 if (!IntersectRect( &monitors[i].rcWork, &rc_work, &monitors[i].rcMonitor ))
140 monitors[i].rcWork = monitors[i].rcMonitor;
141 /* FIXME: using the same device name for all monitors for now */
142 lstrcpyW( monitors[i].szDevice, default_monitor.szDevice );
145 get_primary()->dwFlags |= MONITORINFOF_PRIMARY;
147 else count = 0;
149 XFree( screens );
150 return count;
153 #else /* SONAME_LIBXINERAMA */
155 static inline int query_screens(void)
157 return 0;
160 #endif /* SONAME_LIBXINERAMA */
162 POINT virtual_screen_to_root( INT x, INT y )
164 POINT pt;
165 pt.x = x - virtual_screen_rect.left;
166 pt.y = y - virtual_screen_rect.top;
167 return pt;
170 POINT root_to_virtual_screen( INT x, INT y )
172 POINT pt;
173 pt.x = x + virtual_screen_rect.left;
174 pt.y = y + virtual_screen_rect.top;
175 return pt;
178 RECT get_virtual_screen_rect(void)
180 return virtual_screen_rect;
183 RECT get_primary_monitor_rect(void)
185 return get_primary()->rcMonitor;
188 void xinerama_init( unsigned int width, unsigned int height )
190 MONITORINFOEXW *primary;
191 int i;
192 RECT rect;
194 SetRect( &rect, 0, 0, width, height );
196 if (root_window != DefaultRootWindow( gdi_display ) || !query_screens())
198 default_monitor.rcWork = default_monitor.rcMonitor = rect;
199 if (root_window == DefaultRootWindow( gdi_display ))
200 query_work_area( &default_monitor.rcWork );
201 nb_monitors = 1;
202 monitors = &default_monitor;
205 primary = get_primary();
206 SetRectEmpty( &virtual_screen_rect );
208 /* coordinates (0,0) have to point to the primary monitor origin */
209 OffsetRect( &rect, -primary->rcMonitor.left, -primary->rcMonitor.top );
210 for (i = 0; i < nb_monitors; i++)
212 OffsetRect( &monitors[i].rcMonitor, rect.left, rect.top );
213 OffsetRect( &monitors[i].rcWork, rect.left, rect.top );
214 UnionRect( &virtual_screen_rect, &virtual_screen_rect, &monitors[i].rcMonitor );
215 TRACE( "monitor %p: %s work %s%s\n",
216 index_to_monitor(i), wine_dbgstr_rect(&monitors[i].rcMonitor),
217 wine_dbgstr_rect(&monitors[i].rcWork),
218 (monitors[i].dwFlags & MONITORINFOF_PRIMARY) ? " (primary)" : "" );
221 TRACE( "virtual size: %s primary: %s\n",
222 wine_dbgstr_rect(&virtual_screen_rect), wine_dbgstr_rect(&primary->rcMonitor) );
226 /***********************************************************************
227 * X11DRV_GetMonitorInfo (X11DRV.@)
229 BOOL CDECL X11DRV_GetMonitorInfo( HMONITOR handle, LPMONITORINFO info )
231 int i = monitor_to_index( handle );
233 if (i == -1)
235 SetLastError( ERROR_INVALID_HANDLE );
236 return FALSE;
238 info->rcMonitor = monitors[i].rcMonitor;
239 info->rcWork = monitors[i].rcWork;
240 info->dwFlags = monitors[i].dwFlags;
241 if (info->cbSize >= sizeof(MONITORINFOEXW))
242 lstrcpyW( ((MONITORINFOEXW *)info)->szDevice, monitors[i].szDevice );
243 return TRUE;
247 /***********************************************************************
248 * X11DRV_EnumDisplayMonitors (X11DRV.@)
250 BOOL CDECL X11DRV_EnumDisplayMonitors( HDC hdc, LPRECT rect, MONITORENUMPROC proc, LPARAM lp )
252 int i;
254 if (hdc)
256 POINT origin;
257 RECT limit;
259 if (!GetDCOrgEx( hdc, &origin )) return FALSE;
260 if (GetClipBox( hdc, &limit ) == ERROR) return FALSE;
262 if (rect && !IntersectRect( &limit, &limit, rect )) return TRUE;
264 for (i = 0; i < nb_monitors; i++)
266 RECT monrect = monitors[i].rcMonitor;
267 OffsetRect( &monrect, -origin.x, -origin.y );
268 if (IntersectRect( &monrect, &monrect, &limit ))
269 if (!proc( index_to_monitor(i), hdc, &monrect, lp ))
270 return FALSE;
273 else
275 for (i = 0; i < nb_monitors; i++)
277 RECT unused;
278 if (!rect || IntersectRect( &unused, &monitors[i].rcMonitor, rect ))
279 if (!proc( index_to_monitor(i), 0, &monitors[i].rcMonitor, lp ))
280 return FALSE;
283 return TRUE;