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