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