include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / winmm / mci.c
blob959801aa835c8e084c00fd8b9ab8c83d5dd1a5d0
1 /*
2 * MCI internal functions
4 * Copyright 1998/1999 Eric Pouech
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.
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.
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
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
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "wingdi.h"
48 #include "mmsystem.h"
49 #include "winuser.h"
50 #include "winnls.h"
51 #include "winreg.h"
52 #include "wownt32.h"
54 #include "digitalv.h"
55 #include "winemm.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(mci);
61 /* First MCI valid device ID (0 means error) */
62 #define MCI_MAGIC 0x0001
64 /* MCI settings */
65 static const WCHAR wszHklmMci [] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\MCI";
67 static WINE_MCIDRIVER *MciDrivers;
69 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
70 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
72 /* dup a string and uppercase it */
73 static inline LPWSTR str_dup_upper( LPCWSTR str )
75 WCHAR *p = wcsdup( str );
76 if (p) CharUpperW( p );
77 return p;
80 /**************************************************************************
81 * MCI_GetDriver [internal]
83 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
85 LPWINE_MCIDRIVER wmd = 0;
87 EnterCriticalSection(&WINMM_cs);
88 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
89 if (wmd->wDeviceID == wDevID)
90 break;
92 LeaveCriticalSection(&WINMM_cs);
93 return wmd;
96 /**************************************************************************
97 * MCI_GetDriverFromString [internal]
99 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
101 LPWINE_MCIDRIVER wmd;
102 UINT ret = 0;
104 if (!lpstrName)
105 return 0;
107 if (!wcsicmp(lpstrName, L"ALL"))
108 return MCI_ALL_DEVICE_ID;
110 EnterCriticalSection(&WINMM_cs);
111 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
112 if (wmd->lpstrAlias && wcsicmp(wmd->lpstrAlias, lpstrName) == 0) {
113 ret = wmd->wDeviceID;
114 break;
117 LeaveCriticalSection(&WINMM_cs);
119 return ret;
122 /**************************************************************************
123 * MCI_MessageToString [internal]
125 static const char* MCI_MessageToString(UINT wMsg)
127 #define CASE(s) case (s): return #s
129 switch (wMsg) {
130 CASE(DRV_LOAD);
131 CASE(DRV_ENABLE);
132 CASE(DRV_OPEN);
133 CASE(DRV_CLOSE);
134 CASE(DRV_DISABLE);
135 CASE(DRV_FREE);
136 CASE(DRV_CONFIGURE);
137 CASE(DRV_QUERYCONFIGURE);
138 CASE(DRV_INSTALL);
139 CASE(DRV_REMOVE);
140 CASE(DRV_EXITSESSION);
141 CASE(DRV_EXITAPPLICATION);
142 CASE(DRV_POWER);
143 CASE(MCI_BREAK);
144 CASE(MCI_CLOSE);
145 CASE(MCI_CLOSE_DRIVER);
146 CASE(MCI_COPY);
147 CASE(MCI_CUE);
148 CASE(MCI_CUT);
149 CASE(MCI_DELETE);
150 CASE(MCI_ESCAPE);
151 CASE(MCI_FREEZE);
152 CASE(MCI_PAUSE);
153 CASE(MCI_PLAY);
154 CASE(MCI_GETDEVCAPS);
155 CASE(MCI_INFO);
156 CASE(MCI_LOAD);
157 CASE(MCI_OPEN);
158 CASE(MCI_OPEN_DRIVER);
159 CASE(MCI_PASTE);
160 CASE(MCI_PUT);
161 CASE(MCI_REALIZE);
162 CASE(MCI_RECORD);
163 CASE(MCI_RESUME);
164 CASE(MCI_SAVE);
165 CASE(MCI_SEEK);
166 CASE(MCI_SET);
167 CASE(MCI_SOUND);
168 CASE(MCI_SPIN);
169 CASE(MCI_STATUS);
170 CASE(MCI_STEP);
171 CASE(MCI_STOP);
172 CASE(MCI_SYSINFO);
173 CASE(MCI_UNFREEZE);
174 CASE(MCI_UPDATE);
175 CASE(MCI_WHERE);
176 CASE(MCI_WINDOW);
177 /* constants for digital video */
178 CASE(MCI_CAPTURE);
179 CASE(MCI_MONITOR);
180 CASE(MCI_RESERVE);
181 CASE(MCI_SETAUDIO);
182 CASE(MCI_SIGNAL);
183 CASE(MCI_SETVIDEO);
184 CASE(MCI_QUALITY);
185 CASE(MCI_LIST);
186 CASE(MCI_UNDO);
187 CASE(MCI_CONFIGURE);
188 CASE(MCI_RESTORE);
189 #undef CASE
190 default:
191 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
195 static LPWSTR MCI_strdupAtoW( LPCSTR str )
197 LPWSTR ret;
198 INT len;
200 if (!str) return NULL;
201 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
202 ret = malloc( len * sizeof(WCHAR) );
203 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
204 return ret;
207 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
209 if (msg < DRV_RESERVED) return 0;
211 switch (msg)
213 case MCI_CLOSE:
214 case MCI_CONFIGURE:
215 case MCI_PLAY:
216 case MCI_SEEK:
217 case MCI_STOP:
218 case MCI_PAUSE:
219 case MCI_GETDEVCAPS:
220 case MCI_SPIN:
221 case MCI_SET:
222 case MCI_STEP:
223 case MCI_RECORD:
224 case MCI_BREAK:
225 case MCI_STATUS:
226 case MCI_CUE:
227 case MCI_REALIZE:
228 case MCI_PUT:
229 case MCI_WHERE:
230 case MCI_FREEZE:
231 case MCI_UNFREEZE:
232 case MCI_CUT:
233 case MCI_COPY:
234 case MCI_PASTE:
235 case MCI_UPDATE:
236 case MCI_RESUME:
237 case MCI_DELETE:
238 case MCI_MONITOR:
239 case MCI_SIGNAL:
240 case MCI_UNDO:
241 return 0;
243 case MCI_OPEN:
244 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
245 * structure, larger than MCI_WAVE_OPEN_PARMS */
246 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
247 MCI_ANIM_OPEN_PARMSW *mci_openW;
248 DWORD_PTR *ptr;
250 ptr = malloc(sizeof(DWORD_PTR) + sizeof(*mci_openW));
251 if (!ptr) return -1;
253 *ptr++ = *dwParam2; /* save the previous pointer */
254 *dwParam2 = (DWORD_PTR)ptr;
255 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
257 if (dwParam1 & MCI_NOTIFY)
258 mci_openW->dwCallback = mci_openA->dwCallback;
260 if (dwParam1 & MCI_OPEN_TYPE)
262 if (dwParam1 & MCI_OPEN_TYPE_ID)
263 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
264 else
265 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
267 if (dwParam1 & MCI_OPEN_ELEMENT)
269 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
270 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
271 else
272 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
274 if (dwParam1 & MCI_OPEN_ALIAS)
275 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
276 /* We don't know how many DWORD follow, as
277 * the structure depends on the device. */
278 if (HIWORD(dwParam1))
279 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
281 return 1;
283 case MCI_WINDOW:
284 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
286 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
287 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
289 mci_windowW = malloc(sizeof(*mci_windowW));
290 if (!mci_windowW) return -1;
292 *dwParam2 = (DWORD_PTR)mci_windowW;
294 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
296 if (dwParam1 & MCI_NOTIFY)
297 mci_windowW->dwCallback = mci_windowA->dwCallback;
298 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
299 mci_windowW->hWnd = mci_windowA->hWnd;
300 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
301 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
303 return 1;
305 return 0;
307 case MCI_SYSINFO:
308 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
310 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
311 MCI_SYSINFO_PARMSW *mci_sysinfoW;
312 DWORD_PTR *ptr;
314 ptr = malloc(sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
315 if (!ptr) return -1;
317 *ptr++ = *dwParam2; /* save the previous pointer */
318 *dwParam2 = (DWORD_PTR)ptr;
319 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
321 if (dwParam1 & MCI_NOTIFY)
322 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
324 /* Size is measured in numbers of characters, despite what MSDN says. */
325 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
326 mci_sysinfoW->lpstrReturn = malloc(mci_sysinfoW->dwRetSize * sizeof(WCHAR));
327 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
328 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
329 return 1;
331 return 0;
332 case MCI_INFO:
334 MCI_DGV_INFO_PARMSA *mci_infoA = (MCI_DGV_INFO_PARMSA *)*dwParam2;
335 MCI_DGV_INFO_PARMSW *mci_infoW;
336 DWORD_PTR *ptr;
338 ptr = malloc(sizeof(*mci_infoW) + sizeof(DWORD_PTR));
339 if (!ptr) return -1;
341 *ptr++ = *dwParam2; /* save the previous pointer */
342 *dwParam2 = (DWORD_PTR)ptr;
343 mci_infoW = (MCI_DGV_INFO_PARMSW *)ptr;
345 if (dwParam1 & MCI_NOTIFY)
346 mci_infoW->dwCallback = mci_infoA->dwCallback;
348 /* Size is measured in numbers of characters. */
349 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
350 mci_infoW->lpstrReturn = malloc(mci_infoW->dwRetSize * sizeof(WCHAR));
351 if (dwParam1 & MCI_DGV_INFO_ITEM)
352 mci_infoW->dwItem = mci_infoA->dwItem;
353 return 1;
355 case MCI_SAVE:
356 case MCI_LOAD:
357 case MCI_CAPTURE:
358 case MCI_RESTORE:
359 { /* All these commands have the same layout: callback + string + optional rect */
360 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
361 MCI_OVLY_LOAD_PARMSW *mci_loadW;
363 mci_loadW = malloc(sizeof(*mci_loadW));
364 if (!mci_loadW) return -1;
366 *dwParam2 = (DWORD_PTR)mci_loadW;
367 if (dwParam1 & MCI_NOTIFY)
368 mci_loadW->dwCallback = mci_loadA->dwCallback;
369 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
370 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
371 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
372 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
373 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
374 mci_loadW->rc = mci_loadA->rc;
375 return 1;
377 case MCI_SOUND:
378 case MCI_ESCAPE:
379 { /* All these commands have the same layout: callback + string */
380 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
381 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
383 mci_vd_escapeW = malloc(sizeof(*mci_vd_escapeW));
384 if (!mci_vd_escapeW) return -1;
386 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
387 if (dwParam1 & MCI_NOTIFY)
388 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
389 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
390 return 1;
392 case MCI_SETAUDIO:
393 case MCI_SETVIDEO:
394 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
395 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
396 return 0;
397 /* fall through to default */
398 case MCI_RESERVE:
399 case MCI_QUALITY:
400 case MCI_LIST:
401 default:
402 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
403 return 0; /* pass through untouched */
407 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
408 DWORD result)
410 switch (msg)
412 case MCI_OPEN:
414 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
415 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
416 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
418 mci_openA->wDeviceID = mci_openW->wDeviceID;
420 if (dwParam1 & MCI_OPEN_TYPE)
422 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
423 free((WCHAR *)mci_openW->lpstrDeviceType);
425 if (dwParam1 & MCI_OPEN_ELEMENT)
427 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
428 free((WCHAR *)mci_openW->lpstrElementName);
430 if (dwParam1 & MCI_OPEN_ALIAS)
431 free((WCHAR *)mci_openW->lpstrAlias);
432 free(ptr);
434 break;
435 case MCI_WINDOW:
436 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
438 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
440 free((void *)mci_windowW->lpstrText);
441 free(mci_windowW);
443 break;
445 case MCI_SYSINFO:
446 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
448 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
449 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
450 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
452 if (!result)
454 WideCharToMultiByte(CP_ACP, 0,
455 mci_sysinfoW->lpstrReturn, -1,
456 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
457 NULL, NULL);
460 free(mci_sysinfoW->lpstrReturn);
461 free(ptr);
463 break;
464 case MCI_INFO:
466 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
467 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
468 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
470 if (!result)
472 WideCharToMultiByte(CP_ACP, 0,
473 mci_infoW->lpstrReturn, -1,
474 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
475 NULL, NULL);
478 free(mci_infoW->lpstrReturn);
479 free(ptr);
481 break;
482 case MCI_SAVE:
483 case MCI_LOAD:
484 case MCI_CAPTURE:
485 case MCI_RESTORE:
486 { /* All these commands have the same layout: callback + string + optional rect */
487 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
489 free((void *)mci_loadW->lpfilename);
490 free(mci_loadW);
492 break;
493 case MCI_SOUND:
494 case MCI_ESCAPE:
495 { /* All these commands have the same layout: callback + string */
496 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
498 free((void *)mci_vd_escapeW->lpstrCommand);
499 free(mci_vd_escapeW);
501 break;
503 default:
504 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
505 break;
509 /**************************************************************************
510 * MCI_GetDevTypeFromFileName [internal]
512 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
514 LPCWSTR tmp;
515 HKEY hKey;
516 if ((tmp = wcsrchr(fileName, '.'))) {
517 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE,
518 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions",
519 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
520 DWORD dwLen = len;
521 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
522 RegCloseKey( hKey );
523 if (lRet == ERROR_SUCCESS) return 0;
525 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
527 return MCIERR_EXTENSION_NOT_FOUND;
530 /**************************************************************************
531 * MCI_GetDevTypeFromResource [internal]
533 static UINT MCI_GetDevTypeFromResource(LPCWSTR lpstrName)
535 WCHAR buf[32];
536 UINT uDevType;
537 for (uDevType = MCI_DEVTYPE_FIRST; uDevType <= MCI_DEVTYPE_LAST; uDevType++) {
538 if (LoadStringW(hWinMM32Instance, uDevType, buf, ARRAY_SIZE(buf))) {
539 /* FIXME: ignore digits suffix */
540 if (!wcsicmp(buf, lpstrName))
541 return uDevType;
544 return 0;
547 #define MAX_MCICMDTABLE 20
548 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
550 typedef struct tagWINE_MCICMDTABLE {
551 UINT uDevType;
552 HGLOBAL hMem;
553 const BYTE* lpTable;
554 UINT nVerbs; /* number of verbs in command table */
555 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
556 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
558 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
560 /**************************************************************************
561 * MCI_IsCommandTableValid [internal]
563 static BOOL MCI_IsCommandTableValid(UINT uTbl)
565 const BYTE* lmem;
566 LPCWSTR str;
567 DWORD flg;
568 WORD eid;
569 int idx = 0;
570 BOOL inCst = FALSE;
572 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
573 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
575 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
576 return FALSE;
578 lmem = S_MciCmdTable[uTbl].lpTable;
579 do {
580 str = (LPCWSTR)lmem;
581 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
582 flg = *(const DWORD*)lmem;
583 eid = *(const WORD*)(lmem + sizeof(DWORD));
584 lmem += sizeof(DWORD) + sizeof(WORD);
585 idx ++;
586 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
587 switch (eid) {
588 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
589 case MCI_STRING: if (inCst) return FALSE; break;
590 case MCI_HWND: /* Occurs inside MCI_CONSTANT as in "window handle default" */
591 case MCI_HPAL:
592 case MCI_HDC:
593 case MCI_INTEGER: if (!*str) return FALSE; break;
594 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
595 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
596 case MCI_FLAG: if (!*str) return FALSE; break;
597 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
598 case MCI_RECT: if (!*str || inCst) return FALSE; break;
599 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
600 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
601 default: return FALSE;
603 } while (eid != MCI_END_COMMAND_LIST);
604 return TRUE;
607 /**************************************************************************
608 * MCI_DumpCommandTable [internal]
610 static BOOL MCI_DumpCommandTable(UINT uTbl)
612 const BYTE* lmem;
613 LPCWSTR str;
614 WORD eid;
616 if (!MCI_IsCommandTableValid(uTbl)) {
617 ERR("Ooops: %d is not valid\n", uTbl);
618 return FALSE;
621 lmem = S_MciCmdTable[uTbl].lpTable;
622 do {
623 do {
624 /* DWORD flg; */
625 str = (LPCWSTR)lmem;
626 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
627 /* flg = *(const DWORD*)lmem; */
628 eid = *(const WORD*)(lmem + sizeof(DWORD));
629 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
630 lmem += sizeof(DWORD) + sizeof(WORD);
631 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
632 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
633 } while (eid != MCI_END_COMMAND_LIST);
634 return TRUE;
638 /**************************************************************************
639 * MCI_GetCommandTable [internal]
641 static UINT MCI_GetCommandTable(UINT uDevType)
643 UINT uTbl;
644 WCHAR buf[32];
645 LPCWSTR str = NULL;
647 /* first look up existing for existing devType */
648 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
649 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
650 return uTbl;
653 /* well try to load id */
654 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
655 if (LoadStringW(hWinMM32Instance, uDevType, buf, ARRAY_SIZE(buf))) {
656 str = buf;
658 } else if (uDevType == 0) {
659 str = L"CORE";
661 uTbl = MCI_NO_COMMAND_TABLE;
662 if (str) {
663 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
664 HANDLE hMem = 0;
666 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
667 if (hMem) {
668 uTbl = MCI_SetCommandTable(hMem, uDevType);
669 } else {
670 WARN("No command table found in resource %p[%s]\n",
671 hWinMM32Instance, debugstr_w(str));
674 TRACE("=> %d\n", uTbl);
675 return uTbl;
678 /**************************************************************************
679 * MCI_SetCommandTable [internal]
681 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
683 int uTbl;
684 static BOOL bInitDone = FALSE;
686 /* <HACK>
687 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
688 * can be called with 0 as a uDevType to retrieve it.
689 * </HACK>
691 if (!bInitDone) {
692 bInitDone = TRUE;
693 MCI_GetCommandTable(0);
695 TRACE("(%p, %u)\n", hMem, uDevType);
696 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
697 if (!S_MciCmdTable[uTbl].lpTable) {
698 const BYTE* lmem;
699 LPCWSTR str;
700 WORD eid;
701 WORD count;
703 S_MciCmdTable[uTbl].uDevType = uDevType;
704 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
705 S_MciCmdTable[uTbl].hMem = hMem;
707 if (TRACE_ON(mci)) {
708 MCI_DumpCommandTable(uTbl);
711 /* create the verbs table */
712 /* get # of entries */
713 lmem = S_MciCmdTable[uTbl].lpTable;
714 count = 0;
715 do {
716 str = (LPCWSTR)lmem;
717 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
718 eid = *(const WORD*)(lmem + sizeof(DWORD));
719 lmem += sizeof(DWORD) + sizeof(WORD);
720 if (eid == MCI_COMMAND_HEAD)
721 count++;
722 } while (eid != MCI_END_COMMAND_LIST);
724 S_MciCmdTable[uTbl].aVerbs = malloc(count * sizeof(const WCHAR *));
725 S_MciCmdTable[uTbl].nVerbs = count;
727 lmem = S_MciCmdTable[uTbl].lpTable;
728 count = 0;
729 do {
730 str = (LPCWSTR)lmem;
731 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
732 eid = *(const WORD*)(lmem + sizeof(DWORD));
733 lmem += sizeof(DWORD) + sizeof(WORD);
734 if (eid == MCI_COMMAND_HEAD)
735 S_MciCmdTable[uTbl].aVerbs[count++] = str;
736 } while (eid != MCI_END_COMMAND_LIST);
737 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
738 return uTbl;
742 return MCI_NO_COMMAND_TABLE;
745 /**************************************************************************
746 * MCI_UnLoadMciDriver [internal]
748 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
750 LPWINE_MCIDRIVER* tmp;
752 if (!wmd)
753 return TRUE;
755 CloseDriver(wmd->hDriver, 0, 0);
757 if (wmd->dwPrivate != 0)
758 WARN("Unloading mci driver with non nul dwPrivate field\n");
760 EnterCriticalSection(&WINMM_cs);
761 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
762 if (*tmp == wmd) {
763 *tmp = wmd->lpNext;
764 break;
767 LeaveCriticalSection(&WINMM_cs);
769 free(wmd->lpstrDeviceType);
770 free(wmd->lpstrAlias);
772 free(wmd);
773 return TRUE;
776 /**************************************************************************
777 * MCI_OpenMciDriver [internal]
779 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
781 WCHAR libName[128];
783 if (!DRIVER_GetLibName(drvTyp, L"MCI", libName, sizeof(libName)))
784 return FALSE;
786 /* First load driver */
787 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
788 return wmd->hDriver != NULL;
791 /**************************************************************************
792 * MCI_LoadMciDriver [internal]
794 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
796 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
797 WINE_MCIDRIVER* wmd = calloc(1, sizeof(*wmd));
798 MCI_OPEN_DRIVER_PARMSW modp;
799 DWORD dwRet = 0;
801 if (!wmd || !strDevTyp) {
802 dwRet = MCIERR_OUT_OF_MEMORY;
803 goto errCleanUp;
806 wmd->lpfnYieldProc = MCI_DefYieldProc;
807 wmd->dwYieldData = VK_CANCEL;
808 wmd->CreatorThread = GetCurrentThreadId();
810 EnterCriticalSection(&WINMM_cs);
811 /* wmd must be inserted in list before sending opening the driver, because it
812 * may want to lookup at wDevID
814 wmd->lpNext = MciDrivers;
815 MciDrivers = wmd;
817 for (modp.wDeviceID = MCI_MAGIC;
818 MCI_GetDriver(modp.wDeviceID) != 0;
819 modp.wDeviceID++);
821 wmd->wDeviceID = modp.wDeviceID;
823 LeaveCriticalSection(&WINMM_cs);
825 TRACE("wDevID=%04X\n", modp.wDeviceID);
827 modp.lpstrParams = NULL;
829 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
830 /* silence warning if all is used... some bogus program use commands like
831 * 'open all'...
833 if (wcsicmp(strDevTyp, L"ALL") == 0) {
834 dwRet = MCIERR_CANNOT_USE_ALL;
835 } else {
836 FIXME("Couldn't load driver for type %s.\n",
837 debugstr_w(strDevTyp));
838 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
840 goto errCleanUp;
843 /* FIXME: should also check that module's description is of the form
844 * MODULENAME:[MCI] comment
847 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
848 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
849 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
851 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
852 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
854 wmd->lpstrDeviceType = strDevTyp;
855 wmd->wType = modp.wType;
857 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
858 modp.wDeviceID, modp.wType, modp.wDeviceID);
859 *lpwmd = wmd;
860 return 0;
861 errCleanUp:
862 MCI_UnLoadMciDriver(wmd);
863 free(strDevTyp);
864 *lpwmd = 0;
865 return dwRet;
868 /**************************************************************************
869 * MCI_SendCommandFrom32 [internal]
871 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
873 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
874 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
876 if (wmd) {
877 if(wmd->CreatorThread != GetCurrentThreadId())
878 return MCIERR_INVALID_DEVICE_NAME;
880 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
882 return dwRet;
885 /**************************************************************************
886 * MCI_FinishOpen [internal]
888 * Three modes of operation:
889 * 1 open foo.ext ... -> OPEN_ELEMENT with lpstrElementName=foo.ext
890 * open sequencer!foo.ext same with lpstrElementName=foo.ext
891 * 2 open new type waveaudio -> OPEN_ELEMENT with empty ("") lpstrElementName
892 * 3 open sequencer -> OPEN_ELEMENT unset, and
893 * capability sequencer (auto-open) likewise
895 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
896 DWORD dwParam)
898 LPCWSTR alias = NULL;
899 /* Open always defines an alias for further reference */
900 if (dwParam & MCI_OPEN_ALIAS) { /* open ... alias */
901 alias = lpParms->lpstrAlias;
902 if (MCI_GetDriverFromString(alias))
903 return MCIERR_DUPLICATE_ALIAS;
904 } else {
905 if ((dwParam & MCI_OPEN_ELEMENT) /* open file.wav */
906 && !(dwParam & MCI_OPEN_ELEMENT_ID))
907 alias = lpParms->lpstrElementName;
908 else if (dwParam & MCI_OPEN_TYPE ) /* open cdaudio */
909 alias = wmd->lpstrDeviceType;
910 if (alias && MCI_GetDriverFromString(alias))
911 return MCIERR_DEVICE_OPEN;
913 if (alias) {
914 wmd->lpstrAlias = wcsdup(alias);
915 if (!wmd->lpstrAlias) return MCIERR_OUT_OF_MEMORY;
916 /* In most cases, natives adds MCI_OPEN_ALIAS to the flags passed to the driver.
917 * Don't. The drivers don't care about the winmm alias. */
919 lpParms->wDeviceID = wmd->wDeviceID;
921 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
922 (DWORD_PTR)lpParms);
925 /**************************************************************************
926 * MCI_FindCommand [internal]
928 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
930 UINT idx;
932 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
933 return NULL;
935 /* another improvement would be to have the aVerbs array sorted,
936 * so that we could use a dichotomic search on it, rather than this dumb
937 * array look up
939 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
940 if (wcsicmp(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
941 return S_MciCmdTable[uTbl].aVerbs[idx];
944 return NULL;
947 /**************************************************************************
948 * MCI_GetReturnType [internal]
950 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
952 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + lstrlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
953 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
954 return *(const DWORD*)(lpCmd + 1);
956 return 0L;
959 /**************************************************************************
960 * MCI_GetMessage [internal]
962 static WORD MCI_GetMessage(LPCWSTR lpCmd)
964 return (WORD)*(const DWORD*)(lpCmd + lstrlenW(lpCmd) + 1);
967 /**************************************************************************
968 * MCI_GetDWord [internal]
970 * Accept 0 -1 255 255:0 255:255:255:255 :::1 1::: 2::3 ::4: 12345678
971 * Refuse -1:0 0:-1 :: 256:0 1:256 0::::1
973 static BOOL MCI_GetDWord(DWORD* data, LPWSTR* ptr)
975 LPWSTR ret = *ptr;
976 DWORD total = 0, shift = 0;
977 BOOL sign = FALSE, digits = FALSE;
979 while (*ret == ' ' || *ret == '\t') ret++;
980 if (*ret == '-') {
981 ret++;
982 sign = TRUE;
984 for(;;) {
985 DWORD val = 0;
986 while ('0' <= *ret && *ret <= '9') {
987 val = *ret++ - '0' + 10 * val;
988 digits = TRUE;
990 switch (*ret) {
991 case '\0': break;
992 case '\t':
993 case ' ': ret++; break;
994 default: return FALSE;
995 case ':':
996 if ((val >= 256) || (shift >= 24)) return FALSE;
997 total |= val << shift;
998 shift += 8;
999 ret++;
1000 continue;
1003 if (!digits) return FALSE;
1004 if (shift && (val >= 256 || sign)) return FALSE;
1005 total |= val << shift;
1006 *data = sign ? -total : total;
1007 *ptr = ret;
1008 return TRUE;
1012 /**************************************************************************
1013 * MCI_GetString [internal]
1015 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1017 LPWSTR ptr = *args;
1019 /* see if we have a quoted string */
1020 if (*ptr == '"') {
1021 ptr = wcschr(*str = ptr + 1, '"');
1022 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1023 /* FIXME: shall we escape \" from string ?? */
1024 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1025 *ptr++ = '\0'; /* remove trailing " */
1026 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1027 } else {
1028 ptr = wcschr(ptr, ' ');
1030 if (ptr) {
1031 *ptr++ = '\0';
1032 } else {
1033 ptr = *args + lstrlenW(*args);
1035 *str = *args;
1038 *args = ptr;
1039 return 0;
1042 #define MCI_DATA_SIZE 16
1044 /**************************************************************************
1045 * MCI_ParseOptArgs [internal]
1047 static DWORD MCI_ParseOptArgs(DWORD* data, int _offset, LPCWSTR lpCmd,
1048 LPWSTR args, LPDWORD dwFlags)
1050 int len, offset;
1051 const char* lmem;
1052 LPCWSTR str;
1053 DWORD dwRet, flg, cflg = 0;
1054 WORD eid;
1055 BOOL inCst, found;
1057 /* loop on arguments */
1058 while (*args) {
1059 lmem = (const char*)lpCmd;
1060 found = inCst = FALSE;
1061 offset = _offset;
1063 /* skip any leading white space(s) */
1064 while (*args == ' ') args++;
1065 TRACE("args=%s\n", debugstr_w(args));
1067 do { /* loop on options for command table for the requested verb */
1068 str = (LPCWSTR)lmem;
1069 lmem += ((len = lstrlenW(str)) + 1) * sizeof(WCHAR);
1070 flg = *(const DWORD*)lmem;
1071 eid = *(const WORD*)(lmem + sizeof(DWORD));
1072 lmem += sizeof(DWORD) + sizeof(WORD);
1073 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1075 switch (eid) {
1076 case MCI_CONSTANT:
1077 inCst = TRUE; cflg = flg; break;
1078 case MCI_END_CONSTANT:
1079 /* there may be additional integral values after flag in constant */
1080 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1081 *dwFlags |= cflg;
1083 inCst = FALSE; cflg = 0;
1084 break;
1085 case MCI_RETURN:
1086 if (offset != _offset) {
1087 FIXME("MCI_RETURN not in first position\n");
1088 return MCIERR_PARSER_INTERNAL;
1092 if (wcsnicmp(args, str, len) == 0 &&
1093 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1094 /* store good values into data[] */
1095 args += len;
1096 while (*args == ' ') args++;
1097 found = TRUE;
1099 switch (eid) {
1100 case MCI_COMMAND_HEAD:
1101 case MCI_RETURN:
1102 case MCI_END_COMMAND:
1103 case MCI_END_COMMAND_LIST:
1104 case MCI_CONSTANT: /* done above */
1105 case MCI_END_CONSTANT: /* done above */
1106 break;
1107 case MCI_FLAG:
1108 *dwFlags |= flg;
1109 TRACE("flag=%08lx\n", flg);
1110 break;
1111 case MCI_HWND:
1112 case MCI_HPAL:
1113 case MCI_HDC:
1114 case MCI_INTEGER:
1115 if (inCst) {
1116 data[offset] |= flg;
1117 *dwFlags |= cflg;
1118 inCst = FALSE;
1119 TRACE("flag=%08lx constant=%08lx\n", cflg, flg);
1120 } else {
1121 *dwFlags |= flg;
1122 if (!MCI_GetDWord(&(data[offset]), &args)) {
1123 return MCIERR_BAD_INTEGER;
1125 TRACE("flag=%08lx int=%ld\n", flg, data[offset]);
1127 break;
1128 case MCI_RECT:
1129 /* store rect in data (offset..offset+3) */
1130 *dwFlags |= flg;
1131 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1132 !MCI_GetDWord(&(data[offset+1]), &args) ||
1133 !MCI_GetDWord(&(data[offset+2]), &args) ||
1134 !MCI_GetDWord(&(data[offset+3]), &args)) {
1135 return MCIERR_BAD_INTEGER;
1137 TRACE("flag=%08lx for rectangle\n", flg);
1138 break;
1139 case MCI_STRING:
1140 *dwFlags |= flg;
1141 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1142 return dwRet;
1143 TRACE("flag=%08lx string=%s\n", flg, debugstr_w(*(LPWSTR*)&data[offset]));
1144 break;
1145 default: ERR("oops\n");
1147 /* exit inside while loop, except if just entered in constant area definition */
1148 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1149 } else {
1150 /* have offset incremented if needed */
1151 switch (eid) {
1152 case MCI_COMMAND_HEAD:
1153 case MCI_RETURN:
1154 case MCI_END_COMMAND:
1155 case MCI_END_COMMAND_LIST:
1156 case MCI_CONSTANT:
1157 case MCI_FLAG: break;
1158 case MCI_HWND:
1159 case MCI_HPAL:
1160 case MCI_HDC: if (!inCst) offset += sizeof(HANDLE)/sizeof(DWORD); break;
1161 case MCI_INTEGER: if (!inCst) offset++; break;
1162 case MCI_END_CONSTANT: offset++; break;
1163 case MCI_STRING: offset += sizeof(LPWSTR)/sizeof(DWORD); break;
1164 case MCI_RECT: offset += 4; break;
1165 default: ERR("oops\n");
1168 } while (eid != MCI_END_COMMAND);
1169 if (!found) {
1170 WARN("Optarg %s not found\n", debugstr_w(args));
1171 return MCIERR_UNRECOGNIZED_COMMAND;
1173 if (offset == MCI_DATA_SIZE) {
1174 FIXME("Internal data[] buffer overflow\n");
1175 return MCIERR_PARSER_INTERNAL;
1178 return 0;
1181 /**************************************************************************
1182 * MCI_HandleReturnValues [internal]
1184 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1185 MCI_GENERIC_PARMS *params, LPWSTR lpstrRet, UINT uRetLen)
1187 if (lpstrRet) {
1188 switch (retType) {
1189 case 0: /* nothing to return */
1190 break;
1191 case MCI_INTEGER:
1193 DWORD data = *(DWORD *)(params + 1);
1194 switch (dwRet & 0xFFFF0000ul) {
1195 case 0:
1196 case MCI_INTEGER_RETURNED:
1197 swprintf(lpstrRet, uRetLen, L"%d", data);
1198 break;
1199 case MCI_RESOURCE_RETURNED:
1200 /* return string which ID is HIWORD(data),
1201 * string is loaded from mmsystem.dll */
1202 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1203 break;
1204 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1205 /* return string which ID is HIWORD(data),
1206 * string is loaded from driver */
1207 /* FIXME: this is wrong for a 16 bit handle */
1208 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1209 HIWORD(data), lpstrRet, uRetLen);
1210 break;
1211 case MCI_COLONIZED3_RETURN:
1212 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d",
1213 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1214 LOBYTE(HIWORD(data)));
1215 break;
1216 case MCI_COLONIZED4_RETURN:
1217 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d:%02d",
1218 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1219 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1220 break;
1221 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1223 break;
1225 case 13: /* MCI_INTEGER64 */
1227 DWORD_PTR data = *(DWORD_PTR *)(params + 1);
1228 switch (dwRet & 0xFFFF0000ul) {
1229 case 0:
1230 case MCI_INTEGER_RETURNED:
1231 swprintf(lpstrRet, uRetLen, L"%ld", data);
1232 break;
1233 case MCI_RESOURCE_RETURNED:
1234 /* return string which ID is HIWORD(data),
1235 * string is loaded from mmsystem.dll */
1236 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1237 break;
1238 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1239 /* return string which ID is HIWORD(data),
1240 * string is loaded from driver */
1241 /* FIXME: this is wrong for a 16 bit handle */
1242 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1243 HIWORD(data), lpstrRet, uRetLen);
1244 break;
1245 case MCI_COLONIZED3_RETURN:
1246 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d",
1247 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1248 LOBYTE(HIWORD(data)));
1249 break;
1250 case MCI_COLONIZED4_RETURN:
1251 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d:%02d",
1252 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1253 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1254 break;
1255 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1257 break;
1259 case MCI_STRING:
1260 switch (dwRet & 0xFFFF0000ul) {
1261 case 0:
1262 /* nothing to do data[0] == lpstrRet */
1263 break;
1264 case MCI_INTEGER_RETURNED:
1266 DWORD *data = (DWORD *)(params + 1);
1267 *data = *(LPDWORD)lpstrRet;
1268 swprintf(lpstrRet, uRetLen, L"%d", *data);
1269 break;
1271 default:
1272 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1273 break;
1275 break;
1276 case MCI_RECT:
1278 DWORD *data = (DWORD *)(params + 1);
1279 if (dwRet & 0xFFFF0000ul)
1280 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1281 swprintf(lpstrRet, uRetLen, L"%d %d %d %d", data[0], data[1], data[2], data[3]);
1282 break;
1284 default: FIXME("Unknown MCI return type %ld\n", retType);
1287 return LOWORD(dwRet);
1290 /**************************************************************************
1291 * mciSendStringW [WINMM.@]
1293 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1294 UINT uRetLen, HWND hwndCallback)
1296 LPWSTR verb, dev, args, devType = NULL;
1297 LPWINE_MCIDRIVER wmd = 0;
1298 MCIDEVICEID uDevID, auto_open = 0;
1299 DWORD dwFlags = 0, dwRet = 0;
1300 int offset = 0;
1301 DWORD retType;
1302 LPCWSTR lpCmd = 0;
1303 WORD wMsg = 0;
1304 union
1306 MCI_GENERIC_PARMS generic;
1307 MCI_OPEN_PARMSW open;
1308 MCI_SOUND_PARMSW sound;
1309 MCI_SYSINFO_PARMSW sysinfo;
1310 DWORD dw[MCI_DATA_SIZE];
1311 } data;
1313 TRACE("(%s, %p, %d, %p)\n",
1314 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1315 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1317 if (!lpstrCommand[0])
1318 return MCIERR_MISSING_COMMAND_STRING;
1320 /* format is <command> <device> <optargs> */
1321 if (!(verb = wcsdup(lpstrCommand)))
1322 return MCIERR_OUT_OF_MEMORY;
1323 CharLowerW(verb);
1325 memset(&data, 0, sizeof(data));
1327 if (!(args = wcschr(verb, ' '))) {
1328 dwRet = MCIERR_MISSING_DEVICE_NAME;
1329 goto errCleanUp;
1331 *args++ = '\0';
1332 if ((dwRet = MCI_GetString(&dev, &args))) {
1333 goto errCleanUp;
1335 uDevID = wcsicmp(dev, L"ALL") ? 0 : MCI_ALL_DEVICE_ID;
1337 /* Determine devType from open */
1338 if (!wcscmp(verb, L"open")) {
1339 LPWSTR tmp;
1340 WCHAR buf[128];
1342 /* case dev == 'new' has to be handled */
1343 if (!wcscmp(dev, L"new")) {
1344 dev = 0;
1345 if ((devType = wcsstr(args, L"type ")) != NULL) {
1346 devType += 5;
1347 tmp = wcschr(devType, ' ');
1348 if (tmp) *tmp = '\0';
1349 devType = str_dup_upper(devType);
1350 if (tmp) *tmp = ' ';
1351 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1352 } else {
1353 WARN("open new requires device type\n");
1354 dwRet = MCIERR_MISSING_DEVICE_NAME;
1355 goto errCleanUp;
1357 dwFlags |= MCI_OPEN_ELEMENT;
1358 data.open.lpstrElementName = &L""[0];
1359 } else if ((devType = wcschr(dev, '!')) != NULL) {
1360 *devType++ = '\0';
1361 tmp = devType; devType = dev; dev = tmp;
1363 dwFlags |= MCI_OPEN_TYPE;
1364 data.open.lpstrDeviceType = devType;
1365 devType = str_dup_upper(devType);
1366 dwFlags |= MCI_OPEN_ELEMENT;
1367 data.open.lpstrElementName = dev;
1368 } else if (DRIVER_GetLibName(dev, L"MCI", buf, sizeof(buf))) {
1369 /* this is the name of a mci driver's type */
1370 tmp = wcschr(dev, ' ');
1371 if (tmp) *tmp = '\0';
1372 data.open.lpstrDeviceType = dev;
1373 devType = str_dup_upper(dev);
1374 if (tmp) *tmp = ' ';
1375 dwFlags |= MCI_OPEN_TYPE;
1376 } else {
1377 if ((devType = wcsstr(args, L"type ")) != NULL) {
1378 devType += 5;
1379 tmp = wcschr(devType, ' ');
1380 if (tmp) *tmp = '\0';
1381 devType = str_dup_upper(devType);
1382 if (tmp) *tmp = ' ';
1383 /* dwFlags and lpstrDeviceType will be correctly set in ParseOpt loop */
1384 } else {
1385 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1386 goto errCleanUp;
1388 devType = str_dup_upper(buf);
1390 dwFlags |= MCI_OPEN_ELEMENT;
1391 data.open.lpstrElementName = dev;
1393 if (MCI_ALL_DEVICE_ID == uDevID) {
1394 dwRet = MCIERR_CANNOT_USE_ALL;
1395 goto errCleanUp;
1397 if (!wcsstr(args, L" alias ") && !dev) {
1398 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1399 goto errCleanUp;
1402 dwRet = MCI_LoadMciDriver(devType, &wmd);
1403 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1404 dwRet = MCIERR_INVALID_DEVICE_NAME;
1405 if (dwRet)
1406 goto errCleanUp;
1407 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))
1408 && (lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb))) {
1409 /* auto-open uses the core command table */
1410 switch (MCI_GetMessage(lpCmd)) {
1411 case MCI_SOUND: /* command does not use a device name */
1412 case MCI_SYSINFO:
1413 break;
1414 case MCI_CLOSE: /* don't auto-open for close */
1415 case MCI_BREAK: /* no auto-open for system commands */
1416 dwRet = MCIERR_INVALID_DEVICE_NAME;
1417 goto errCleanUp;
1418 break;
1419 default:
1421 WCHAR buf[138], retbuf[6];
1422 swprintf(buf, ARRAY_SIZE(buf), L"open %s wait", dev);
1423 /* open via mciSendString handles quoting, dev!file syntax and alias creation */
1424 if ((dwRet = mciSendStringW(buf, retbuf, ARRAY_SIZE(retbuf), 0)) != 0)
1425 goto errCleanUp;
1426 auto_open = wcstoul(retbuf, NULL, 10);
1427 TRACE("auto-opened %u for %s\n", auto_open, debugstr_w(dev));
1429 /* FIXME: test for notify flag (how to preparse?) before opening */
1430 wmd = MCI_GetDriver(auto_open);
1431 if (!wmd) {
1432 ERR("No auto-open device %u\n", auto_open);
1433 dwRet = MCIERR_INVALID_DEVICE_ID;
1434 goto errCleanUp;
1440 /* get the verb in the different command tables */
1441 if (wmd) {
1442 /* try the device specific command table */
1443 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1444 if (!lpCmd) {
1445 /* try the type specific command table */
1446 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1447 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1448 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1449 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1452 /* try core command table */
1453 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1455 if (!lpCmd) {
1456 TRACE("Command %s not found!\n", debugstr_w(verb));
1457 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1458 goto errCleanUp;
1460 wMsg = MCI_GetMessage(lpCmd);
1462 /* set return information */
1463 offset = sizeof(data.generic);
1464 switch (retType = MCI_GetReturnType(lpCmd)) {
1465 case 0:
1466 break;
1467 case MCI_INTEGER:
1468 offset += sizeof(DWORD);
1469 break;
1470 case MCI_STRING:
1471 data.sysinfo.lpstrReturn = lpstrRet;
1472 data.sysinfo.dwRetSize = uRetLen;
1473 offset = FIELD_OFFSET( MCI_SYSINFO_PARMSW, dwNumber );
1474 break;
1475 case MCI_RECT:
1476 offset += 4 * sizeof(DWORD);
1477 break;
1478 case 13: /* MCI_INTEGER64 */
1479 offset += sizeof(DWORD_PTR);
1480 break;
1481 default:
1482 FIXME("Unknown MCI return type %ld\n", retType);
1483 dwRet = MCIERR_PARSER_INTERNAL;
1484 goto errCleanUp;
1487 TRACE("verb=%s on dev=%s; offset=%d\n",
1488 debugstr_w(verb), debugstr_w(dev), offset);
1490 if ((dwRet = MCI_ParseOptArgs(data.dw, offset / sizeof(DWORD), lpCmd, args, &dwFlags)))
1491 goto errCleanUp;
1493 /* set up call back */
1494 if (auto_open) {
1495 if (dwFlags & MCI_NOTIFY) {
1496 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1497 goto errCleanUp;
1499 /* FIXME: the command should get its own notification window set up and
1500 * ask for device closing while processing the notification mechanism.
1501 * hwndCallback = ...
1502 * dwFlags |= MCI_NOTIFY;
1503 * In the meantime special-case all commands but PLAY and RECORD below. */
1505 if (dwFlags & MCI_NOTIFY) {
1506 data.generic.dwCallback = (DWORD_PTR)hwndCallback;
1509 switch (wMsg) {
1510 case MCI_OPEN:
1511 if (wcscmp(verb, L"open")) {
1512 FIXME("Cannot open with command %s\n", debugstr_w(verb));
1513 dwRet = MCIERR_DRIVER_INTERNAL;
1514 wMsg = 0;
1515 goto errCleanUp;
1517 break;
1518 case MCI_SYSINFO:
1519 /* Requirements on dev depend on the flags:
1520 * alias with INSTALLNAME, name like "digitalvideo"
1521 * with QUANTITY and NAME. */
1523 data.sysinfo.wDeviceType = MCI_ALL_DEVICE_ID;
1524 if (uDevID != MCI_ALL_DEVICE_ID) {
1525 if (dwFlags & MCI_SYSINFO_INSTALLNAME)
1526 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1527 else if (!(data.sysinfo.wDeviceType = MCI_GetDevTypeFromResource(dev))) {
1528 dwRet = MCIERR_DEVICE_TYPE_REQUIRED;
1529 goto errCleanUp;
1533 break;
1534 case MCI_SOUND:
1535 /* FIXME: name is optional, "sound" is a valid command.
1536 * FIXME: Parse "sound notify" as flag, not as name. */
1537 data.sound.lpstrSoundName = dev;
1538 dwFlags |= MCI_SOUND_NAME;
1539 break;
1542 TRACE("[%d, %s, %08lx, %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx]\n",
1543 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(wMsg), dwFlags,
1544 data.dw[0], data.dw[1], data.dw[2], data.dw[3], data.dw[4],
1545 data.dw[5], data.dw[6], data.dw[7], data.dw[8], data.dw[9]);
1547 if (wMsg == MCI_OPEN) {
1548 if ((dwRet = MCI_FinishOpen(wmd, &data.open, dwFlags)))
1549 goto errCleanUp;
1550 /* FIXME: notification is not properly shared across two opens */
1551 } else {
1552 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, wMsg, dwFlags, (DWORD_PTR)&data);
1554 if (!LOWORD(dwRet)) {
1555 TRACE("=> 1/ %lx (%s)\n", dwRet, debugstr_w(lpstrRet));
1556 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, &data.generic, lpstrRet, uRetLen);
1557 TRACE("=> 2/ %lx (%s)\n", dwRet, debugstr_w(lpstrRet));
1558 } else
1559 TRACE("=> %lx\n", dwRet);
1561 errCleanUp:
1562 if (auto_open) {
1563 /* PLAY and RECORD are the only known non-immediate commands */
1564 if (LOWORD(dwRet) || !(wMsg == MCI_PLAY || wMsg == MCI_RECORD))
1565 MCI_SendCommand(auto_open, MCI_CLOSE, 0, 0);
1566 else
1567 FIXME("leaking auto-open device %u\n", auto_open);
1569 if (wMsg == MCI_OPEN && LOWORD(dwRet) && wmd)
1570 MCI_UnLoadMciDriver(wmd);
1571 free(devType);
1572 free(verb);
1573 return dwRet;
1576 /**************************************************************************
1577 * mciSendStringA [WINMM.@]
1579 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1580 UINT uRetLen, HWND hwndCallback)
1582 LPWSTR lpwstrCommand;
1583 LPWSTR lpwstrRet = NULL;
1584 UINT ret;
1585 INT len;
1587 /* FIXME: is there something to do with lpstrReturnString ? */
1588 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1589 lpwstrCommand = malloc( len * sizeof(WCHAR) );
1590 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1591 if (lpstrRet)
1593 if (uRetLen) *lpstrRet = '\0'; /* NT-w2k3 use memset(lpstrRet, 0, uRetLen); */
1594 lpwstrRet = malloc( uRetLen * sizeof(WCHAR) );
1595 if (!lpwstrRet) {
1596 free( lpwstrCommand );
1597 return MCIERR_OUT_OF_MEMORY;
1600 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1601 if (!ret && lpwstrRet)
1602 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1603 free(lpwstrCommand);
1604 free(lpwstrRet);
1605 return ret;
1608 /**************************************************************************
1609 * mciExecute [WINMM.@]
1611 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1613 char strRet[256];
1614 DWORD ret;
1616 TRACE("(%s)!\n", lpstrCommand);
1618 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1619 if (ret != 0) {
1620 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1621 sprintf(strRet, "Unknown MCI error (%ld)", ret);
1623 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1625 /* FIXME: what shall I return ? */
1626 return TRUE;
1629 /**************************************************************************
1630 * mciLoadCommandResource [WINMM.@]
1632 * Strangely, this function only exists as a UNICODE one.
1634 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1636 UINT ret = MCI_NO_COMMAND_TABLE;
1637 HRSRC hRsrc = 0;
1638 HGLOBAL hMem;
1640 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1642 /* if a file named "resname.mci" exits, then load resource "resname" from it
1643 * otherwise directly from driver
1644 * We don't support it (who uses this feature ?), but we check anyway
1646 if (!type) {
1647 #if 0
1648 /* FIXME: we should put this back into order, but I never found a program
1649 * actually using this feature, so we may not need it
1651 char buf[128];
1652 OFSTRUCT ofs;
1654 strcat(strcpy(buf, resname), ".mci");
1655 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1656 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1658 #endif
1660 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1661 (hMem = LoadResource(hInst, hRsrc))) {
1662 ret = MCI_SetCommandTable(hMem, type);
1663 FreeResource(hMem);
1665 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1667 TRACE("=> %04x\n", ret);
1668 return ret;
1671 /**************************************************************************
1672 * mciFreeCommandResource [WINMM.@]
1674 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1676 TRACE("(%08x)!\n", uTable);
1678 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1679 return FALSE;
1681 FreeResource(S_MciCmdTable[uTable].hMem);
1682 S_MciCmdTable[uTable].hMem = NULL;
1683 S_MciCmdTable[uTable].lpTable = NULL;
1684 free(S_MciCmdTable[uTable].aVerbs);
1685 S_MciCmdTable[uTable].aVerbs = 0;
1686 S_MciCmdTable[uTable].nVerbs = 0;
1687 return TRUE;
1690 /**************************************************************************
1691 * MCI_Open [internal]
1693 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1695 WCHAR strDevTyp[128];
1696 DWORD dwRet;
1697 LPWINE_MCIDRIVER wmd = NULL;
1699 TRACE("(%08lX, %p)\n", dwParam, lpParms);
1700 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1702 /* only two low bytes are generic, the other ones are dev type specific */
1703 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1704 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1705 MCI_NOTIFY|MCI_WAIT)
1706 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0)
1707 FIXME("Unsupported yet dwFlags=%08lX\n", dwParam);
1708 #undef WINE_MCIDRIVER_SUPP
1710 strDevTyp[0] = 0;
1712 if (dwParam & MCI_OPEN_TYPE) {
1713 if (dwParam & MCI_OPEN_TYPE_ID) {
1714 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1716 if (uDevType < MCI_DEVTYPE_FIRST || uDevType > MCI_DEVTYPE_LAST ||
1717 !LoadStringW(hWinMM32Instance, uDevType, strDevTyp, ARRAY_SIZE(strDevTyp))) {
1718 dwRet = MCIERR_BAD_INTEGER;
1719 goto errCleanUp;
1721 } else {
1722 LPWSTR ptr;
1723 if (lpParms->lpstrDeviceType == NULL) {
1724 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1725 goto errCleanUp;
1727 lstrcpyW(strDevTyp, lpParms->lpstrDeviceType);
1728 ptr = wcschr(strDevTyp, '!');
1729 if (ptr) {
1730 /* this behavior is not documented in windows. However, since, in
1731 * some occasions, MCI_OPEN handling is translated by WinMM into
1732 * a call to mciSendString("open <type>"); this code shall be correct
1734 if (dwParam & MCI_OPEN_ELEMENT) {
1735 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1736 debugstr_w(lpParms->lpstrElementName),
1737 debugstr_w(strDevTyp));
1738 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1739 goto errCleanUp;
1741 dwParam |= MCI_OPEN_ELEMENT;
1742 *ptr++ = 0;
1743 /* FIXME: not a good idea to write in user supplied buffer */
1744 lpParms->lpstrElementName = ptr;
1748 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1751 if (dwParam & MCI_OPEN_ELEMENT) {
1752 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1754 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1755 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1756 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1757 goto errCleanUp;
1760 if (!lpParms->lpstrElementName) {
1761 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1762 goto errCleanUp;
1765 /* type, if given as a parameter, supersedes file extension */
1766 if (!strDevTyp[0] &&
1767 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1768 strDevTyp, sizeof(strDevTyp))) {
1769 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1770 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1771 goto errCleanUp;
1773 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1774 lstrcpyW(strDevTyp, L"CDAUDIO");
1778 if (strDevTyp[0] == 0) {
1779 FIXME("Couldn't load driver\n");
1780 dwRet = MCIERR_INVALID_DEVICE_NAME;
1781 goto errCleanUp;
1784 if (dwParam & MCI_OPEN_ALIAS) {
1785 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1786 if (!lpParms->lpstrAlias) {
1787 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1788 goto errCleanUp;
1792 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1793 goto errCleanUp;
1796 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1797 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08lx], closing\n", dwRet);
1798 /* FIXME: is dwRet the correct ret code ? */
1799 goto errCleanUp;
1802 /* only handled devices fall through */
1803 TRACE("wDevID=%04X wDeviceID=%d dwRet=%ld\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1804 return 0;
1806 errCleanUp:
1807 if (wmd) MCI_UnLoadMciDriver(wmd);
1808 return dwRet;
1811 /**************************************************************************
1812 * MCI_Close [internal]
1814 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1816 DWORD dwRet;
1817 LPWINE_MCIDRIVER wmd;
1819 TRACE("(%04x, %08lX, %p)\n", wDevID, dwParam, lpParms);
1821 /* Every device must handle MCI_NOTIFY on its own. */
1822 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1823 while (MciDrivers) {
1824 /* Retrieve the device ID under lock, but send the message without,
1825 * the driver might be calling some winmm functions from another
1826 * thread before being fully stopped.
1828 EnterCriticalSection(&WINMM_cs);
1829 if (!MciDrivers)
1831 LeaveCriticalSection(&WINMM_cs);
1832 break;
1834 wDevID = MciDrivers->wDeviceID;
1835 LeaveCriticalSection(&WINMM_cs);
1836 MCI_Close(wDevID, dwParam, lpParms);
1838 return 0;
1841 if (!(wmd = MCI_GetDriver(wDevID))) {
1842 return MCIERR_INVALID_DEVICE_ID;
1845 if(wmd->CreatorThread != GetCurrentThreadId())
1846 return MCIERR_INVALID_DEVICE_NAME;
1848 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1850 MCI_UnLoadMciDriver(wmd);
1852 return dwRet;
1855 /**************************************************************************
1856 * MCI_WriteString [internal]
1858 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1860 DWORD ret = 0;
1862 if (lpSrcStr) {
1863 if (dstSize <= lstrlenW(lpSrcStr)) {
1864 ret = MCIERR_PARAM_OVERFLOW;
1865 } else {
1866 lstrcpyW(lpDstStr, lpSrcStr);
1868 } else {
1869 *lpDstStr = 0;
1871 return ret;
1874 /**************************************************************************
1875 * MCI_Sysinfo [internal]
1877 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1879 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1880 WCHAR buf[2048], *s, *p;
1881 LPWINE_MCIDRIVER wmd;
1882 HKEY hKey;
1884 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1885 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1887 TRACE("(%08x, %08lX, %p[num=%ld, wDevTyp=%u])\n",
1888 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1889 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1890 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1892 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1893 case MCI_SYSINFO_QUANTITY:
1894 if (lpParms->dwRetSize < sizeof(DWORD))
1895 return MCIERR_PARAM_OVERFLOW;
1896 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1897 if (uDevID == MCI_ALL_DEVICE_ID) {
1898 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1899 if (dwFlags & MCI_SYSINFO_OPEN) {
1900 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1901 EnterCriticalSection(&WINMM_cs);
1902 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1903 cnt++;
1905 LeaveCriticalSection(&WINMM_cs);
1906 } else {
1907 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1908 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1909 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1910 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1911 RegCloseKey( hKey );
1913 if (GetPrivateProfileStringW(L"MCI", 0, L"", buf, ARRAY_SIZE(buf), L"system.ini"))
1914 for (s = buf; *s; s += lstrlenW(s) + 1) cnt++;
1916 } else {
1917 if (dwFlags & MCI_SYSINFO_OPEN) {
1918 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1919 EnterCriticalSection(&WINMM_cs);
1920 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1921 if (wmd->wType == lpParms->wDeviceType) cnt++;
1923 LeaveCriticalSection(&WINMM_cs);
1924 } else {
1925 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1926 FIXME("Don't know how to get # of MCI devices of a given type\n");
1927 /* name = LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType))
1928 * then lookup registry and/or system.ini for name, ignoring digits suffix */
1929 switch (LOWORD(lpParms->wDeviceType)) {
1930 case MCI_DEVTYPE_CD_AUDIO:
1931 case MCI_DEVTYPE_WAVEFORM_AUDIO:
1932 case MCI_DEVTYPE_SEQUENCER:
1933 cnt = 1;
1934 break;
1935 default: /* "digitalvideo" gets 0 because it's not in the registry */
1936 cnt = 0;
1940 *(DWORD*)lpParms->lpstrReturn = cnt;
1941 TRACE("(%ld) => '%ld'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1942 ret = MCI_INTEGER_RETURNED;
1943 /* return ret; Only Win9x sends a notification in this case. */
1944 break;
1945 case MCI_SYSINFO_INSTALLNAME:
1946 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1947 if ((wmd = MCI_GetDriver(uDevID))) {
1948 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1949 wmd->lpstrDeviceType);
1950 } else {
1951 ret = (uDevID == MCI_ALL_DEVICE_ID)
1952 ? MCIERR_CANNOT_USE_ALL : MCIERR_INVALID_DEVICE_NAME;
1954 TRACE("(%ld) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1955 break;
1956 case MCI_SYSINFO_NAME:
1957 s = NULL;
1958 if (dwFlags & MCI_SYSINFO_OPEN) {
1959 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1960 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1961 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1962 EnterCriticalSection(&WINMM_cs);
1963 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1964 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1965 if (uDevID == MCI_ALL_DEVICE_ID ||
1966 lpParms->wDeviceType == wmd->wType) {
1967 cnt++;
1968 if (cnt == lpParms->dwNumber) {
1969 s = wmd->lpstrAlias;
1970 break;
1974 LeaveCriticalSection(&WINMM_cs);
1975 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1976 } else if (MCI_ALL_DEVICE_ID == uDevID) {
1977 TRACE("MCI_SYSINFO_NAME: device #%ld\n", lpParms->dwNumber);
1978 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1979 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1980 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1981 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1982 lpParms->dwNumber <= cnt) {
1983 DWORD bufLen = ARRAY_SIZE(buf);
1984 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1985 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1986 s = buf;
1988 RegCloseKey( hKey );
1990 if (!s) {
1991 if (GetPrivateProfileStringW(L"MCI", 0, L"", buf, ARRAY_SIZE(buf), L"system.ini")) {
1992 for (p = buf; *p; p += lstrlenW(p) + 1, cnt++) {
1993 TRACE("%ld: %s\n", cnt, debugstr_w(p));
1994 if (cnt == lpParms->dwNumber - 1) {
1995 s = p;
1996 break;
2001 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
2002 } else {
2003 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
2004 /* Cheating: what is asked for is the nth device from the registry. */
2005 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
2006 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
2007 ret = MCIERR_OUTOFRANGE;
2008 else {
2009 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
2010 lpParms->lpstrReturn, lpParms->dwRetSize);
2011 ret = 0;
2014 TRACE("(%ld) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
2015 break;
2016 default:
2017 TRACE("Unsupported flag value=%08lx\n", dwFlags);
2018 ret = MCIERR_UNRECOGNIZED_KEYWORD;
2020 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
2021 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
2022 return ret;
2025 /**************************************************************************
2026 * MCI_Break [internal]
2028 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
2030 DWORD dwRet;
2032 if (lpParms == NULL)
2033 return MCIERR_NULL_PARAMETER_BLOCK;
2035 TRACE("(%08x, %08lX, vkey %04X, hwnd %p)\n", wDevID, dwFlags,
2036 lpParms->nVirtKey, lpParms->hwndBreak);
2038 dwRet = MCI_SendCommandFrom32(wDevID, MCI_BREAK, dwFlags, (DWORD_PTR)lpParms);
2039 if (!dwRet && (dwFlags & MCI_NOTIFY))
2040 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2041 return dwRet;
2044 /**************************************************************************
2045 * MCI_Sound [internal]
2047 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
2049 DWORD dwRet;
2051 if (dwFlags & MCI_SOUND_NAME) {
2052 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2053 else dwRet = PlaySoundW(lpParms->lpstrSoundName, NULL,
2054 SND_ALIAS | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2055 ? 0 : MCIERR_HARDWARE;
2056 } else dwRet = PlaySoundW((LPCWSTR)SND_ALIAS_SYSTEMDEFAULT, NULL,
2057 SND_ALIAS_ID | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2058 ? 0 : MCIERR_HARDWARE;
2060 if (!dwRet && lpParms && (dwFlags & MCI_NOTIFY))
2061 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2062 return dwRet;
2065 /**************************************************************************
2066 * MCI_SendCommand [internal]
2068 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2070 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
2072 switch (wMsg) {
2073 case MCI_OPEN:
2074 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
2075 break;
2076 case MCI_CLOSE:
2077 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
2078 break;
2079 case MCI_SYSINFO:
2080 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2081 break;
2082 case MCI_BREAK:
2083 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2084 break;
2085 case MCI_SOUND:
2086 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2087 break;
2088 default:
2089 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
2090 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2091 dwRet = MCIERR_CANNOT_USE_ALL;
2092 } else {
2093 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
2095 break;
2097 return dwRet;
2100 /**************************************************************************
2101 * MCI_CleanUp [internal]
2103 * Some MCI commands need to be cleaned-up (when not called from
2104 * mciSendString), because MCI drivers return extra information for string
2105 * transformation. This function gets rid of them.
2107 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
2109 if (LOWORD(dwRet))
2110 return LOWORD(dwRet);
2112 switch (wMsg) {
2113 case MCI_GETDEVCAPS:
2114 switch (dwRet & 0xFFFF0000ul) {
2115 case 0:
2116 case MCI_COLONIZED3_RETURN:
2117 case MCI_COLONIZED4_RETURN:
2118 case MCI_INTEGER_RETURNED:
2119 /* nothing to do */
2120 break;
2121 case MCI_RESOURCE_RETURNED:
2122 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2124 LPMCI_GETDEVCAPS_PARMS lmgp;
2126 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
2127 TRACE("Changing %08lx to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2128 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2130 break;
2131 default:
2132 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2133 HIWORD(dwRet), MCI_MessageToString(wMsg));
2135 break;
2136 case MCI_STATUS:
2137 switch (dwRet & 0xFFFF0000ul) {
2138 case 0:
2139 case MCI_COLONIZED3_RETURN:
2140 case MCI_COLONIZED4_RETURN:
2141 case MCI_INTEGER_RETURNED:
2142 /* nothing to do */
2143 break;
2144 case MCI_RESOURCE_RETURNED:
2145 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2147 LPMCI_STATUS_PARMS lsp;
2149 lsp = (LPMCI_STATUS_PARMS)dwParam2;
2150 TRACE("Changing %08Ix to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2151 lsp->dwReturn = LOWORD(lsp->dwReturn);
2153 break;
2154 default:
2155 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2156 HIWORD(dwRet), MCI_MessageToString(wMsg));
2158 break;
2159 case MCI_SYSINFO:
2160 switch (dwRet & 0xFFFF0000ul) {
2161 case 0:
2162 case MCI_INTEGER_RETURNED:
2163 /* nothing to do */
2164 break;
2165 default:
2166 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2168 break;
2169 default:
2170 if (HIWORD(dwRet)) {
2171 FIXME("Got non null hiword for dwRet=0x%08Ix for command %s\n",
2172 dwRet, MCI_MessageToString(wMsg));
2174 break;
2176 return LOWORD(dwRet);
2179 /**************************************************************************
2180 * mciGetErrorStringW [WINMM.@]
2182 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2184 BOOL ret = FALSE;
2186 if (lpstrBuffer != NULL && uLength > 0 &&
2187 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2189 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2190 ret = TRUE;
2193 return ret;
2196 /**************************************************************************
2197 * mciGetErrorStringA [WINMM.@]
2199 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2201 BOOL ret = FALSE;
2203 if (lpstrBuffer != NULL && uLength > 0 &&
2204 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2206 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2207 ret = TRUE;
2210 return ret;
2213 /**************************************************************************
2214 * mciDriverNotify [WINMM.@]
2216 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2218 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2220 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2223 /**************************************************************************
2224 * mciGetDriverData [WINMM.@]
2226 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2228 LPWINE_MCIDRIVER wmd;
2230 TRACE("(%04x)\n", uDeviceID);
2232 wmd = MCI_GetDriver(uDeviceID);
2234 if (!wmd) {
2235 WARN("Bad uDeviceID\n");
2236 return 0L;
2239 return wmd->dwPrivate;
2242 /**************************************************************************
2243 * mciSetDriverData [WINMM.@]
2245 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2247 LPWINE_MCIDRIVER wmd;
2249 TRACE("(%04x, %08Ix)\n", uDeviceID, data);
2251 wmd = MCI_GetDriver(uDeviceID);
2253 if (!wmd) {
2254 WARN("Bad uDeviceID\n");
2255 return FALSE;
2258 wmd->dwPrivate = data;
2259 return TRUE;
2262 /**************************************************************************
2263 * mciSendCommandW [WINMM.@]
2266 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2268 DWORD dwRet;
2270 TRACE("(%08x, %s, %08Ix, %08Ix)\n",
2271 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2273 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2274 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2275 TRACE("=> %08lx\n", dwRet);
2276 return dwRet;
2279 /**************************************************************************
2280 * mciSendCommandA [WINMM.@]
2282 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2284 DWORD ret;
2285 int mapped;
2287 TRACE("(%08x, %s, %08Ix, %08Ix)\n",
2288 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2290 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2291 if (mapped == -1)
2293 FIXME("message %04x mapping failed\n", wMsg);
2294 return MCIERR_OUT_OF_MEMORY;
2296 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2297 if (mapped)
2298 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2299 return ret;
2302 /**************************************************************************
2303 * mciGetDeviceIDA [WINMM.@]
2305 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2307 LPWSTR w = MCI_strdupAtoW(lpstrName);
2308 UINT ret = MCIERR_OUT_OF_MEMORY;
2310 if (w)
2312 ret = mciGetDeviceIDW(w);
2313 free(w);
2315 return ret;
2318 /**************************************************************************
2319 * mciGetDeviceIDW [WINMM.@]
2321 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2323 return MCI_GetDriverFromString(lpwstrName);
2326 /**************************************************************************
2327 * MCI_DefYieldProc [internal]
2329 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2331 INT16 ret;
2332 MSG msg;
2334 TRACE("(0x%04x, 0x%08lx)\n", wDevID, data);
2336 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2337 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2338 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2339 ret = 0;
2340 } else {
2341 msg.hwnd = HWND_32(HIWORD(data));
2342 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2343 ret = -1;
2345 return ret;
2348 /**************************************************************************
2349 * mciSetYieldProc [WINMM.@]
2351 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2353 LPWINE_MCIDRIVER wmd;
2355 TRACE("(%u, %p, %08lx)\n", uDeviceID, fpYieldProc, dwYieldData);
2357 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2358 WARN("Bad uDeviceID\n");
2359 return FALSE;
2362 wmd->lpfnYieldProc = fpYieldProc;
2363 wmd->dwYieldData = dwYieldData;
2365 return TRUE;
2368 /**************************************************************************
2369 * mciGetDeviceIDFromElementIDA [WINMM.@]
2371 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2373 LPWSTR w = MCI_strdupAtoW(lpstrType);
2374 UINT ret = 0;
2376 if (w)
2378 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2379 free(w);
2381 return ret;
2384 /**************************************************************************
2385 * mciGetDeviceIDFromElementIDW [WINMM.@]
2387 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2389 /* FIXME: that's rather strange, there is no
2390 * mciGetDeviceIDFromElementID32A in winmm.spec
2392 FIXME("(%lu, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2393 return 0;
2396 /**************************************************************************
2397 * mciGetYieldProc [WINMM.@]
2399 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2401 LPWINE_MCIDRIVER wmd;
2403 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2405 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2406 WARN("Bad uDeviceID\n");
2407 return NULL;
2409 if (!wmd->lpfnYieldProc) {
2410 WARN("No proc set\n");
2411 return NULL;
2413 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2414 return wmd->lpfnYieldProc;
2417 /**************************************************************************
2418 * mciGetCreatorTask [WINMM.@]
2420 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2422 LPWINE_MCIDRIVER wmd;
2423 HTASK ret = 0;
2425 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2427 TRACE("(%u) => %p\n", uDeviceID, ret);
2428 return ret;
2431 /**************************************************************************
2432 * mciDriverYield [WINMM.@]
2434 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2436 LPWINE_MCIDRIVER wmd;
2437 UINT ret = 0;
2439 TRACE("(%04x)\n", uDeviceID);
2441 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2442 MSG msg;
2443 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2444 } else {
2445 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2448 return ret;