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