File: plat_sun.c

package info (click to toggle)
workman 1.3.4-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,164 kB
  • ctags: 1,170
  • sloc: ansic: 14,491; makefile: 142; sh: 44
file content (1139 lines) | stat: -rw-r--r-- 23,488 bytes parent folder | download | duplicates (5)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
/*
 * This file is part of WorkMan, the civilized CD player program
 * (c) 1991-1997 by Steven Grimm (original author)
 * (c) by Dirk F"orsterling (current 'author' = maintainer)
 * The maintainer can be contacted by his e-mail address:
 * milliByte@DeathsDoor.com 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Sun-specific drive control routines.
 */

#ifdef sun
static char plat_sun_id[] = "$Id: plat_sun.c,v 1.6 1999/02/04 16:52:16 dirk Exp $";

#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#include "config.h"

#include <ustat.h>
#include <unistd.h>
#include <signal.h>
#ifdef solbourne
# include <mfg/dklabel.h>
# include <mfg/dkio.h>
# include <sys/unistd.h>
# include <dev/srvar.h>
#else /* A real Sun */
# ifdef SYSV
#  include <poll.h>
#  include <stdlib.h>
#  include <sys/cdio.h>
#  include <sys/socket.h>
#  include <sys/scsi/impl/uscsi.h>
#  include "wmcdda.h"
# else
#  include <sys/buf.h>
#  include <sun/dkio.h>
#  include <scsi/targets/srdef.h>
#  include <scsi/impl/uscsi.h>
#  include <scsi/generic/commands.h>
# endif
#endif

#include "struct.h"
#include "proto.h"

/*
 * need to remove the following garbage.
 *
void *malloc();
char *strchr(), *getenv();
char *realpath();
 *
 * should do. Uncomment if not.
 */

int	min_volume = 0;
int	max_volume = 255;

extern char	*cd_device, *cddaslave_path;
extern int	intermittent_dev;

int	cdda_slave = -1;

int	current_end;

#if defined(SYSV) && defined(SIGTHAW)
#ifdef __GNUC__
void sigthawinit(void) __attribute__ ((constructor));
#else
#pragma init(sigthawinit)
#endif /* GNUC */

static int last_left, last_right;
static struct wm_drive *thecd;

static void thawme(int sig)
{
	change_mode(NULL, STOPPED, NULL);
	codec_init();
	if( thecd )
	  gen_set_volume(thecd, last_left, last_right);
} /* thawme */

void sigthawinit( void )
{
	struct sigaction sa;

	sa.sa_handler = thawme;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;

	sigaction(SIGTHAW, &sa, NULL);
}

#endif /* SYSV && SIGTHAW */

/*
 * find_cdrom
 *
 * Determine the name of the CD-ROM device.
 *
 * Use the first of /vol/dev/aliases/cdrom0, /dev/rdsk/c0t6d0s2, and /dev/rsr0
 * that exists.  (Check for /vol/dev/aliases, not cdrom0, since it won't be
 * there if there's no CD in the drive.)  This is done so a single SunOS 4.x
 * binary can be used on any 4.x or higher Sun system.
 */
void
find_cdrom( void )
{
	if (access("/vol/dev/aliases", X_OK) == 0)
	{
		/* Volume manager.  Device might not be there. */
		intermittent_dev = 1;

		/* If vold is running us, it'll tell us the device name. */
		cd_device = getenv("VOLUME_DEVICE");
		if (cd_device == NULL)
			cd_device = "/vol/dev/aliases/cdrom0";
	}
	else if (access("/dev/rdsk/c0t6d0s2", F_OK) == 0)
	{
		/* Solaris 2.x w/o volume manager. */
		cd_device = "/dev/rdsk/c0t6d0s2";
	}
	else if (access("/dev/rcd0", F_OK) == 0)
	{
		cd_device = "/dev/rcd0";
	}
	else if (access("/dev/rsr0", F_OK) == 0)
		cd_device = "/dev/rsr0";
	else
	{
		fprintf(stderr, "Couldn't find a CD device!\n");
		exit(1);
	}
}

/*
 * Wait for an acknowledgement from the CDDA slave.
 */
static int
get_ack(int fd)
{
#if defined(BUILD_CDDA) && defined(WMCDDA_DONE) /* { */
	struct cdda_block	blk;

	do
		if (read(fd, &blk, sizeof(blk)) <= 0)
			return (0);
	while (blk.status != WMCDDA_ACK);
#endif /* } */

	return (1);
}

/*
 * Initialize the drive.  A no-op for the generic driver.
 */
int
gen_init( struct wm_drive *d )
{
	codec_init();
	return (0);
}

/*
 * Try to initialize the CDDA slave.  Returns 0 on error.
 */
int
cdda_init( struct wm_drive *d )
{
#if defined(BUILD_CDDA) && defined(WMCDDA_DONE) /* { */
	int	slavefds[2];

	if (cdda_slave > -1)
		return (1);

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, slavefds))
	{
		perror("socketpair");
		return (0);
	}

	switch (fork()) {
	case 0:
		close(slavefds[0]);
		dup2(slavefds[1], 1);
		dup2(slavefds[1], 0);
		close(slavefds[1]);
		close(d->fd);
		/* Try the default path first. */
		execl(cddaslave_path, cddaslave_path, cd_device, NULL);
		/* Search $PATH if that didn't work. */
		execlp("cddaslave", "cddaslave", cd_device, NULL);
		perror(cddaslave_path);
		exit(1);

	case -1:
		close(slavefds[0]);
		close(slavefds[1]);
		perror("fork");
		return (0);
	}

	close(slavefds[1]);
	cdda_slave = slavefds[0];

	if (get_ack(cdda_slave))
		about_set_drivetype("Digital Audio", "", "");
	else
	{
		cdda_slave = -1;
		codec_start();
		return (0);
	}

	return (1);

#else /* BUILD_CDDA } { */
	/*
	 * If we're not building CDDA support, don't even bother trying.
	 */
	return (0);
#endif
}

/*
 * Turn off the CDDA slave.
 */
void
cdda_kill( struct wm_drive *d )
{
	if (cdda_slave > -1)
	{
		write(cdda_slave, "Q", 1);
		get_ack(cdda_slave);
		wait(NULL);
		cdda_slave = -1;
		codec_start();
	}
}

/*
 * Get the number of tracks on the CD.
 */
int
gen_get_trackcount( struct wm_drive *d, int *tracks )
{
	struct cdrom_tochdr	hdr;

	if (ioctl(d->fd, CDROMREADTOCHDR, &hdr))
		return (-1);
	
	*tracks = hdr.cdth_trk1;
	return (0);
}

/*
 * Get the start time and mode (data or audio) of a track.
 */
int
gen_get_trackinfo( struct wm_drive *d, int track, int *data, int *startframe)
{
	struct cdrom_tocentry	entry;

	entry.cdte_track = track;
	entry.cdte_format = CDROM_MSF;

	if (ioctl(d->fd, CDROMREADTOCENTRY, &entry))
		return (-1);
	
	*startframe =	entry.cdte_addr.msf.minute * 60 * 75 +
			entry.cdte_addr.msf.second * 75 +
			entry.cdte_addr.msf.frame;
	*data = entry.cdte_ctrl & CDROM_DATA_TRACK ? 1 : 0;
	
	return (0);
}

/*
 * Get the number of frames on the CD.
 */
int
gen_get_cdlen(struct wm_drive *d, int *frames )
{
	int		tmp;

	return (gen_get_trackinfo(d, CDROM_LEADOUT, &tmp, frames));
}


/* Alarm signal handler. */
static void do_nothing( int x ) { x++; }

/*
 * Get the current status of the drive: the current play mode, the absolute
 * position from start of disc (in frames), and the current track and index
 * numbers if the CD is playing or paused.
 */
int
gen_get_drive_status( struct wm_drive *d,
		      enum cd_modes oldmode,
		      enum cd_modes *mode,
		      int *pos, int *track, int *index )
{
	struct cdrom_subchnl		sc;
	struct itimerval		old_timer, new_timer;
	struct sigaction		old_sig, new_sig;

	/* If we can't get status, the CD is ejected, so default to that. */
	*mode = EJECTED;

	/* Is the device open? */
	if (d->fd < 0)
	{
		switch (wmcd_open(d)) {
		case -1:	/* error */
			return (-1);

		case 1:		/* retry */
			return (0);
		}
	}

#if defined(BUILD_CDDA) && defined(WMCDDA_DONE) /* { */
	if ((oldmode == PAUSED || oldmode == PLAYING || oldmode == STOPPED) &&
		cdda_slave > -1)
	{
		struct cdda_block	blk;
		struct pollfd		fds;
		int			gotone = 0;

		fds.fd = cdda_slave;
		fds.events = POLLRDNORM;

		*mode = oldmode;

		while (poll(&fds, 1, 0) > 0)
		{
			read(cdda_slave, &blk, sizeof(blk));
			gotone = 1;
		}

		/* We only want to use the latest status report. */
		if (gotone)
		{
			if (blk.status == WMCDDA_PLAYED)
			{
				*track = blk.track;
				*index = blk.index;
				*pos = blk.minute * 60 * 75 +
					blk.second * 75 +
					blk.frame;

				*mode = PLAYING;
			}
			else if (blk.status == WMCDDA_DONE)
				*mode = TRACK_DONE;
			else if (blk.status == WMCDDA_STOPPED)
			{
				if (oldmode == PLAYING || oldmode == PAUSED)
					*mode = PAUSED;
				else
					*mode = STOPPED;
			}
			else if (blk.status == WMCDDA_ERROR)
			{
				/*
				 * An error near the end of the CD probably
				 * just means we hit the end.
				 */
				*mode = TRACK_DONE;
			}
			else if (blk.status == WMCDDA_EJECTED)
			{
				*mode = EJECTED;
			}
		}

		return (0);
	}
#endif /* } */

	/*
	 * Solaris 2.2 hangs on this ioctl if someone else ejects the CD.
	 * So we schedule a signal to break out of the hang if the call
	 * takes an unreasonable amount of time.  The signal handler and
	 * timer are restored immediately to avoid interfering with XView.
	 */
	if (intermittent_dev)
	{
		/*
		 * First clear out the timer so XView's signal doesn't happen
		 * while we're diddling with the signal handler.
		 */
		timerclear(&new_timer.it_interval);
		timerclear(&new_timer.it_value);
		setitimer(ITIMER_REAL, &new_timer, &old_timer);

		/*
		 * Now install the no-op signal handler.
		 */
		new_sig.sa_handler = do_nothing;
		memset(&new_sig.sa_mask, 0, sizeof(new_sig.sa_mask));
		new_sig.sa_flags = 0;
		if (sigaction(SIGALRM, &new_sig, &old_sig))
			perror("sigaction");

		/*
		 * And finally, set the timer.
		 */
		new_timer.it_value.tv_sec = 2;
		setitimer(ITIMER_REAL, &new_timer, NULL);
	}

	sc.cdsc_format = CDROM_MSF;

	if (ioctl(d->fd, CDROMSUBCHNL, &sc))
	{
		if (intermittent_dev)
		{
			sigaction(SIGALRM, &old_sig, NULL);
			setitimer(ITIMER_REAL, &old_timer, NULL);

			/* If the device can disappear, let it do so. */
			close(d->fd);
			d->fd = -1;
		}

		return (0);
	}

	if (intermittent_dev)
	{
		sigaction(SIGALRM, &old_sig, NULL);
		setitimer(ITIMER_REAL, &old_timer, NULL);
	}

	switch (sc.cdsc_audiostatus) {
	case CDROM_AUDIO_PLAY:
		*mode = PLAYING;
		*track = sc.cdsc_trk;
		*index = sc.cdsc_ind;
		*pos = sc.cdsc_absaddr.msf.minute * 60 * 75 +
			sc.cdsc_absaddr.msf.second * 75 +
			sc.cdsc_absaddr.msf.frame;
		break;

	case CDROM_AUDIO_PAUSED:
	case CDROM_AUDIO_INVALID:
	case CDROM_AUDIO_NO_STATUS:
		if (oldmode == PLAYING || oldmode == PAUSED)
		{
			*mode = PAUSED;
			*track = sc.cdsc_trk;
			*index = sc.cdsc_ind;
			*pos = sc.cdsc_absaddr.msf.minute * 60 * 75 +
				sc.cdsc_absaddr.msf.second * 75 +
				sc.cdsc_absaddr.msf.frame;
		}
		else
			*mode = STOPPED;
		break;

	/* CD ejected manually during play. */
	case CDROM_AUDIO_ERROR:
		break;

	case CDROM_AUDIO_COMPLETED:
		*mode = TRACK_DONE; /* waiting for next track. */
		break;

	default:
		*mode = UNKNOWN;
		break;
	}

	return (0);
}

/*
 * Set the volume level for the left and right channels.  Their values
 * range from 0 to 100.
 */
int
gen_set_volume( struct wm_drive *d, int left, int right )
{
	struct cdrom_volctrl v;

#if defined(SIGTHAW) && defined(SYSV)
	last_left = left;
	last_right = right;
	thecd = d;
#endif
	
	if (cdda_slave > -1)
	{
		int		bal, vol;
		unsigned char	cmd[2];

		bal = (right - left) + 100;
		bal *= 255;
		bal /= 200;
		if (right > left)
			vol = right;
		else
			vol = left;
		vol *= 255;
		vol /= 100;

		cmd[0] = 'B';
		cmd[1] = bal;
		write(cdda_slave, cmd, 2);
		cmd[0] = 'V';
		cmd[1] = vol;
		write(cdda_slave, cmd, 2);
		/*
		 * Don't wait for the ack, or the user won't be able to drag
		 * the volume slider smoothly.
		 */

		return (0);
	}

	left = (left * (max_volume - min_volume)) / 100 + min_volume;
	right = (right * (max_volume - min_volume)) / 100 + min_volume;

	v.channel0 = left < 0 ? 0 : left > 255 ? 255 : left;
	v.channel1 = right < 0 ? 0 : right > 255 ? 255 : right;

	return (ioctl(d->fd, CDROMVOLCTRL, &v));
}

/*
 * Pause the CD.
 */
int
gen_pause( struct wm_drive *d )
{
	if (cdda_slave > -1)
	{
		int	dummy, mode = PLAYING;

		write(cdda_slave, "S", 1);
		get_ack(cdda_slave);
/*		while (mode != PAUSED)
			gen_get_drive_status(d, PAUSED, &mode, &dummy, &dummy,
						&dummy);
*/
		return (0);
	}

	codec_stop();
	return (ioctl(d->fd, CDROMPAUSE));
}

/*
 * Resume playing the CD (assuming it was paused.)
 */
int
gen_resume( struct wm_drive *d )
{
	if (cdda_slave > -1)
		return (1);

	codec_start();
	return (ioctl(d->fd, CDROMRESUME));
}

/*
 * Stop the CD.
 */
int
gen_stop( struct wm_drive *d )
{
	if (cdda_slave > -1)
	{
		write(cdda_slave, "S", 1);
		get_ack(cdda_slave);

		/*
		 * The WMCDDA_STOPPED status message will be caught by
		 * gen_get_drive_status.
		 */

		return (0);
	}
	codec_stop();
	return (ioctl(d->fd, CDROMSTOP));
}

/*
 * Play the CD from one position to another.
 *
 *	d		Drive structure.
 *	start		Frame to start playing at.
 *	end		End of this chunk.
 *	realstart	Beginning of this chunk (<= start)
 */
int
gen_play( struct wm_drive *d, int start, int end, int realstart)
{
	struct cdrom_msf		msf;
	unsigned char			cmdbuf[10];

	current_end = end;

	if (cdda_slave > -1)
	{
		cmdbuf[0] = 'P';
		cmdbuf[1] = start / (60 * 75);
		cmdbuf[2] = (start % (60*75)) / 75;
		cmdbuf[3] = start % 75;
		cmdbuf[4] = end / (60*75);
		cmdbuf[5] = (end % (60*75)) / 75;
		cmdbuf[6] = end % 75;
		cmdbuf[7] = realstart / (60 * 75);
		cmdbuf[8] = (realstart % (60*75)) / 75;
		cmdbuf[9] = realstart % 75;

		/* Write the play command and make sure the slave has it. */
		write(cdda_slave, cmdbuf, 10);
		get_ack(cdda_slave);

		return (0);
	}

	msf.cdmsf_min0 = start / (60*75);
	msf.cdmsf_sec0 = (start % (60*75)) / 75;
	msf.cdmsf_frame0 = start % 75;
	msf.cdmsf_min1 = end / (60*75);
	msf.cdmsf_sec1 = (end % (60*75)) / 75;
	msf.cdmsf_frame1 = end % 75;

	codec_start();
	if (ioctl(d->fd, CDROMSTART))
		return (-1);
	if (ioctl(d->fd, CDROMPLAYMSF, &msf))
		return (-2);

	return (0);
}

/*
 * Eject the current CD, if there is one.
 */
int
gen_eject( struct wm_drive *d )
{
	struct stat	stbuf;
	struct ustat	ust;

	if (fstat(d->fd, &stbuf) != 0)
		return (-2);

	/* Is this a mounted filesystem? */
	if (ustat(stbuf.st_rdev, &ust) == 0)
		return (-3);

	if (cdda_slave > -1)
	{
		write(cdda_slave, "S", 1);
		get_ack(cdda_slave);
	}

	if (ioctl(d->fd, CDROMEJECT))
		return (-1);

	/* Close the device if it needs to vanish. */
	if (intermittent_dev)
	{
		close(d->fd);
		d->fd = -1;
		/* Also remember to tell the cddaslave since volume
		   manager switches links around on us */
		if (cdda_slave > -1)
		{
		    write(cdda_slave, "E", 1);
		    get_ack(cdda_slave);
		}
	}

	return (0);
}

/*
 * Read the initial volume from the drive, if available.  Each channel
 * ranges from 0 to 100, with -1 indicating data not available.
 */
int
gen_get_volume( struct wm_drive *d, int *left, int *right )
{
#if defined(BUILD_CDDA) && defined(WMCDDA_DONE) /* { */
	struct cdda_block	blk;

	if (cdda_slave > -1)
	{
		write(cdda_slave, "G", 1);
		get_ack(cdda_slave);
		read(cdda_slave, &blk, sizeof(blk));

		*left = *right = (blk.volume * 100 + 254) / 255;

		if (blk.balance < 110)
			*right = (((blk.volume * blk.balance + 127) / 128) *
				  100 + 254) / 255;
		else if (blk.balance > 146)
			*left = (((blk.volume * (255 - blk.balance) +
				   127) / 128) * 100 + 254) / 255;

		return (0);
	}
#endif /* } */

	*left = *right = -1;

	return (wm_scsi2_get_volume(d, left, right));
}

#ifdef BUILD_CDDA /* { */

/*
 * Tell the CDDA slave to set the play direction.
 */
void
gen_set_direction( int newdir )
{
	unsigned char	buf[2];

	if (cdda_slave > -1)
	{
		buf[0] = 'd';
		buf[1] = newdir;
		write(cdda_slave, buf, 2);
		get_ack(cdda_slave);
	}
}

/*
 * Tell the CDDA slave to set the play speed.
 */
void
gen_set_speed( int speed )
{
	unsigned char	buf[2];

	if (cdda_slave > -1)
	{
		buf[0] = 's';
		buf[1] = speed;
		write(cdda_slave, buf, 2);
		get_ack(cdda_slave);
	}
}

/*
 * Tell the CDDA slave to set the loudness level.
 */
void
gen_set_loudness( int loud )
{
	unsigned char	buf[2];

	if (cdda_slave > -1)
	{
		buf[0] = 'L';
		buf[1] = loud;
		write(cdda_slave, buf, 2);
		get_ack(cdda_slave);
	}
}

/*
 * Tell the CDDA slave to start (or stop) saving to a file.
 */
void
gen_save( char *filename )
{
	int	len;

	if (filename == NULL || filename[0] == '\0')
		len = 0;
	else
		len = strlen(filename);
	write(cdda_slave, "F", 1);
	write(cdda_slave, &len, sizeof(len));
	if (len)
		write(cdda_slave, filename, len);
	get_ack(cdda_slave);
}

#endif /* BUILD_CDDA } */

#ifndef solbourne
/*
 * Send an arbitrary SCSI command out the bus and optionally wait for
 * a reply if "retbuf" isn't NULL.
 */
int
wm_scsi( struct wm_drive *d,
	 unsigned char *cdb,
	 int cdblen, void *retbuf,
	 int retbuflen, int getreply )
{
	char			x;
	struct uscsi_cmd	cmd;

	memset(&cmd, 0, sizeof(cmd));
	cmd.uscsi_cdb = (void *) cdb;
	cmd.uscsi_cdblen = cdblen;
	cmd.uscsi_bufaddr = retbuf ? retbuf : (void *)&x;
	cmd.uscsi_buflen = retbuf ? retbuflen : 0;
	cmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
	if (getreply)
		cmd.uscsi_flags |= USCSI_READ;
	
	if (ioctl(d->fd, USCSICMD, &cmd))
		return (-1);
	
	if (cmd.uscsi_status)
		return (-1);
	
	return (0);
}
#else

int wm_scsi() { return (-1); }

#endif

/*
 * Open the CD device and figure out what kind of drive is attached.
 */
int
wmcd_open( struct wm_drive *d )
{
	int		fd;
	static int	warned = 0;
	char		vendor[32], model[32], rev[32];

	if (cd_device == NULL)
		find_cdrom();

	if (d->fd >= 0)		/* Device already open? */
		return (0);
	
	d->fd = open(cd_device, 0);
	if (d->fd < 0)
	{
		/* Solaris 2.2 volume manager moves links around */
		if (errno == ENOENT && intermittent_dev)
			return (1);

		if (errno == EACCES)
		{
			if (!warned)
			{
				char	realname[MAXPATHLEN];

				if (realpath(cd_device, realname) == NULL)
				{
					perror("realpath");
					return (-1);
				}

				fprintf(stderr,
		"As root, please run\n\nchmod 666 %s\n\n%s\n", realname,
		"to give yourself permission to access the CD-ROM device.");
				warned++;
			}
		}
		else if (errno != ENXIO)
		{
			perror(cd_device);
			exit(1);
		}

		/* No CD in drive. */
		return (1);
	}

	if (warned)
	{
		warned = 0;
		fprintf(stderr, "Thank you.\n");
	}

	/* Now fill in the relevant parts of the wm_drive structure. */
	fd = d->fd;

	/*
	 * See if we can do digital audio.
	 */
	if (cdda_init(d))
		enable_cdda_controls(1);

	/* Can we figure out the drive type? */
	vendor[0] = model[0] = rev[0] = '\0';
	if (wm_scsi_get_drive_type(d, vendor, model, rev)) {
		if (errno == EPERM) {
			/*
			 * Solaris 2.4 seems to refuse to do USCSICMD ioctls
			 * when not running as root.  SunOS 4.x allows it
			 * as an unprivileged user, though.
			 */
			fprintf(stderr, "Warning: WorkMan can't adapt itself to your drive unless it runs as root.\n");
		} else {
			fprintf(stderr, "Warning: WorkMan couldn't determine drive type\n");
		}
		*d = *(find_drive_struct("", "", ""));
		about_set_drivetype("unknown", "IDE", "*");
	} else {
		*d = *(find_drive_struct(vendor, model, rev));
		about_set_drivetype(d->vendor, d->model, rev);
	}

	d->fd = fd;

	(d->init)(d);

	return (0);
}

/*
 * The following code activates the internal CD audio passthrough on
 * SPARCstation 5 systems (and possibly others.)
 *
 * Thanks to <stevep@ctc.ih.att.com>, Roger Oscarsson <roger@cs.umu.se>
 * and Steve McKinty <>
 *
 * Most CD drives have a headphone socket on the front, but it
 * is often more convenient to route the audio though the
 * built-in audio device. That way the user can leave their
 * headphones plugged-in to the base system, for use with
 * other audio stuff like ShowMeTV
 */

#ifdef CODEC /* { */
#ifdef SYSV /* { */

# include <sys/ioctl.h>
# include <sys/audioio.h>
# include <stdlib.h>

#else /* } { */

# include <sun/audioio.h>
# define AUDIO_DEV_SS5STYLE 5
typedef int audio_device_t;

#endif /* } */
#endif /* } */

/*
 * Don't do anything with /dev/audio if we can't set it to high quality.
 * Also, don't do anything real if it's not Solaris.
 */
#if !defined(AUDIO_ENCODING_LINEAR) || !defined(CODEC) || !defined(SYSV) /* { */
codec_init() { return 0; }
codec_start() { return 0; }
codec_stop() { return 0; }
#else

#ifndef AUDIO_INTERNAL_CD_IN
#define AUDIO_INTERNAL_CD_IN	0x4
#endif

static char* devname = 0;
static char* ctlname = 0;
static int ctl_fd = -1;
static int port = AUDIO_LINE_IN;
int internal_audio = 1;

codec_init( void )
{
    register int i;
    char* ctlname;
    audio_info_t foo;
    audio_device_t aud_dev;

    if (internal_audio == 0)
    {
        ctl_fd = -1;
        return(0);
    }
    
    if (!(devname = getenv("AUDIODEV"))) devname = "/dev/audio";
    ctlname = strcat(strcpy(malloc(strlen(devname) + 4), devname), "ctl");
    if ((ctl_fd = open(ctlname, O_WRONLY, 0)) < 0) {
	perror(ctlname);
	return -1;
    }
    if (ioctl(ctl_fd, AUDIO_GETDEV, &aud_dev) < 0) {
	close(ctl_fd);
	ctl_fd = -1;
	return -1;
    }
/*
 * Instead of filtering the "OLD_SUN_AUDIO", try to find the new ones.
 * Not sure if this is all correct.
 */
#ifdef SYSV
    if (strcmp(aud_dev.name, "SUNW,CS4231") &&
        strcmp(aud_dev.name, "SUNW,sb16") &&
        strcmp(aud_dev.name, "SUNW,sbpro"))
#else
    if (aud_dev != AUDIO_DEV_SS5STYLE)
#endif
    {
	close(ctl_fd);
	ctl_fd = -1;
	return 0;					/* but it's okay */
    }

/*
 * Does the chosen device have an internal CD port?
 * If so, use it. If not then try and use the
 * Line In port.
 */
    if (ioctl(ctl_fd, AUDIO_GETINFO, &foo) < 0)
    {
        perror("AUDIO_GETINFO");
        close(ctl_fd);
        ctl_fd = -1;
        return(-1);
    }
    if (foo.record.avail_ports & AUDIO_INTERNAL_CD_IN)
        port = AUDIO_INTERNAL_CD_IN;
    else
        port = AUDIO_LINE_IN;

/*
 * now set it up to use it. See audio(7I)
 */
    
    AUDIO_INITINFO(&foo);
    foo.record.port = port;
    foo.record.balance = foo.play.balance = AUDIO_MID_BALANCE;
#ifdef BUILD_CDDA
    if (cdda_slave > -1)
	foo.monitor_gain = 0;
    else
#endif
	foo.monitor_gain = AUDIO_MAX_GAIN;
/*
 * These next ones are tricky. The voulme will depend on the CD drive
 * volume (set by the knob on the drive and/or by workman's volume
 * control), the audio device record gain and the audio device
 * play gain.  For simplicity we set the latter two to something
 * reasonable, but we don't force them to be reset if the user
 * wants to change them.
*/
    foo.record.gain = (AUDIO_MAX_GAIN * 80) / 100;
    foo.play.gain = (AUDIO_MAX_GAIN * 40) / 100;
    	
    ioctl(ctl_fd, AUDIO_SETINFO, &foo);
    return 0;
}

static kick_codec( void ) {
    audio_info_t foo;
    int dev_fd;
    int retval = 0;

/*
 * Open the audio device, not the control device. This
 * will fail if someone else has taken it.
 */
      
    if ((dev_fd = open(devname, O_WRONLY|O_NDELAY, 0)) < 0) {
	perror(devname);
	return -1;
    }

    AUDIO_INITINFO(&foo);
    foo.record.port = port;
    foo.monitor_gain = AUDIO_MAX_GAIN;
    
/* These can only be set on the real device */
    foo.play.sample_rate = 44100;
    foo.play.channels = 2;
    foo.play.precision = 16;
    foo.play.encoding = AUDIO_ENCODING_LINEAR;
    
    if ((retval = ioctl(dev_fd, AUDIO_SETINFO, &foo)) < 0)
    	perror(devname);

    close(dev_fd);
    return retval;
}

codec_start( void )
{
audio_info_t foo;

    if (ctl_fd < 0)
	return 0;

    if (ioctl(ctl_fd, AUDIO_GETINFO, &foo) < 0)
	return -1;

    if (foo.play.channels != 2) return kick_codec();
    if (foo.play.encoding != AUDIO_ENCODING_LINEAR) return kick_codec();
    if (foo.play.precision != 16) return kick_codec();
    if (foo.play.sample_rate != 44100) return kick_codec();

    if (foo.record.channels != 2) return kick_codec();
    if (foo.record.encoding != AUDIO_ENCODING_LINEAR) return kick_codec();
    if (foo.record.precision != 16) return kick_codec();
    if (foo.record.sample_rate != 44100) return kick_codec();

    if (foo.monitor_gain != AUDIO_MAX_GAIN) return kick_codec();
    if (foo.record.port != port) return kick_codec();

    return 0;
}

codec_stop( void ) { return 0; }

#endif /* CODEC } */
#endif /* sun } */