1 /*
2 * MCI internal functions
3 *
4 * Copyright 1998/1999 Eric Pouech
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 /* TODO:
22 * - implement WINMM (32bit) multitasking and use it in all MCI drivers
23 * instead of the home grown one
24 * - 16bit mmTaskXXX functions are currently broken because the 16
25 * loader does not support binary command lines => provide Wine's
26 * own mmtask.tsk not using binary command line.
27 * - correctly handle the MCI_ALL_DEVICE_ID in functions.
28 * - finish mapping 16 <=> 32 of MCI structures and commands
29 * - implement auto-open feature (ie, when a string command is issued
30 * for a not yet opened device, MCI automatically opens it)
31 * - use a default registry setting to replace the [mci] section in
32 * configuration file (layout of info in registry should be compatible
33 * with all Windows' version - which use different layouts of course)
34 * - implement automatic open
35 * + only works on string interface, on regular devices (don't work on all
36 * nor custom devices)
37 * - command table handling isn't thread safe
38 */
39
40 /* to be cross checked:
41 * - heapalloc for *sizeof(WCHAR) when needed
42 * - size of string in WCHAR or bytes? (#chars for MCI_INFO, #bytes for MCI_SYSINFO)
43 */
44
45 #include "config.h"
46 #include "wine/port.h"
47
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <string.h>
52
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "mmsystem.h"
57 #include "winuser.h"
58 #include "winnls.h"
59 #include "winreg.h"
60 #include "wownt32.h"
61
62 #include "digitalv.h"
63 #include "winemm.h"
64
65 #include "wine/debug.h"
66 #include "wine/unicode.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(mci);
69
70 WINMM_MapType (*pFnMciMapMsg16To32W) (WORD,WORD,DWORD,DWORD_PTR*) = NULL;
71 WINMM_MapType (*pFnMciUnMapMsg16To32W)(WORD,WORD,DWORD,DWORD_PTR) = NULL;
72 WINMM_MapType (*pFnMciMapMsg32WTo16) (WORD,WORD,DWORD,DWORD_PTR*) = NULL;
73 WINMM_MapType (*pFnMciUnMapMsg32WTo16)(WORD,WORD,DWORD,DWORD_PTR) = NULL;
74
75 /* First MCI valid device ID (0 means error) */
76 #define MCI_MAGIC 0x0001
77
78 /* MCI settings */
79 static const WCHAR wszHklmMci [] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','M','C','I',0};
80 static const WCHAR wszNull [] = {0};
81 static const WCHAR wszAll [] = {'A','L','L',0};
82 static const WCHAR wszMci [] = {'M','C','I',0};
83 static const WCHAR wszOpen [] = {'o','p','e','n',0};
84 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
85
86 static WINE_MCIDRIVER *MciDrivers;
87
88 /* dup a string and uppercase it */
89 static inline LPWSTR str_dup_upper( LPCWSTR str )
90 {
91 INT len = (strlenW(str) + 1) * sizeof(WCHAR);
92 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
93 if (p)
94 {
95 memcpy( p, str, len );
96 CharUpperW( p );
97 }
98 return p;
99 }
100
101 /**************************************************************************
102 * MCI_GetDriver [internal]
103 */
104 LPWINE_MCIDRIVER MCI_GetDriver(UINT16 wDevID)
105 {
106 LPWINE_MCIDRIVER wmd = 0;
107
108 EnterCriticalSection(&WINMM_cs);
109 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
110 if (wmd->wDeviceID == wDevID)
111 break;
112 }
113 LeaveCriticalSection(&WINMM_cs);
114 return wmd;
115 }
116
117 /**************************************************************************
118 * MCI_GetDriverFromString [internal]
119 */
120 UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
121 {
122 LPWINE_MCIDRIVER wmd;
123 UINT ret = 0;
124
125 if (!lpstrName)
126 return 0;
127
128 if (!strcmpiW(lpstrName, wszAll))
129 return MCI_ALL_DEVICE_ID;
130
131 EnterCriticalSection(&WINMM_cs);
132 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
133 if (wmd->lpstrElementName && strcmpW(wmd->lpstrElementName, lpstrName) == 0) {
134 ret = wmd->wDeviceID;
135 break;
136 }
137 if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
138 ret = wmd->wDeviceID;
139 break;
140 }
141 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
142 ret = wmd->wDeviceID;
143 break;
144 }
145 }
146 LeaveCriticalSection(&WINMM_cs);
147
148 return ret;
149 }
150
151 /**************************************************************************
152 * MCI_MessageToString [internal]
153 */
154 const char* MCI_MessageToString(UINT wMsg)
155 {
156 static char buffer[100];
157
158 #define CASE(s) case (s): return #s
159
160 switch (wMsg) {
161 CASE(DRV_LOAD);
162 CASE(DRV_ENABLE);
163 CASE(DRV_OPEN);
164 CASE(DRV_CLOSE);
165 CASE(DRV_DISABLE);
166 CASE(DRV_FREE);
167 CASE(DRV_CONFIGURE);
168 CASE(DRV_QUERYCONFIGURE);
169 CASE(DRV_INSTALL);
170 CASE(DRV_REMOVE);
171 CASE(DRV_EXITSESSION);
172 CASE(DRV_EXITAPPLICATION);
173 CASE(DRV_POWER);
174 CASE(MCI_BREAK);
175 CASE(MCI_CLOSE);
176 CASE(MCI_CLOSE_DRIVER);
177 CASE(MCI_COPY);
178 CASE(MCI_CUE);
179 CASE(MCI_CUT);
180 CASE(MCI_DELETE);
181 CASE(MCI_ESCAPE);
182 CASE(MCI_FREEZE);
183 CASE(MCI_PAUSE);
184 CASE(MCI_PLAY);
185 CASE(MCI_GETDEVCAPS);
186 CASE(MCI_INFO);
187 CASE(MCI_LOAD);
188 CASE(MCI_OPEN);
189 CASE(MCI_OPEN_DRIVER);
190 CASE(MCI_PASTE);
191 CASE(MCI_PUT);
192 CASE(MCI_REALIZE);
193 CASE(MCI_RECORD);
194 CASE(MCI_RESUME);
195 CASE(MCI_SAVE);
196 CASE(MCI_SEEK);
197 CASE(MCI_SET);
198 CASE(MCI_SPIN);
199 CASE(MCI_STATUS);
200 CASE(MCI_STEP);
201 CASE(MCI_STOP);
202 CASE(MCI_SYSINFO);
203 CASE(MCI_UNFREEZE);
204 CASE(MCI_UPDATE);
205 CASE(MCI_WHERE);
206 CASE(MCI_WINDOW);
207 /* constants for digital video */
208 CASE(MCI_CAPTURE);
209 CASE(MCI_MONITOR);
210 CASE(MCI_RESERVE);
211 CASE(MCI_SETAUDIO);
212 CASE(MCI_SIGNAL);
213 CASE(MCI_SETVIDEO);
214 CASE(MCI_QUALITY);
215 CASE(MCI_LIST);
216 CASE(MCI_UNDO);
217 CASE(MCI_CONFIGURE);
218 CASE(MCI_RESTORE);
219 #undef CASE
220 default:
221 sprintf(buffer, "MCI_<<%04X>>", wMsg);
222 return buffer;
223 }
224 }
225
226 LPWSTR MCI_strdupAtoW( LPCSTR str )
227 {
228 LPWSTR ret;
229 INT len;
230
231 if (!str) return NULL;
232 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
233 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
234 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
235 return ret;
236 }
237
238 LPSTR MCI_strdupWtoA( LPCWSTR str )
239 {
240 LPSTR ret;
241 INT len;
242
243 if (!str) return NULL;
244 len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
245 ret = HeapAlloc( GetProcessHeap(), 0, len );
246 if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
247 return ret;
248 }
249
250 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
251 {
252 if (msg < DRV_RESERVED) return 0;
253
254 switch (msg)
255 {
256 case MCI_CLOSE:
257 case MCI_CONFIGURE:
258 case MCI_PLAY:
259 case MCI_SEEK:
260 case MCI_STOP:
261 case MCI_PAUSE:
262 case MCI_GETDEVCAPS:
263 case MCI_SPIN:
264 case MCI_SET:
265 case MCI_STEP:
266 case MCI_RECORD:
267 case MCI_BREAK:
268 case MCI_SOUND:
269 case MCI_STATUS:
270 case MCI_CUE:
271 case MCI_REALIZE:
272 case MCI_PUT:
273 case MCI_WHERE:
274 case MCI_FREEZE:
275 case MCI_UNFREEZE:
276 case MCI_CUT:
277 case MCI_COPY:
278 case MCI_PASTE:
279 case MCI_UPDATE:
280 case MCI_RESUME:
281 case MCI_DELETE:
282 case MCI_MONITOR:
283 case MCI_SETAUDIO:
284 case MCI_SIGNAL:
285 case MCI_SETVIDEO:
286 case MCI_LIST:
287 return 0;
288
289 case MCI_OPEN:
290 {
291 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA*)*dwParam2;
292 MCI_OPEN_PARMSW *mci_openW;
293 DWORD_PTR *ptr;
294
295 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW) + 2 * sizeof(DWORD));
296 if (!ptr) return -1;
297
298 *ptr++ = *dwParam2; /* save the previous pointer */
299 *dwParam2 = (DWORD_PTR)ptr;
300 mci_openW = (MCI_OPEN_PARMSW *)ptr;
301
302 if (dwParam1 & MCI_NOTIFY)
303 mci_openW->dwCallback = mci_openA->dwCallback;
304
305 if (dwParam1 & MCI_OPEN_TYPE)
306 {
307 if (dwParam1 & MCI_OPEN_TYPE_ID)
308 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
309 else
310 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
311 }
312 if (dwParam1 & MCI_OPEN_ELEMENT)
313 {
314 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
315 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
316 else
317 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
318 }
319 if (dwParam1 & MCI_OPEN_ALIAS)
320 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
321 /* FIXME: this is only needed for specific types of MCI devices, and
322 * may cause a segfault if the two DWORD:s don't exist at the end of
323 * mci_openA
324 */
325 memcpy(mci_openW + 1, mci_openA + 1, 2 * sizeof(DWORD));
326 }
327 return 1;
328
329 case MCI_WINDOW:
330 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
331 {
332 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
333 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
334
335 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
336 if (!mci_windowW) return -1;
337
338 *dwParam2 = (DWORD_PTR)mci_windowW;
339
340 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
341
342 if (dwParam1 & MCI_NOTIFY)
343 mci_windowW->dwCallback = mci_windowA->dwCallback;
344 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
345 mci_windowW->hWnd = mci_windowA->hWnd;
346 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
347 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
348
349 return 1;
350 }
351 return 0;
352
353 case MCI_SYSINFO:
354 {
355 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
356 MCI_SYSINFO_PARMSW *mci_sysinfoW;
357 DWORD_PTR *ptr;
358
359 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
360 if (!ptr) return -1;
361
362 *ptr++ = *dwParam2; /* save the previous pointer */
363 *dwParam2 = (DWORD_PTR)ptr;
364 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
365
366 if (dwParam1 & MCI_NOTIFY)
367 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
368
369 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
370 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize);
371 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
372 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
373 return 1;
374 }
375 case MCI_INFO:
376 {
377 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
378 MCI_INFO_PARMSW *mci_infoW;
379 DWORD_PTR *ptr;
380
381 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
382 if (!ptr) return -1;
383
384 *ptr++ = *dwParam2; /* save the previous pointer */
385 *dwParam2 = (DWORD_PTR)ptr;
386 mci_infoW = (MCI_INFO_PARMSW *)ptr;
387
388 if (dwParam1 & MCI_NOTIFY)
389 mci_infoW->dwCallback = mci_infoA->dwCallback;
390
391 mci_infoW->dwRetSize = mci_infoA->dwRetSize * sizeof(WCHAR); /* it's not the same as SYSINFO !!! */
392 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize);
393 return 1;
394 }
395 case MCI_SAVE:
396 {
397 MCI_SAVE_PARMSA *mci_saveA = (MCI_SAVE_PARMSA *)*dwParam2;
398 MCI_SAVE_PARMSW *mci_saveW;
399
400 mci_saveW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_saveW));
401 if (!mci_saveW) return -1;
402
403 *dwParam2 = (DWORD_PTR)mci_saveW;
404 if (dwParam1 & MCI_NOTIFY)
405 mci_saveW->dwCallback = mci_saveA->dwCallback;
406 mci_saveW->lpfilename = MCI_strdupAtoW(mci_saveA->lpfilename);
407 return 1;
408 }
409 case MCI_LOAD:
410 {
411 MCI_LOAD_PARMSA *mci_loadA = (MCI_LOAD_PARMSA *)*dwParam2;
412 MCI_LOAD_PARMSW *mci_loadW;
413
414 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
415 if (!mci_loadW) return -1;
416
417 *dwParam2 = (DWORD_PTR)mci_loadW;
418 if (dwParam1 & MCI_NOTIFY)
419 mci_loadW->dwCallback = mci_loadA->dwCallback;
420 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
421 return 1;
422 }
423
424 case MCI_ESCAPE:
425 {
426 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
427 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
428
429 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
430 if (!mci_vd_escapeW) return -1;
431
432 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
433 if (dwParam1 & MCI_NOTIFY)
434 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
435 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
436 return 1;
437 }
438 default:
439 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
440 return -1;
441 }
442 }
443
444 static DWORD MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
445 DWORD result)
446 {
447 switch (msg)
448 {
449 case MCI_OPEN:
450 {
451 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
452 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
453 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)(ptr + 1);
454
455 mci_openA->wDeviceID = mci_openW->wDeviceID;
456
457 if (dwParam1 & MCI_OPEN_TYPE)
458 {
459 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
460 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
461 }
462 if (dwParam1 & MCI_OPEN_ELEMENT)
463 {
464 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
465 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
466 }
467 if (dwParam1 & MCI_OPEN_ALIAS)
468 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
469 HeapFree(GetProcessHeap(), 0, ptr);
470 }
471 break;
472 case MCI_WINDOW:
473 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
474 {
475 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
476
477 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
478 HeapFree(GetProcessHeap(), 0, mci_windowW);
479 }
480 break;
481
482 case MCI_SYSINFO:
483 {
484 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
485 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
486 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)(ptr + 1);
487
488 if (!result)
489 {
490 mci_sysinfoA->dwNumber = mci_sysinfoW->dwNumber;
491 mci_sysinfoA->wDeviceType = mci_sysinfoW->wDeviceType;
492 if (dwParam1 & MCI_SYSINFO_QUANTITY)
493 *(DWORD*)mci_sysinfoA->lpstrReturn = *(DWORD*)mci_sysinfoW->lpstrReturn;
494 else
495 WideCharToMultiByte(CP_ACP, 0,
496 mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
497 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
498 NULL, NULL);
499 }
500
501 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
502 HeapFree(GetProcessHeap(), 0, ptr);
503 }
504 break;
505 case MCI_INFO:
506 {
507 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
508 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
509 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)(ptr + 1);
510
511 if (!result)
512 {
513 WideCharToMultiByte(CP_ACP, 0,
514 mci_infoW->lpstrReturn, mci_infoW->dwRetSize / sizeof(WCHAR),
515 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
516 NULL, NULL);
517 }
518
519 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
520 HeapFree(GetProcessHeap(), 0, ptr);
521 }
522 break;
523 case MCI_SAVE:
524 {
525 MCI_SAVE_PARMSW *mci_saveW = (MCI_SAVE_PARMSW *)dwParam2;
526
527 HeapFree(GetProcessHeap(), 0, (void*)mci_saveW->lpfilename);
528 HeapFree(GetProcessHeap(), 0, mci_saveW);
529 }
530 break;
531 case MCI_LOAD:
532 {
533 MCI_LOAD_PARMSW *mci_loadW = (MCI_LOAD_PARMSW *)dwParam2;
534
535 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
536 HeapFree(GetProcessHeap(), 0, mci_loadW);
537 }
538 break;
539 case MCI_ESCAPE:
540 {
541 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
542
543 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
544 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
545 }
546 break;
547
548 default:
549 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
550 break;
551 }
552
553 return result;
554 }
555
556 /**************************************************************************
557 * MCI_GetDevTypeFromFileName [internal]
558 */
559 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
560 {
561 LPCWSTR tmp;
562 HKEY hKey;
563 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
564 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
565 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
566 if ((tmp = strrchrW(fileName, '.'))) {
567 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
568 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
569 DWORD dwLen = len;
570 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
571 RegCloseKey( hKey );
572 if (lRet == ERROR_SUCCESS) return 0;
573 }
574 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
575 }
576 return MCIERR_EXTENSION_NOT_FOUND;
577 }
578
579 #define MAX_MCICMDTABLE 20
580 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
581
582 typedef struct tagWINE_MCICMDTABLE {
583 UINT uDevType;
584 const BYTE* lpTable;
585 UINT nVerbs; /* number of verbs in command table */
586 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
587 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
588
589 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
590
591 /**************************************************************************
592 * MCI_IsCommandTableValid [internal]
593 */
594 static BOOL MCI_IsCommandTableValid(UINT uTbl)
595 {
596 const BYTE* lmem;
597 LPCWSTR str;
598 DWORD flg;
599 WORD eid;
600 int idx = 0;
601 BOOL inCst = FALSE;
602
603 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
604 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
605
606 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
607 return FALSE;
608
609 lmem = S_MciCmdTable[uTbl].lpTable;
610 do {
611 str = (LPCWSTR)lmem;
612 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
613 flg = *(const DWORD*)lmem;
614 eid = *(const WORD*)(lmem + sizeof(DWORD));
615 lmem += sizeof(DWORD) + sizeof(WORD);
616 idx ++;
617 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
618 switch (eid) {
619 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
620 case MCI_STRING: if (inCst) return FALSE; break;
621 case MCI_INTEGER: if (!*str) return FALSE; break;
622 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
623 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
624 case MCI_FLAG: if (!*str) return FALSE; break;
625 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
626 case MCI_RECT: if (!*str || inCst) return FALSE; break;
627 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
628 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
629 default: return FALSE;
630 }
631 } while (eid != MCI_END_COMMAND_LIST);
632 return TRUE;
633 }
634
635 /**************************************************************************
636 * MCI_DumpCommandTable [internal]
637 */
638 static BOOL MCI_DumpCommandTable(UINT uTbl)
639 {
640 const BYTE* lmem;
641 LPCWSTR str;
642 DWORD flg;
643 WORD eid;
644
645 if (!MCI_IsCommandTableValid(uTbl)) {
646 ERR("Ooops: %d is not valid\n", uTbl);
647 return FALSE;
648 }
649
650 lmem = S_MciCmdTable[uTbl].lpTable;
651 do {
652 do {
653 str = (LPCWSTR)lmem;
654 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
655 flg = *(const DWORD*)lmem;
656 eid = *(const WORD*)(lmem + sizeof(DWORD));
657 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
658 lmem += sizeof(DWORD) + sizeof(WORD);
659 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
660 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
661 } while (eid != MCI_END_COMMAND_LIST);
662 return TRUE;
663 }
664
665
666 /**************************************************************************
667 * MCI_GetCommandTable [internal]
668 */
669 static UINT MCI_GetCommandTable(UINT uDevType)
670 {
671 UINT uTbl;
672 WCHAR buf[32];
673 LPCWSTR str = NULL;
674
675 /* first look up existing for existing devType */
676 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
677 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
678 return uTbl;
679 }
680
681 /* well try to load id */
682 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
683 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
684 str = buf;
685 }
686 } else if (uDevType == 0) {
687 static const WCHAR wszCore[] = {'C','O','R','E',0};
688 str = wszCore;
689 }
690 uTbl = MCI_NO_COMMAND_TABLE;
691 if (str) {
692 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
693 HANDLE hMem = 0;
694
695 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
696 if (hMem) {
697 uTbl = MCI_SetCommandTable(LockResource(hMem), uDevType);
698 } else {
699 WARN("No command table found in resource %p[%s]\n",
700 hWinMM32Instance, debugstr_w(str));
701 }
702 }
703 TRACE("=> %d\n", uTbl);
704 return uTbl;
705 }
706
707 /**************************************************************************
708 * MCI_SetCommandTable [internal]
709 */
710 UINT MCI_SetCommandTable(void *table, UINT uDevType)
711 {
712 int uTbl;
713 static BOOL bInitDone = FALSE;
714
715 /* <HACK>
716 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
717 * can be called with 0 as a uDevType to retrieve it.
718 * </HACK>
719 */
720 if (!bInitDone) {
721 bInitDone = TRUE;
722 MCI_GetCommandTable(0);
723 }
724 TRACE("(%p, %u)\n", table, uDevType);
725 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
726 if (!S_MciCmdTable[uTbl].lpTable) {
727 const BYTE* lmem;
728 LPCWSTR str;
729 WORD eid;
730 WORD count;
731
732 S_MciCmdTable[uTbl].uDevType = uDevType;
733 S_MciCmdTable[uTbl].lpTable = table;
734
735 if (TRACE_ON(mci)) {
736 MCI_DumpCommandTable(uTbl);
737 }
738
739 /* create the verbs table */
740 /* get # of entries */
741 lmem = S_MciCmdTable[uTbl].lpTable;
742 count = 0;
743 do {
744 str = (LPCWSTR)lmem;
745 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
746 eid = *(const WORD*)(lmem + sizeof(DWORD));
747 lmem += sizeof(DWORD) + sizeof(WORD);
748 if (eid == MCI_COMMAND_HEAD)
749 count++;
750 } while (eid != MCI_END_COMMAND_LIST);
751
752 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
753 S_MciCmdTable[uTbl].nVerbs = count;
754
755 lmem = S_MciCmdTable[uTbl].lpTable;
756 count = 0;
757 do {
758 str = (LPCWSTR)lmem;
759 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
760 eid = *(const WORD*)(lmem + sizeof(DWORD));
761 lmem += sizeof(DWORD) + sizeof(WORD);
762 if (eid == MCI_COMMAND_HEAD)
763 S_MciCmdTable[uTbl].aVerbs[count++] = str;
764 } while (eid != MCI_END_COMMAND_LIST);
765 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
766 return uTbl;
767 }
768 }
769
770 return MCI_NO_COMMAND_TABLE;
771 }
772
773 /**************************************************************************
774 * MCI_DeleteCommandTable [internal]
775 */
776 BOOL MCI_DeleteCommandTable(UINT uTbl, BOOL delete)
777 {
778 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
779 return FALSE;
780
781 if (delete) HeapFree(GetProcessHeap(), 0, (void*)S_MciCmdTable[uTbl].lpTable);
782 S_MciCmdTable[uTbl].lpTable = NULL;
783 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTbl].aVerbs);
784 S_MciCmdTable[uTbl].aVerbs = 0;
785 return TRUE;
786 }
787
788 /**************************************************************************
789 * MCI_UnLoadMciDriver [internal]
790 */
791 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
792 {
793 LPWINE_MCIDRIVER* tmp;
794
795 if (!wmd)
796 return TRUE;
797
798 CloseDriver(wmd->hDriver, 0, 0);
799
800 if (wmd->dwPrivate != 0)
801 WARN("Unloading mci driver with non nul dwPrivate field\n");
802
803 EnterCriticalSection(&WINMM_cs);
804 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
805 if (*tmp == wmd) {
806 *tmp = wmd->lpNext;
807 break;
808 }
809 }
810 LeaveCriticalSection(&WINMM_cs);
811
812 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
813 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
814 HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
815
816 HeapFree(GetProcessHeap(), 0, wmd);
817 return TRUE;
818 }
819
820 /**************************************************************************
821 * MCI_OpenMciDriver [internal]
822 */
823 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
824 {
825 WCHAR libName[128];
826
827 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
828 return FALSE;
829
830 wmd->bIs32 = 0xFFFF;
831 /* First load driver */
832 if ((wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp))) {
833 wmd->bIs32 = TRUE;
834 } else if (WINMM_CheckForMMSystem() && pFnMciMapMsg32WTo16) {
835 WINMM_MapType res;
836
837 switch (res = pFnMciMapMsg32WTo16(0, DRV_OPEN, 0, &lp)) {
838 case WINMM_MAP_MSGERROR:
839 TRACE("Not handled yet (DRV_OPEN)\n");
840 break;
841 case WINMM_MAP_NOMEM:
842 TRACE("Problem mapping msg=DRV_OPEN from 32W to 16\n");
843 break;
844 case WINMM_MAP_OK:
845 case WINMM_MAP_OKMEM:
846 if ((wmd->hDriver = OpenDriver(drvTyp, wszMci, lp)))
847 wmd->bIs32 = FALSE;
848 if (res == WINMM_MAP_OKMEM)
849 pFnMciUnMapMsg32WTo16(0, DRV_OPEN, 0, lp);
850 break;
851 }
852 }
853 return (wmd->bIs32 == 0xFFFF) ? FALSE : TRUE;
854 }
855
856 /**************************************************************************
857 * MCI_LoadMciDriver [internal]
858 */
859 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
860 {
861 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
862 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
863 MCI_OPEN_DRIVER_PARMSW modp;
864 DWORD dwRet = 0;
865
866 if (!wmd || !strDevTyp) {
867 dwRet = MCIERR_OUT_OF_MEMORY;
868 goto errCleanUp;
869 }
870
871 wmd->lpfnYieldProc = MCI_DefYieldProc;
872 wmd->dwYieldData = VK_CANCEL;
873 wmd->CreatorThread = GetCurrentThreadId();
874
875 EnterCriticalSection(&WINMM_cs);
876 /* wmd must be inserted in list before sending opening the driver, because it
877 * may want to lookup at wDevID
878 */
879 wmd->lpNext = MciDrivers;
880 MciDrivers = wmd;
881
882 for (modp.wDeviceID = MCI_MAGIC;
883 MCI_GetDriver(modp.wDeviceID) != 0;
884 modp.wDeviceID++);
885
886 wmd->wDeviceID = modp.wDeviceID;
887
888 LeaveCriticalSection(&WINMM_cs);
889
890 TRACE("wDevID=%04X\n", modp.wDeviceID);
891
892 modp.lpstrParams = NULL;
893
894 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
895 /* silence warning if all is used... some bogus program use commands like
896 * 'open all'...
897 */
898 if (strcmpiW(strDevTyp, wszAll) == 0) {
899 dwRet = MCIERR_CANNOT_USE_ALL;
900 } else {
901 FIXME("Couldn't load driver for type %s.\n",
902 debugstr_w(strDevTyp));
903 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
904 }
905 goto errCleanUp;
906 }
907
908 /* FIXME: should also check that module's description is of the form
909 * MODULENAME:[MCI] comment
910 */
911
912 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
913 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
914 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
915
916 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
917 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
918
919 wmd->lpstrDeviceType = strDevTyp;
920 wmd->wType = modp.wType;
921
922 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
923 modp.wDeviceID, modp.wType, modp.wDeviceID);
924 *lpwmd = wmd;
925 return 0;
926 errCleanUp:
927 MCI_UnLoadMciDriver(wmd);
928 HeapFree(GetProcessHeap(), 0, strDevTyp);
929 *lpwmd = 0;
930 return dwRet;
931 }
932
933 /**************************************************************************
934 * MCI_FinishOpen [internal]
935 */
936 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
937 DWORD dwParam)
938 {
939 if (dwParam & MCI_OPEN_ELEMENT)
940 {
941 wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
942 strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
943 }
944 if (dwParam & MCI_OPEN_ALIAS)
945 {
946 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
947 strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
948 }
949 lpParms->wDeviceID = wmd->wDeviceID;
950
951 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
952 (DWORD)lpParms);
953 }
954
955 /**************************************************************************
956 * MCI_FindCommand [internal]
957 */
958 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
959 {
960 UINT idx;
961
962 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
963 return NULL;
964
965 /* another improvement would be to have the aVerbs array sorted,
966 * so that we could use a dichotomic search on it, rather than this dumb
967 * array look up
968 */
969 for (idx = 0; idx < S_Mc