Release 961013
[wine] / multimedia / mcicda.c
1 /*
2  * Sample MCI CDAUDIO Wine Driver for Linux
3  *
4  * Copyright 1994 Martin Ayotte
5  */
6
7 #ifndef WINELIB
8 #define BUILTIN_MMSYSTEM
9 #endif 
10
11 #ifdef BUILTIN_MMSYSTEM
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <sys/ioctl.h>
19 #include "windows.h"
20 #include "user.h"
21 #include "driver.h"
22 #include "mmsystem.h"
23 #include "stddebug.h"
24 #include "debug.h"
25
26 #ifdef linux
27 #include <linux/soundcard.h>
28 #include <linux/cdrom.h>
29 #elif __FreeBSD__
30 #include <machine/soundcard.h>
31 #include <sys/cdio.h>
32 #endif
33
34 #define SOUND_DEV "/dev/dsp"
35 #ifdef __FreeBSD__
36 #define CDAUDIO_DEV "/dev/rcd0c"
37 #else
38 #define CDAUDIO_DEV "/dev/cdrom"
39 #endif
40
41 #ifdef SOUND_VERSION
42 #define IOCTL(a,b,c)            ioctl(a,b,&c)
43 #else
44 #define IOCTL(a,b,c)            (c = ioctl(a,b,c) )
45 #endif
46
47 #define MAX_CDAUDIODRV          2
48 #define MAX_CDAUDIO_TRACKS      256
49
50 #define CDFRAMES_PERSEC         75
51 #define CDFRAMES_PERMIN         4500
52 #define SECONDS_PERMIN          60
53
54 #if defined(linux) || defined(__FreeBSD__)
55 typedef struct {
56     int     nUseCount;          /* Incremented for each shared open */
57     BOOL    fShareable;         /* TRUE if first open was shareable */
58     WORD    wNotifyDeviceID;    /* MCI device ID with a pending notification */
59     HANDLE16 hCallback;          /* Callback handle for pending notification */
60         MCI_OPEN_PARMS openParms;
61         DWORD   dwTimeFormat;
62         int             unixdev;
63 #ifdef linux
64         struct cdrom_subchnl    sc;
65 #elif __FreeBSD__
66         struct cd_sub_channel_info      sc;
67 #endif
68         int             mode;
69         UINT    nCurTrack;
70         DWORD   dwCurFrame;
71         UINT    nTracks;
72         DWORD   dwTotalLen;
73         LPDWORD lpdwTrackLen;
74         LPDWORD lpdwTrackPos;
75         DWORD   dwFirstOffset;
76         } LINUX_CDAUDIO;
77
78 static LINUX_CDAUDIO    CDADev[MAX_CDAUDIODRV];
79 #endif
80
81
82 /*-----------------------------------------------------------------------*/
83
84
85 /**************************************************************************
86 *                               CDAUDIO_GetNumberOfTracks               [internal]
87 */
88 static UINT CDAUDIO_GetNumberOfTracks(UINT wDevID)
89 {
90 #if defined(linux) || defined(__FreeBSD__)
91 #ifdef linux
92         struct cdrom_tochdr     hdr;
93 #elif __FreeBSD__
94         struct ioc_toc_header   hdr;
95 #endif
96         if (CDADev[wDevID].nTracks == 0) {
97                 if (ioctl(CDADev[wDevID].unixdev,
98 #ifdef linux
99                           CDROMREADTOCHDR
100 #elif __FreeBSD__
101                           CDIOREADTOCHEADER
102 #endif
103                           , &hdr)) {
104                         dprintf_cdaudio(stddeb,
105                                 "GetNumberOfTracks(%u) // Error occured !\n", 
106                                 wDevID);
107                         return (WORD)-1;
108                         }
109 #ifdef linux
110                 CDADev[wDevID].nTracks = hdr.cdth_trk1;
111 #elif __FreeBSD__
112                 CDADev[wDevID].nTracks = hdr.ending_track - hdr.starting_track + 1;
113 #endif
114                 }
115         return CDADev[wDevID].nTracks;
116 #else
117         return (WORD)-1;
118 #endif
119 }
120
121
122 /**************************************************************************
123 *                               CDAUDIO_GetTracksInfo                   [internal]
124 */
125 static BOOL CDAUDIO_GetTracksInfo(UINT wDevID)
126 {
127 #if defined(linux) || defined(__FreeBSD__)
128         int             i, length;
129         int             start, last_start = 0;
130         int             total_length = 0;
131 #ifdef linux
132         struct cdrom_tocentry   entry;
133 #elif __FreeBSD__
134         struct ioc_read_toc_entry       entry;
135         struct cd_toc_entry             toc_buffer;
136 #endif
137         if (CDADev[wDevID].nTracks == 0) {
138                 if (CDAUDIO_GetNumberOfTracks(wDevID) == (WORD)-1) return FALSE;
139                 }
140         dprintf_cdaudio(stddeb,"CDAUDIO_GetTracksInfo // nTracks=%u\n", 
141                 CDADev[wDevID].nTracks);
142         if (CDADev[wDevID].lpdwTrackLen != NULL) 
143                 free(CDADev[wDevID].lpdwTrackLen);
144         CDADev[wDevID].lpdwTrackLen = (LPDWORD)malloc(
145                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
146         if (CDADev[wDevID].lpdwTrackPos != NULL) 
147                 free(CDADev[wDevID].lpdwTrackPos);
148         CDADev[wDevID].lpdwTrackPos = (LPDWORD)malloc(
149                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
150         if (CDADev[wDevID].lpdwTrackLen == NULL ||
151                 CDADev[wDevID].lpdwTrackPos == NULL) {
152                         dprintf_cdaudio(stddeb,
153                                 "CDAUDIO_GetTracksInfo // error allocating track table !\n");
154                 return FALSE;
155                 }
156         memset(CDADev[wDevID].lpdwTrackLen, 0, 
157                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
158         memset(CDADev[wDevID].lpdwTrackPos, 0, 
159                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
160         for (i = 0; i <= CDADev[wDevID].nTracks; i++) {
161                 if (i == CDADev[wDevID].nTracks)
162 #ifdef linux
163                         entry.cdte_track = CDROM_LEADOUT;
164 #elif __FreeBSD__
165 #define LEADOUT 0xaa
166                         entry.starting_track = LEADOUT; /* XXX */
167 #endif
168                 else
169 #ifdef linux
170                         entry.cdte_track = i + 1;
171 #elif __FreeBSD__
172                         entry.starting_track = i + 1;
173 #endif
174 #ifdef linux
175                 entry.cdte_format = CDROM_MSF;
176 #elif __FreeBSD__
177                 bzero((char *)&toc_buffer, sizeof(toc_buffer));
178                 entry.address_format = CD_MSF_FORMAT;
179                 entry.data_len = sizeof(toc_buffer);
180                 entry.data = &toc_buffer;
181 #endif
182                 if (ioctl(CDADev[wDevID].unixdev, 
183 #ifdef linux
184                           CDROMREADTOCENTRY
185 #elif __FreeBSD__
186                           CDIOREADTOCENTRYS
187 #endif
188                           , &entry)) {
189                         dprintf_cdaudio(stddeb,
190                                 "CDAUDIO_GetTracksInfo // error read entry\n");
191                         return FALSE;
192                         }
193 #ifdef linux
194                 start = CDFRAMES_PERSEC * (SECONDS_PERMIN * 
195                         entry.cdte_addr.msf.minute + entry.cdte_addr.msf.second) + 
196                         entry.cdte_addr.msf.frame;
197 #elif __FreeBSD__
198                 start = CDFRAMES_PERSEC * (SECONDS_PERMIN *
199                         toc_buffer.addr.msf.minute + toc_buffer.addr.msf.second) +
200                         toc_buffer.addr.msf.frame;
201 #endif
202                 if (i == 0) {
203                         last_start = start;
204                         CDADev[wDevID].dwFirstOffset = start;
205                         dprintf_cdaudio(stddeb,
206                                 "CDAUDIO_GetTracksInfo // dwFirstOffset=%u\n", 
207                                 start);
208                         }
209                 else {
210                         length = start - last_start;
211                         last_start = start;
212                         start = last_start - length;
213                         total_length += length;
214                         CDADev[wDevID].lpdwTrackLen[i - 1] = length;
215                         CDADev[wDevID].lpdwTrackPos[i - 1] = start;
216                         dprintf_cdaudio(stddeb,
217                         "CDAUDIO_GetTracksInfo // track #%u start=%u len=%u\n",
218                                 i, start, length);
219                         }
220                 }
221         CDADev[wDevID].dwTotalLen = total_length;
222         dprintf_cdaudio(stddeb,"CDAUDIO_GetTracksInfo // total_len=%u\n", 
223                 total_length);
224         fflush(stdout);
225         return TRUE;
226 #else
227         return FALSE;
228 #endif
229 }
230
231
232 /**************************************************************************
233 *                               CDAUDIO_mciOpen                 [internal]
234 */
235 static DWORD CDAUDIO_mciOpen(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMS lpParms)
236 {
237 #if defined(linux) || defined(__FreeBSD__)
238         dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen(%04X, %08lX, %p);\n", 
239                                         wDevID, dwFlags, lpParms);
240         if (lpParms == NULL) return MCIERR_INTERNAL;
241         wDevID = lpParms->wDeviceID;
242         if (CDADev[wDevID].nUseCount > 0) {
243                 /* The driver already open on this channel */
244                 /* If the driver was opened shareable before and this open specifies */
245                 /* shareable then increment the use count */
246                 if (CDADev[wDevID].fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
247                         ++CDADev[wDevID].nUseCount;
248                 else
249                         return MCIERR_MUST_USE_SHAREABLE;
250                 }
251         else {
252                 CDADev[wDevID].nUseCount = 1;
253                 CDADev[wDevID].fShareable = dwFlags & MCI_OPEN_SHAREABLE;
254                 }
255     if (dwFlags & MCI_OPEN_ELEMENT) {
256                 dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen // MCI_OPEN_ELEMENT !\n");
257 /*              return MCIERR_NO_ELEMENT_ALLOWED; */
258                 }
259         memcpy(&CDADev[wDevID].openParms, lpParms, sizeof(MCI_OPEN_PARMS));
260         CDADev[wDevID].wNotifyDeviceID = lpParms->wDeviceID;
261         CDADev[wDevID].unixdev = open (CDAUDIO_DEV, O_RDONLY, 0);
262         if (CDADev[wDevID].unixdev == -1) {
263                 dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen // can't open '%s' !\n", CDAUDIO_DEV);
264                 return MCIERR_HARDWARE;
265                 }
266         CDADev[wDevID].mode = 0;
267         CDADev[wDevID].dwTimeFormat = MCI_FORMAT_TMSF;
268         CDADev[wDevID].nCurTrack = 0;
269         CDADev[wDevID].nTracks = 0;
270         CDADev[wDevID].dwTotalLen = 0;
271         CDADev[wDevID].dwFirstOffset = 0;
272         CDADev[wDevID].lpdwTrackLen = NULL;
273         CDADev[wDevID].lpdwTrackPos = NULL;
274         if (!CDAUDIO_GetTracksInfo(wDevID)) {
275                 dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen // error reading TracksInfo !\n");
276 /*              return MCIERR_INTERNAL; */
277                 }
278         if (dwFlags & MCI_NOTIFY) {
279                 dprintf_cdaudio(stddeb,
280                         "CDAUDIO_mciOpen // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
281                         lpParms->dwCallback);
282                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
283                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
284                 }
285         return 0;
286 #else
287         return MCIERR_HARDWARE;
288 #endif
289 }
290
291 /**************************************************************************
292 *                               CDAUDIO_mciClose                [internal]
293 */
294 static DWORD CDAUDIO_mciClose(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
295 {
296 #if defined(linux) || defined(__FreeBSD__)
297         dprintf_cdaudio(stddeb,"CDAUDIO_mciClose(%u, %08lX, %p);\n", 
298                 wDevID, dwParam, lpParms);
299         if (CDADev[wDevID].lpdwTrackLen != NULL) free(CDADev[wDevID].lpdwTrackLen);
300         if (CDADev[wDevID].lpdwTrackPos != NULL) free(CDADev[wDevID].lpdwTrackPos);
301         close(CDADev[wDevID].unixdev);
302 #endif
303         return 0;
304 }
305
306 /**************************************************************************
307 *                               CDAUDIO_mciGetDevCaps   [internal]
308 */
309 static DWORD CDAUDIO_mciGetDevCaps(UINT wDevID, DWORD dwFlags, 
310                                                 LPMCI_GETDEVCAPS_PARMS lpParms)
311 {
312 #if defined(linux) || defined(__FreeBSD__)
313         dprintf_cdaudio(stddeb,"CDAUDIO_mciGetDevCaps(%u, %08lX, %p);\n", 
314                 wDevID, dwFlags, lpParms);
315         if (lpParms == NULL) return MCIERR_INTERNAL;
316         if (dwFlags & MCI_GETDEVCAPS_ITEM) {
317                 dprintf_cdaudio(stddeb,
318                 "CDAUDIO_mciGetDevCaps // MCI_GETDEVCAPS_ITEM dwItem=%08lX;\n",
319                                 lpParms->dwItem);
320                 switch(lpParms->dwItem) {
321                         case MCI_GETDEVCAPS_CAN_RECORD:
322                                 lpParms->dwReturn = FALSE;
323                                 break;
324                         case MCI_GETDEVCAPS_HAS_AUDIO:
325                                 lpParms->dwReturn = TRUE;
326                                 break;
327                         case MCI_GETDEVCAPS_HAS_VIDEO:
328                                 lpParms->dwReturn = FALSE;
329                                 break;
330                         case MCI_GETDEVCAPS_DEVICE_TYPE:
331                                 lpParms->dwReturn = MCI_DEVTYPE_CD_AUDIO;
332                                 break;
333                         case MCI_GETDEVCAPS_USES_FILES:
334                                 lpParms->dwReturn = FALSE;
335                                 break;
336                         case MCI_GETDEVCAPS_COMPOUND_DEVICE:
337                                 lpParms->dwReturn = FALSE;
338                                 break;
339                         case MCI_GETDEVCAPS_CAN_EJECT:
340                                 lpParms->dwReturn = TRUE;
341                                 break;
342                         case MCI_GETDEVCAPS_CAN_PLAY:
343                                 lpParms->dwReturn = TRUE;
344                                 break;
345                         case MCI_GETDEVCAPS_CAN_SAVE:
346                                 lpParms->dwReturn = FALSE;
347                                 break;
348                         default:
349                                 return MCIERR_UNRECOGNIZED_COMMAND;
350                         }
351                 }
352         dprintf_cdaudio(stddeb,
353                 "CDAUDIO_mciGetDevCaps // lpParms->dwReturn=%08lX;\n", 
354                 lpParms->dwReturn);
355         return 0;
356 #else
357         return MCIERR_INTERNAL;
358 #endif
359 }
360
361 /**************************************************************************
362 *                               CDAUDIO_mciInfo                 [internal]
363 */
364 static DWORD CDAUDIO_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMS lpParms)
365 {
366 #if defined(linux) || defined(__FreeBSD__)
367         dprintf_cdaudio(stddeb,"CDAUDIO_mciInfo(%u, %08lX, %p);\n", 
368                 wDevID, dwFlags, lpParms);
369         if (lpParms == NULL) return MCIERR_INTERNAL;
370         lpParms->lpstrReturn = NULL;
371         switch(dwFlags) {
372                 case MCI_INFO_PRODUCT:
373                         lpParms->lpstrReturn = "Linux CDROM 0.5";
374                         break;
375                 default:
376                         return MCIERR_UNRECOGNIZED_COMMAND;
377                 }
378         if (lpParms->lpstrReturn != NULL)
379                 lpParms->dwRetSize = strlen(lpParms->lpstrReturn);
380         else
381                 lpParms->dwRetSize = 0;
382         return 0;
383 #else
384         return MCIERR_INTERNAL;
385 #endif
386 }
387
388
389 /**************************************************************************
390 *                               CDAUDIO_CalcFrame                       [internal]
391 */
392 static DWORD CDAUDIO_CalcFrame(UINT wDevID, DWORD dwFormatType, DWORD dwTime)
393 {
394         DWORD   dwFrame = 0;
395 #if defined(linux) || defined(__FreeBSD__)
396         UINT    wTrack;
397     
398         dprintf_cdaudio(stddeb,"CDAUDIO_CalcFrame(%u, %08lX, %lu);\n", 
399                 wDevID, dwFormatType, dwTime);
400     
401         switch (dwFormatType) {
402                 case MCI_FORMAT_MILLISECONDS:
403                         dwFrame = dwTime * CDFRAMES_PERSEC / 1000;
404                         dprintf_cdaudio(stddeb,
405                                 "CDAUDIO_CalcFrame // MILLISECONDS %lu\n", 
406                                 dwFrame);
407                         break;
408                 case MCI_FORMAT_MSF:
409                         dprintf_cdaudio(stddeb,
410                                 "CDAUDIO_CalcFrame // MSF %02u:%02u:%02u\n",
411                                 MCI_MSF_MINUTE(dwTime), MCI_MSF_SECOND(dwTime), 
412                                 MCI_MSF_FRAME(dwTime));
413                         dwFrame += CDFRAMES_PERMIN * MCI_MSF_MINUTE(dwTime);
414                         dwFrame += CDFRAMES_PERSEC * MCI_MSF_SECOND(dwTime);
415                         dwFrame += MCI_MSF_FRAME(dwTime);
416                         break;
417                 default:
418                         /* unknown format ! force TMSF ! ... */
419                         dwFormatType = MCI_FORMAT_TMSF;
420                 case MCI_FORMAT_TMSF:
421                         wTrack = MCI_TMSF_TRACK(dwTime);
422                         dprintf_cdaudio(stddeb,
423                         "CDAUDIO_CalcFrame // TMSF %02u-%02u:%02u:%02u\n",
424                                         MCI_TMSF_TRACK(dwTime), MCI_TMSF_MINUTE(dwTime), 
425                                         MCI_TMSF_SECOND(dwTime), MCI_TMSF_FRAME(dwTime));
426                         dprintf_cdaudio(stddeb,
427                                 "CDAUDIO_CalcFrame // TMSF trackpos[%u]=%lu\n",
428                                 wTrack, CDADev[wDevID].lpdwTrackPos[wTrack - 1]);
429                         dwFrame = CDADev[wDevID].lpdwTrackPos[wTrack - 1];
430                         dwFrame += CDFRAMES_PERMIN * MCI_TMSF_MINUTE(dwTime);
431                         dwFrame += CDFRAMES_PERSEC * MCI_TMSF_SECOND(dwTime);
432                         dwFrame += MCI_TMSF_FRAME(dwTime);
433                         break;
434                 }
435 #endif
436         return dwFrame;
437 }
438
439
440 /**************************************************************************
441 *                               CDAUDIO_GetCDStatus                             [internal]
442 */
443 static BOOL CDAUDIO_GetCDStatus(UINT wDevID)
444 {
445 #if defined(linux) || defined(__FreeBSD__)
446         int             oldmode = CDADev[wDevID].mode;
447 #ifdef __FreeBSD__
448         struct ioc_read_subchannel      read_sc;
449
450         read_sc.address_format = CD_MSF_FORMAT;
451         read_sc.data_format    = CD_CURRENT_POSITION;
452         read_sc.track          = 0;
453         read_sc.data_len       = sizeof(CDADev[wDevID].sc);
454         read_sc.data           = (struct cd_sub_channel_info *)&CDADev[wDevID].sc;
455 #elif linux
456         CDADev[wDevID].sc.cdsc_format = CDROM_MSF;
457 #endif
458         if (ioctl(CDADev[wDevID].unixdev,
459 #ifdef linux
460                   CDROMSUBCHNL, &CDADev[wDevID].sc
461 #elif __FreeBSD__
462                   CDIOCREADSUBCHANNEL, &read_sc
463 #endif
464                   )) {
465                 dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // opened or no_media !\n");
466                 CDADev[wDevID].mode = MCI_MODE_NOT_READY;
467                 return TRUE;
468                 }
469         switch (
470 #ifdef linux
471                 CDADev[wDevID].sc.cdsc_audiostatus
472 #elif __FreeBSD__
473                 CDADev[wDevID].sc.header.audio_status
474 #endif
475                 ) {
476 #ifdef linux
477                 case CDROM_AUDIO_INVALID:
478 #elif __FreeBSD__
479                 case CD_AS_AUDIO_INVALID:
480 #endif
481                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // device doesn't support status, returning NOT_READY.\n");
482 #ifdef linux
483                         CDADev[wDevID].mode = MCI_MODE_NOT_READY;
484 #elif __FreeBSD__
485                         CDADev[wDevID].mode = MCI_MODE_STOP;
486 #endif
487                         break;
488 #ifdef linux
489                 case CDROM_AUDIO_NO_STATUS: 
490 #elif __FreeBSD__
491                 case CD_AS_NO_STATUS:
492 #endif
493                         CDADev[wDevID].mode = MCI_MODE_STOP;
494                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // MCI_MODE_STOP !\n");
495                         break;
496 #ifdef linux
497                 case CDROM_AUDIO_PLAY: 
498 #elif __FreeBSD__
499                 case CD_AS_PLAY_IN_PROGRESS:
500 #endif
501                         CDADev[wDevID].mode = MCI_MODE_PLAY;
502                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // MCI_MODE_PLAY !\n");
503                         break;
504 #ifdef linux
505                 case CDROM_AUDIO_PAUSED:
506 #elif __FreeBSD__
507                 case CD_AS_PLAY_PAUSED:
508 #endif
509                         CDADev[wDevID].mode = MCI_MODE_PAUSE;
510                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // MCI_MODE_PAUSE !\n");
511                         break;
512                 default:
513                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // status=%02X !\n",
514 #ifdef linux
515                                         CDADev[wDevID].sc.cdsc_audiostatus
516 #elif __FreeBSD__
517                                         CDADev[wDevID].sc.header.audio_status
518 #endif
519                                         );
520                 }
521 #ifdef linux
522         CDADev[wDevID].nCurTrack = CDADev[wDevID].sc.cdsc_trk;
523         CDADev[wDevID].dwCurFrame = 
524                 CDFRAMES_PERMIN * CDADev[wDevID].sc.cdsc_absaddr.msf.minute +
525                 CDFRAMES_PERSEC * CDADev[wDevID].sc.cdsc_absaddr.msf.second +
526                 CDADev[wDevID].sc.cdsc_absaddr.msf.frame;
527 #elif __FreeBSD__
528         CDADev[wDevID].nCurTrack = CDADev[wDevID].sc.what.position.track_number;
529         CDADev[wDevID].dwCurFrame = 
530                 CDFRAMES_PERMIN * CDADev[wDevID].sc.what.position.absaddr.msf.minute +
531                 CDFRAMES_PERSEC * CDADev[wDevID].sc.what.position.absaddr.msf.second +
532                 CDADev[wDevID].sc.what.position.absaddr.msf.frame;
533 #endif
534         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // %02u-%02u:%02u:%02u \n",
535 #ifdef linux
536                 CDADev[wDevID].sc.cdsc_trk,
537                 CDADev[wDevID].sc.cdsc_absaddr.msf.minute,
538                 CDADev[wDevID].sc.cdsc_absaddr.msf.second,
539                 CDADev[wDevID].sc.cdsc_absaddr.msf.frame
540 #elif __FreeBSD__
541                 CDADev[wDevID].sc.what.position.track_number,
542                 CDADev[wDevID].sc.what.position.absaddr.msf.minute,
543                 CDADev[wDevID].sc.what.position.absaddr.msf.second,
544                 CDADev[wDevID].sc.what.position.absaddr.msf.frame
545 #endif
546                         );
547         if (oldmode != CDADev[wDevID].mode && oldmode == MCI_MODE_OPEN) {
548                 if (!CDAUDIO_GetTracksInfo(wDevID)) {
549             dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // error updating TracksInfo !\n");
550                         return MCIERR_INTERNAL;
551                         }
552                 }
553         return TRUE;
554 #else
555         return FALSE;
556 #endif
557 }
558
559
560 /**************************************************************************
561 *                               CDAUDIO_CalcTime                        [internal]
562 */
563 static DWORD CDAUDIO_CalcTime(UINT wDevID, DWORD dwFormatType, DWORD dwFrame)
564 {
565         DWORD   dwTime = 0;
566 #if defined(linux) || defined(__FreeBSD__)
567         UINT    wTrack;
568         UINT    wMinutes;
569         UINT    wSeconds;
570         UINT    wFrames;
571         dprintf_cdaudio(stddeb,"CDAUDIO_CalcTime(%u, %08lX, %lu);\n", 
572                 wDevID, dwFormatType, dwFrame);
573
574         switch (dwFormatType) {
575                 case MCI_FORMAT_MILLISECONDS:
576                         dwTime = dwFrame / CDFRAMES_PERSEC * 1000;
577                         dprintf_cdaudio(stddeb,
578                                 "CDAUDIO_CalcTime // MILLISECONDS %lu\n", 
579                                 dwTime);
580                         break;
581                 case MCI_FORMAT_MSF:
582                         wMinutes = dwFrame / CDFRAMES_PERMIN;
583                         wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
584                         wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - 
585                                                                 CDFRAMES_PERSEC * wSeconds;
586                         dwTime = MCI_MAKE_MSF(wMinutes, wSeconds, wFrames);
587                         dprintf_cdaudio(stddeb,"CDAUDIO_CalcTime // MSF %02u:%02u:%02u -> dwTime=%lu\n",
588                                                                 wMinutes, wSeconds, wFrames, dwTime);
589                         break;
590                 default:
591                         /* unknown format ! force TMSF ! ... */
592                         dwFormatType = MCI_FORMAT_TMSF;
593                 case MCI_FORMAT_TMSF:
594                         for (wTrack = 0; wTrack < CDADev[wDevID].nTracks; wTrack++) {
595 /*                              dwTime += CDADev[wDevID].lpdwTrackLen[wTrack - 1];
596                                 printf("Adding trk#%u curpos=%u \n", dwTime);
597                                 if (dwTime >= dwFrame) break; */
598                                 if (CDADev[wDevID].lpdwTrackPos[wTrack - 1] >= dwFrame) break;
599                                 }
600                         wMinutes = dwFrame / CDFRAMES_PERMIN;
601                         wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
602                         wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - 
603                                                                 CDFRAMES_PERSEC * wSeconds;
604                         dwTime = MCI_MAKE_TMSF(wTrack, wMinutes, wSeconds, wFrames);
605                         dprintf_cdaudio(stddeb,
606                                 "CDAUDIO_CalcTime // %02u-%02u:%02u:%02u\n",
607                                         wTrack, wMinutes, wSeconds, wFrames);
608                         break;
609                 }
610 #endif
611         return dwTime;
612 }
613
614
615 /**************************************************************************
616 *                               CDAUDIO_mciStatus               [internal]
617 */
618 static DWORD CDAUDIO_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
619 {
620 #if defined(linux) || defined(__FreeBSD__)
621         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus(%u, %08lX, %p);\n", 
622                 wDevID, dwFlags, lpParms);
623         if (lpParms == NULL) return MCIERR_INTERNAL;
624         if (CDADev[wDevID].unixdev == 0) return MMSYSERR_NOTENABLED;
625         if (dwFlags & MCI_NOTIFY) {
626                 dprintf_cdaudio(stddeb,
627                         "CDAUDIO_mciStatus // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
628                         lpParms->dwCallback);
629                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
630                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
631                 }
632         if (dwFlags & MCI_STATUS_ITEM) {
633                 switch(lpParms->dwItem) {
634                         case MCI_STATUS_CURRENT_TRACK:
635                                 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
636                                 lpParms->dwReturn = CDADev[wDevID].nCurTrack;
637                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // CURRENT_TRACK=%lu!\n", lpParms->dwReturn);
638                                 return 0;
639                         case MCI_STATUS_LENGTH:
640                                 if (CDADev[wDevID].nTracks == 0) {
641                                         if (!CDAUDIO_GetTracksInfo(wDevID)) {
642                                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // error reading TracksInfo !\n");
643                                                 return MCIERR_INTERNAL;
644                                                 }
645                                         }
646                                 if (dwFlags & MCI_TRACK) {
647                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_TRACK #%lu LENGTH=??? !\n",
648                                                         lpParms->dwTrack);
649                                         if (lpParms->dwTrack > CDADev[wDevID].nTracks)
650                                                 return MCIERR_OUTOFRANGE;
651                                         lpParms->dwReturn = CDADev[wDevID].lpdwTrackLen[lpParms->dwTrack];
652                                         }
653                                 else
654                                         lpParms->dwReturn = CDADev[wDevID].dwTotalLen;
655                                 lpParms->dwReturn = CDAUDIO_CalcTime(wDevID, 
656                                         CDADev[wDevID].dwTimeFormat, lpParms->dwReturn);
657                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // LENGTH=%lu !\n", lpParms->dwReturn);
658                                 return 0;
659                         case MCI_STATUS_MODE:
660                                 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
661                                 lpParms->dwReturn = CDADev[wDevID].mode;
662                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_MODE=%08lX !\n",
663                                                                                                 lpParms->dwReturn);
664                                 return 0;
665                         case MCI_STATUS_MEDIA_PRESENT:
666                                 lpParms->dwReturn = (CDADev[wDevID].nTracks > 0) ? TRUE : FALSE;
667                                 if (lpParms->dwReturn == FALSE)
668                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MEDIA_NOT_PRESENT !\n");
669                                 else
670                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_MEDIA_PRESENT !\n");
671                                 return 0;
672                         case MCI_STATUS_NUMBER_OF_TRACKS:
673                                 lpParms->dwReturn = CDAUDIO_GetNumberOfTracks(wDevID);
674                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_NUMBER_OF_TRACKS = %lu !\n",
675                                                                                                         lpParms->dwReturn);
676                                 if (lpParms->dwReturn == (WORD)-1) return MCIERR_INTERNAL;
677                                 return 0;
678                         case MCI_STATUS_POSITION:
679                                 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
680                                 lpParms->dwReturn = CDADev[wDevID].dwCurFrame;
681                                 if (dwFlags & MCI_STATUS_START) {
682                                         lpParms->dwReturn = CDADev[wDevID].dwFirstOffset;
683                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // get MCI_STATUS_START !\n");
684                                         }
685                                 if (dwFlags & MCI_TRACK) {
686                                         if (lpParms->dwTrack > CDADev[wDevID].nTracks)
687                                                 return MCIERR_OUTOFRANGE;
688                                         lpParms->dwReturn = CDADev[wDevID].lpdwTrackPos[lpParms->dwTrack - 1];
689                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // get MCI_TRACK #%lu !\n", lpParms->dwTrack);
690                                         }
691                                 lpParms->dwReturn = CDAUDIO_CalcTime(wDevID,
692                                         CDADev[wDevID].dwTimeFormat, lpParms->dwReturn);
693                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_POSITION=%08lX !\n",
694                                                                                                                 lpParms->dwReturn);
695                                 return 0;
696                         case MCI_STATUS_READY:
697                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_READY !\n");
698                                 lpParms->dwReturn = TRUE;
699                                 return 0;
700                         case MCI_STATUS_TIME_FORMAT:
701                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_TIME_FORMAT !\n");
702                                 lpParms->dwReturn = CDADev[wDevID].dwTimeFormat;
703                                 return 0;
704                         default:
705                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // unknown command %08lX !\n", lpParms->dwItem);
706                                 return MCIERR_UNRECOGNIZED_COMMAND;
707                         }
708                 }
709         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // not MCI_STATUS_ITEM !\n");
710         return 0;
711 #else
712         return MMSYSERR_NOTENABLED;
713 #endif
714 }
715
716
717 /**************************************************************************
718 *                               CDAUDIO_mciPlay                 [internal]
719 */
720 static DWORD CDAUDIO_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
721 {
722 #if defined(linux) || defined(__FreeBSD__)
723         int     start, end;
724 #ifdef linux
725         struct  cdrom_msf       msf;
726 #elif __FreeBSD__
727         struct  ioc_play_msf    msf;
728 #endif
729         dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay(%u, %08lX, %p);\n", 
730                 wDevID, dwFlags, lpParms);
731         if (lpParms == NULL) return MCIERR_INTERNAL;
732         if (CDADev[wDevID].unixdev == 0) return MMSYSERR_NOTENABLED;
733         start = 0;              end = CDADev[wDevID].dwTotalLen;
734         CDADev[wDevID].nCurTrack = 1;
735         if (dwFlags & MCI_FROM) {
736                 start = CDAUDIO_CalcFrame(wDevID, 
737                         CDADev[wDevID].dwTimeFormat, lpParms->dwFrom); 
738         dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // MCI_FROM=%08lX -> %u \n",
739                                 lpParms->dwFrom, start);
740                 }
741         if (dwFlags & MCI_TO) {
742                 end = CDAUDIO_CalcFrame(wDevID, 
743                         CDADev[wDevID].dwTimeFormat, lpParms->dwTo);
744                 dprintf_cdaudio(stddeb,
745                         "CDAUDIO_mciPlay // MCI_TO=%08lX -> %u \n",
746                         lpParms->dwTo, end);
747                 }
748         start += CDADev[wDevID].dwFirstOffset;  
749         end += CDADev[wDevID].dwFirstOffset;
750 #ifdef linux
751         msf.cdmsf_min0 = start / CDFRAMES_PERMIN;
752         msf.cdmsf_sec0 = (start % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
753         msf.cdmsf_frame0 = start % CDFRAMES_PERSEC;
754         msf.cdmsf_min1 = end / CDFRAMES_PERMIN;
755         msf.cdmsf_sec1 = (end % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
756         msf.cdmsf_frame1 = end % CDFRAMES_PERSEC;
757 #elif __FreeBSD__
758         msf.start_m     = start / CDFRAMES_PERMIN;
759         msf.start_s     = (start % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
760         msf.start_f     = start % CDFRAMES_PERSEC;
761         msf.end_m       = end / CDFRAMES_PERMIN;
762         msf.end_s       = (end % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
763         msf.end_f       = end % CDFRAMES_PERSEC;
764 #endif
765         if (ioctl(CDADev[wDevID].unixdev,
766 #ifdef linux
767                   CDROMSTART
768 #elif __FreeBSD__
769                   CDIOCSTART
770 #endif
771                   )) {
772                 dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // motor doesn't start !\n");
773                 return MCIERR_HARDWARE;
774                 }
775         if (ioctl(CDADev[wDevID].unixdev, 
776 #ifdef linux
777                   CDROMPLAYMSF
778 #elif __FreeBSD__
779                   CDIOCPLAYMSF
780 #endif
781                   , &msf)) {
782                 dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // device doesn't play !\n");
783                 return MCIERR_HARDWARE;
784                 }
785         dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // msf = %d:%d:%d %d:%d:%d\n",
786 #ifdef linux
787                 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
788                 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1
789 #elif __FreeBSD__
790                 msf.start_m, msf.start_s, msf.start_f,
791                 msf.end_m,   msf.end_s,   msf.end_f
792 #endif
793                         );
794         CDADev[wDevID].mode = MCI_MODE_PLAY;
795         if (dwFlags & MCI_NOTIFY) {
796                 dprintf_cdaudio(stddeb,
797                         "CDAUDIO_mciPlay // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
798                         lpParms->dwCallback);
799 /*
800                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
801                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
802 */
803                 }
804         return 0;
805 #else
806         return MCIERR_HARDWARE;
807 #endif
808 }
809
810 /**************************************************************************
811 *                               CDAUDIO_mciStop                 [internal]
812 */
813 static DWORD CDAUDIO_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
814 {
815 #if defined(linux) || defined(__FreeBSD__)
816         dprintf_cdaudio(stddeb,"CDAUDIO_mciStop(%u, %08lX, %p);\n", 
817                 wDevID, dwFlags, lpParms);
818         if (lpParms == NULL) return MCIERR_INTERNAL;
819         if (ioctl(CDADev[wDevID].unixdev,
820 #ifdef linux
821                   CDROMSTOP
822 #elif __FreeBSD__
823                   CDIOCSTOP
824 #endif
825                   )) return MCIERR_HARDWARE;
826         CDADev[wDevID].mode = MCI_MODE_STOP;
827         if (dwFlags & MCI_NOTIFY) {
828                 dprintf_cdaudio(stddeb,
829                         "CDAUDIO_mciStop // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
830                         lpParms->dwCallback);
831                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
832                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
833                 }
834         return 0;
835 #else
836         return MCIERR_HARDWARE;
837 #endif
838 }
839
840 /**************************************************************************
841 *                               CDAUDIO_mciPause                [internal]
842 */
843 static DWORD CDAUDIO_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
844 {
845 #if defined(linux) || defined(__FreeBSD__)
846         dprintf_cdaudio(stddeb,"CDAUDIO_mciPause(%u, %08lX, %p);\n", 
847                 wDevID, dwFlags, lpParms);
848         if (lpParms == NULL) return MCIERR_INTERNAL;
849         if (ioctl(CDADev[wDevID].unixdev,
850 #ifdef linux
851                   CDROMPAUSE
852 #elif __FreeBSD__
853                   CDIOCPAUSE
854 #endif
855                   )) return MCIERR_HARDWARE;
856         CDADev[wDevID].mode = MCI_MODE_PAUSE;
857         if (dwFlags & MCI_NOTIFY) {
858         dprintf_cdaudio(stddeb,
859                 "CDAUDIO_mciPause // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
860                 lpParms->dwCallback);
861                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
862                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
863                 }
864         return 0;
865 #else
866         return MCIERR_HARDWARE;
867 #endif
868 }
869
870 /**************************************************************************
871 *                               CDAUDIO_mciResume               [internal]
872 */
873 static DWORD CDAUDIO_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
874 {
875 #if defined(linux) || defined(__FreeBSD__)
876         dprintf_cdaudio(stddeb,"CDAUDIO_mciResume(%u, %08lX, %p);\n", 
877                 wDevID, dwFlags, lpParms);
878         if (lpParms == NULL) return MCIERR_INTERNAL;
879         if (ioctl(CDADev[wDevID].unixdev, 
880 #ifdef linux
881                   CDROMRESUME
882 #elif __FreeBSD__
883                   CDIOCRESUME
884 #endif
885                   )) return MCIERR_HARDWARE;
886         CDADev[wDevID].mode = MCI_MODE_STOP;
887         if (dwFlags & MCI_NOTIFY) {
888                 dprintf_cdaudio(stddeb,
889                         "CDAUDIO_mciResume // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
890                         lpParms->dwCallback);
891                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
892                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
893                 }
894         return 0;
895 #else
896         return MCIERR_HARDWARE;
897 #endif
898 }
899
900 /**************************************************************************
901 *                               CDAUDIO_mciSeek                 [internal]
902 */
903 static DWORD CDAUDIO_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
904 {
905 #if defined(linux) || defined(__FreeBSD__)
906         DWORD   dwRet;
907         MCI_PLAY_PARMS PlayParms;
908         dprintf_cdaudio(stddeb,"CDAUDIO_mciSeek(%u, %08lX, %p);\n", 
909                 wDevID, dwFlags, lpParms);
910         if (lpParms == NULL) return MCIERR_INTERNAL;
911         if (ioctl(CDADev[wDevID].unixdev,
912 #ifdef linux
913                   CDROMRESUME
914 #elif __FreeBSD__
915                   CDIOCRESUME
916 #endif
917                   )) {
918                 perror("ioctl CDROMRESUME");
919                 return MCIERR_HARDWARE;
920         }
921         CDADev[wDevID].mode = MCI_MODE_SEEK;
922         switch(dwFlags) {
923                 case MCI_SEEK_TO_START:
924                         PlayParms.dwFrom = 0;
925                         break;
926                 case MCI_SEEK_TO_END:
927                         PlayParms.dwFrom = CDADev[wDevID].dwTotalLen;
928                         break;
929                 case MCI_TO:
930                         PlayParms.dwFrom = lpParms->dwTo;
931                         break;
932                 }
933         dwRet = CDAUDIO_mciPlay(wDevID, MCI_WAIT | MCI_FROM, &PlayParms);
934         if (dwRet != 0) return dwRet;
935         dwRet = CDAUDIO_mciStop(wDevID, MCI_WAIT, (LPMCI_GENERIC_PARMS)&PlayParms);
936         if (dwFlags & MCI_NOTIFY) {
937                 dprintf_cdaudio(stddeb,
938                         "CDAUDIO_mciSeek // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
939                         lpParms->dwCallback);
940                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
941                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
942                 }
943         return dwRet;
944 #else
945         return MCIERR_HARDWARE;
946 #endif
947 }
948
949
950 /**************************************************************************
951 *                               CDAUDIO_mciSet                  [internal]
952 */
953 static DWORD CDAUDIO_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
954 {
955 #if defined(linux) || defined(__FreeBSD__)
956         dprintf_cdaudio(stddeb,"CDAUDIO_mciSet(%u, %08lX, %p);\n", 
957                 wDevID, dwFlags, lpParms);
958         if (lpParms == NULL) return MCIERR_INTERNAL;
959 /*
960         printf("CDAUDIO_mciSet // dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
961         printf("CDAUDIO_mciSet // dwAudio=%08lX\n", lpParms->dwAudio);
962 */
963         if (dwFlags & MCI_SET_TIME_FORMAT) {
964                 switch (lpParms->dwTimeFormat) {
965                         case MCI_FORMAT_MILLISECONDS:
966                                 dprintf_cdaudio(stddeb,
967                                 "CDAUDIO_mciSet // MCI_FORMAT_MILLISECONDS !\n");
968                                 break;
969                         case MCI_FORMAT_MSF:
970                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciSet // MCI_FORMAT_MSF !\n");
971                                 break;
972                         case MCI_FORMAT_TMSF:
973                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciSet // MCI_FORMAT_TMSF !\n");
974                                 break;
975                         default:
976                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciSet // bad time format !\n");
977                                 return MCIERR_BAD_TIME_FORMAT;
978                         }
979                 CDADev[wDevID].dwTimeFormat = lpParms->dwTimeFormat;
980                 }
981         if (dwFlags & MCI_SET_DOOR_OPEN) {
982                 dprintf_cdaudio(stddeb,
983                         "CDAUDIO_mciSet // MCI_SET_DOOR_OPEN !\n");
984 #ifdef __FreeBSD__
985                 if (ioctl(CDADev[wDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
986                 if (ioctl(CDADev[wDevID].unixdev, CDIOCEJECT)) return MCIERR_HARDWARE;
987                 if (ioctl(CDADev[wDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
988 #elif linux
989                 if (ioctl(CDADev[wDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
990 #endif
991                 CDADev[wDevID].nTracks = 0;
992                 }
993         if (dwFlags & MCI_SET_DOOR_CLOSED) {
994                 dprintf_cdaudio(stddeb,
995                         "CDAUDIO_mciSet // MCI_SET_DOOR_CLOSED !\n");
996 #ifdef __FreeBSD__
997                 if (ioctl(CDADev[wDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
998                 if (ioctl(CDADev[wDevID].unixdev, CDIOCCLOSE)) return MCIERR_HARDWARE;
999                 if (ioctl(CDADev[wDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
1000 #elif linux
1001                 if (ioctl(CDADev[wDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
1002                           /* XXX should it be ",1" ??? */
1003 #endif
1004                 CDADev[wDevID].nTracks = 0;
1005                 }
1006         if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
1007         if (dwFlags & MCI_SET_ON) return MCIERR_UNSUPPORTED_FUNCTION;
1008         if (dwFlags & MCI_SET_OFF) return MCIERR_UNSUPPORTED_FUNCTION;
1009         if (dwFlags & MCI_NOTIFY) {
1010                 dprintf_cdaudio(stddeb,
1011                         "CDAUDIO_mciSet // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
1012                         lpParms->dwCallback);
1013                 mciDriverNotify((HWND)LOWORD(lpParms->dwCallback), 
1014                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1015                 }
1016         return 0;
1017 #else
1018         return MCIERR_HARDWARE;
1019 #endif
1020 }
1021
1022
1023 /**************************************************************************
1024 *                               CDAUDIO_DriverProc              [sample driver]
1025 */
1026 LONG CDAUDIO_DriverProc(DWORD dwDevID, HDRVR16 hDriv, WORD wMsg, 
1027                                                         DWORD dwParam1, DWORD dwParam2)
1028 {
1029 #if defined(linux) || defined(__FreeBSD__)
1030         switch(wMsg) {
1031                 case DRV_LOAD:
1032                         return 1;
1033                 case DRV_FREE:
1034                         return 1;
1035                 case DRV_OPEN:
1036                 case MCI_OPEN_DRIVER:
1037                 case MCI_OPEN:
1038                         return CDAUDIO_mciOpen(dwDevID, dwParam1, (LPMCI_OPEN_PARMS)PTR_SEG_TO_LIN(dwParam2)); 
1039                 case DRV_CLOSE:
1040                 case MCI_CLOSE_DRIVER:
1041                 case MCI_CLOSE:
1042                         return CDAUDIO_mciClose(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1043                 case DRV_ENABLE:
1044                         return 1;
1045                 case DRV_DISABLE:
1046                         return 1;
1047                 case DRV_QUERYCONFIGURE:
1048                         return 1;
1049                 case DRV_CONFIGURE:
1050                         MessageBox((HWND)NULL, "Sample MultiMedia Linux Driver !", 
1051                                                                 "MMLinux Driver", MB_OK);
1052                         return 1;
1053                 case DRV_INSTALL:
1054                         return DRVCNF_RESTART;
1055                 case DRV_REMOVE:
1056                         return DRVCNF_RESTART;
1057                 case MCI_GETDEVCAPS:
1058                         return CDAUDIO_mciGetDevCaps(dwDevID, dwParam1, 
1059                                 (LPMCI_GETDEVCAPS_PARMS)PTR_SEG_TO_LIN(dwParam2));
1060                 case MCI_INFO:
1061                         return CDAUDIO_mciInfo(dwDevID, dwParam1, 
1062                                 (LPMCI_INFO_PARMS)PTR_SEG_TO_LIN(dwParam2));
1063                 case MCI_STATUS:
1064                         return CDAUDIO_mciStatus(dwDevID, dwParam1, 
1065                                 (LPMCI_STATUS_PARMS)PTR_SEG_TO_LIN(dwParam2));
1066                 case MCI_SET:
1067                         return CDAUDIO_mciSet(dwDevID, dwParam1, 
1068                                 (LPMCI_SET_PARMS)PTR_SEG_TO_LIN(dwParam2));
1069                 case MCI_PLAY:
1070                         return CDAUDIO_mciPlay(dwDevID, dwParam1, 
1071                                 (LPMCI_PLAY_PARMS)PTR_SEG_TO_LIN(dwParam2));
1072                 case MCI_STOP:
1073                         return CDAUDIO_mciStop(dwDevID, dwParam1, 
1074                                 (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1075                 case MCI_PAUSE:
1076                         return CDAUDIO_mciPause(dwDevID, dwParam1, 
1077                                 (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1078                 case MCI_RESUME:
1079                         return CDAUDIO_mciResume(dwDevID, dwParam1, 
1080                                 (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1081                 case MCI_SEEK:
1082                         return CDAUDIO_mciSeek(dwDevID, dwParam1, 
1083                                 (LPMCI_SEEK_PARMS)PTR_SEG_TO_LIN(dwParam2));
1084                 case MCI_SET_DOOR_OPEN:
1085                         dprintf_cdaudio(stddeb,
1086                                 "CDAUDIO_DriverProc // MCI_SET_DOOR_OPEN !\n");
1087 #ifdef __FreeBSD__
1088                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
1089                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCEJECT)) return MCIERR_HARDWARE;
1090                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
1091 #elif linux
1092                         if (ioctl(CDADev[dwDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
1093 #endif
1094                         CDADev[dwDevID].nTracks = 0;
1095                         return 0;
1096                 case MCI_SET_DOOR_CLOSED:
1097                         dprintf_cdaudio(stddeb,"CDAUDIO_DriverProc // MCI_SET_DOOR_CLOSED !\n");
1098 #ifdef __FreeBSD__
1099                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
1100                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCCLOSE)) return MCIERR_HARDWARE;
1101                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
1102 #elif linux
1103                         if (ioctl(CDADev[dwDevID].unixdev, CDROMEJECT, 1)) return MCIERR_HARDWARE;
1104 #endif
1105                         CDADev[dwDevID].nTracks = 0;
1106                         return 0;
1107                 default:
1108                         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1109                 }
1110 #else
1111         return MCIERR_HARDWARE;
1112 #endif
1113 }
1114
1115
1116 /*-----------------------------------------------------------------------*/
1117
1118 #endif /* #ifdef BUILTIN_MMSYSTEM */