1 #include <cdaudio.h>
2 #include "audiere.h"
3 #include "debug.h"
4 #include "internal.h"
5 #include "utility.h"
6 
7 
8 namespace audiere {
9 
10   class CDDeviceUnix : public RefImplementation<CDDevice> {
11   public:
CDDeviceUnix(int device,const char * name)12     CDDeviceUnix(int device, const char* name) {
13       m_device = device;
14       m_name = name;
15     }
16 
~CDDeviceUnix()17     ~CDDeviceUnix() {
18       stop();
19       cd_finish(m_device);
20     }
21 
getName()22     const char* ADR_CALL getName() {
23       return m_name.c_str();
24     }
25 
getTrackCount()26     int ADR_CALL getTrackCount() {
27       disc_info disc;
28       if (cd_stat(m_device, &disc) == -1) {
29 	return 0;
30       }
31 
32       if (containsCD()) {
33 	return disc.disc_total_tracks;
34       } else {
35 	return 0;
36       }
37     }
38 
play(int track)39     void ADR_CALL play(int track) {
40       cd_play_track(m_device, track + 1, track + 1);
41     }
42 
stop()43     void ADR_CALL stop() {
44       cd_stop(m_device);
45     }
46 
pause()47     void ADR_CALL pause() {
48       cd_pause(m_device);
49     }
50 
resume()51     void ADR_CALL resume() {
52       cd_resume(m_device);
53     }
54 
isPlaying()55     bool ADR_CALL isPlaying() {
56       disc_info disc;
57       if (cd_stat(m_device, &disc) == -1) {
58 	return false;
59       }
60 
61       return disc.disc_mode == CDAUDIO_PLAYING;
62     }
63 
containsCD()64     bool ADR_CALL containsCD() {
65       disc_info disc;
66       if (cd_stat(m_device, &disc) == -1) {
67 	return false;
68       }
69 
70       return disc.disc_present != 0;
71     }
72 
isDoorOpen()73     bool ADR_CALL isDoorOpen() {
74       ADR_LOG("Warning: libcdaudio does not allow checking the door status.");
75       return false;
76     }
77 
openDoor()78     void ADR_CALL openDoor() {
79       cd_eject(m_device);
80     }
81 
closeDoor()82     void ADR_CALL closeDoor() {
83       cd_close(m_device);
84     }
85 
86   private:
87     int m_device;
88     std::string m_name;
89   };
90 
91 
AdrEnumerateCDDevices()92   ADR_EXPORT(const char*) AdrEnumerateCDDevices() {
93     // There is no clear way to do device enumeration with libcdaudio.
94     return "";
95   }
96 
AdrOpenCDDevice(const char * name)97   ADR_EXPORT(CDDevice*) AdrOpenCDDevice(const char* name) {
98     // Stupid non-const-correct APIs.
99     int device = cd_init_device(const_cast<char*>(name));
100     if (device == -1) {
101       return 0;
102     } else {
103       return new CDDeviceUnix(device, name);
104     }
105   }
106 
107 }
108