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