Fix vf_tcdump's compilation
[mplayer/kovensky.git] / loader / drv.c
blob9479e6944dd83eb3f36dfa1564deb2c3c488d1c6
1 /*
2 * Modified for use with MPlayer, detailed changelog at
3 * http://svn.mplayerhq.hu/mplayer/trunk/
4 */
6 #include "config.h"
7 #include "debug.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #ifdef __FreeBSD__
12 #include <sys/time.h>
13 #endif
15 #include "win32.h"
16 #include "wine/driver.h"
17 #include "wine/pe_image.h"
18 #include "wine/winreg.h"
19 #include "wine/vfw.h"
20 #include "registry.h"
21 #ifdef WIN32_LOADER
22 #include "ldt_keeper.h"
23 #endif
24 #include "drv.h"
25 #ifndef __MINGW32__
26 #include "ext.h"
27 #endif
29 #ifndef WIN32_LOADER
30 char* def_path=WIN32_PATH;
31 #else
32 extern char* def_path;
33 #endif
35 #if 1
38 * STORE_ALL/REST_ALL seems like an attempt to workaround problems due to
39 * WINAPI/no-WINAPI bustage.
41 * There should be no need for the STORE_ALL/REST_ALL hack once all
42 * function definitions agree with their prototypes (WINAPI-wise) and
43 * we make sure, that we do not call these functions without a proper
44 * prototype in scope.
47 #define STORE_ALL
48 #define REST_ALL
49 #else
50 // this asm code is no longer needed
51 #define STORE_ALL \
52 __asm__ volatile ( \
53 "push %%ebx\n\t" \
54 "push %%ecx\n\t" \
55 "push %%edx\n\t" \
56 "push %%esi\n\t" \
57 "push %%edi\n\t"::)
59 #define REST_ALL \
60 __asm__ volatile ( \
61 "pop %%edi\n\t" \
62 "pop %%esi\n\t" \
63 "pop %%edx\n\t" \
64 "pop %%ecx\n\t" \
65 "pop %%ebx\n\t"::)
66 #endif
68 static int needs_free=0;
69 void SetCodecPath(const char* path)
71 if(needs_free)free(def_path);
72 if(path==0)
74 def_path=WIN32_PATH;
75 needs_free=0;
76 return;
78 def_path = malloc(strlen(path)+1);
79 strcpy(def_path, path);
80 needs_free=1;
83 static DWORD dwDrvID = 0;
85 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT message,
86 LPARAM lParam1, LPARAM lParam2)
88 DRVR* module=(DRVR*)hDriver;
89 int result;
90 #ifndef __svr4__
91 char qw[300];
92 #endif
93 #ifdef DETAILED_OUT
94 printf("SendDriverMessage: driver %X, message %X, arg1 %X, arg2 %X\n", hDriver, message, lParam1, lParam2);
95 #endif
96 if (!module || !module->hDriverModule || !module->DriverProc) return -1;
97 #ifndef __svr4__
98 __asm__ volatile ("fsave (%0)\n\t": :"r"(&qw));
99 #endif
101 #ifdef WIN32_LOADER
102 Setup_FS_Segment();
103 #endif
105 STORE_ALL;
106 result=module->DriverProc(module->dwDriverID, hDriver, message, lParam1, lParam2);
107 REST_ALL;
109 #ifndef __svr4__
110 __asm__ volatile ("frstor (%0)\n\t": :"r"(&qw));
111 #endif
113 #ifdef DETAILED_OUT
114 printf("\t\tResult: %X\n", result);
115 #endif
116 return result;
119 void DrvClose(HDRVR hDriver)
121 if (hDriver)
123 DRVR* d = (DRVR*)hDriver;
124 if (d->hDriverModule)
126 #ifdef WIN32_LOADER
127 Setup_FS_Segment();
128 #endif
129 if (d->DriverProc)
131 SendDriverMessage(hDriver, DRV_CLOSE, 0, 0);
132 d->dwDriverID = 0;
133 SendDriverMessage(hDriver, DRV_FREE, 0, 0);
135 FreeLibrary(d->hDriverModule);
137 free(d);
139 #ifdef WIN32_LOADER
140 CodecRelease();
141 #endif
144 //DrvOpen(LPCSTR lpszDriverName, LPCSTR lpszSectionName, LPARAM lParam2)
145 HDRVR DrvOpen(LPARAM lParam2)
147 NPDRVR hDriver;
148 char unknown[0x124];
149 const char* filename = (const char*) ((ICOPEN*) lParam2)->pV1Reserved;
151 #ifdef WIN32_LOADER
152 Setup_LDT_Keeper();
153 #endif
154 printf("Loading codec DLL: '%s'\n",filename);
156 hDriver = malloc(sizeof(DRVR));
157 if (!hDriver)
158 return (HDRVR) 0;
159 memset((void*)hDriver, 0, sizeof(DRVR));
161 #ifdef WIN32_LOADER
162 CodecAlloc();
163 Setup_FS_Segment();
164 #endif
166 hDriver->hDriverModule = LoadLibraryA(filename);
167 if (!hDriver->hDriverModule)
169 printf("Can't open library %s\n", filename);
170 DrvClose((HDRVR)hDriver);
171 return (HDRVR) 0;
174 hDriver->DriverProc = (DRIVERPROC) GetProcAddress(hDriver->hDriverModule,
175 "DriverProc");
176 if (!hDriver->DriverProc)
178 printf("Library %s is not a valid VfW/ACM codec\n", filename);
179 DrvClose((HDRVR)hDriver);
180 return (HDRVR) 0;
183 TRACE("DriverProc == %X\n", hDriver->DriverProc);
184 SendDriverMessage((HDRVR)hDriver, DRV_LOAD, 0, 0);
185 TRACE("DRV_LOAD Ok!\n");
186 SendDriverMessage((HDRVR)hDriver, DRV_ENABLE, 0, 0);
187 TRACE("DRV_ENABLE Ok!\n");
188 hDriver->dwDriverID = ++dwDrvID; // generate new id
190 // open driver and remmeber proper DriverID
191 hDriver->dwDriverID = SendDriverMessage((HDRVR)hDriver, DRV_OPEN, (LPARAM) unknown, lParam2);
192 TRACE("DRV_OPEN Ok!(%X)\n", hDriver->dwDriverID);
194 printf("Loaded DLL driver %s at %x\n", filename, hDriver->hDriverModule);
195 return (HDRVR)hDriver;