Don't open device if already open.
[wine] / dlls / x11drv / init.c
1 /*
2  * X11 graphics driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "x11drv.h"
30 #include "x11font.h"
31 #include "ddrawi.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
35
36 Display *gdi_display;  /* display to use for all GDI functions */
37
38 /* a few dynamic device caps */
39 static int log_pixels_x;  /* pixels per logical inch in x direction */
40 static int log_pixels_y;  /* pixels per logical inch in y direction */
41 static int horz_size;     /* horz. size of screen in millimeters */
42 static int vert_size;     /* vert. size of screen in millimeters */
43 static int palette_size;
44 unsigned int text_caps = (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE |
45                           TC_CR_ANY | TC_SA_DOUBLE | TC_SA_INTEGER |
46                           TC_SA_CONTIN | TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE);
47                           /* X11R6 adds TC_SF_X_YINDEP, Xrender adds TC_VA_ABLE */
48
49
50 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
51 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
52
53 static const WCHAR INIFontSection[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
54                                        'W','i','n','e','\\','C','o','n','f','i','g','\\',
55                                        'f','o','n','t','s','\0'};
56 static const WCHAR INIResolution[] = {'R','e','s','o','l','u','t','i','o','n','\0'};
57
58 /******************************************************************************
59  *      get_dpi
60  *
61  * get the dpi from the registry
62  */
63 static DWORD get_dpi( void )
64 {
65     DWORD dpi = 96;
66     HKEY hkey;
67
68     if(RegOpenKeyW(HKEY_LOCAL_MACHINE, INIFontSection, &hkey) == ERROR_SUCCESS)
69     {
70         char buffer[20];
71         DWORD type, count = sizeof(buffer);
72         if(RegQueryValueExW(hkey, INIResolution, 0, &type, buffer, &count) == ERROR_SUCCESS)
73             if(atoi(buffer) != 96)
74                 MESSAGE("Please use the registry key HKEY_CURRENT_CONFIG\\Software\\Fonts\\LogPixels\n"
75                         "to set the screen resolution and remove the \"Resolution\" entry in the config file\n");
76         RegCloseKey(hkey);
77     }
78
79     if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
80     {
81         DWORD type, size, new_dpi;
82
83         size = sizeof(new_dpi);
84         if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
85         {
86             if(type == REG_DWORD && new_dpi != 0)
87                 dpi = new_dpi;
88         }
89         RegCloseKey(hkey);
90     }
91     return dpi;
92 }
93
94 /**********************************************************************
95  *           X11DRV_GDI_Initialize
96  */
97 void X11DRV_GDI_Initialize( Display *display )
98 {
99     gdi_display = display;
100
101     /* Initialize XRender */
102     X11DRV_XRender_Init();
103
104     palette_size = X11DRV_PALETTE_Init();
105
106     X11DRV_BITMAP_Init();
107
108     /* Initialize device caps */
109     log_pixels_x = log_pixels_y = get_dpi();
110     horz_size = MulDiv( screen_width, 254, log_pixels_x * 10 );
111     vert_size = MulDiv( screen_height, 254, log_pixels_y * 10 );
112
113     /* Initialize fonts and text caps */
114     X11DRV_FONT_Init(log_pixels_x, log_pixels_y);
115 }
116
117 /**********************************************************************
118  *           X11DRV_GDI_Finalize
119  */
120 void X11DRV_GDI_Finalize(void)
121 {
122     X11DRV_PALETTE_Cleanup();
123     XCloseDisplay( gdi_display );
124     gdi_display = NULL;
125 }
126
127 /**********************************************************************
128  *           X11DRV_CreateDC
129  */
130 BOOL X11DRV_CreateDC( HDC hdc, X11DRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
131                       LPCWSTR output, const DEVMODEW* initData )
132 {
133     X11DRV_PDEVICE *physDev;
134
135     physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
136     if (!physDev) return FALSE;
137
138     *pdev = physDev;
139     physDev->hdc = hdc;
140
141     if (GetObjectType( hdc ) == OBJ_MEMDC)
142     {
143         if (!BITMAP_stock_phys_bitmap.hbitmap)
144             BITMAP_stock_phys_bitmap.hbitmap = GetCurrentObject( hdc, OBJ_BITMAP );
145         physDev->bitmap    = &BITMAP_stock_phys_bitmap;
146         physDev->drawable  = BITMAP_stock_phys_bitmap.pixmap;
147         physDev->depth     = 1;
148     }
149     else
150     {
151         physDev->bitmap    = NULL;
152         physDev->drawable  = root_window;
153         physDev->depth     = screen_depth;
154     }
155     physDev->region = CreateRectRgn( 0, 0, 0, 0 );
156
157     wine_tsx11_lock();
158     physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
159     XSetGraphicsExposures( gdi_display, physDev->gc, False );
160     XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
161     XFlush( gdi_display );
162     wine_tsx11_unlock();
163     return TRUE;
164 }
165
166
167 /**********************************************************************
168  *           X11DRV_DeleteDC
169  */
170 BOOL X11DRV_DeleteDC( X11DRV_PDEVICE *physDev )
171 {
172     if(physDev->xrender)
173       X11DRV_XRender_DeleteDC( physDev );
174     DeleteObject( physDev->region );
175     wine_tsx11_lock();
176     XFreeGC( gdi_display, physDev->gc );
177     while (physDev->used_visuals-- > 0)
178         XFree(physDev->visuals[physDev->used_visuals]);
179     wine_tsx11_unlock();
180     HeapFree( GetProcessHeap(), 0, physDev );
181     return TRUE;
182 }
183
184
185 /***********************************************************************
186  *           GetDeviceCaps    (X11DRV.@)
187  */
188 INT X11DRV_GetDeviceCaps( X11DRV_PDEVICE *physDev, INT cap )
189 {
190     switch(cap)
191     {
192     case DRIVERVERSION:
193         return 0x300;
194     case TECHNOLOGY:
195         return DT_RASDISPLAY;
196     case HORZSIZE:
197         return horz_size;
198     case VERTSIZE:
199         return vert_size;
200     case HORZRES:
201         return screen_width;
202     case VERTRES:
203         return screen_height;
204     case BITSPIXEL:
205         return screen_depth;
206     case PLANES:
207         return 1;
208     case NUMBRUSHES:
209         return -1;
210     case NUMPENS:
211         return -1;
212     case NUMMARKERS:
213         return 0;
214     case NUMFONTS:
215         return 0;
216     case NUMCOLORS:
217         /* MSDN: Number of entries in the device's color table, if the device has
218          * a color depth of no more than 8 bits per pixel.For devices with greater
219          * color depths, -1 is returned. */
220         return (screen_depth > 8) ? -1 : (1 << screen_depth);
221     case PDEVICESIZE:
222         return sizeof(X11DRV_PDEVICE);
223     case CURVECAPS:
224         return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
225                 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
226     case LINECAPS:
227         return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
228                 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
229     case POLYGONALCAPS:
230         return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
231                 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
232     case TEXTCAPS:
233         return text_caps;
234     case CLIPCAPS:
235         return CP_REGION;
236     case RASTERCAPS:
237         return (RC_BITBLT | RC_BANDING | RC_SCALING | RC_BITMAP64 | RC_DI_BITMAP |
238                 RC_DIBTODEV | RC_BIGFONT | RC_STRETCHBLT | RC_STRETCHDIB | RC_DEVBITS |
239                 (palette_size ? RC_PALETTE : 0));
240     case ASPECTX:
241     case ASPECTY:
242         return 36;
243     case ASPECTXY:
244         return 51;
245     case LOGPIXELSX:
246         return log_pixels_x;
247     case LOGPIXELSY:
248         return log_pixels_y;
249     case CAPS1:
250         FIXME("(%p): CAPS1 is unimplemented, will return 0\n", physDev->hdc );
251         /* please see wingdi.h for the possible bit-flag values that need
252            to be returned. also, see
253            http://msdn.microsoft.com/library/ddkdoc/win95ddk/graphcnt_1m0p.htm */
254         return 0;
255     case SIZEPALETTE:
256         return palette_size;
257     case NUMRESERVED:
258     case COLORRES:
259     case PHYSICALWIDTH:
260     case PHYSICALHEIGHT:
261     case PHYSICALOFFSETX:
262     case PHYSICALOFFSETY:
263     case SCALINGFACTORX:
264     case SCALINGFACTORY:
265     case VREFRESH:
266     case DESKTOPVERTRES:
267     case DESKTOPHORZRES:
268     case BTLALIGNMENT:
269         return 0;
270     default:
271         FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
272         return 0;
273     }
274 }
275
276
277 /**********************************************************************
278  *           ExtEscape  (X11DRV.@)
279  */
280 INT X11DRV_ExtEscape( X11DRV_PDEVICE *physDev, INT escape, INT in_count, LPCVOID in_data,
281                       INT out_count, LPVOID out_data )
282 {
283     switch(escape)
284     {
285     case QUERYESCSUPPORT:
286         if (in_data)
287         {
288             switch (*(const INT *)in_data)
289             {
290             case DCICOMMAND:
291                 return DD_HAL_VERSION;
292             case X11DRV_ESCAPE:
293                 return TRUE;
294             }
295         }
296         break;
297
298     case DCICOMMAND:
299         if (in_data)
300         {
301             const DCICMD *lpCmd = in_data;
302             if (lpCmd->dwVersion != DD_VERSION) break;
303             return X11DRV_DCICommand(in_count, lpCmd, out_data);
304         }
305         break;
306
307     case X11DRV_ESCAPE:
308         if (in_data && in_count >= sizeof(enum x11drv_escape_codes))
309         {
310             switch(*(const enum x11drv_escape_codes *)in_data)
311             {
312             case X11DRV_GET_DISPLAY:
313                 if (out_count >= sizeof(Display *))
314                 {
315                     *(Display **)out_data = gdi_display;
316                     return TRUE;
317                 }
318                 break;
319             case X11DRV_GET_DRAWABLE:
320                 if (out_count >= sizeof(Drawable))
321                 {
322                     *(Drawable *)out_data = physDev->drawable;
323                     return TRUE;
324                 }
325                 break;
326             case X11DRV_GET_FONT:
327                 if (out_count >= sizeof(Font))
328                 {
329                     fontObject* pfo = XFONT_GetFontObject( physDev->font );
330                     if (pfo == NULL) return FALSE;
331                     *(Font *)out_data = pfo->fs->fid;
332                     return TRUE;
333                 }
334                 break;
335             case X11DRV_SET_DRAWABLE:
336                 if (in_count >= sizeof(struct x11drv_escape_set_drawable))
337                 {
338                     const struct x11drv_escape_set_drawable *data = (const struct x11drv_escape_set_drawable *)in_data;
339                     if(physDev->xrender) X11DRV_XRender_UpdateDrawable( physDev );
340                     physDev->org = data->org;
341                     physDev->drawable = data->drawable;
342                     physDev->drawable_org = data->drawable_org;
343                     wine_tsx11_lock();
344                     XSetSubwindowMode( gdi_display, physDev->gc, data->mode );
345                     wine_tsx11_unlock();
346                     return TRUE;
347                 }
348                 break;
349             case X11DRV_START_EXPOSURES:
350                 wine_tsx11_lock();
351                 XSetGraphicsExposures( gdi_display, physDev->gc, True );
352                 wine_tsx11_unlock();
353                 physDev->exposures = 0;
354                 return TRUE;
355             case X11DRV_END_EXPOSURES:
356                 if (out_count >= sizeof(HRGN))
357                 {
358                     HRGN hrgn = 0, tmp = 0;
359
360                     wine_tsx11_lock();
361                     XSetGraphicsExposures( gdi_display, physDev->gc, False );
362                     if (physDev->exposures)
363                     {
364                         for (;;)
365                         {
366                             XEvent event;
367
368                             XWindowEvent( gdi_display, physDev->drawable, ~0, &event );
369                             if (event.type == NoExpose) break;
370                             if (event.type == GraphicsExpose)
371                             {
372                                 int x = event.xgraphicsexpose.x - physDev->org.x;
373                                 int y = event.xgraphicsexpose.y - physDev->org.y;
374
375                                 TRACE( "got %d,%d %dx%d count %d\n", x, y,
376                                        event.xgraphicsexpose.width,
377                                        event.xgraphicsexpose.height,
378                                        event.xgraphicsexpose.count );
379
380                                 if (!tmp) tmp = CreateRectRgn( 0, 0, 0, 0 );
381                                 SetRectRgn( tmp, x, y,
382                                             x + event.xgraphicsexpose.width,
383                                             y + event.xgraphicsexpose.height );
384                                 if (hrgn) CombineRgn( hrgn, hrgn, tmp, RGN_OR );
385                                 else
386                                 {
387                                     hrgn = tmp;
388                                     tmp = 0;
389                                 }
390                                 if (!event.xgraphicsexpose.count) break;
391                             }
392                             else
393                             {
394                                 ERR( "got unexpected event %d\n", event.type );
395                                 break;
396                             }
397                         }
398                         if (tmp) DeleteObject( tmp );
399                     }
400                     wine_tsx11_unlock();
401                     *(HRGN *)out_data = hrgn;
402                     return TRUE;
403                 }
404                 break;
405             case X11DRV_GET_DCE:
406                 if (out_count >= sizeof(struct dce *))
407                 {
408                     *(struct dce **)out_data = physDev->dce;
409                     return TRUE;
410                 }
411                 break;
412             case X11DRV_SET_DCE:
413                 if (in_count >= sizeof(struct x11drv_escape_set_dce))
414                 {
415                     const struct x11drv_escape_set_dce *data = (const struct x11drv_escape_set_dce *)in_data;
416                     physDev->dce = data->dce;
417                     return TRUE;
418                 }
419                 break;
420             }
421         }
422         break;
423     }
424     return 0;
425 }