~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to kscd/libwm/plat_linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: plat_linux.c,v 1.15.2.2 2001/09/24 12:05:57 mklingens Exp $
 
2
 * $Id: plat_linux.c,v 1.37 2005/02/01 20:12:07 aseigo Exp $
3
3
 *
4
4
 * This file is part of WorkMan, the civilized CD player library
5
5
 * (c) 1991-1997 by Steven Grimm (original author)
25
25
 * Linux-specific drive control routines.  Very similar to the Sun module.
26
26
 */
27
27
 
28
 
static char plat_linux_id[] = "$Id: plat_linux.c,v 1.15.2.2 2001/09/24 12:05:57 mklingens Exp $";
29
 
 
30
28
#if defined(__linux__)
31
29
 
32
30
#include <errno.h>
34
32
#include <stdlib.h>
35
33
#include <fcntl.h>
36
34
#include <unistd.h>
37
 
#include <malloc.h>
38
35
#include <string.h>
39
36
#include <sys/types.h>
40
37
#include <sys/param.h>
41
38
#include <sys/socket.h>
42
39
#include <sys/stat.h>
43
40
#include <sys/wait.h>
 
41
/* Try to get around bug #29274 */
 
42
#include <linux/version.h>
 
43
#if 0
 
44
/* this breaks the build on ia64 and s390 for example. 
 
45
   sys/types.h is already included and should provide __u64.
 
46
   please tell where we really need this and let's try to find
 
47
   a working #if case for everyone ... adrian@suse.de */
 
48
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,50)) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,21) && LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
 
49
#undef __GNUC__
 
50
typedef unsigned long long __u64; 
 
51
#endif
 
52
#endif
44
53
 
45
54
#include "include/wm_config.h"
46
55
#include "include/wm_struct.h"
58
67
  #endif
59
68
#endif
60
69
 
 
70
 
61
71
#include <sys/time.h>
62
72
#include <sys/ioctl.h>
 
73
 
 
74
#ifndef __GNUC__
 
75
#define __GNUC__ 1
 
76
#endif
 
77
/* needed for vanilla kernel headers, which do provide __u64 only
 
78
   for ansi */
 
79
#undef __STRICT_ANSI__
 
80
/* needed for non-ansi kernel headers */
 
81
#define asm __asm__
 
82
#define inline __inline__
 
83
#include <asm/types.h>
63
84
#include <linux/cdrom.h>
64
 
#include <linux/version.h>
 
85
#undef asm
 
86
#undef inline
65
87
 
66
88
#include "include/wm_cdda.h"
67
89
#include "include/wm_struct.h"
84
106
# define SCSI_IOCTL_SEND_COMMAND 1
85
107
#endif
86
108
 
 
109
#ifdef BUILD_CDDA
 
110
int gen_cdda_init( struct wm_drive *d );
 
111
#endif
 
112
 
87
113
int     min_volume = 0;
88
114
int     max_volume = 255;
89
115
 
91
117
int mixer;
92
118
char mixer_dev_name[20] = "/dev/mixer";
93
119
#endif
94
 
extern char     *cd_device, *cddaslave_path;
95
 
 
96
 
int     cdda_slave = -1;
97
120
 
98
121
/*-------------------------------------------------------*
99
122
 *
118
141
int
119
142
wmcd_open( struct wm_drive *d )
120
143
{
121
 
  int           fd;
122
 
  static int    warned = 0;
123
 
  int           retval = 0;
 
144
  int fd;
124
145
  char vendor[32], model[32], rev[32];
125
 
  
126
 
  if (cd_device == NULL)
127
 
    cd_device = DEFAULT_CD_DEVICE;
128
 
    
129
 
  
130
 
  if (d->fd >= 0)               /* Device already open? */
131
 
    {
 
146
 
 
147
  if (d->cd_device == NULL)
 
148
    d->cd_device = DEFAULT_CD_DEVICE;
 
149
 
 
150
 
 
151
  if (d->fd >= 0) { /* Device already open? */
132
152
/*      wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): [device is open (fd=%d)]\n", d->fd);*/
133
153
      return (0);
134
 
    }
 
154
  }
 
155
 
 
156
  fd = open(d->cd_device, O_RDONLY | O_NONBLOCK);
 
157
  wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): device=%s fd=%d\n", d->cd_device, fd);
135
158
  
136
 
  fd = open(cd_device, O_RDONLY | O_NONBLOCK);
137
 
 
138
159
  if (fd < 0)
139
 
    {
140
 
      if (errno == EACCES)
141
 
        {
142
 
          return -EACCES;
143
 
        }
144
 
      /* Hack proposed by Carey Evans, introduced by Debian maintainer :
145
 
       * treat EIO like ENXIO since some Linux drives do never return ENXIO
146
 
       * ENOMEDIUM is returned by Kernel 2.2.x unified drivers.
147
 
       */
148
 
      else if ((errno != ENXIO) && (errno != EIO) && (errno != ENOMEDIUM))
149
 
        {
150
 
          return (-6);
151
 
        }
152
 
      
153
 
      /* No CD in drive. */
154
 
      retval = 1;
155
 
    }
156
 
  
157
 
  
158
 
#ifdef LINUX_SCSI_PASSTHROUGH
 
160
    return -errno;
 
161
 
 
162
  /* Now fill in the relevant parts of the wm_drive structure. */
 
163
  d->fd = fd;
 
164
 
 
165
  /*
 
166
   * See if we can do digital audio.
 
167
   */
 
168
#if defined(BUILD_CDDA)
 
169
  if(d->cdda && gen_cdda_init(d)) {
 
170
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): failed in gen_cdda_init\n");
 
171
    gen_close(d);
 
172
    return -1;
 
173
  }
 
174
#endif
 
175
 
159
176
  /* Can we figure out the drive type? */
160
 
  retval = wm_scsi_get_drive_type(d, vendor, model, rev);
161
 
  if (retval == WM_ERR_SCSI_INQUIRY_FAILED)
162
 
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): After failed inquiry\n");
163
 
#endif
164
 
 
165
 
  *d = *(find_drive_struct(vendor, model, rev));
166
 
  wm_drive_settype(vendor, model, rev);
167
 
  
168
 
  d->fd = fd;
169
 
  (d->init)(d);
170
 
  return retval;
 
177
  if (wm_scsi_get_drive_type(d, vendor, model, rev)) {
 
178
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_open(): inquiry failed\n");
 
179
    strcpy(vendor, "Generic");
 
180
    strcpy(model, "drive type");
 
181
    strcpy(rev, "");
 
182
  }
 
183
 
 
184
  if(find_drive_struct(vendor, model, rev) < 0) {
 
185
    gen_close(d);
 
186
    return -1;
 
187
  }
 
188
 
 
189
  if(d->proto->gen_init)
 
190
    return (d->proto->gen_init)(d);
 
191
 
 
192
  return 0;
171
193
} /* wmcd_open() */
172
194
 
173
195
/*
181
203
  
182
204
  do {
183
205
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen\n");
184
 
    if (d->fd >= 0)             /* Device really open? */
185
 
      {
186
 
        wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closing the device\n");
187
 
        if(!close( d->fd ))   /* close it! */
188
 
          d->fd = -1;      /* closed */
189
 
      }
 
206
    gen_close(d);
190
207
    wm_susleep( 1000 );
191
208
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calls wmcd_open()\n");
192
209
    status = wmcd_open( d ); /* open it as usual */
248
265
 * send packet over cdrom interface
249
266
 * kernel >= 2.2.16
250
267
 *----------------------------------------*/
251
 
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,2,16))
 
268
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,2,15))       
252
269
 
253
270
  struct cdrom_generic_command cdc;
254
271
  struct request_sense sense;
255
 
  int ret, capability;
 
272
  int capability;
256
273
 
257
274
  wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wm_scsi over CDROM_SEND_PACKET entered\n");
258
275
 
261
278
  if(!(capability & CDC_GENERIC_PACKET))
262
279
  {
263
280
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "your CDROM or/and kernel don't support CDC_GENERIC_PACKET ...\n");
264
 
    printf("your CDROM or/and kernel don't support CDC_GENERIC_PACKET ...\n");
265
281
    return -1;
266
282
  }
267
283
 
274
290
  cdc.buflen = retbuflen;
275
291
  cdc.stat = 0;
276
292
  cdc.sense = &sense;
277
 
  cdc.data_direction = getreply;
 
293
  cdc.data_direction = getreply?CGC_DATA_READ:CGC_DATA_WRITE;
278
294
 
279
295
  /* sendpacket_over_cdrom_interface() */
280
296
  return ioctl(d->fd, CDROM_SEND_PACKET, &cdc);
285
301
#endif
286
302
} /* wm_scsi */
287
303
 
 
304
int
 
305
gen_close( struct wm_drive *d )
 
306
{
 
307
  if(d->fd != -1) {
 
308
    wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closing the device\n");
 
309
    close(d->fd);
 
310
    d->fd = -1;
 
311
  }
 
312
  return 0;
 
313
}
 
314
 
288
315
/*--------------------------------*
289
316
 * Keep the CD open all the time.
290
317
 * disabled, analogous to 1.4b3
303
330
        if (fork())
304
331
                exit(0);
305
332
 
 
333
#if defined(O_NOFOLLOW)
 
334
        if ((fd = open("/tmp/cd.lock", O_RDWR | O_CREAT | O_NOFOLLOW, 0666)) < 0)
 
335
#else
306
336
        if ((fd = open("/tmp/cd.lock", O_RDWR | O_CREAT, 0666)) < 0)
 
337
#endif
307
338
                exit(0);
308
339
        fl.l_type = F_WRLCK;
309
340
        fl.l_whence = 0;
328
359
 * numbers if the CD is playing or paused.
329
360
 *--------------------------------------------------------------------------*/
330
361
int
331
 
gen_get_drive_status( struct wm_drive *d, enum wm_cd_modes oldmode,
332
 
                      enum wm_cd_modes *mode, int *pos, int *track, int *index )
 
362
gen_get_drive_status( struct wm_drive *d, int oldmode,
 
363
  int *mode, int *pos, int *track, int *ind )
333
364
{
334
 
  struct cdrom_subchnl          sc;
 
365
  struct cdrom_subchnl sc;
 
366
  int ret;
335
367
  
336
368
#ifdef SBPCD_HACK
337
369
  static int prevpos = 0;
338
370
#endif
339
371
  
340
 
  /* If we can't get status, the CD is ejected, so default to that. */
341
 
  *mode = WM_CDM_EJECTED;
342
372
  
343
373
  /* Is the device open? */
344
 
  if (d->fd < 0)
345
 
    {
346
 
      switch (wmcd_open(d))
347
 
        {
348
 
        case -1:        /* error */
349
 
          return (-1);
350
 
        case 1:         /* retry */
351
 
          return (0);
352
 
        }
 
374
  if (d->fd < 0) {
 
375
    ret = wmcd_open(d);
 
376
    if(ret < 0) /* error */
 
377
      return ret;
 
378
          
 
379
    if(ret == 1) {
 
380
      /* retry */
 
381
      *mode = WM_CDM_UNKNOWN;
 
382
      return 0;
353
383
    }
354
 
 
 
384
  }
355
385
 
356
386
  /* Try to get rid of the door locking    */
357
387
  /* Don't care about return value. If it  */
358
388
  /* works - fine. If not - ...            */
359
 
  ioctl( d->fd, CDROM_LOCKDOOR, 0 ); 
360
 
 
 
389
  ioctl(d->fd, CDROM_LOCKDOOR, 0); 
 
390
  
 
391
  *mode = WM_CDM_UNKNOWN;
 
392
  
361
393
  sc.cdsc_format = CDROM_MSF;
362
394
 
363
 
  if (ioctl(d->fd, CDROMSUBCHNL, &sc))
364
 
    return (0);
 
395
#if defined(BUILD_CDDA)
 
396
  IFCDDA(d) {
 
397
    if(!cdda_get_drive_status(d, oldmode, mode, pos, track, ind)) {
 
398
      if(*mode == WM_CDM_STOPPED)
 
399
        *mode = WM_CDM_UNKNOWN; /* dont believe */
 
400
    }
 
401
  } else
 
402
#endif
 
403
  if(!ioctl(d->fd, CDROMSUBCHNL, &sc)) {
 
404
    switch (sc.cdsc_audiostatus) {
 
405
    case CDROM_AUDIO_PLAY:
 
406
      *mode = WM_CDM_PLAYING;
 
407
      *track = sc.cdsc_trk;
 
408
      *ind = sc.cdsc_ind;
 
409
      *pos = sc.cdsc_absaddr.msf.minute * 60 * 75 +
 
410
        sc.cdsc_absaddr.msf.second * 75 +
 
411
        sc.cdsc_absaddr.msf.frame;
 
412
#ifdef SBPCD_HACK
 
413
      if( *pos < prevpos ) {
 
414
        if( (prevpos - *pos) < 75 ) {
 
415
          *mode = WM_CDM_TRACK_DONE;
 
416
        }
 
417
      }
 
418
    
 
419
      prevpos = *pos;
 
420
#endif
 
421
      break;
 
422
    
 
423
    case CDROM_AUDIO_PAUSED:
 
424
      if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED) {
 
425
        *mode = WM_CDM_PAUSED;
 
426
        *track = sc.cdsc_trk;
 
427
        *ind = sc.cdsc_ind;
 
428
        *pos = sc.cdsc_absaddr.msf.minute * 60 * 75 +
 
429
          sc.cdsc_absaddr.msf.second * 75 +
 
430
          sc.cdsc_absaddr.msf.frame;
 
431
      } else
 
432
        *mode = WM_CDM_STOPPED;
 
433
      break;
 
434
    
 
435
    case CDROM_AUDIO_NO_STATUS:
 
436
        *mode = WM_CDM_STOPPED;
 
437
      break;
 
438
 
 
439
    case CDROM_AUDIO_COMPLETED:
 
440
      *mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
 
441
      break;
 
442
    
 
443
    case CDROM_AUDIO_INVALID: /**/
 
444
    default:
 
445
      *mode = WM_CDM_UNKNOWN;
 
446
      break;
 
447
    }
 
448
  }
365
449
  
366
 
  switch (sc.cdsc_audiostatus) {
367
 
  case CDROM_AUDIO_PLAY:
368
 
    *mode = WM_CDM_PLAYING;
369
 
    *track = sc.cdsc_trk;
370
 
    *index = sc.cdsc_ind;
371
 
    *pos = sc.cdsc_absaddr.msf.minute * 60 * 75 +
372
 
      sc.cdsc_absaddr.msf.second * 75 +
373
 
      sc.cdsc_absaddr.msf.frame;
374
 
#ifdef SBPCD_HACK
375
 
    if( *pos < prevpos )
376
 
      {
377
 
        if( (prevpos - *pos) < 75 )
378
 
          {
379
 
            *mode = WM_CDM_TRACK_DONE;
380
 
          }
381
 
      }
382
 
    
383
 
    prevpos = *pos;
384
 
#endif
385
 
    break;
386
 
    
387
 
  case CDROM_AUDIO_PAUSED:
388
 
  case CDROM_AUDIO_NO_STATUS:
389
 
  case CDROM_AUDIO_INVALID: /**/
390
 
    if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
391
 
      {
392
 
        *mode = WM_CDM_PAUSED;
393
 
        *track = sc.cdsc_trk;
394
 
        *index = sc.cdsc_ind;
395
 
        *pos = sc.cdsc_absaddr.msf.minute * 60 * 75 +
396
 
          sc.cdsc_absaddr.msf.second * 75 +
397
 
          sc.cdsc_absaddr.msf.frame;
398
 
      }
399
 
    else
 
450
  if(WM_CDS_NO_DISC(*mode)) {
 
451
    /* verify status of drive */
 
452
    ret = ioctl(d->fd, CDROM_DRIVE_STATUS, 0/* slot */);
 
453
    if(ret == CDS_DISC_OK)
 
454
      ret = ioctl(d->fd, CDROM_DISC_STATUS, 0);
 
455
    switch(ret) {
 
456
    case CDS_NO_DISC:
 
457
      *mode = WM_CDM_NO_DISC;
 
458
      break;
 
459
    case CDS_TRAY_OPEN:
 
460
      *mode = WM_CDM_EJECTED;
 
461
      break;
 
462
    case CDS_AUDIO:
 
463
    case CDS_MIXED:
400
464
      *mode = WM_CDM_STOPPED;
401
 
    break;
402
 
    
403
 
  case CDROM_AUDIO_COMPLETED:
404
 
    *mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
405
 
    break;
406
 
    
407
 
  default:
408
 
    *mode = WM_CDM_UNKNOWN;
409
 
    break;
 
465
      break;
 
466
    case CDS_DRIVE_NOT_READY:
 
467
    case CDS_NO_INFO:
 
468
    case CDS_DATA_1:
 
469
    case CDS_DATA_2:
 
470
    case CDS_XA_2_1:
 
471
    case CDS_XA_2_2:
 
472
    default:
 
473
      *mode = WM_CDM_UNKNOWN;
 
474
    }
410
475
  }
411
 
  
 
476
 
412
477
  return (0);
413
478
} /* gen_get_drive_status */
414
479
 
455
520
int
456
521
gen_get_cdlen(struct wm_drive *d, int *frames)
457
522
{
458
 
  int           tmp;
 
523
  int tmp;
459
524
 
460
525
  return gen_get_trackinfo( d, CDROM_LEADOUT, &tmp, frames);
461
526
} /* gen_get_cdlen() */
465
530
 * Play the CD from one position to another (both in frames.)
466
531
 *------------------------------------------------------------*/
467
532
int
468
 
gen_play(struct wm_drive *d, int start, int end)
 
533
gen_play(struct wm_drive *d, int start, int end, int realstart)
469
534
{
470
 
  struct cdrom_msf              msf;
 
535
  struct cdrom_msf msf;
 
536
 
 
537
  CDDARETURN(d) cdda_play(d, start, end, realstart);
471
538
 
472
539
  msf.cdmsf_min0 = start / (60*75);
473
540
  msf.cdmsf_sec0 = (start % (60*75)) / 75;
476
543
  msf.cdmsf_sec1 = (end % (60*75)) / 75;
477
544
  msf.cdmsf_frame1 = end % 75;
478
545
  
479
 
  if (ioctl(d->fd, CDROMPLAYMSF, &msf))
480
 
    {
481
 
      if (ioctl(d->fd, CDROMSTART))
482
 
        return (-1);
483
 
      if (ioctl(d->fd, CDROMPLAYMSF, &msf))
484
 
        return (-2);
485
 
    }
 
546
  if (ioctl(d->fd, CDROMPLAYMSF, &msf)) {
 
547
    if (ioctl(d->fd, CDROMSTART))
 
548
      return (-1);
 
549
    if (ioctl(d->fd, CDROMPLAYMSF, &msf))
 
550
      return (-2);
 
551
  }
486
552
  
487
553
  /*
488
554
   * I hope no drive gets really confused after CDROMSTART
505
571
int
506
572
gen_pause(struct wm_drive *d)
507
573
{
 
574
  CDDARETURN(d) cdda_pause(d);
508
575
  return (ioctl(d->fd, CDROMPAUSE));
509
576
}
510
577
 
514
581
int
515
582
gen_resume(struct wm_drive *d)
516
583
{
 
584
  CDDARETURN(d) cdda_pause(d);
517
585
  return (ioctl(d->fd, CDROMRESUME));
518
586
}
519
587
 
523
591
int
524
592
gen_stop(struct wm_drive *d)
525
593
{
 
594
  CDDARETURN(d) cdda_stop(d);
526
595
  return (ioctl(d->fd, CDROMSTOP));
527
596
}
528
597
 
543
612
  
544
613
  wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "ejecting?\n");
545
614
 
546
 
  if (fstat(d->fd, &stbuf) != 0)
547
 
    {
 
615
  if (fstat(d->fd, &stbuf) != 0) {
548
616
      wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "that weird fstat() thingy\n");
549
617
      return (-2);
550
 
    }
 
618
  }
551
619
  
552
620
  /* Is this a mounted filesystem? */
553
621
#if !defined(BSD_MOUNTTEST)
572
640
    }
573
641
  while ((mnt = getmntent (fp)) != NULL)
574
642
    {
575
 
      if (strcmp (mnt->mnt_fsname, cd_device) == 0)
 
643
      if (strcmp (mnt->mnt_fsname, d->cd_device) == 0)
576
644
        {
577
645
          wm_lib_message(WM_MSG_LEVEL_ERROR|WM_MSG_CLASS, "CDROM already mounted (according to mtab). Operation aborted.\n");
578
646
          endmntent (fp);
582
650
  endmntent (fp);
583
651
#endif /* BSD_MOUNTTEST */
584
652
 
 
653
  IFCDDA(d) {
 
654
    cdda_eject(d);
 
655
  }
 
656
 
585
657
  ioctl( d->fd, CDROM_LOCKDOOR, 0 );
586
658
 
587
659
  if (ioctl(d->fd, CDROMEJECT))
604
676
     * device open.
605
677
     */
606
678
    if (intermittent_dev)
607
 
      {
608
 
        close(d->fd);
609
 
        d->fd = -1;
610
 
      }                   
 
679
      gen_close(d);
611
680
#endif
612
681
  
613
682
  return (0);
627
696
    return (-1);
628
697
#else
629
698
  wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen() closing tray...\n");
630
 
  if(!close(d->fd))
 
699
  if(!gen_close(d))
631
700
    {
632
 
      d->fd=-1;
633
701
      return(wmcd_reopen(d));
634
702
    } else {
635
703
      return(-1);
636
704
    }
637
705
#endif /* CDROMCLOSETRAY */
638
 
#else /* CAN_CLOSE */
 
706
#endif /* CAN_CLOSE */
639
707
  /* Always succeed if the drive can't close. */
640
708
  return(0);
641
 
#endif /* CAN_CLOSE */
642
709
} /* gen_closetray() */
643
710
 
644
711
 
655
722
{
656
723
#ifdef CURVED_VOLUME
657
724
  return ((max * max - (max - vol) * (max - vol)) *
658
 
          (max_volume - min_volume) / (max * max) + min_volume);                        
 
725
    (max_volume - min_volume) / (max * max) + min_volume);                        
659
726
#else
660
727
  return ((vol * (max_volume - min_volume)) / max + min_volume);
661
728
#endif        
662
729
} /* scale_volume() */
663
730
 
 
731
static int
 
732
unscale_volume( int vol, int max )
 
733
{
 
734
#ifdef CURVED_VOLUME
 
735
  /* FIXME do it simpler */
 
736
  int tmp = (((max_volume - min_volume - vol) * max * max) - (vol + min_volume));
 
737
  return max - sqrt((tmp/(max_volume - min_volume)));                        
 
738
#else
 
739
  return (((vol - min_volume) * max) / (max_volume - min_volume));
 
740
#endif        
 
741
} /* unscale_volume() */
 
742
 
664
743
/*---------------------------------------------------------------------*
665
744
 * Set the volume level for the left and right channels.  Their values
666
745
 * range from 0 to 100.
668
747
int
669
748
gen_set_volume( struct wm_drive *d, int left, int right )
670
749
{
671
 
  struct        cdrom_volctrl v;
 
750
  struct cdrom_volctrl v;
672
751
  
 
752
  CDDARETURN(d) cdda_set_volume(d, left, right);
 
753
 
673
754
  /* Adjust the volume to make up for the CD-ROM drive's weirdness. */
674
755
  left = scale_volume(left, 100);
675
756
  right = scale_volume(right, 100);
680
761
  return (ioctl(d->fd, CDROMVOLCTRL, &v));
681
762
} /* gen_set_volume() */
682
763
 
 
764
/*---------------------------------------------------------------------*
 
765
 * Read the volume from the drive, if available.  Each channel
 
766
 * ranges from 0 to 100, with -1 indicating data not available.
 
767
 *---------------------------------------------------------------------*/
 
768
int
 
769
gen_get_volume( struct wm_drive *d, int *left, int *right )
 
770
{
 
771
  struct cdrom_volctrl v;
 
772
  
 
773
  CDDARETURN(d) cdda_get_volume(d, left, right);
 
774
 
 
775
#if defined(CDROMVOLREAD)
 
776
  if(!ioctl(d->fd, CDROMVOLREAD, &v)) {
 
777
    *left = unscale_volume((v.channel0 + v.channel2)/2, 100);
 
778
    *right = unscale_volume((v.channel1 + v.channel3)/2, 100);
 
779
  } else
 
780
#endif
 
781
    /* Suns, HPs, Linux, NEWS can't read the volume; oh well */
 
782
    *left = *right = -1;
 
783
 
 
784
  return 0;
 
785
} /* gen_get_volume() */
 
786
 
683
787
/*------------------------------------------------------------------------*
684
788
 * gen_get_cdtext(drive, buffer, lenght)
685
789
 *
696
800
  return wm_scsi_get_cdtext(d, pp_buffer, p_buffer_lenght);
697
801
} /* gen_get_cdtext() */
698
802
 
699
 
/*---------------------------------------------------------------------*
700
 
 * Read the initial volume from the drive, if available.  Each channel
701
 
 * ranges from 0 to 100, with -1 indicating data not available.
702
 
 *---------------------------------------------------------------------*/
703
 
int
704
 
gen_get_volume( struct wm_drive *d, int *left, int *right )
705
 
{
706
 
#if defined(BUILD_CDDA) && defined(WMCDDA_DONE) /* { */
707
 
  struct cdda_block     blk;
708
 
  
709
 
  if (cdda_slave > -1)
710
 
    {
711
 
      write(cdda_slave, "G", 1);
712
 
      get_ack(cdda_slave);
713
 
      read(cdda_slave, &blk, sizeof(blk));
714
 
      
715
 
      *left = *right = (blk.volume * 100 + 254) / 255;
716
 
      
717
 
      if (blk.balance < 110)
718
 
        {
719
 
          *right = (((blk.volume * blk.balance + 127) / 128) *
720
 
                    100 + 254) / 255;
721
 
        } else if (blk.balance > 146) {
722
 
          *left = (((blk.volume * (255 - blk.balance) +
723
 
                     127) / 128) * 100 + 254) / 255;
724
 
        }
725
 
      return (0);
726
 
    }
727
 
#else /* } */
728
 
  /* Suns, HPs, Linux, NEWS can't read the volume; oh well */
729
 
  *left = *right = -1;
730
 
#endif
731
 
  return (0);
732
 
} /* gen_get_volume() */
733
 
 
734
 
/*-------------------------------------------------------*
735
 
 *
736
 
 *
737
 
 *                    CDDA functions.
738
 
 *
739
 
 *
740
 
 *-------------------------------------------------------*/
741
 
 
742
 
#ifdef BUILD_CDDA /* { */
743
 
 
744
 
/*
745
 
 * Try to initialize the CDDA slave.  Returns 0 on error.
746
 
 */
747
 
int
748
 
cdda_init( struct wm_drive *d )
749
 
{
750
 
#if defined(WMCDDA_DONE) /* { */
751
 
  int   slavefds[2];
752
 
  
753
 
  if (cdda_slave > -1)
754
 
    return (1);
755
 
  
756
 
  fprintf( stderr, "slave okay\n" );
757
 
  
758
 
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, slavefds))
759
 
    {
760
 
      perror("socketpair");
761
 
      return (0);
762
 
    }
763
 
  
764
 
  fprintf( stderr, "going to fork\n" );
765
 
  switch (fork()) {
766
 
  case 0:
767
 
    close(slavefds[0]);
768
 
    dup2(slavefds[1], 1);
769
 
    dup2(slavefds[1], 0);
770
 
    close(slavefds[1]);
771
 
    close(d->fd);
772
 
    /* Try the default path first. */
773
 
    execl(cddaslave_path, cddaslave_path, cd_device, NULL);
774
 
    /* Search $PATH if that didn't work. */
775
 
    execlp("cddaslave", "cddaslave", cd_device, NULL);
776
 
    perror(cddaslave_path);
777
 
    exit(1);
778
 
    
779
 
  case -1:
780
 
    close(slavefds[0]);
781
 
    close(slavefds[1]);
782
 
    perror("fork");
783
 
    return (0);
784
 
  }
785
 
  
786
 
  close(slavefds[1]);
787
 
  cdda_slave = slavefds[0];
788
 
  
789
 
  if (!get_ack(cdda_slave))
790
 
    {
791
 
      fprintf( stderr, "get_ack failed\n" );
792
 
      cdda_slave = -1;
793
 
      /*                codec_start(); */
794
 
      return (0);
795
 
    }
796
 
  return (1);
797
 
 
798
 
#else /* BUILD_CDDA only } { */
799
 
  /*
800
 
   * If we're not building CDDA support, don't even bother trying.
801
 
   */
802
 
  return (0);
803
 
#endif
804
 
} /* cdda_init()  */
805
 
 
806
 
/*
807
 
 * Wait for an acknowledgement from the CDDA slave.
808
 
 */
809
 
static int
810
 
get_ack(int fd)
811
 
{
812
 
#if defined(WMCDDA_DONE) /* { */
813
 
  struct cdda_block     blk;
814
 
  
815
 
  do {
816
 
    if (read(fd, &blk, sizeof(blk)) <= 0)
817
 
      {
818
 
        return (0);
819
 
      }
820
 
  } while (blk.status != WMCDDA_ACK);
821
 
#endif /* } */
822
 
        return (1);
823
 
} /* get_ack() */
824
 
 
825
 
/*
826
 
 * Turn off the CDDA slave.
827
 
 */
828
 
void
829
 
cdda_kill( struct wm_drive *d )
830
 
{
831
 
  if (cdda_slave > -1)
832
 
    {
833
 
      write(cdda_slave, "Q", 1);
834
 
      get_ack(cdda_slave);
835
 
      wait(NULL);
836
 
      cdda_slave = -1;
837
 
      /*                codec_start(); */
838
 
    }
839
 
} /* cdda_kill() */
840
 
 
841
 
 
842
 
/*
843
 
 * Tell the CDDA slave to set the play direction.
844
 
 */
845
 
void
846
 
gen_set_direction( int newdir )
847
 
{
848
 
  unsigned char buf[2];
849
 
  
850
 
  if (cdda_slave > -1)
851
 
    {
852
 
      buf[0] = 'd';
853
 
      buf[1] = newdir;
854
 
      write(cdda_slave, buf, 2);
855
 
      get_ack(cdda_slave);
856
 
    }
857
 
}
858
 
 
859
 
/*
860
 
 * Tell the CDDA slave to set the play speed.
861
 
 */
862
 
void
863
 
gen_set_speed( int speed )
864
 
{
865
 
  unsigned char buf[2];
866
 
  
867
 
  if (cdda_slave > -1)
868
 
    {
869
 
      buf[0] = 's';
870
 
      buf[1] = speed;
871
 
      write(cdda_slave, buf, 2);
872
 
      get_ack(cdda_slave);
873
 
    }
874
 
} /* gen_set_speed() */
875
 
 
876
 
/*
877
 
 * Tell the CDDA slave to set the loudness level.
878
 
 */
879
 
void
880
 
gen_set_loudness( int loud )
881
 
{
882
 
  unsigned char buf[2];
883
 
  
884
 
  if (cdda_slave > -1)
885
 
    {
886
 
      buf[0] = 'L';
887
 
      buf[1] = loud;
888
 
      write(cdda_slave, buf, 2);
889
 
      get_ack(cdda_slave);
890
 
    }
891
 
} /* gen_set_loudness() */
892
 
 
893
 
/*
894
 
 * Tell the CDDA slave to start (or stop) saving to a file.
895
 
 */
896
 
void
897
 
gen_save( char *filename )
898
 
{
899
 
  int   len;
900
 
  
901
 
  if (filename == NULL || filename[0] == '\0')
902
 
    len = 0;
903
 
  else
904
 
    len = strlen(filename);
905
 
  write(cdda_slave, "F", 1);
906
 
  write(cdda_slave, &len, sizeof(len));
907
 
  if (len)
908
 
    write(cdda_slave, filename, len);
909
 
  get_ack(cdda_slave);
910
 
} /* gen_save() */
911
 
 
912
 
#endif /* BUILD_CDDA } */
913
 
 
914
803
#endif /* __linux__ */