vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / game / simple_game_sound_test / SimpleSoundTest.cpp
blob4062e0e6176512ee80afb7306fd7d96243c3cc00
1 //------------------------------------------------------------------------------
2 // SimpleSoundTest.cpp
3 //
4 // Unit test for the game kit.
5 //
6 // Copyright (c) 2001 OpenBeOS Project
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
26 // File Name: SimpleSoundTest.h
27 // Author: Christopher ML Zumwalt May (zummy@users.sf.net)
28 // Description: BSimpleGameSound test application
29 //------------------------------------------------------------------------------
31 #include <stdlib.h>
33 #include <Button.h>
34 #include <Slider.h>
35 #include <CheckBox.h>
36 #include <FilePanel.h>
37 #include <TextControl.h>
39 #include "SimpleGameSound.h"
40 #include "SimpleSoundTest.h"
42 const int32 SLIDER_PAN = 'SPan';
43 const int32 SLIDER_GAIN = 'Gain';
44 const int32 BUTTON_START = 'Strt';
45 const int32 BUTTON_STOP = 'Stop';
46 const int32 BUTTON_BROWSE = 'Open';
47 const int32 CHECK_LOOP = 'Loop';
49 int main(int, char**)
51 SimpleSoundApp app("application/x-vnd.OBOS.GameKitApp");
52 app.Run();
53 return 0;
57 // SimpleSoundApp ---------------------------------------------------------
58 SimpleSoundApp::SimpleSoundApp(const char *signature)
59 : BApplication(signature)
64 void SimpleSoundApp::ReadyToRun()
66 fWin = new SimpleSoundWin(BRect(100, 200, 330, 450), "GameKit Unit Test");
67 fWin->Show();
71 void SimpleSoundApp::RefsReceived(BMessage* msg)
73 uint32 type;
74 int32 count;
75 msg->GetInfo("refs", &type, &count);
76 if (type == B_REF_TYPE)
78 // Load the files
79 for(int32 i = 0; i < count; i++)
81 entry_ref file;
82 if (msg->FindRef("refs", i, &file) == B_OK)
84 BSimpleGameSound * sound = new BSimpleGameSound(&file);
86 if (sound->InitCheck() == B_OK)
87 fWin->SetSound(sound);
88 else
89 delete sound;
96 // SimpleSoundWin ---------------------------------------------------------------
97 SimpleSoundWin::SimpleSoundWin(BRect frame, const char * title)
98 : BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS),
99 fSound(NULL),
100 fPanel(NULL)
102 BRect r(10, 10, 100, 40);
103 fBrowse = new BButton(r, "buttonBrowse", "Browse", new BMessage(BUTTON_BROWSE));
104 fBrowse->SetEnabled(true);
105 AddChild(fBrowse);
107 // Add the button which start and stop playback of the choosen sound
108 r.OffsetBy(0, 50);
109 fStart = new BButton(r, "buttonStart", "Start", new BMessage(BUTTON_START));
110 fStart->SetEnabled(false);
111 AddChild(fStart);
113 r.OffsetBy(110, 0);
114 fStop = new BButton(r, "buttonStop", "Stop", new BMessage(BUTTON_STOP));
115 fStop->SetEnabled(false);
116 AddChild(fStop);
118 // Control the sound's volumn (gain)
119 r.Set(10, 100, 200, 40);
120 fGain = new BSlider(r, "sliderGain", "Gain", new BMessage(SLIDER_GAIN), 0, 100);
121 fGain->SetHashMarks(B_HASH_MARKS_BOTTOM);
122 fGain->SetHashMarkCount(10);
123 fGain->SetLimitLabels("Mute", "Full");
124 fGain->SetPosition(1.0);
125 fGain->SetEnabled(false);
126 AddChild(fGain);
128 // Control the sound's pan
129 r.OffsetBy(0, 60);
130 fPan = new BSlider(r, "sliderPan", "Pan", new BMessage(SLIDER_PAN), -100, 100);
131 fPan->SetHashMarks(B_HASH_MARKS_BOTTOM);
132 fPan->SetHashMarkCount(10);
133 fPan->SetLimitLabels("Left", "Right");
134 fPan->SetEnabled(false);
135 AddChild(fPan);
137 r.Set(10, 220, 110, 40);
138 fRamp = new BTextControl(r, "txtRamp", "Ramp", "20", NULL);
139 fRamp->SetDivider(30);
140 fRamp->SetEnabled(true);
141 AddChild(fRamp);
143 r.Set(120, 220, 200, 40);
144 fLoop = new BCheckBox(r, "chkLoop", "Loop", new BMessage(CHECK_LOOP));
145 fLoop->SetEnabled(false);
146 AddChild(fLoop);
150 SimpleSoundWin::~SimpleSoundWin()
152 delete fPanel;
153 delete fSound;
157 void SimpleSoundWin::Quit()
159 be_app->PostMessage(B_QUIT_REQUESTED);
160 BWindow::Quit();
164 void SimpleSoundWin::MessageReceived(BMessage *msg)
166 int32 pos;
167 bigtime_t ramp = bigtime_t(atoi(fRamp->Text()) * 100000.0);
169 switch (msg->what)
171 case BUTTON_START:
172 fStop->SetEnabled(true);
173 fGain->SetEnabled(true);
174 fPan->SetEnabled(true);
175 fLoop->SetEnabled(true);
177 fSound->StartPlaying();
178 break;
180 case BUTTON_STOP:
181 fStop->SetEnabled(false);
182 fGain->SetEnabled(false);
183 fPan->SetEnabled(false);
184 fLoop->SetEnabled(false);
186 fSound->StopPlaying();
187 break;
189 case SLIDER_GAIN:
190 fSound->SetGain(fGain->Position(), ramp);
191 break;
193 case SLIDER_PAN:
194 pos = fPan->Value();
195 fSound->SetPan(pos / 100.0, ramp);
196 break;
198 case BUTTON_BROWSE:
199 if (!fPanel) fPanel = new BFilePanel(B_OPEN_PANEL, &be_app_messenger);
201 fPanel->Show();
202 break;
204 case CHECK_LOOP:
205 fSound->SetIsLooping(fLoop->Value() == B_CONTROL_ON);
206 break;
208 default:
209 BWindow::MessageReceived(msg);
210 break;
215 void SimpleSoundWin::SetSound(BSimpleGameSound * sound)
217 delete fSound;
219 // enable the Start button if we were given a valid sound
220 Lock();
222 fStart->SetEnabled((sound != NULL));
223 fLoop->SetValue(B_CONTROL_OFF);
224 fGain->SetPosition(1.0);
225 fPan->SetPosition(0.0);
227 Unlock();
229 fSound = sound;