formatting, enabled volume control under linux
[btb/d2x.git] / arch / sdl / rbaudio.c
1 /* $Id: rbaudio.c,v 1.5 2003-03-15 13:12:07 btb Exp $ */
2 /*
3  *
4  * SDL CD Audio functions
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include <SDL.h>
17
18 #ifdef __linux__
19 #include <sys/ioctl.h>
20 #include <linux/cdrom.h>
21 #endif
22
23 #include "pstypes.h"
24 #include "error.h"
25 #include "args.h"
26 #include "rbaudio.h"
27
28 static SDL_CD *s_cd = NULL;
29 extern int Redbook_playing;
30 static int initialised = 0;
31
32 void RBAExit()
33 {
34         if (initialised)
35         {
36                 SDL_CDStop(s_cd);
37                 SDL_CDClose(s_cd);
38         }
39 }
40
41 void RBAInit()
42 {
43         if (initialised) return;
44         if (FindArg("-nocdrom")) return; 
45
46         if (SDL_Init(SDL_INIT_CDROM) < 0)
47         {
48                 Warning("SDL library initialisation failed: %s.",SDL_GetError());
49                 return;
50         }
51
52         if (SDL_CDNumDrives() == 0)
53         {
54                 Warning("No cdrom drives found!\n");
55                 return;
56         }
57         s_cd = SDL_CDOpen(0);
58         if (s_cd == NULL) {
59                 Warning("Could not open cdrom for redbook audio!\n");
60                 return;
61         }
62         atexit(RBAExit);
63         initialised = 1;
64 }
65
66 int RBAEnabled()
67 {
68         return 1;
69 }
70
71 void RBARegisterCD()
72 {
73
74 }
75
76 int RBAPlayTrack(int a)
77 {
78         if (!initialised) return -1;
79
80         if (CD_INDRIVE(SDL_CDStatus(s_cd)) ) {
81                 SDL_CDPlayTracks(s_cd, a-1, 0, 0, 0);
82         }
83         return a;
84 }
85
86 void RBAStop()
87 {
88         if (!initialised) return;
89         SDL_CDStop(s_cd);
90 }
91
92 void RBASetVolume(int volume)
93 {
94 #ifdef __linux__
95         int cdfile, level;
96         struct cdrom_volctrl volctrl;
97
98         if (!initialised) return;
99
100         cdfile = s_cd->id;
101         level = volume * 3;
102
103         if ((level<0) || (level>255)) {
104                 fprintf(stderr, "illegal volume value (allowed values 0-255)\n");
105                 return;
106         }
107
108         volctrl.channel0
109                 = volctrl.channel1
110                 = volctrl.channel2
111                 = volctrl.channel3
112                 = level;
113         if ( ioctl(cdfile, CDROMVOLCTRL, &volctrl) == -1 ) {
114                 fprintf(stderr, "CDROMVOLCTRL ioctl failed\n");
115                 return;
116         }
117 #endif
118 }
119
120 void RBAPause()
121 {
122         if (!initialised) return;
123         SDL_CDPause(s_cd);
124 }
125
126 int RBAResume()
127 {
128         if (!initialised) return -1;
129         SDL_CDResume(s_cd);
130         return 1;
131 }
132
133 int RBAGetNumberOfTracks()
134 {
135         if (!initialised) return -1;
136         SDL_CDStatus(s_cd);
137         return s_cd->numtracks;
138 }
139
140 int RBAPlayTracks(int tracknum,int something)
141 {
142         if (!initialised) return -1;
143         if (CD_INDRIVE(SDL_CDStatus(s_cd)) ) {
144                 SDL_CDPlayTracks(s_cd, tracknum-1, 0, 0, 0);
145         }
146         return tracknum;
147 }
148
149 int RBAGetTrackNum()
150 {
151         if (!initialised) return -1;
152         SDL_CDStatus(s_cd);
153         return s_cd->cur_track;
154 }
155
156 int RBAPeekPlayStatus()
157 {
158         return (SDL_CDStatus(s_cd) == CD_PLAYING);
159 }
160
161 int CD_blast_mixer()
162 {
163         return 0;
164 }