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