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