File: cdctl.h

package info (click to toggle)
wmcdplay 1.0beta1-13
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 640 kB
  • ctags: 831
  • sloc: cpp: 1,487; sh: 9; makefile: 6
file content (440 lines) | stat: -rw-r--r-- 12,975 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// cdctl.h - CDCtl class provides easy control of cd audio functions
// 05/09/98  Release 1.0 Beta1
// Copyright (C) 1998  Sam Hawker <shawkie@geocities.com>
// This software comes with ABSOLUTELY NO WARRANTY
// This software is free software, and you are welcome to redistribute it
// under certain conditions
// See the README file for a more complete notice.

// Although cdctl.h is an integral part of wmcdplay, it may also be distributed seperately.

// Change this define to alter the size of forward and backward skips (in frames)
// Yes, I know this should really be a method of CDCtl
#define _CDCTL_SKIP_SIZE 1125

// Try defining some of these. They may improve performance or reliability
// (or just plain make it work)
// #define _CDCTL_STOP_BEFORE_PLAY
// #define _CDCTL_START_BEFORE_PLAY
// #define _CDCTL_SOFT_STOP

// Define this if it stops after each track
#define _CDCTL_SENSITIVE_EOT
// If it still stops for a while between tracks, increase this (0-75 is a sensible range)
#define _CDCTL_SENSITIVITY 0

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef __linux__
#include <linux/cdrom.h>
#endif
#ifdef __GNU__
#include <sys/cdrom.h>
#endif

// CD status values
#define ssData     0
#define ssStopped  1
#define ssPlaying  2
#define ssPaused   3
#define ssNoCD     4
#define ssTrayOpen 5

// Audio command values
#define acStop   0
#define acPlay   1
#define acPause  2
#define acResume 3
#define acPrev   4
#define acNext   5
#define acRewd   6
#define acFFwd   7
#define acEject  8
#define acClose  9

// Track selection values (what to do when I've played the requested track)
// Note:      Track selection is not perfect - so use tsNone if you want to avoid trouble.
//            Basically, if we receive a CDROM_AUDIO_COMPLETED status, then we have to decide what to do.
//            If we think the last play command was ours (Next/Prev/FFwd/Rewd don't count), then we do something,
//            depending on the current track selection mode.
// Failures:  Sometimes we may think we sent the last play command when we did not (if we didn't see play stop in
//            in between).
//            If another application is polling the status, it may receive the CDROM_AUDIO_COMPLETED we are looking
//            for, and we will not, so will think play was stopped manually.
//            Similarly, we may read the CDROM_AUDIO_COMPLETED status when we don't want it, such that the other
//            application never sees it.
// Verdict:   Linux audio cdrom handling is broken.
// Update:    New define _CDCTL_SENSITIVE_EOT may help in cases where CDROM_AUDIO_COMPLETED is not being returned
//            correctly. It may, however, interfere with other running cd players.

// Update:    I think this works like a dream now. Even with many cd players sharing a cdrom. Let me know if not!!

#define tsNone      0    // Just stop
#define tsRepeat    1    // Play it again
#define tsNext      2    // Play next track (stop at end of CD)
#define tsRepeatCD  3    // Play next track (start from first track if end is reached)
#define tsRandom    4    // Play a track at random

class CDCtl
{
public:
   CDCtl(char *dname){
      device=(char *)malloc(sizeof(char)*(strlen(dname)+1));
      strcpy(device,dname);
      srand(getpid());
      tracksel=tsRandom;
      tskOurPlay=false;

      if((cdfdopen = (cdfd = open(device,O_RDONLY | O_NONBLOCK))) != -1) {
         status_state=ssNoCD;
         status_track=0;
         status_pos=0;
         cd_trklist=NULL;
         doStatus();
         readVolume();
      }
   }
   ~CDCtl(){
      if(cdfdopen){
         close(cdfd);
         if(device!=NULL)
            free(device);
         if(cd_trklist!=NULL)
            free(cd_trklist);
      }
   }
   bool openOK(){
      return cdfdopen;
   }
   void doAudioCommand(int cmd){
      if(cdfdopen){
         int newtrk=status_track;
	 switch(cmd){
	  case acStop:

             #ifdef _CDCTL_SOFT_STOP
             ioctl(cdfd,CDROMSTART);
             #endif
             #ifndef _CDCTL_SOFT_STOP
             ioctl(cdfd,CDROMSTOP);
             #endif
             tskOurPlay=false;

          break;
	  case acPlay:
             status_state=ssPlaying;
             select(status_track);
             tskOurPlay=true;
          break;
          case acPause:
             ioctl(cdfd,CDROMPAUSE);
          break;
	  case acResume:
             ioctl(cdfd,CDROMRESUME);
          break;
	  case acPrev:
             newtrk--;
             if(newtrk<0)
                newtrk=cd_tracks-1;
             select(newtrk);
          break;
 	  case acNext:
             newtrk++;
             if(newtrk>cd_tracks-1)
                newtrk=0;
             select(newtrk);
          break;
	  case acRewd:
	     if(status_pos>cd_trklist[status_track].track_start+_CDCTL_SKIP_SIZE){
                status_pos-=_CDCTL_SKIP_SIZE;
                play();
             }
          break;
          case acFFwd:
	    if(status_pos<cd_trklist[status_track].track_start+cd_trklist[status_track].track_len-_CDCTL_SKIP_SIZE){
               status_pos+=_CDCTL_SKIP_SIZE;
               play();
            }
          break;
	  case acEject:
             if(ioctl(cdfd,CDROMEJECT))
                status_state=ssNoCD;
             else
                status_state=ssTrayOpen;
          break;
	  case acClose:
             ioctl(cdfd,CDROMCLOSETRAY);
             status_state=ssNoCD;
          break;
         }
         doStatus();
      }
   }
   void doStatus(){
      if(cdfdopen){
         struct cdrom_subchnl sc;
         sc.cdsc_format=CDROM_MSF;
         if(ioctl(cdfd, CDROMSUBCHNL, &sc)){
            if(status_state!=ssNoCD)
               status_state=ssTrayOpen;
	    status_track=0;
            status_pos=0;
            tskOurPlay=false;
         }
         else{
            if(status_state==ssNoCD || status_state==ssTrayOpen)
	       readTOC();
            int start,now,stop;
            switch(sc.cdsc_audiostatus){
             case CDROM_AUDIO_PLAY:
                if(status_state==ssStopped)
                   tskOurPlay=false;
                status_state=ssPlaying;
             break;
             case CDROM_AUDIO_PAUSED:
                if(status_state==ssStopped)
                   tskOurPlay=false;
                status_state=ssPaused;
             break;
             case CDROM_AUDIO_COMPLETED:
	        if(tskOurPlay){
                   status_state=ssPlaying;
	           selecttrack();
	           doStatus();
	           return;
	        }
	        else
	           status_state=ssStopped;
	     break;
             default:

                #ifdef _CDCTL_SENSITIVE_EOT
                if(tskOurPlay){
                   start = cd_trklist[status_track].track_start;
                   stop = start + cd_trklist[status_track].track_len - _CDCTL_SENSITIVITY;
                   now = ((sc.cdsc_absaddr.msf.minute) * 60 + sc.cdsc_absaddr.msf.second) * 75 + sc.cdsc_absaddr.msf.frame - CD_MSF_OFFSET;
/*
cout << "=============" << endl;
cout << "start:" << start << endl;
cout << "stop:" << stop << endl;
cout << "now:" << now << endl;
*/
	           if(now>0 && (now<start || now>=stop)){
                      status_state=ssPlaying;
                      selecttrack();
/*
cout << "Play track " << status_track << endl;
cout << "=============" << endl;
*/
                      doStatus();
                      return;
                   }
                   else
                      status_state=ssStopped;
	        }
                else
                #endif

                   status_state=ssStopped;
            }
            trackinfo(&sc);
            if(cd_trklist[status_track].track_data)
               status_state=ssData;
         }
      }
   }
   void setVolume(int l, int r){
      if(cdfdopen){
         struct cdrom_volctrl vol;
         vol.channel0=l;
         vol.channel1=r;
         ioctl(cdfd,CDROMVOLCTRL,&vol);
         readVolume();
      }
   }
   void readVolume(){
      if(cdfdopen){
         struct cdrom_volctrl vol;
         ioctl(cdfd,CDROMVOLREAD,&vol);
         status_volumel=vol.channel0;
         status_volumer=vol.channel1;
      }
   }
   int getVolumeL(){
      return status_volumel;
   }
   int getVolumeR(){
      return status_volumer;
   }
   void setTrackSelection(int ts){
      tracksel=ts;
   }
   int getTrackSelection(){
      return tracksel;
   }
   char *getDevName(){
      return device;
   }
   int getCDTracks(){
      return cd_tracks;
   }
   int getCDLen(){
      return cd_len;
   }
   int getTrackStart(int trk){
      return cd_trklist[trk-1].track_start;
   }
   int getTrackLen(int trk){
      return cd_trklist[trk-1].track_len;
   }
   bool getTrackData(int trk){
      return cd_trklist[trk-1].track_data;
   }
   int getStatusState(){
      return status_state;
   }
   int getStatusTrack(){
      return status_track+1;
   }
   int getStatusPosAbs(){
      return status_pos-cd_trklist[0].track_start;
   }
   int getStatusPosRel(){
      return status_pos-cd_trklist[status_track].track_start;
   }
private:
   void readTOC(){
      if(cd_trklist!=NULL)
         free(cd_trklist);
      struct cdrom_tochdr hdr;
      ioctl(cdfd, CDROMREADTOCHDR, &hdr);
      cd_tracks=hdr.cdth_trk1;
      cd_trklist=(struct CDTrack *)malloc(cd_tracks*sizeof(struct CDTrack));
      struct cdrom_tocentry te;
      int prev_addr=0;
      for(int i=0;i<=cd_tracks;i++){
         if(i==cd_tracks)
            te.cdte_track=CDROM_LEADOUT;
         else
            te.cdte_track=i+1;
         te.cdte_format=CDROM_MSF;    // I think it is ok to read this as LBA, but for a quiet life...
         ioctl(cdfd, CDROMREADTOCENTRY, &te);
         int this_addr=((te.cdte_addr.msf.minute * 60) + te.cdte_addr.msf.second) * 75 + te.cdte_addr.msf.frame - CD_MSF_OFFSET;
         if(i>0)
	    cd_trklist[i-1].track_len = this_addr - prev_addr - 1;
         prev_addr=this_addr;
         if(i<cd_tracks){
            cd_trklist[i].track_data = te.cdte_ctrl & CDROM_DATA_TRACK ? true : false;
	    cd_trklist[i].track_start = this_addr;
         }
         else
            cd_len = this_addr;
      }
   }
   void trackinfo(struct cdrom_subchnl *subchnl){
      if(status_state==ssPlaying || status_state==ssPaused){
         status_pos=((subchnl->cdsc_absaddr.msf.minute) * 60 + subchnl->cdsc_absaddr.msf.second) * 75 + subchnl->cdsc_absaddr.msf.frame - CD_MSF_OFFSET;
         for(status_track=0;status_track<cd_tracks;status_track++){
            if(status_pos<cd_trklist[status_track].track_start+cd_trklist[status_track].track_len)
               break;
         }
      }
   }
   void play(){
      struct cdrom_msf pmsf;
      int abs0=status_pos + CD_MSF_OFFSET;
      int abs1=cd_trklist[status_track].track_start + cd_trklist[status_track].track_len - 1 + CD_MSF_OFFSET;
      pmsf.cdmsf_min0=abs0/(75*60);
      pmsf.cdmsf_min1=abs1/(75*60);
      pmsf.cdmsf_sec0=(abs0%(75*60))/75;
      pmsf.cdmsf_sec1=(abs1%(75*60))/75;
      pmsf.cdmsf_frame0=abs0%75;
      pmsf.cdmsf_frame1=abs1%75;

      #ifdef _CDCTL_STOP_BEFORE_PLAY
      ioctl(cdfd,CDROMSTOP);
      #endif
      #ifdef _CDCTL_START_BEFORE_PLAY
      ioctl(cdfd,CDROMSTART);
      #endif

      ioctl(cdfd,CDROMPLAYMSF,&pmsf);
   }
   void select(int trk){
      status_track=trk;
      status_pos=cd_trklist[status_track].track_start;
      if(status_state==ssPlaying){
         if(cd_trklist[status_track].track_data){

             #ifdef _CDCTL_HARD_STOP
             ioctl(cdfd,CDROMSTOP);
             #endif
             #ifndef _CDCTL_HARD_STOP
             ioctl(cdfd,CDROMSTART);
             #endif
             tskOurPlay=false;

         }
         else
            play();
      }
   }
   void selecttrack(){
      int newtrk=status_track;
      do{
         switch(tracksel){
          case tsNone:
             tskOurPlay=false;
             return;
          break;
          case tsRepeat:
             // do nothing
          break;
          case tsNext:
             newtrk++;
             if(newtrk>=cd_tracks){
                tskOurPlay=false;
                return;
             }
          break;
          case tsRepeatCD:
             newtrk++;
             if(newtrk>=cd_tracks)
                newtrk=0;
          break;
          case tsRandom:
             newtrk+=(int)((cd_tracks-1)*(float)rand()/RAND_MAX+1);
             if(newtrk>=cd_tracks)
                newtrk-=cd_tracks;
          break;
         }
      }  while(cd_trklist[newtrk].track_data);
      select(newtrk);
      play();
   }
   int cdfd;
   int cdfdopen;
   char *device;
   int tracksel;
   bool tskOurPlay;

   struct CDTrack{
      int track_start;
      int track_len;
      bool track_data;
   };

   int cd_tracks;
   int cd_len;
   struct CDTrack *cd_trklist;
   int status_state;
   int status_track;
   int status_pos;
   int status_volumel;
   int status_volumer;
};