summaryrefslogtreecommitdiffstats
path: root/loader/driver.c
blob: 09dc49f8dc9819990acf16a95add6ba8b869b761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <config.h>
#include <stdio.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdlib.h>
#ifdef __FreeBSD__
#include <sys/time.h>
#endif

#include "win32.h"
#include "wine/driver.h"
#include "wine/pe_image.h"
#include "wine/winreg.h"
#include "wine/vfw.h"
#include "registry.h"
#include "ldt_keeper.h"
#include "driver.h"


#if 1
/*
 * STORE_ALL/REST_ALL seems like an attempt to workaround problems due to
 * WINAPI/no-WINAPI bustage.
 *
 * There should be no need for the STORE_ALL/REST_ALL hack once all 
 * function definitions agree with their prototypes (WINAPI-wise) and
 * we make sure, that we do not call these functions without a proper
 * prototype in scope.
 */
#define	STORE_ALL	/**/
#define	REST_ALL	/**/
#else
#define STORE_ALL \
    __asm__( \
    "push %%ebx\n\t" \
    "push %%ecx\n\t" \
    "push %%edx\n\t" \
    "push %%esi\n\t" \
    "push %%edi\n\t"::)

#define REST_ALL \
    __asm__( \
    "pop %%edi\n\t" \
    "pop %%esi\n\t" \
    "pop %%edx\n\t" \
    "pop %%ecx\n\t" \
    "pop %%ebx\n\t"::)
#endif



    
static DWORD dwDrvID = 0;


LRESULT WINAPI SendDriverMessage( HDRVR hDriver, UINT message,
                                    LPARAM lParam1, LPARAM lParam2 )
{
    DRVR* module=(DRVR*)hDriver;
    int result;
#ifdef DETAILED_OUT    
    printf("SendDriverMessage: driver %X, message %X, arg1 %X, arg2 %X\n", hDriver, message, lParam1, lParam2);
#endif
    if(module==0)return -1;
    if(module->hDriverModule==0)return -1;
    if(module->DriverProc==0)return -1;
    STORE_ALL;
    result=module->DriverProc(module->dwDriverID,1,message,lParam1,lParam2);
    REST_ALL;
#ifdef DETAILED_OUT    
    printf("\t\tResult: %X\n", result);
#endif    
    return result;
}				    

static NPDRVR DrvAlloc(HDRVR*lpDriver, LPUINT lpDrvResult)
{
    NPDRVR npDriver;
    /* allocate and lock handle */
    if (lpDriver)
    {
      if ( (*lpDriver = (HDRVR) malloc(sizeof(DRVR))) )
        {
            if ((npDriver = (NPDRVR) *lpDriver))
            {
                    *lpDrvResult = MMSYSERR_NOERROR;
                    return (npDriver);
            }
            free((NPDRVR)*lpDriver);
        }
        return (*lpDrvResult = MMSYSERR_NOMEM, (NPDRVR) 0);
    }
    return (*lpDrvResult = MMSYSERR_INVALPARAM, (NPDRVR) 0);
}

                                                                                                                    
static void DrvFree(HDRVR hDriver)
{
    int i;
    Setup_FS_Segment();
    if(hDriver)
    	if(((DRVR*)hDriver)->hDriverModule)
    	if(((DRVR*)hDriver)->DriverProc)
	(((DRVR*)hDriver)->DriverProc)(((DRVR*)hDriver)->dwDriverID, hDriver, DRV_CLOSE, 0, 0);
    if(hDriver)	{
            if(((DRVR*)hDriver)->hDriverModule)
    		if(((DRVR*)hDriver)->DriverProc)
			(((DRVR*)hDriver)->DriverProc)(0, hDriver, DRV_FREE, 0, 0);
		FreeLibrary(((DRVR*)hDriver)->hDriverModule);
        	free((NPDRVR)hDriver);
		return;	
    }
}

void DrvClose(HDRVR hdrvr)
{
    DrvFree(hdrvr);
}


char* win32_codec_name=NULL;  // must be set before calling DrvOpen() !!!

HDRVR VFWAPI
DrvOpen(LPARAM lParam2)
{
    ICOPEN *icopen=(ICOPEN *) lParam2;
    UINT uDrvResult;
    HDRVR hDriver;
    NPDRVR npDriver;
    char unknown[0x24];
//    char* codec_name=icopen->fccHandler;

    Setup_LDT_Keeper();

    if (!(npDriver = DrvAlloc(&hDriver, &uDrvResult)))
	return ((HDRVR) 0);

    if (!(npDriver->hDriverModule = LoadLibraryA(win32_codec_name))) {
     	printf("Can't open library %s\n", win32_codec_name);
        DrvFree(hDriver);
        return ((HDRVR) 0);
    }

#if 0
    {
        unsigned char *p=((char*)npDriver->hDriverModule);
        double *dp;
        int i;
        p+=0x14c0;
        for(i=0;i<16;i++)printf(" %02X",p[i]); printf("\n");
        dp=(double*)p;
        printf("divx bitrate = %f\n",(float)(*dp));
//    	*(double*)((char*)npDriver->hDriverModule+0x14c0)=bitrate;
    }
#endif
   
    if (!(npDriver->DriverProc = (DRIVERPROC)
             GetProcAddress(npDriver->hDriverModule, "DriverProc"))) {
#if 1
         printf("Library %s is not a VfW/ACM valid codec\n", win32_codec_name);
#else
        // Try DirectShow...
         GETCLASS func=(GETCLASS)GetProcAddress(npDriver->hDriverModule,"DllGetClassObject");
         if(!func)
           printf("Library %s is not a valid VfW/ACM/DShow codec\n", win32_codec_name);
         else {
            HRESULT result;
            struct IClassFactory* factory=0;
	    struct IUnknown* object=0;
            GUID CLSID_Voxware={0x73f7a062, 0x8829, 0x11d1,
                {0xb5, 0x50, 0x00, 0x60, 0x97, 0x24, 0x2d, 0x8d}};
            GUID* id=&CLSID_Voxware;
            
            result=func(id, &IID_IClassFactory, (void**)&factory);
            if(result || (!factory)) printf("No such class object (wrong/missing GUID?)\n");

            printf("Calling factory->vt->CreateInstance()\n");
            printf("addr = %X\n",(unsigned int)factory->vt->CreateInstance);
            result=factory->vt->CreateInstance(factory, 0, &IID_IUnknown, (void**)&object);
            printf("Calling factory->vt->Release()\n");
            factory->vt->Release((struct IUnknown*)factory);
            if(result || (!object)) printf("Class factory failure\n");

            printf("DirectShow codecs not yet supported...\n");
         }
#endif

         FreeLibrary(npDriver->hDriverModule);
         DrvFree(hDriver);
         return ((HDRVR) 0);

    }

    //TRACE("DriverProc == %X\n", npDriver->DriverProc);
     npDriver->dwDriverID = ++dwDrvID;

     Setup_FS_Segment();

	STORE_ALL;
        (npDriver->DriverProc)(0, hDriver, DRV_LOAD, 0, 0);
	REST_ALL;
	//TRACE("DRV_LOAD Ok!\n");
	STORE_ALL;
	(npDriver->DriverProc)(0, hDriver, DRV_ENABLE, 0, 0);
	REST_ALL;
	//TRACE("DRV_ENABLE Ok!\n");

     // open driver 
    STORE_ALL;
     npDriver->dwDriverID=(npDriver->DriverProc)(npDriver->dwDriverID, hDriver, DRV_OPEN,
         (LPARAM) (LPSTR) unknown, lParam2);
    REST_ALL;

    //TRACE("DRV_OPEN Ok!(%X)\n", npDriver->dwDriverID);

    if (uDrvResult)
    {
         DrvFree(hDriver);
         hDriver = (HDRVR) 0;
     }
     
//     printf("Successfully loaded codec %s\n",win32_codec_name);
     
     return (hDriver);
}