"inline" is used elsewhere and more portable than "__inline"
[mplayer/glamo.git] / loader / driver.c
blobaedf345c4bd9fe3990f84414ff5271c6fd5db361
1 /*
2 * Modified for use with MPlayer, detailed changelog at
3 * http://svn.mplayerhq.hu/mplayer/trunk/
4 * $Id$
5 */
7 #include "config.h"
8 #include "debug.h"
10 #include <stdio.h>
11 #ifdef HAVE_MALLOC_H
12 #include <malloc.h>
13 #endif
14 #include <stdlib.h>
15 #ifdef __FreeBSD__
16 #include <sys/time.h>
17 #endif
19 #include "win32.h"
20 #include "wine/driver.h"
21 #include "wine/pe_image.h"
22 #include "wine/winreg.h"
23 #include "wine/vfw.h"
24 #include "registry.h"
25 #ifdef WIN32_LOADER
26 #include "ldt_keeper.h"
27 #endif
28 #include "driver.h"
29 #ifndef __MINGW32__
30 #include "ext.h"
31 #endif
33 #ifndef WIN32_LOADER
34 char* def_path=WIN32_PATH;
35 #else
36 extern char* def_path;
37 #endif
39 #if 1
42 * STORE_ALL/REST_ALL seems like an attempt to workaround problems due to
43 * WINAPI/no-WINAPI bustage.
45 * There should be no need for the STORE_ALL/REST_ALL hack once all
46 * function definitions agree with their prototypes (WINAPI-wise) and
47 * we make sure, that we do not call these functions without a proper
48 * prototype in scope.
51 #define STORE_ALL
52 #define REST_ALL
53 #else
54 // this asm code is no longer needed
55 #define STORE_ALL \
56 __asm__ __volatile__ ( \
57 "push %%ebx\n\t" \
58 "push %%ecx\n\t" \
59 "push %%edx\n\t" \
60 "push %%esi\n\t" \
61 "push %%edi\n\t"::)
63 #define REST_ALL \
64 __asm__ __volatile__ ( \
65 "pop %%edi\n\t" \
66 "pop %%esi\n\t" \
67 "pop %%edx\n\t" \
68 "pop %%ecx\n\t" \
69 "pop %%ebx\n\t"::)
70 #endif
72 static int needs_free=0;
73 void SetCodecPath(const char* path)
75 if(needs_free)free(def_path);
76 if(path==0)
78 def_path=WIN32_PATH;
79 needs_free=0;
80 return;
82 def_path = (char*) malloc(strlen(path)+1);
83 strcpy(def_path, path);
84 needs_free=1;
87 static DWORD dwDrvID = 0;
89 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT message,
90 LPARAM lParam1, LPARAM lParam2)
92 DRVR* module=(DRVR*)hDriver;
93 int result;
94 #ifndef __svr4__
95 char qw[300];
96 #endif
97 #ifdef DETAILED_OUT
98 printf("SendDriverMessage: driver %X, message %X, arg1 %X, arg2 %X\n", hDriver, message, lParam1, lParam2);
99 #endif
100 if (!module || !module->hDriverModule || !module->DriverProc) return -1;
101 #ifndef __svr4__
102 __asm__ __volatile__ ("fsave (%0)\n\t": :"r"(&qw));
103 #endif
105 #ifdef WIN32_LOADER
106 Setup_FS_Segment();
107 #endif
109 STORE_ALL;
110 result=module->DriverProc(module->dwDriverID, hDriver, message, lParam1, lParam2);
111 REST_ALL;
113 #ifndef __svr4__
114 __asm__ __volatile__ ("frstor (%0)\n\t": :"r"(&qw));
115 #endif
117 #ifdef DETAILED_OUT
118 printf("\t\tResult: %X\n", result);
119 #endif
120 return result;
123 void DrvClose(HDRVR hDriver)
125 if (hDriver)
127 DRVR* d = (DRVR*)hDriver;
128 if (d->hDriverModule)
130 #ifdef WIN32_LOADER
131 Setup_FS_Segment();
132 #endif
133 if (d->DriverProc)
135 SendDriverMessage(hDriver, DRV_CLOSE, 0, 0);
136 d->dwDriverID = 0;
137 SendDriverMessage(hDriver, DRV_FREE, 0, 0);
139 FreeLibrary(d->hDriverModule);
141 free(d);
143 #ifdef WIN32_LOADER
144 CodecRelease();
145 #endif
148 //DrvOpen(LPCSTR lpszDriverName, LPCSTR lpszSectionName, LPARAM lParam2)
149 HDRVR DrvOpen(LPARAM lParam2)
151 NPDRVR hDriver;
152 int i;
153 char unknown[0x124];
154 const char* filename = (const char*) ((ICOPEN*) lParam2)->pV1Reserved;
156 #ifdef MPLAYER
157 #ifdef WIN32_LOADER
158 Setup_LDT_Keeper();
159 #endif
160 printf("Loading codec DLL: '%s'\n",filename);
161 #endif
163 hDriver = (NPDRVR) malloc(sizeof(DRVR));
164 if (!hDriver)
165 return ((HDRVR) 0);
166 memset((void*)hDriver, 0, sizeof(DRVR));
168 #ifdef WIN32_LOADER
169 CodecAlloc();
170 Setup_FS_Segment();
171 #endif
173 hDriver->hDriverModule = LoadLibraryA(filename);
174 if (!hDriver->hDriverModule)
176 printf("Can't open library %s\n", filename);
177 DrvClose((HDRVR)hDriver);
178 return ((HDRVR) 0);
181 hDriver->DriverProc = (DRIVERPROC) GetProcAddress(hDriver->hDriverModule,
182 "DriverProc");
183 if (!hDriver->DriverProc)
185 printf("Library %s is not a valid VfW/ACM codec\n", filename);
186 DrvClose((HDRVR)hDriver);
187 return ((HDRVR) 0);
190 TRACE("DriverProc == %X\n", hDriver->DriverProc);
191 SendDriverMessage((HDRVR)hDriver, DRV_LOAD, 0, 0);
192 TRACE("DRV_LOAD Ok!\n");
193 SendDriverMessage((HDRVR)hDriver, DRV_ENABLE, 0, 0);
194 TRACE("DRV_ENABLE Ok!\n");
195 hDriver->dwDriverID = ++dwDrvID; // generate new id
197 // open driver and remmeber proper DriverID
198 hDriver->dwDriverID = SendDriverMessage((HDRVR)hDriver, DRV_OPEN, (LPARAM) unknown, lParam2);
199 TRACE("DRV_OPEN Ok!(%X)\n", hDriver->dwDriverID);
201 printf("Loaded DLL driver %s at %x\n", filename, hDriver->hDriverModule);
202 return (HDRVR)hDriver;