]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/mmedia/cdunix.cpp
More details about the internal module name changes
[wxWidgets.git] / contrib / src / mmedia / cdunix.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // Name: cdlinux.cpp
3 // Purpose: wxMMedia
4 // Author: Guilhem Lavaux
5 // Created: 1997
6 // Updated: 1998
7 // Copyright: (C) 1997, 1998, Guilhem Lavaux
8 // CVS Id: $Id$
9 // License: wxWindows license
10 ////////////////////////////////////////////////////////////////////////////////
11 #ifdef __GNUG__
12 #pragma implementation "cdunix.h"
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/defs.h"
19 #endif
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if HAVE_KNOWN_CDROM_INTERFACE
26
27 // ---------------------------------------------------------------------------
28 // MMedia headers
29 // ---------------------------------------------------------------------------
30
31 #include "wx/mmedia/cdbase.h"
32 #include "wx/mmedia/cdunix.h"
33
34 // ---------------------------------------------------------------------------
35 // System headers
36 // ---------------------------------------------------------------------------
37
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43
44 #ifdef __linux__
45 #include <linux/cdrom.h>
46 #else
47 // For Solaris
48 #include <sys/cdio.h>
49 #endif
50
51 // ---------------------------------------------------------------------------
52 // Implementation
53 // ---------------------------------------------------------------------------
54
55 IMPLEMENT_DYNAMIC_CLASS(wxCDAudioLinux, wxCDAudio)
56
57 wxCDAudioLinux::wxCDAudioLinux()
58 : wxCDAudio(), m_fd(-1)
59 {
60 OpenDevice(wxT("/dev/cdrom"));
61 }
62
63 wxCDAudioLinux::wxCDAudioLinux(const wxString& dev_name)
64 : wxCDAudio(), m_fd(-1)
65 {
66 OpenDevice(dev_name);
67 }
68
69 wxCDAudioLinux::~wxCDAudioLinux()
70 {
71 if (m_fd != -1) {
72 close(m_fd);
73 wxDELETE(m_trksize);
74 wxDELETE(m_trkpos);
75 }
76 }
77
78 void wxCDAudioLinux::OpenDevice(const wxString& dev_name)
79 {
80 struct cdrom_tocentry entry, old_entry;
81 struct cdrom_tochdr diskinf;
82 struct cdrom_msf0 *msf = &entry.cdte_addr.msf,
83 *old_msf = &old_entry.cdte_addr.msf;
84 wxCDtime *the_track;
85 wxCDtime tot_tm;
86 wxUint8 nb_tracks, i;
87 int hour, minute, second;
88
89 if (m_fd != -1)
90 return;
91
92 m_fd = open(dev_name.mb_str(), O_RDONLY);
93 if (m_fd == -1) {
94 m_toc = NULL;
95 return;
96 }
97 m_status = STOPPED;
98
99 ioctl(m_fd, CDROMREADTOCHDR, &diskinf);
100
101 nb_tracks = diskinf.cdth_trk1-diskinf.cdth_trk0+1;
102 m_trksize = new wxCDtime[nb_tracks+1];
103 m_trkpos = new wxCDtime[nb_tracks+1];
104
105 old_msf->minute = 0;
106 old_msf->second = 0;
107 for (i=diskinf.cdth_trk0;i<=diskinf.cdth_trk1;i++) {
108 entry.cdte_track = i;
109 entry.cdte_format = CDROM_MSF;
110 ioctl(m_fd, CDROMREADTOCENTRY, &entry);
111
112 minute = msf->minute - old_msf->minute;
113 second = msf->second - old_msf->second;
114 if (second < 0) {
115 minute--;
116 second += 60;
117 }
118
119 hour = minute / 60;
120 minute %= 60;
121
122 the_track = &m_trksize[i-diskinf.cdth_trk0];
123 the_track->track = i-diskinf.cdth_trk0;
124 the_track->hour = hour;
125 the_track->min = minute;
126 the_track->sec = second;
127
128 the_track = &m_trkpos[i-diskinf.cdth_trk0];
129 the_track->track = i-diskinf.cdth_trk0;
130 the_track->hour = old_msf->minute / 60;
131 the_track->min = old_msf->minute % 60;
132 the_track->sec = old_msf->second;
133 old_entry = entry;
134 }
135
136 entry.cdte_track = CDROM_LEADOUT;
137 entry.cdte_format = CDROM_MSF;
138 ioctl(m_fd, CDROMREADTOCENTRY, &entry);
139
140 tot_tm.track = nb_tracks;
141 tot_tm.hour = msf->minute / 60;
142 tot_tm.min = msf->minute % 60;
143 tot_tm.sec = msf->second % 60;
144
145 m_trksize[nb_tracks].track = nb_tracks;
146 minute = msf->minute - old_msf->minute;
147 second = msf->second - old_msf->second;
148 if (second < 0) {
149 minute--;
150 second += 60;
151 }
152 hour = minute / 60;
153 minute %= 60;
154
155 m_trksize[nb_tracks].hour = hour;
156 m_trksize[nb_tracks].min = minute;
157 m_trksize[nb_tracks].sec = second;
158 m_trkpos[nb_tracks].track = nb_tracks;
159 m_trkpos[nb_tracks].hour = old_msf->minute / 60;
160 m_trkpos[nb_tracks].min = old_msf->minute % 60;
161 m_trkpos[nb_tracks].sec = old_msf->second;
162
163 m_toc = new CDtoc(tot_tm, m_trksize, m_trkpos);
164 }
165
166 bool wxCDAudioLinux::Play(const wxCDtime& beg_time, const wxCDtime& end_time)
167 {
168 struct cdrom_msf track_msf;
169
170 track_msf.cdmsf_min0 = beg_time.hour * 60 + beg_time.min;
171 track_msf.cdmsf_sec0 = beg_time.sec;
172 track_msf.cdmsf_frame0 = 0;
173 track_msf.cdmsf_min1 = end_time.hour * 60 + end_time.min;
174 track_msf.cdmsf_sec1 = end_time.sec;
175 track_msf.cdmsf_frame1 = 0;
176 return (ioctl(m_fd, CDROMPLAYMSF, &track_msf) != -1);
177 }
178
179 bool wxCDAudioLinux::Pause()
180 {
181 return (ioctl(m_fd, CDROMPAUSE, 0) != -1);
182 }
183
184 bool wxCDAudioLinux::Resume()
185 {
186 return (ioctl(m_fd, CDROMRESUME, 0) != -1);
187 }
188
189 wxCDAudio::CDstatus wxCDAudioLinux::GetStatus()
190 {
191 struct cdrom_subchnl subchnl;
192 ioctl(m_fd, CDROMSUBCHNL, &subchnl);
193 switch (subchnl.cdsc_audiostatus) {
194 case CDROM_AUDIO_PLAY: return PLAYING;
195 case CDROM_AUDIO_PAUSED: return PAUSED;
196 case CDROM_AUDIO_COMPLETED: return STOPPED;
197 }
198
199 return STOPPED;
200 }
201
202 wxCDtime wxCDAudioLinux::GetTime()
203 {
204 wxCDtime cdtime;
205 struct cdrom_subchnl subchnl;
206
207 ioctl(m_fd, CDROMSUBCHNL, &subchnl);
208 cdtime.track = subchnl.cdsc_trk;
209 cdtime.min = subchnl.cdsc_reladdr.msf.minute;
210 cdtime.hour = cdtime.min / 60;
211 cdtime.min %= 60;
212 cdtime.sec = subchnl.cdsc_reladdr.msf.second;
213
214 return cdtime;
215 }
216
217 wxCDAudio::CDtoc& wxCDAudioLinux::GetToc()
218 {
219 return *m_toc;
220 }
221
222 #endif