Keep track of per-column information inside the listview.
[wine] / dlls / x11drv / dga2.c
1 /*
2  * DirectDraw DGA2 interface
3  *
4  * Copyright 2001 TransGaming Technologies, Inc.
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 #ifdef HAVE_LIBXXF86DGA2
24
25 #include "ts_xlib.h"
26 #include <X11/extensions/xf86dga.h>
27
28 #include "x11drv.h"
29 #include "x11ddraw.h"
30 #include "dga2.h"
31
32 #include "windef.h"
33 #include "wingdi.h"
34 #include "ddrawi.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
38
39 extern int usedga;
40
41 LPDDHALMODEINFO xf86dga2_modes;
42 unsigned xf86dga2_mode_count;
43 static XDGAMode* modes;
44 static int dga_event, dga_error;
45
46 static void convert_mode(XDGAMode *mode, LPDDHALMODEINFO info)
47 {
48   info->dwWidth        = mode->viewportWidth;
49   info->dwHeight       = mode->viewportHeight;
50   info->wRefreshRate   = mode->verticalRefresh;
51   info->lPitch         = mode->bytesPerScanline;
52   info->dwBPP          = (mode->depth < 24) ? mode->depth : mode->bitsPerPixel;
53   info->wFlags         = (mode->depth == 8) ? DDMODEINFO_PALETTIZED : 0;
54   info->dwRBitMask     = mode->redMask;
55   info->dwGBitMask     = mode->greenMask;
56   info->dwBBitMask     = mode->blueMask;
57   info->dwAlphaBitMask = 0;
58   TRACE(" width=%ld, height=%ld, bpp=%ld, refresh=%d\n",
59         info->dwWidth, info->dwHeight, info->dwBPP, info->wRefreshRate);
60 }
61
62 static int DGA2ErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
63 {
64     return 1;
65 }
66
67 void X11DRV_XF86DGA2_Init(void)
68 {
69   int nmodes, major, minor, i;
70   Bool ok;
71
72   if (xf86dga2_modes) return; /* already initialized? */
73
74   /* if in desktop mode, don't use DGA */
75   if (root_window != DefaultRootWindow(gdi_display)) return;
76
77   if (!usedga) return;
78
79   wine_tsx11_lock();
80   ok = XDGAQueryExtension(gdi_display, &dga_event, &dga_error);
81   if (ok)
82   {
83       X11DRV_expect_error(gdi_display, DGA2ErrorHandler, NULL);
84       ok = XDGAQueryVersion(gdi_display, &major, &minor);
85       if (X11DRV_check_error()) ok = FALSE;
86   }
87   wine_tsx11_unlock();
88   if (!ok) return;
89
90   if (major < 2) return; /* only bother with DGA 2+ */
91
92   /* test that it works */
93   wine_tsx11_lock();
94   if ((ok = XDGAOpenFramebuffer(gdi_display, DefaultScreen(gdi_display))))
95   {
96       XDGACloseFramebuffer(gdi_display, DefaultScreen(gdi_display));
97       /* retrieve modes */
98       modes = XDGAQueryModes(gdi_display, DefaultScreen(gdi_display), &nmodes);
99       if (!modes) ok = FALSE;
100   }
101   else WARN("disabling XF86DGA2 (insufficient permissions?)\n");
102   wine_tsx11_unlock();
103   if (!ok) return;
104
105   TRACE("DGA modes: count=%d\n", nmodes);
106
107   xf86dga2_mode_count = nmodes+1;
108   xf86dga2_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * (nmodes+1));
109
110   /* make dummy mode for exiting DGA */
111   memset(&xf86dga2_modes[0], 0, sizeof(xf86dga2_modes[0]));
112
113   /* convert modes to DDHALMODEINFO format */
114   for (i=0; i<nmodes; i++)
115     convert_mode(&modes[i], &xf86dga2_modes[i+1]);
116
117   TRACE("Enabling XF86DGA2 mode\n");
118 }
119
120 void X11DRV_XF86DGA2_Cleanup(void)
121 {
122   if (modes) TSXFree(modes);
123 }
124
125 static XDGADevice *dga_dev;
126
127 static VIDMEM dga_mem = {
128   VIDMEM_ISRECTANGULAR | VIDMEM_ISHEAP
129 };
130
131 static DWORD PASCAL X11DRV_XF86DGA2_SetMode(LPDDHAL_SETMODEDATA data)
132 {
133   LPDDRAWI_DIRECTDRAW_LCL ddlocal = data->lpDD->lpExclusiveOwner;
134   DWORD vram;
135   Display *display = gdi_display;
136
137   data->ddRVal = DD_OK;
138   wine_tsx11_lock();
139   if (data->dwModeIndex) {
140     /* enter DGA */
141     XDGADevice *new_dev = NULL;
142     if (dga_dev || XDGAOpenFramebuffer(display, DefaultScreen(display)))
143       new_dev = XDGASetMode(display, DefaultScreen(display), modes[data->dwModeIndex-1].num);
144     if (new_dev) {
145       XDGASetViewport(display, DefaultScreen(display), 0, 0, XDGAFlipImmediate);
146       if (dga_dev) {
147         VirtualFree(dga_dev->data, 0, MEM_RELEASE);
148         XFree(dga_dev);
149       } else {
150         XDGASelectInput(display, DefaultScreen(display),
151                           KeyPressMask|KeyReleaseMask|
152                           ButtonPressMask|ButtonReleaseMask|
153                           PointerMotionMask);
154         X11DRV_EVENT_SetDGAStatus((HWND)ddlocal->hWnd, dga_event);
155         X11DRV_EVENT_SetInputMethod(X11DRV_INPUT_RELATIVE);
156       }
157       dga_dev = new_dev;
158       vram = dga_dev->mode.bytesPerScanline * dga_dev->mode.imageHeight;
159       VirtualAlloc(dga_dev->data, vram, MEM_RESERVE|MEM_SYSTEM, PAGE_READWRITE);
160       dga_mem.fpStart = (FLATPTR)dga_dev->data;
161       dga_mem.u1.dwWidth = dga_dev->mode.bytesPerScanline;
162       dga_mem.u2.dwHeight = dga_dev->mode.imageHeight;
163       X11DRV_DDHAL_SwitchMode(data->dwModeIndex, dga_dev->data, &dga_mem);
164       X11DRV_DD_IsDirect = TRUE;
165     }
166     else {
167       ERR("failed\n");
168       if (!dga_dev) XDGACloseFramebuffer(display, DefaultScreen(display));
169       data->ddRVal = DDERR_GENERIC;
170     }
171   }
172   else if (dga_dev) {
173     /* exit DGA */
174     X11DRV_DD_IsDirect = FALSE;
175     X11DRV_DDHAL_SwitchMode(0, NULL, NULL);
176     XDGASetMode(display, DefaultScreen(display), 0);
177     VirtualFree(dga_dev->data, 0, MEM_RELEASE);
178     X11DRV_EVENT_SetInputMethod(X11DRV_INPUT_ABSOLUTE);
179     X11DRV_EVENT_SetDGAStatus(0, -1);
180     XFree(dga_dev);
181     XDGACloseFramebuffer(display, DefaultScreen(display));
182     dga_dev = NULL;
183   }
184   wine_tsx11_unlock();
185   return DDHAL_DRIVER_HANDLED;
186 }
187
188 static LPDDHAL_CREATESURFACE X11DRV_XF86DGA2_old_create_surface;
189
190 static DWORD PASCAL X11DRV_XF86DGA2_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
191 {
192   LPDDRAWI_DDRAWSURFACE_LCL lcl = *data->lplpSList;
193   LPDDRAWI_DDRAWSURFACE_GBL gbl = lcl->lpGbl;
194   LPDDSURFACEDESC2 desc = (LPDDSURFACEDESC2)data->lpDDSurfaceDesc;
195   HRESULT hr = DDHAL_DRIVER_NOTHANDLED;
196
197   if (X11DRV_XF86DGA2_old_create_surface)
198     hr = X11DRV_XF86DGA2_old_create_surface(data);
199
200   if (desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)) {
201     gbl->fpVidMem = 0; /* tell ddraw to allocate the memory */
202     hr = DDHAL_DRIVER_HANDLED;
203   }
204   return hr;
205 }
206
207 static DWORD PASCAL X11DRV_XF86DGA2_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
208 {
209   Display *display = gdi_display;
210   wine_tsx11_lock();
211   data->lpDDPalette->u1.dwReserved1 = XDGACreateColormap(display, DefaultScreen(display),
212                                                          dga_dev, AllocAll);
213   wine_tsx11_unlock();
214   if (data->lpColorTable)
215     X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1, 0, 256,
216                                data->lpColorTable);
217   data->ddRVal = DD_OK;
218   return DDHAL_DRIVER_HANDLED;
219 }
220
221 static DWORD PASCAL X11DRV_XF86DGA2_Flip(LPDDHAL_FLIPDATA data)
222 {
223   Display *display = gdi_display;
224   if (data->lpSurfCurr == X11DRV_DD_Primary) {
225     DWORD ofs = data->lpSurfCurr->lpGbl->fpVidMem - dga_mem.fpStart;
226     wine_tsx11_lock();
227     XDGASetViewport(display, DefaultScreen(display),
228                     (ofs % dga_dev->mode.bytesPerScanline)*8/dga_dev->mode.bitsPerPixel,
229                     ofs / dga_dev->mode.bytesPerScanline,
230                     XDGAFlipImmediate);
231     wine_tsx11_unlock();
232   }
233   data->ddRVal = DD_OK;
234   return DDHAL_DRIVER_HANDLED;
235 }
236
237 static DWORD PASCAL X11DRV_XF86DGA2_SetPalette(LPDDHAL_SETPALETTEDATA data)
238 {
239   Display *display = gdi_display;
240   if ((data->lpDDSurface == X11DRV_DD_Primary) &&
241       data->lpDDPalette && data->lpDDPalette->u1.dwReserved1)
242   {
243       wine_tsx11_lock();
244       XDGAInstallColormap(display, DefaultScreen(display), data->lpDDPalette->u1.dwReserved1);
245       wine_tsx11_unlock();
246   }
247   data->ddRVal = DD_OK;
248   return DDHAL_DRIVER_HANDLED;
249 }
250
251 int X11DRV_XF86DGA2_CreateDriver(LPDDHALINFO info)
252 {
253   if (!xf86dga2_mode_count) return 0; /* no DGA */
254
255   info->dwNumModes = xf86dga2_mode_count;
256   info->lpModeInfo = xf86dga2_modes;
257   info->dwModeIndex = 0;
258
259   X11DRV_XF86DGA2_old_create_surface = info->lpDDCallbacks->CreateSurface;
260   info->lpDDCallbacks->SetMode = X11DRV_XF86DGA2_SetMode;
261   info->lpDDCallbacks->CreateSurface = X11DRV_XF86DGA2_CreateSurface;
262   info->lpDDCallbacks->CreatePalette = X11DRV_XF86DGA2_CreatePalette;
263   info->lpDDSurfaceCallbacks->Flip = X11DRV_XF86DGA2_Flip;
264   info->lpDDSurfaceCallbacks->SetPalette = X11DRV_XF86DGA2_SetPalette;
265   return TRUE;
266 }
267
268 #endif /* HAVE_LIBXXF86DGA2 */