Release 940804
[wine/gsoc-2012-control.git] / misc / mcicda.c
blob93096f28992183a3baae2ab2a7578c1864d25665
1 /*
2 * Sample MCI CDAUDIO Wine Driver for Linux
4 * Copyright 1994 Martin Ayotte
5 */
6 static char Copyright[] = "Copyright Martin Ayotte, 1994";
8 #ifndef WINELIB
9 #define BUILTIN_MMSYSTEM
10 #endif
12 #ifdef BUILTIN_MMSYSTEM
15 #define DEBUG_CDAUDIO
18 #include "stdio.h"
19 #include "win.h"
20 #include "user.h"
21 #include "driver.h"
22 #include "mmsystem.h"
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #ifdef linux
27 #include <linux/soundcard.h>
28 #include <linux/cdrom.h>
29 #endif
31 #define SOUND_DEV "/dev/dsp"
32 #define CDAUDIO_DEV "/dev/sbpcd"
34 #ifdef SOUND_VERSION
35 #define IOCTL(a,b,c) ioctl(a,b,&c)
36 #else
37 #define IOCTL(a,b,c) (c = ioctl(a,b,c) )
38 #endif
40 #define MAX_CDAUDIODRV 2
41 #define MAX_CDAUDIO_TRACKS 256
43 #define CDFRAMES_PERSEC 75
44 #define CDFRAMES_PERMIN 4500
45 #define SECONDS_PERMIN 60
47 #ifdef linux
48 typedef struct {
49 int nUseCount; /* Incremented for each shared open */
50 BOOL fShareable; /* TRUE if first open was shareable */
51 WORD wNotifyDeviceID; /* MCI device ID with a pending notification */
52 HANDLE hCallback; /* Callback handle for pending notification */
53 MCI_OPEN_PARMS openParms;
54 DWORD dwTimeFormat;
55 int unixdev;
56 struct cdrom_subchnl sc;
57 int mode;
58 UINT nCurTrack;
59 DWORD dwCurFrame;
60 UINT nTracks;
61 DWORD dwTotalLen;
62 LPDWORD lpdwTrackLen;
63 LPDWORD lpdwTrackPos;
64 DWORD dwFirstOffset;
65 } LINUX_CDAUDIO;
67 static LINUX_CDAUDIO CDADev[MAX_CDAUDIODRV];
68 #endif
70 UINT CDAUDIO_GetNumberOfTracks(UINT wDevID);
71 BOOL CDAUDIO_GetTracksInfo(UINT wDevID);
72 BOOL CDAUDIO_GetCDStatus(UINT wDevID);
73 DWORD CDAUDIO_CalcTime(UINT wDevID, DWORD dwFormatType, DWORD dwFrame);
76 /*-----------------------------------------------------------------------*/
79 /**************************************************************************
80 * CDAUDIO_mciOpen [internal]
82 DWORD CDAUDIO_mciOpen(DWORD dwFlags, LPMCI_OPEN_PARMS lpParms)
84 #ifdef linux
85 UINT wDevID;
86 int cdrom;
87 #ifdef DEBUG_CDAUDIO
88 printf("CDAUDIO_mciOpen(%08X, %08X);\n", dwFlags, lpParms);
89 #endif
90 if (lpParms == NULL) return MCIERR_INTERNAL;
91 wDevID = lpParms->wDeviceID;
92 if (CDADev[wDevID].nUseCount > 0) {
93 /* The driver already open on this channel */
94 /* If the driver was% op, ened shareable before and this open specifies */
95 /* shareable then increment the use count */
96 if (CDADev[wDevID].fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
97 ++CDADev[wDevID].nUseCount;
98 else
99 return MCIERR_MUST_USE_SHAREABLE;
101 else {
102 CDADev[wDevID].nUseCount = 1;
103 CDADev[wDevID].fShareable = dwFlags & MCI_OPEN_SHAREABLE;
105 if (dwFlags & MCI_OPEN_ELEMENT) {
106 printf("CDAUDIO_mciOpen // MCI_OPEN_ELEMENT !\n");
107 /* return MCIERR_NO_ELEMENT_ALLOWED; */
109 memcpy(&CDADev[wDevID].openParms, lpParms, sizeof(MCI_OPEN_PARMS));
110 CDADev[wDevID].wNotifyDeviceID = lpParms->wDeviceID;
111 CDADev[wDevID].unixdev = open (CDAUDIO_DEV, O_RDONLY, 0);
112 if (CDADev[wDevID].unixdev == -1) {
113 printf("CDAUDIO_mciOpen // can't open '%s' !\n", CDAUDIO_DEV);
114 return MCIERR_HARDWARE;
116 CDADev[wDevID].mode = 0;
117 CDADev[wDevID].dwTimeFormat = MCI_FORMAT_TMSF;
118 CDADev[wDevID].nCurTrack = 0;
119 CDADev[wDevID].nTracks = 0;
120 CDADev[wDevID].dwTotalLen = 0;
121 CDADev[wDevID].dwFirstOffset = 0;
122 CDADev[wDevID].lpdwTrackLen = NULL;
123 CDADev[wDevID].lpdwTrackPos = NULL;
124 if (!CDAUDIO_GetTracksInfo(wDevID)) {
125 printf("CDAUDIO_mciOpen // error reading TracksInfo !\n");
126 /* return MCIERR_INTERNAL; */
128 if (dwFlags & MCI_NOTIFY) {
129 printf("CDAUDIO_mciOpen // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
130 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
131 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
133 return 0;
134 #else
135 return MCIERR_HARDWARE;
136 #endif
139 /**************************************************************************
140 * CDAUDIO_mciClose [internal]
142 DWORD CDAUDIO_mciClose(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
144 #ifdef linux
145 #ifdef DEBUG_CDAUDIO
146 printf("CDAUDIO_mciClose(%u, %08X, %08X);\n", wDevID, dwParam, lpParms);
147 #endif
148 if (CDADev[wDevID].lpdwTrackLen != NULL) free(CDADev[wDevID].lpdwTrackLen);
149 if (CDADev[wDevID].lpdwTrackPos != NULL) free(CDADev[wDevID].lpdwTrackPos);
150 close(CDADev[wDevID].unixdev);
151 #endif
154 /**************************************************************************
155 * CDAUDIO_mciGetDevCaps [internal]
157 DWORD CDAUDIO_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
158 LPMCI_GETDEVCAPS_PARMS lpParms)
160 #ifdef linux
161 #ifdef DEBUG_CDAUDIO
162 printf("CDAUDIO_mciGetDevCaps(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
163 #endif
164 if (lpParms == NULL) return MCIERR_INTERNAL;
165 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
166 printf("CDAUDIO_mciGetDevCaps // MCI_GETDEVCAPS_ITEM dwItem=%08X;\n",
167 lpParms->dwItem);
168 switch(lpParms->dwItem) {
169 case MCI_GETDEVCAPS_CAN_RECORD:
170 lpParms->dwReturn = FALSE;
171 break;
172 case MCI_GETDEVCAPS_HAS_AUDIO:
173 lpParms->dwReturn = TRUE;
174 break;
175 case MCI_GETDEVCAPS_HAS_VIDEO:
176 lpParms->dwReturn = FALSE;
177 break;
178 case MCI_GETDEVCAPS_DEVICE_TYPE:
179 lpParms->dwReturn = MCI_DEVTYPE_CD_AUDIO;
180 break;
181 case MCI_GETDEVCAPS_USES_FILES:
182 lpParms->dwReturn = FALSE;
183 break;
184 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
185 lpParms->dwReturn = FALSE;
186 break;
187 case MCI_GETDEVCAPS_CAN_EJECT:
188 lpParms->dwReturn = TRUE;
189 break;
190 case MCI_GETDEVCAPS_CAN_PLAY:
191 lpParms->dwReturn = TRUE;
192 break;
193 case MCI_GETDEVCAPS_CAN_SAVE:
194 lpParms->dwReturn = FALSE;
195 break;
196 default:
197 return MCIERR_UNRECOGNIZED_COMMAND;
200 printf("CDAUDIO_mciGetDevCaps // lpParms->dwReturn=%08X;\n", lpParms->dwReturn);
201 return 0;
202 #else
203 return MCIERR_INTERNAL;
204 #endif
207 /**************************************************************************
208 * CDAUDIO_mciInfo [internal]
210 DWORD CDAUDIO_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMS lpParms)
212 #ifdef linux
213 #ifdef DEBUG_CDAUDIO
214 printf("CDAUDIO_mciInfo(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
215 #endif
216 if (lpParms == NULL) return MCIERR_INTERNAL;
217 lpParms->lpstrReturn = NULL;
218 switch(dwFlags) {
219 case MCI_INFO_PRODUCT:
220 lpParms->lpstrReturn = "Linux CDROM 0.5";
221 break;
222 default:
223 return MCIERR_UNRECOGNIZED_COMMAND;
225 if (lpParms->lpstrReturn != NULL)
226 lpParms->dwRetSize = strlen(lpParms->lpstrReturn);
227 else
228 lpParms->dwRetSize = 0;
229 return 0;
230 #else
231 return MCIERR_INTERNAL;
232 #endif
235 /**************************************************************************
236 * CDAUDIO_mciStatus [internal]
238 DWORD CDAUDIO_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
240 #ifdef linux
241 #ifdef DEBUG_CDAUDIO
242 printf("CDAUDIO_mciStatus(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
243 #endif
244 if (lpParms == NULL) return MCIERR_INTERNAL;
245 if (CDADev[wDevID].unixdev == 0) return MMSYSERR_NOTENABLED;
246 if (dwFlags & MCI_NOTIFY) {
247 #ifdef DEBUG_CDAUDIO
248 printf("CDAUDIO_mciStatus // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
249 #endif
250 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
251 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
253 if (dwFlags & MCI_STATUS_ITEM) {
254 switch(lpParms->dwItem) {
255 case MCI_STATUS_CURRENT_TRACK:
256 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
257 lpParms->dwReturn = CDADev[wDevID].nCurTrack;
258 #ifdef DEBUG_CDAUDIO
259 printf("CDAUDIO_mciStatus // CURRENT_TRACK=%u!\n", lpParms->dwReturn);
260 #endif
261 return 0;
262 case MCI_STATUS_LENGTH:
263 if (CDADev[wDevID].nTracks == 0) {
264 if (!CDAUDIO_GetTracksInfo(wDevID)) {
265 printf("CDAUDIO_mciStatus // error reading TracksInfo !\n");
266 return MCIERR_INTERNAL;
269 if (dwFlags & MCI_TRACK) {
270 printf("CDAUDIO_mciStatus // MCI_TRACK #%u LENGTH=??? !\n",
271 lpParms->dwTrack);
272 if (lpParms->dwTrack > CDADev[wDevID].nTracks)
273 return MCIERR_OUTOFRANGE;
274 lpParms->dwReturn = CDADev[wDevID].lpdwTrackLen[lpParms->dwTrack];
276 else
277 lpParms->dwReturn = CDADev[wDevID].dwTotalLen;
278 lpParms->dwReturn = CDAUDIO_CalcTime(wDevID,
279 CDADev[wDevID].dwTimeFormat, lpParms->dwReturn);
280 printf("CDAUDIO_mciStatus // LENGTH=%u !\n", lpParms->dwReturn);
281 return 0;
282 case MCI_STATUS_MODE:
283 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
284 lpParms->dwReturn = CDADev[wDevID].mode;
285 #ifdef DEBUG_CDAUDIO
286 printf("CDAUDIO_mciStatus // MCI_STATUS_MODE=%08X !\n",
287 lpParms->dwReturn);
288 #endif
289 return 0;
290 case MCI_STATUS_MEDIA_PRESENT:
291 lpParms->dwReturn = (CDADev[wDevID].nTracks > 0) ? TRUE : FALSE;
292 if (lpParms->dwReturn == FALSE)
293 printf("CDAUDIO_mciStatus // MEDIA_NOT_PRESENT !\n");
294 else
295 printf("CDAUDIO_mciStatus // MCI_STATUS_MEDIA_PRESENT !\n");
296 return 0;
297 case MCI_STATUS_NUMBER_OF_TRACKS:
298 lpParms->dwReturn = CDAUDIO_GetNumberOfTracks(wDevID);
299 printf("CDAUDIO_mciStatus // MCI_STATUS_NUMBER_OF_TRACKS = %u !\n",
300 lpParms->dwReturn);
301 if (lpParms->dwReturn == (WORD)-1) return MCIERR_INTERNAL;
302 return 0;
303 case MCI_STATUS_POSITION:
304 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
305 lpParms->dwReturn = CDADev[wDevID].dwCurFrame;
306 if (dwFlags & MCI_STATUS_START) {
307 lpParms->dwReturn = CDADev[wDevID].dwFirstOffset;
308 #ifdef DEBUG_CDAUDIO
309 printf("CDAUDIO_mciStatus // get MCI_STATUS_START !\n");
310 #endif
312 if (dwFlags & MCI_TRACK) {
313 if (lpParms->dwTrack > CDADev[wDevID].nTracks)
314 return MCIERR_OUTOFRANGE;
315 lpParms->dwReturn = CDADev[wDevID].lpdwTrackPos[lpParms->dwTrack - 1];
316 #ifdef DEBUG_CDAUDIO
317 printf("CDAUDIO_mciStatus // get MCI_TRACK #%u !\n", lpParms->dwTrack);
318 #endif
320 lpParms->dwReturn = CDAUDIO_CalcTime(wDevID,
321 CDADev[wDevID].dwTimeFormat, lpParms->dwReturn);
322 #ifdef DEBUG_CDAUDIO
323 printf("CDAUDIO_mciStatus // MCI_STATUS_POSITION=%08X !\n",
324 lpParms->dwReturn);
325 #endif
326 return 0;
327 case MCI_STATUS_READY:
328 printf("CDAUDIO_mciStatus // MCI_STATUS_READY !\n");
329 lpParms->dwReturn = TRUE;
330 return 0;
331 case MCI_STATUS_TIME_FORMAT:
332 printf("CDAUDIO_mciStatus // MCI_STATUS_TIME_FORMAT !\n");
333 lpParms->dwReturn = MCI_FORMAT_MILLISECONDS;
334 return 0;
335 default:
336 printf("CDAUDIO_mciStatus // unknowm command %04X !\n", lpParms->dwItem);
337 return MCIERR_UNRECOGNIZED_COMMAND;
340 printf("CDAUDIO_mciStatus // not MCI_STATUS_ITEM !\n");
341 return 0;
342 #else
343 return MMSYSERR_NOTENABLED;
344 #endif
348 /**************************************************************************
349 * CDAUDIO_CalcTime [internal]
351 DWORD CDAUDIO_CalcTime(UINT wDevID, DWORD dwFormatType, DWORD dwFrame)
353 DWORD dwTime = 0;
354 #ifdef linux
355 UINT wTrack;
356 UINT wMinutes;
357 UINT wSeconds;
358 UINT wFrames;
359 #ifdef DEBUG_CDAUDIO
360 printf("CDAUDIO_CalcTime(%u, %08X, %lu);\n", wDevID, dwFormatType, dwFrame);
361 #endif
362 TryAGAIN:
363 switch (dwFormatType) {
364 case MCI_FORMAT_MILLISECONDS:
365 dwTime = dwFrame / CDFRAMES_PERSEC * 1000;
366 #ifdef DEBUG_CDAUDIO
367 printf("CDAUDIO_CalcTime // MILLISECONDS %u\n", dwTime);
368 #endif
369 break;
370 case MCI_FORMAT_MSF:
371 wMinutes = dwFrame / CDFRAMES_PERMIN;
372 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
373 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes -
374 CDFRAMES_PERSEC * wSeconds;
375 dwTime = MCI_MAKE_MSF(wMinutes, wSeconds, wFrames);
376 #ifdef DEBUG_CDAUDIO
377 printf("CDAUDIO_CalcTime // MSF %02u:%02u:%02u -> dwTime=%u\n",
378 wMinutes, wSeconds, wFrames, dwTime);
379 #endif
380 break;
381 case MCI_FORMAT_TMSF:
382 for (wTrack = 0; wTrack < CDADev[wDevID].nTracks; wTrack++) {
383 /* dwTime += CDADev[wDevID].lpdwTrackLen[wTrack - 1];
384 printf("Adding trk#%u curpos=%u \n", dwTime);
385 if (dwTime >= dwFrame) break; */
386 if (CDADev[wDevID].lpdwTrackPos[wTrack - 1] >= dwFrame) break;
388 wMinutes = dwFrame / CDFRAMES_PERMIN;
389 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
390 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes -
391 CDFRAMES_PERSEC * wSeconds;
392 dwTime = MCI_MAKE_TMSF(wTrack, wMinutes, wSeconds, wFrames);
393 #ifdef DEBUG_CDAUDIO
394 printf("CDAUDIO_CalcTime // %02u-%02u:%02u:%02u\n",
395 wTrack, wMinutes, wSeconds, wFrames);
396 #endif
397 break;
398 default:
399 /* unknown format ! force TMSF ! ... */
400 dwFormatType = MCI_FORMAT_TMSF;
401 goto TryAGAIN;
403 #endif
404 return dwTime;
408 /**************************************************************************
409 * CDAUDIO_CalcFrame [internal]
411 DWORD CDAUDIO_CalcFrame(UINT wDevID, DWORD dwFormatType, DWORD dwTime)
413 DWORD dwFrame = 0;
414 #ifdef linux
415 UINT wTrack;
416 #ifdef DEBUG_CDAUDIO
417 printf("CDAUDIO_CalcFrame(%u, %08X, %lu);\n", wDevID, dwFormatType, dwTime);
418 #endif
419 TryAGAIN:
420 switch (dwFormatType) {
421 case MCI_FORMAT_MILLISECONDS:
422 dwFrame = dwTime * CDFRAMES_PERSEC / 1000;
423 #ifdef DEBUG_CDAUDIO
424 printf("CDAUDIO_CalcFrame // MILLISECONDS %u\n", dwFrame);
425 #endif
426 break;
427 case MCI_FORMAT_MSF:
428 #ifdef DEBUG_CDAUDIO
429 printf("CDAUDIO_CalcFrame // MSF %02u:%02u:%02u\n",
430 MCI_MSF_MINUTE(dwTime), MCI_MSF_SECOND(dwTime),
431 MCI_MSF_FRAME(dwTime));
432 #endif
433 dwFrame += CDFRAMES_PERMIN * MCI_MSF_MINUTE(dwTime);
434 dwFrame += CDFRAMES_PERSEC * MCI_MSF_SECOND(dwTime);
435 dwFrame += MCI_MSF_FRAME(dwTime);
436 break;
437 case MCI_FORMAT_TMSF:
438 wTrack = MCI_TMSF_TRACK(dwTime);
439 #ifdef DEBUG_CDAUDIO
440 printf("CDAUDIO_CalcFrame // TMSF %02u-%02u:%02u:%02u\n",
441 MCI_TMSF_TRACK(dwTime), MCI_TMSF_MINUTE(dwTime),
442 MCI_TMSF_SECOND(dwTime), MCI_TMSF_FRAME(dwTime));
443 printf("CDAUDIO_CalcFrame // TMSF trackpos[%u]=%u\n",
444 wTrack, CDADev[wDevID].lpdwTrackPos[wTrack - 1]);
445 #endif
446 dwFrame = CDADev[wDevID].lpdwTrackPos[wTrack - 1];
447 dwFrame += CDFRAMES_PERMIN * MCI_TMSF_MINUTE(dwTime);
448 dwFrame += CDFRAMES_PERSEC * MCI_TMSF_SECOND(dwTime);
449 dwFrame += MCI_TMSF_FRAME(dwTime);
450 break;
451 default:
452 /* unknown format ! force TMSF ! ... */
453 dwFormatType = MCI_FORMAT_TMSF;
454 goto TryAGAIN;
456 #endif
457 return dwFrame;
461 /**************************************************************************
462 * CDAUDIO_GetNumberOfTracks [internal]
464 UINT CDAUDIO_GetNumberOfTracks(UINT wDevID)
466 #ifdef linux
467 struct cdrom_tochdr hdr;
468 if (CDADev[wDevID].nTracks == 0) {
469 if (ioctl(CDADev[wDevID].unixdev, CDROMREADTOCHDR, &hdr)) {
470 printf("GetNumberOfTracks(%u) // Error occured !\n", wDevID);
471 return (WORD)-1;
473 CDADev[wDevID].nTracks = hdr.cdth_trk1;
475 return CDADev[wDevID].nTracks;
476 #else
477 return (WORD)-1;
478 #endif
481 /**************************************************************************
482 * CDAUDIO_GetNumberOfTracks [internal]
484 BOOL CDAUDIO_GetTracksInfo(UINT wDevID)
486 #ifdef linux
487 int i, length;
488 int start, last_start;
489 int total_length = 0;
490 struct cdrom_tocentry entry;
491 if (CDADev[wDevID].nTracks == 0) {
492 if (CDAUDIO_GetNumberOfTracks(wDevID) == (WORD)-1) return FALSE;
494 printf("CDAUDIO_GetTracksInfo // nTracks=%u\n", CDADev[wDevID].nTracks);
495 if (CDADev[wDevID].lpdwTrackLen != NULL)
496 free(CDADev[wDevID].lpdwTrackLen);
497 CDADev[wDevID].lpdwTrackLen = (LPDWORD)malloc(
498 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
499 if (CDADev[wDevID].lpdwTrackPos != NULL)
500 free(CDADev[wDevID].lpdwTrackPos);
501 CDADev[wDevID].lpdwTrackPos = (LPDWORD)malloc(
502 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
503 if (CDADev[wDevID].lpdwTrackLen == NULL ||
504 CDADev[wDevID].lpdwTrackPos == NULL) {
505 printf("CDAUDIO_GetTracksInfo // error allocating track table !\n");
506 return FALSE;
508 memset(CDADev[wDevID].lpdwTrackLen, 0,
509 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
510 memset(CDADev[wDevID].lpdwTrackPos, 0,
511 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
512 for (i = 0; i <= CDADev[wDevID].nTracks; i++) {
513 if (i == CDADev[wDevID].nTracks)
514 entry.cdte_track = CDROM_LEADOUT;
515 else
516 entry.cdte_track = i + 1;
517 entry.cdte_format = CDROM_MSF;
518 if (ioctl(CDADev[wDevID].unixdev, CDROMREADTOCENTRY, &entry)) {
519 printf("CDAUDIO_GetTracksInfo // error read entry\n");
520 return FALSE;
522 start = CDFRAMES_PERSEC * (SECONDS_PERMIN *
523 entry.cdte_addr.msf.minute + entry.cdte_addr.msf.second) +
524 entry.cdte_addr.msf.frame;
525 if (i == 0) {
526 CDADev[wDevID].dwFirstOffset = last_start = start;
527 printf("CDAUDIO_GetTracksInfo // dwFirstOffset=%u\n", start);
529 else {
530 length = start - last_start;
531 last_start = start;
532 start = last_start - length;
533 total_length += length;
534 CDADev[wDevID].lpdwTrackLen[i - 1] = length;
535 CDADev[wDevID].lpdwTrackPos[i - 1] = start;
536 printf("CDAUDIO_GetTracksInfo // track #%u start=%u len=%u\n",
537 i, start, length);
540 CDADev[wDevID].dwTotalLen = total_length;
541 printf("CDAUDIO_GetTracksInfo // total_len=%u\n", total_length);
542 fflush(stdout);
543 return TRUE;
544 #else
545 return FALSE;
546 #endif
550 /**************************************************************************
551 * CDAUDIO_GetNumberOfTracks [internal]
553 BOOL CDAUDIO_GetCDStatus(UINT wDevID)
555 #ifdef linux
556 int oldmode = CDADev[wDevID].mode;
557 CDADev[wDevID].sc.cdsc_format = CDROM_MSF;
558 if (ioctl(CDADev[wDevID].unixdev, CDROMSUBCHNL, &CDADev[wDevID].sc)) {
559 #ifdef DEBUG_CDAUDIO
560 printf("CDAUDIO_GetCDStatus // opened or no_media !\n");
561 #endif
562 CDADev[wDevID].mode = MCI_MODE_NOT_READY;
563 return TRUE;
565 switch (CDADev[wDevID].sc.cdsc_audiostatus) {
566 case CDROM_AUDIO_INVALID:
567 printf("CDAUDIO_GetCDStatus // device doesn't support status !\n");
568 return FALSE;
569 case CDROM_AUDIO_NO_STATUS:
570 CDADev[wDevID].mode = MCI_MODE_STOP;
571 #ifdef DEBUG_CDAUDIO
572 printf("CDAUDIO_GetCDStatus // MCI_MODE_STOP !\n");
573 #endif
574 break;
575 case CDROM_AUDIO_PLAY:
576 CDADev[wDevID].mode = MCI_MODE_PLAY;
577 #ifdef DEBUG_CDAUDIO
578 printf("CDAUDIO_GetCDStatus // MCI_MODE_PLAY !\n");
579 #endif
580 break;
581 case CDROM_AUDIO_PAUSED:
582 CDADev[wDevID].mode = MCI_MODE_PAUSE;
583 #ifdef DEBUG_CDAUDIO
584 printf("CDAUDIO_GetCDStatus // MCI_MODE_PAUSE !\n");
585 #endif
586 break;
587 default:
588 printf("CDAUDIO_GetCDStatus // status=%02X !\n",
589 CDADev[wDevID].sc.cdsc_audiostatus);
591 CDADev[wDevID].nCurTrack = CDADev[wDevID].sc.cdsc_trk;
592 CDADev[wDevID].dwCurFrame =
593 CDFRAMES_PERMIN * CDADev[wDevID].sc.cdsc_absaddr.msf.minute +
594 CDFRAMES_PERSEC * CDADev[wDevID].sc.cdsc_absaddr.msf.second +
595 CDADev[wDevID].sc.cdsc_absaddr.msf.frame;
596 #ifdef DEBUG_CDAUDIO
597 printf("CDAUDIO_GetCDStatus // %02u-%02u:%02u:%02u \n",
598 CDADev[wDevID].sc.cdsc_trk,
599 CDADev[wDevID].sc.cdsc_absaddr.msf.minute,
600 CDADev[wDevID].sc.cdsc_absaddr.msf.second,
601 CDADev[wDevID].sc.cdsc_absaddr.msf.frame);
602 #endif
603 if (oldmode != CDADev[wDevID].mode && oldmode == MCI_MODE_OPEN) {
604 if (!CDAUDIO_GetTracksInfo(wDevID)) {
605 printf("CDAUDIO_GetCDStatus // error updating TracksInfo !\n");
606 return MCIERR_INTERNAL;
609 return TRUE;
610 #else
611 return FALSE;
612 #endif
615 /**************************************************************************
616 * CDAUDIO_mciPlay [internal]
618 DWORD CDAUDIO_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
620 #ifdef linux
621 int start, end;
622 struct cdrom_msf msf;
623 #ifdef DEBUG_CDAUDIO
624 printf("CDAUDIO_mciPlay(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
625 #endif
626 if (lpParms == NULL) return MCIERR_INTERNAL;
627 if (CDADev[wDevID].unixdev == 0) return MMSYSERR_NOTENABLED;
628 start = 0; end = CDADev[wDevID].dwTotalLen;
629 CDADev[wDevID].nCurTrack = 1;
630 if (dwFlags & MCI_FROM) {
631 start = CDAUDIO_CalcFrame(wDevID,
632 CDADev[wDevID].dwTimeFormat, lpParms->dwFrom);
633 #ifdef DEBUG_CDAUDIO
634 printf("CDAUDIO_mciPlay // MCI_FROM=%08X -> %u \n",
635 lpParms->dwFrom, start);
636 #endif
638 if (dwFlags & MCI_TO) {
639 end = CDAUDIO_CalcFrame(wDevID,
640 CDADev[wDevID].dwTimeFormat, lpParms->dwTo);
641 #ifdef DEBUG_CDAUDIO
642 printf("CDAUDIO_mciPlay // MCI_TO=%08X -> %u \n",
643 lpParms->dwTo, end);
644 #endif
646 start += CDADev[wDevID].dwFirstOffset;
647 end += CDADev[wDevID].dwFirstOffset;
648 msf.cdmsf_min0 = start / CDFRAMES_PERMIN;
649 msf.cdmsf_sec0 = (start % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
650 msf.cdmsf_frame0 = start % CDFRAMES_PERSEC;
651 msf.cdmsf_min1 = end / CDFRAMES_PERMIN;
652 msf.cdmsf_sec1 = (end % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
653 msf.cdmsf_frame1 = end % CDFRAMES_PERSEC;
654 if (ioctl(CDADev[wDevID].unixdev, CDROMSTART)) {
655 printf("CDAUDIO_mciPlay // motor doesn't start !\n");
656 return MCIERR_HARDWARE;
658 if (ioctl(CDADev[wDevID].unixdev, CDROMPLAYMSF, &msf)) {
659 printf("CDAUDIO_mciPlay // device doesn't play !\n");
660 return MCIERR_HARDWARE;
662 #ifdef DEBUG_CDAUDIO
663 printf("CDAUDIO_mciPlay // msf = %d:%d:%d %d:%d:%d\n",
664 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
665 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
666 #endif
667 CDADev[wDevID].mode = MCI_MODE_PLAY;
668 if (dwFlags & MCI_NOTIFY) {
669 printf("CDAUDIO_mciPlay // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
670 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
671 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
673 return 0;
674 #else
675 return MCIERR_HARDWARE;
676 #endif
679 /**************************************************************************
680 * CDAUDIO_mciStop [internal]
682 DWORD CDAUDIO_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
684 #ifdef linux
685 #ifdef DEBUG_CDAUDIO
686 printf("CDAUDIO_mciStop(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
687 #endif
688 if (lpParms == NULL) return MCIERR_INTERNAL;
689 if (ioctl(CDADev[wDevID].unixdev, CDROMSTOP)) return MCIERR_HARDWARE;
690 CDADev[wDevID].mode = MCI_MODE_STOP;
691 if (dwFlags & MCI_NOTIFY) {
692 printf("CDAUDIO_mciStop // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
693 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
694 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
696 return 0;
697 #else
698 return MCIERR_HARDWARE;
699 #endif
702 /**************************************************************************
703 * CDAUDIO_mciPause [internal]
705 DWORD CDAUDIO_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
707 #ifdef linux
708 #ifdef DEBUG_CDAUDIO
709 printf("CDAUDIO_mciPause(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
710 #endif
711 if (lpParms == NULL) return MCIERR_INTERNAL;
712 if (ioctl(CDADev[wDevID].unixdev, CDROMPAUSE)) return MCIERR_HARDWARE;
713 CDADev[wDevID].mode = MCI_MODE_PAUSE;
714 if (dwFlags & MCI_NOTIFY) {
715 printf("CDAUDIO_mciPause // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
716 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
717 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
719 return 0;
720 #else
721 return MCIERR_HARDWARE;
722 #endif
725 /**************************************************************************
726 * CDAUDIO_mciResume [internal]
728 DWORD CDAUDIO_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
730 #ifdef linux
731 #ifdef DEBUG_CDAUDIO
732 printf("CDAUDIO_mciResume(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
733 #endif
734 if (lpParms == NULL) return MCIERR_INTERNAL;
735 if (ioctl(CDADev[wDevID].unixdev, CDROMRESUME)) return MCIERR_HARDWARE;
736 CDADev[wDevID].mode = MCI_MODE_STOP;
737 if (dwFlags & MCI_NOTIFY) {
738 printf("CDAUDIO_mciResume // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
739 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
740 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
742 return 0;
743 #else
744 return MCIERR_HARDWARE;
745 #endif
748 /**************************************************************************
749 * CDAUDIO_mciSeek [internal]
751 DWORD CDAUDIO_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
753 #ifdef linux
754 DWORD dwRet;
755 MCI_PLAY_PARMS PlayParms;
756 #ifdef DEBUG_CDAUDIO
757 printf("CDAUDIO_mciSeek(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
758 #endif
759 if (lpParms == NULL) return MCIERR_INTERNAL;
760 if (ioctl(CDADev[wDevID].unixdev, CDROMRESUME)) return MCIERR_HARDWARE;
761 CDADev[wDevID].mode = MCI_MODE_SEEK;
762 switch(dwFlags) {
763 case MCI_SEEK_TO_START:
764 PlayParms.dwFrom = 0;
765 break;
766 case MCI_SEEK_TO_END:
767 PlayParms.dwFrom = CDADev[wDevID].dwTotalLen;
768 break;
769 case MCI_TO:
770 PlayParms.dwFrom = lpParms->dwTo;
771 break;
773 dwRet = CDAUDIO_mciPlay(wDevID, MCI_WAIT | MCI_FROM, &PlayParms);
774 if (dwRet != 0) return dwRet;
775 dwRet = CDAUDIO_mciStop(wDevID, MCI_WAIT, (LPMCI_GENERIC_PARMS)&PlayParms);
776 if (dwFlags & MCI_NOTIFY) {
777 printf("CDAUDIO_mciSeek // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
778 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
779 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
781 return dwRet;
782 #else
783 return MCIERR_HARDWARE;
784 #endif
788 /**************************************************************************
789 * CDAUDIO_mciSet [internal]
791 DWORD CDAUDIO_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
793 #ifdef linux
794 #ifdef DEBUG_CDAUDIO
795 printf("CDAUDIO_mciSet(%u, %08X, %08X);\n", wDevID, dwFlags, lpParms);
796 #endif
797 if (lpParms == NULL) return MCIERR_INTERNAL;
799 printf("CDAUDIO_mciSet // dwTimeFormat=%08X\n", lpParms->dwTimeFormat);
800 printf("CDAUDIO_mciSet // dwAudio=%08X\n", lpParms->dwAudio);
802 if (dwFlags & MCI_SET_TIME_FORMAT) {
803 switch (lpParms->dwTimeFormat) {
804 case MCI_FORMAT_MILLISECONDS:
805 #ifdef DEBUG_CDAUDIO
806 printf("CDAUDIO_mciSet // MCI_FORMAT_MILLISECONDS !\n");
807 #endif
808 break;
809 case MCI_FORMAT_MSF:
810 #ifdef DEBUG_CDAUDIO
811 printf("CDAUDIO_mciSet // MCI_FORMAT_MSF !\n");
812 #endif
813 break;
814 case MCI_FORMAT_TMSF:
815 #ifdef DEBUG_CDAUDIO
816 printf("CDAUDIO_mciSet // MCI_FORMAT_TMSF !\n");
817 #endif
818 break;
819 default:
820 printf("CDAUDIO_mciSet // bad time format !\n");
821 return MCIERR_BAD_TIME_FORMAT;
823 CDADev[wDevID].dwTimeFormat = lpParms->dwTimeFormat;
825 if (dwFlags & MCI_SET_DOOR_OPEN) {
826 printf("CDAUDIO_mciSet // MCI_SET_DOOR_OPEN !\n");
827 if (ioctl(CDADev[wDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
828 CDADev[wDevID].nTracks = 0;
830 if (dwFlags & MCI_SET_DOOR_CLOSED) {
831 printf("CDAUDIO_mciSet // MCI_SET_DOOR_CLOSED !\n");
832 if (ioctl(CDADev[wDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
833 CDADev[wDevID].nTracks = 0;
835 if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
836 if (dwFlags & MCI_SET_ON) return MCIERR_UNSUPPORTED_FUNCTION;
837 if (dwFlags & MCI_SET_OFF) return MCIERR_UNSUPPORTED_FUNCTION;
838 if (dwFlags & MCI_NOTIFY) {
839 printf("CDAUDIO_mciSet // MCI_NOTIFY_SUCCESSFUL %08X !\n", lpParms->dwCallback);
840 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback),
841 CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
843 return 0;
844 #else
845 return MCIERR_HARDWARE;
846 #endif
850 /**************************************************************************
851 * CDAUDIO_DriverProc [sample driver]
853 LRESULT CDAUDIO_DriverProc(DWORD dwDevID, HDRVR hDriv, WORD wMsg,
854 DWORD dwParam1, DWORD dwParam2)
856 #ifdef linux
857 switch(wMsg) {
858 case DRV_LOAD:
859 return (LRESULT)1L;
860 case DRV_FREE:
861 return (LRESULT)1L;
862 case DRV_OPEN:
863 case MCI_OPEN_DRIVER:
864 case MCI_OPEN:
865 return CDAUDIO_mciOpen(dwParam1, (LPMCI_OPEN_PARMS)dwParam2);
866 case DRV_CLOSE:
867 case MCI_CLOSE_DRIVER:
868 case MCI_CLOSE:
869 return CDAUDIO_mciClose(dwDevID, dwParam1,
870 (LPMCI_GENERIC_PARMS)dwParam2);
871 case DRV_ENABLE:
872 return (LRESULT)1L;
873 case DRV_DISABLE:
874 return (LRESULT)1L;
875 case DRV_QUERYCONFIGURE:
876 return (LRESULT)1L;
877 case DRV_CONFIGURE:
878 MessageBox((HWND)NULL, "Sample MultiMedia Linux Driver !",
879 "MMLinux Driver", MB_OK);
880 return (LRESULT)1L;
881 case DRV_INSTALL:
882 return (LRESULT)DRVCNF_RESTART;
883 case DRV_REMOVE:
884 return (LRESULT)DRVCNF_RESTART;
885 case MCI_GETDEVCAPS:
886 return CDAUDIO_mciGetDevCaps(dwDevID, dwParam1,
887 (LPMCI_GETDEVCAPS_PARMS)dwParam2);
888 case MCI_INFO:
889 return CDAUDIO_mciInfo(dwDevID, dwParam1,
890 (LPMCI_INFO_PARMS)dwParam2);
891 case MCI_STATUS:
892 return CDAUDIO_mciStatus(dwDevID, dwParam1,
893 (LPMCI_STATUS_PARMS)dwParam2);
894 case MCI_SET:
895 return CDAUDIO_mciSet(dwDevID, dwParam1,
896 (LPMCI_SET_PARMS)dwParam2);
897 case MCI_PLAY:
898 return CDAUDIO_mciPlay(dwDevID, dwParam1,
899 (LPMCI_PLAY_PARMS)dwParam2);
900 case MCI_STOP:
901 return CDAUDIO_mciStop(dwDevID, dwParam1,
902 (LPMCI_GENERIC_PARMS)dwParam2);
903 case MCI_PAUSE:
904 return CDAUDIO_mciPause(dwDevID, dwParam1,
905 (LPMCI_GENERIC_PARMS)dwParam2);
906 case MCI_RESUME:
907 return CDAUDIO_mciResume(dwDevID, dwParam1,
908 (LPMCI_GENERIC_PARMS)dwParam2);
909 case MCI_SEEK:
910 return CDAUDIO_mciSeek(dwDevID, dwParam1,
911 (LPMCI_SEEK_PARMS)dwParam2);
912 case MCI_SET_DOOR_OPEN:
913 printf("CDAUDIO_DriverProc // MCI_SET_DOOR_OPEN !\n");
914 if (ioctl(CDADev[dwDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
915 CDADev[dwDevID].nTracks = 0;
916 return 0;
917 case MCI_SET_DOOR_CLOSED:
918 printf("CDAUDIO_DriverProc // MCI_SET_DOOR_CLOSED !\n");
919 if (ioctl(CDADev[dwDevID].unixdev, CDROMEJECT, 1)) return MCIERR_HARDWARE;
920 CDADev[dwDevID].nTracks = 0;
921 return 0;
922 default:
923 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
925 #else
926 return MCIERR_HARDWARE;
927 #endif
931 /*-----------------------------------------------------------------------*/
933 #endif /* #ifdef BUILTIN_MMSYSTEM */