~ubuntu-branches/debian/squeeze/stella/squeeze

« back to all changes in this revision

Viewing changes to src/ui/sound/SndUnix.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Tom Lear
  • Date: 1999-11-06 16:41:05 UTC
  • Revision ID: james.westby@ubuntu.com-19991106164105-iygopamo5mpcozvx
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//============================================================================
 
2
//
 
3
//   SSSS    tt          lll  lll       
 
4
//  SS  SS   tt           ll   ll        
 
5
//  SS     tttttt  eeee   ll   ll   aaaa 
 
6
//   SSSS    tt   ee  ee  ll   ll      aa
 
7
//      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
 
8
//  SS  SS   tt   ee      ll   ll  aa  aa
 
9
//   SSSS     ttt  eeeee llll llll  aaaaa
 
10
//
 
11
// Copyright (c) 1995-1998 by Bradford W. Mott
 
12
//
 
13
// See the file "license" for information on usage and redistribution of
 
14
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
 
15
//
 
16
// $Id: SndUnix.cxx,v 1.1 1998/07/15 20:58:10 bwmott Exp $
 
17
//============================================================================
 
18
 
 
19
#include <sys/types.h>
 
20
#include <sys/wait.h>
 
21
#include <signal.h>
 
22
#include <unistd.h>
 
23
 
 
24
#include "SndUnix.hxx"
 
25
 
 
26
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
27
SoundUnix::SoundUnix()
 
28
    : myDisabled(false),
 
29
      myMute(false)
 
30
{
 
31
  // Initialize to impossible values so they will be reset 
 
32
  // on the first call to the set method
 
33
  myAUDC0 = myAUDC1 = myAUDF0 = myAUDF1 = myAUDV0 = myAUDV1 = 255;
 
34
 
 
35
  int pfd[2];
 
36
 
 
37
  // Create pipe for interprocess communication
 
38
  if(pipe(pfd))
 
39
  {
 
40
    // Oops. We were not able to create pipe so disable myself and return
 
41
    myDisabled = true;
 
42
    return;
 
43
  }
 
44
 
 
45
  // Create new process, setup pipe, and start stella-sound
 
46
  myProcessId = fork();
 
47
 
 
48
  if(myProcessId == 0)
 
49
  {
 
50
    // Close STDIN and put the read end of the pipe in its place
 
51
    dup2(pfd[0], 0);
 
52
 
 
53
    // Close unused file descriptors in child process
 
54
    close(pfd[0]);
 
55
    close(pfd[1]);
 
56
 
 
57
    // Execute the stella-sound server
 
58
    if(execlp("stella-sound", "stella-sound", (char*)0))
 
59
    {
 
60
      exit(1);
 
61
    }
 
62
  }
 
63
  else if(myProcessId > 0)
 
64
  {
 
65
    // Close unused file descriptors in parent process
 
66
    close(pfd[0]);
 
67
 
 
68
    // Save the pipe's write file descriptor
 
69
    myFd = pfd[1];
 
70
  }
 
71
  else
 
72
  {
 
73
    // Couldn't fork so cleanup and disabled myself
 
74
    close(pfd[0]);
 
75
    close(pfd[1]);
 
76
 
 
77
    myDisabled = true;
 
78
  }
 
79
}
 
80
 
 
81
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
82
SoundUnix::~SoundUnix()
 
83
{
 
84
  if(!myDisabled)
 
85
  {
 
86
    // Send quit command to the sound server
 
87
    unsigned char command = 0xC0;
 
88
    write(myFd, &command, 1);
 
89
 
 
90
    // Kill the sound server
 
91
    kill(myProcessId, SIGHUP);
 
92
 
 
93
    // Close descriptors
 
94
    close(myFd);
 
95
  }
 
96
}
 
97
 
 
98
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
99
void SoundUnix::set(Sound::Register reg, uInt8 value)
 
100
{
 
101
  // Return if I'm currently disabled or if the stella-sound process has died
 
102
  if(myDisabled || (waitpid(myProcessId, 0, WNOHANG) == myProcessId))
 
103
  {
 
104
    myDisabled = true;
 
105
    return;
 
106
  }
 
107
 
 
108
  uInt8 command;
 
109
 
 
110
  switch(reg)
 
111
  {
 
112
    case AUDC0:
 
113
    {
 
114
      if(myAUDC0 != (value & 0x0f))
 
115
      {
 
116
        myAUDC0 = (value & 0x0f);
 
117
        command = myAUDC0 | 0x00;
 
118
      }
 
119
      else
 
120
      {
 
121
        return;
 
122
      } 
 
123
      break;
 
124
    }
 
125
 
 
126
    case AUDC1:
 
127
    {
 
128
      if(myAUDC1 != (value & 0x0f))
 
129
      {
 
130
        myAUDC1 = (value & 0x0f);
 
131
        command = myAUDC1 | 0x20;
 
132
      }
 
133
      else
 
134
      {
 
135
        return;
 
136
      } 
 
137
      break;
 
138
    }
 
139
 
 
140
    case AUDF0:
 
141
    {
 
142
      if(myAUDF0 != (value & 0x1f))
 
143
      {
 
144
        myAUDF0 = (value & 0x1f);
 
145
        command = myAUDF0 | 0x40;
 
146
      }
 
147
      else
 
148
      {
 
149
        return;
 
150
      } 
 
151
      break;
 
152
    }
 
153
 
 
154
    case AUDF1:
 
155
    {
 
156
      if(myAUDF1 != (value & 0x1f))
 
157
      {
 
158
        myAUDF1 = (value & 0x1f);
 
159
        command = myAUDF1 | 0x60;
 
160
      }
 
161
      else
 
162
      {
 
163
        return;
 
164
      } 
 
165
      break;
 
166
    }
 
167
 
 
168
    case AUDV0:
 
169
    {
 
170
      if(myAUDV0 != (value & 0x0f))
 
171
      {
 
172
        myAUDV0 = (value & 0x0f);
 
173
        command = myAUDV0 | 0x80;
 
174
      }
 
175
      else
 
176
      {
 
177
        return;
 
178
      } 
 
179
      break;
 
180
    }
 
181
 
 
182
    case AUDV1:
 
183
    {
 
184
      if(myAUDV1 != (value & 0x0f))
 
185
      {
 
186
        myAUDV1 = (value & 0x0f);
 
187
        command = myAUDV1 | 0xA0;
 
188
      }
 
189
      else
 
190
      {
 
191
        return;
 
192
      } 
 
193
      break;
 
194
    }
 
195
 
 
196
    default:
 
197
    {
 
198
      return;
 
199
      break;
 
200
    }
 
201
  }
 
202
 
 
203
  // Send sound command to the stella-sound process
 
204
  write(myFd, &command, 1);
 
205
}
 
206
 
 
207
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
208
void SoundUnix::mute(bool state)
 
209
{
 
210
  // Return if I'm currently disabled or if the stella-sound process has died
 
211
  if(myDisabled || (waitpid(myProcessId, 0, WNOHANG) == myProcessId))
 
212
  {
 
213
    myDisabled = true;
 
214
    return;
 
215
  }
 
216
 
 
217
  myMute = state;
 
218
 
 
219
  uInt8 command;
 
220
  if(myMute)
 
221
  {
 
222
    // Setup the mute enable command
 
223
    command = 0x01 | 0xE0;
 
224
  }
 
225
  else
 
226
  {
 
227
    // Setup the mute disable command
 
228
    command = 0x00 | 0xE0;
 
229
  }
 
230
 
 
231
  // Send sound command to the stella-sound process
 
232
  write(myFd, &command, 1);
 
233
}
 
234