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
22 #include "wine/port.h"
27 #ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
28 #include <X11/extensions/Xinerama.h>
30 #include "wine/library.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
36 static MONITORINFOEXW default_monitor
=
38 sizeof(default_monitor
), /* cbSize */
39 { 0, 0, 0, 0 }, /* rcMonitor */
40 { 0, 0, 0, 0 }, /* rcWork */
41 MONITORINFOF_PRIMARY
, /* dwFlags */
42 { '\\','\\','.','\\','D','I','S','P','L','A','Y','1',0 } /* szDevice */
45 static MONITORINFOEXW
*monitors
;
46 static int nb_monitors
;
48 static inline MONITORINFOEXW
*get_primary(void)
50 /* default to 0 if specified primary is invalid */
51 int idx
= primary_monitor
;
52 if (idx
>= nb_monitors
) idx
= 0;
53 return &monitors
[idx
];
56 static inline HMONITOR
index_to_monitor( int index
)
58 return (HMONITOR
)(UINT_PTR
)(index
+ 1);
61 static inline int monitor_to_index( HMONITOR handle
)
63 UINT_PTR index
= (UINT_PTR
)handle
;
64 if (index
< 1 || index
> nb_monitors
) return -1;
68 static void query_work_area( RECT
*rc_work
)
72 unsigned long count
, remaining
;
75 if (!XGetWindowProperty( gdi_display
, DefaultRootWindow(gdi_display
), x11drv_atom(_NET_WORKAREA
), 0,
76 ~0, False
, XA_CARDINAL
, &type
, &format
, &count
,
77 &remaining
, (unsigned char **)&work_area
))
79 if (type
== XA_CARDINAL
&& format
== 32 && count
>= 4)
81 SetRect( rc_work
, work_area
[0], work_area
[1],
82 work_area
[0] + work_area
[2], work_area
[1] + work_area
[3] );
88 #ifdef SONAME_LIBXINERAMA
90 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
92 MAKE_FUNCPTR(XineramaQueryExtension
);
93 MAKE_FUNCPTR(XineramaQueryScreens
);
95 static void load_xinerama(void)
99 if (!(handle
= wine_dlopen(SONAME_LIBXINERAMA
, RTLD_NOW
, NULL
, 0)))
101 WARN( "failed to open %s\n", SONAME_LIBXINERAMA
);
104 pXineramaQueryExtension
= wine_dlsym( handle
, "XineramaQueryExtension", NULL
, 0 );
105 if (!pXineramaQueryExtension
) WARN( "XineramaQueryScreens not found\n" );
106 pXineramaQueryScreens
= wine_dlsym( handle
, "XineramaQueryScreens", NULL
, 0 );
107 if (!pXineramaQueryScreens
) WARN( "XineramaQueryScreens not found\n" );
110 static int query_screens(void)
112 int i
, count
, event_base
, error_base
;
113 XineramaScreenInfo
*screens
;
114 RECT rc_work
= {0, 0, 0, 0};
116 if (!monitors
) /* first time around */
119 query_work_area( &rc_work
);
121 if (!pXineramaQueryExtension
|| !pXineramaQueryScreens
||
122 !pXineramaQueryExtension( gdi_display
, &event_base
, &error_base
) ||
123 !(screens
= pXineramaQueryScreens( gdi_display
, &count
))) return 0;
125 if (monitors
!= &default_monitor
) HeapFree( GetProcessHeap(), 0, monitors
);
126 if ((monitors
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*monitors
) )))
129 for (i
= 0; i
< nb_monitors
; i
++)
131 monitors
[i
].cbSize
= sizeof( monitors
[i
] );
132 monitors
[i
].rcMonitor
.left
= screens
[i
].x_org
;
133 monitors
[i
].rcMonitor
.top
= screens
[i
].y_org
;
134 monitors
[i
].rcMonitor
.right
= screens
[i
].x_org
+ screens
[i
].width
;
135 monitors
[i
].rcMonitor
.bottom
= screens
[i
].y_org
+ screens
[i
].height
;
136 monitors
[i
].dwFlags
= 0;
137 if (!IntersectRect( &monitors
[i
].rcWork
, &rc_work
, &monitors
[i
].rcMonitor
))
138 monitors
[i
].rcWork
= monitors
[i
].rcMonitor
;
139 /* FIXME: using the same device name for all monitors for now */
140 lstrcpyW( monitors
[i
].szDevice
, default_monitor
.szDevice
);
143 get_primary()->dwFlags
|= MONITORINFOF_PRIMARY
;
151 #else /* SONAME_LIBXINERAMA */
153 static inline int query_screens(void)
158 #endif /* SONAME_LIBXINERAMA */
160 void xinerama_init( unsigned int width
, unsigned int height
)
162 MONITORINFOEXW
*primary
;
166 SetRect( &rect
, 0, 0, width
, height
);
168 if (root_window
!= DefaultRootWindow( gdi_display
) || !query_screens())
170 default_monitor
.rcWork
= default_monitor
.rcMonitor
= rect
;
171 if (root_window
== DefaultRootWindow( gdi_display
))
172 query_work_area( &default_monitor
.rcWork
);
174 monitors
= &default_monitor
;
177 primary
= get_primary();
178 SetRectEmpty( &virtual_screen_rect
);
180 /* coordinates (0,0) have to point to the primary monitor origin */
181 OffsetRect( &rect
, -primary
->rcMonitor
.left
, -primary
->rcMonitor
.top
);
182 for (i
= 0; i
< nb_monitors
; i
++)
184 OffsetRect( &monitors
[i
].rcMonitor
, rect
.left
, rect
.top
);
185 OffsetRect( &monitors
[i
].rcWork
, rect
.left
, rect
.top
);
186 UnionRect( &virtual_screen_rect
, &virtual_screen_rect
, &monitors
[i
].rcMonitor
);
187 TRACE( "monitor %p: %s work %s%s\n",
188 index_to_monitor(i
), wine_dbgstr_rect(&monitors
[i
].rcMonitor
),
189 wine_dbgstr_rect(&monitors
[i
].rcWork
),
190 (monitors
[i
].dwFlags
& MONITORINFOF_PRIMARY
) ? " (primary)" : "" );
193 screen_width
= primary
->rcMonitor
.right
- primary
->rcMonitor
.left
;
194 screen_height
= primary
->rcMonitor
.bottom
- primary
->rcMonitor
.top
;
195 TRACE( "virtual size: %s primary size: %dx%d\n",
196 wine_dbgstr_rect(&virtual_screen_rect
), screen_width
, screen_height
);
200 /***********************************************************************
201 * X11DRV_GetMonitorInfo (X11DRV.@)
203 BOOL CDECL
X11DRV_GetMonitorInfo( HMONITOR handle
, LPMONITORINFO info
)
205 int i
= monitor_to_index( handle
);
209 SetLastError( ERROR_INVALID_HANDLE
);
212 info
->rcMonitor
= monitors
[i
].rcMonitor
;
213 info
->rcWork
= monitors
[i
].rcWork
;
214 info
->dwFlags
= monitors
[i
].dwFlags
;
215 if (info
->cbSize
>= sizeof(MONITORINFOEXW
))
216 lstrcpyW( ((MONITORINFOEXW
*)info
)->szDevice
, monitors
[i
].szDevice
);
221 /***********************************************************************
222 * X11DRV_EnumDisplayMonitors (X11DRV.@)
224 BOOL CDECL
X11DRV_EnumDisplayMonitors( HDC hdc
, LPRECT rect
, MONITORENUMPROC proc
, LPARAM lp
)
233 if (!GetDCOrgEx( hdc
, &origin
)) return FALSE
;
234 if (GetClipBox( hdc
, &limit
) == ERROR
) return FALSE
;
236 if (rect
&& !IntersectRect( &limit
, &limit
, rect
)) return TRUE
;
238 for (i
= 0; i
< nb_monitors
; i
++)
240 RECT monrect
= monitors
[i
].rcMonitor
;
241 OffsetRect( &monrect
, -origin
.x
, -origin
.y
);
242 if (IntersectRect( &monrect
, &monrect
, &limit
))
243 if (!proc( index_to_monitor(i
), hdc
, &monrect
, lp
))
249 for (i
= 0; i
< nb_monitors
; i
++)
252 if (!rect
|| IntersectRect( &unused
, &monitors
[i
].rcMonitor
, rect
))
253 if (!proc( index_to_monitor(i
), 0, &monitors
[i
].rcMonitor
, lp
))