Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
soundserver.segfault.cpp File Reference
#include "lin_time.h"
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL/SDL_mixer.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <string>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "inet_file.h"

Go to the source code of this file.

Classes

struct  Music
 

Macros

#define HAVE_SDL
 

Functions

int GetMaxVolume ()
 
void errorv (char *str, va_list ap)
 
void cleanExit (char *str,...)
 
void changehome (bool to, bool linuxhome=true)
 
Music PlayMusic (const char *file, Music &oldmusic)
 
void music_finished ()
 
void TakeLotsOfPriority ()
 
int main (int argc, char **argv)
 

Variables

struct Music fadeout = 0
 
struct Music fadein = 0
 
float volume = 0
 
int bits = 0
 
int done = 0
 
bool sende = true
 
bool invalid_string = true
 
std::string curmus
 
int mysocket_read = -1
 
int mysocket_write = -1
 

Macro Definition Documentation

#define HAVE_SDL

Definition at line 133 of file soundserver.segfault.cpp.

Function Documentation

void changehome ( bool  to,
bool  linuxhome = true 
)

Definition at line 257 of file soundserver.segfault.cpp.

258 {
259  static std::vector< std::string >paths;
260  if (to) {
261  char mycurpath[8192];
262  getcwd( mycurpath, 8191 );
263  mycurpath[8191] = '\0';
264  paths.push_back( std::string( mycurpath ) );
265 #ifndef _WIN32
266  if (linuxhome) {
267  struct passwd *pwent;
268  pwent = getpwuid( getuid() );
269  chdir( pwent->pw_dir );
270  }
271 #endif
272  chdir( ".vegastrike" );
273  } else if ( !paths.empty() ) {
274  chdir( paths.back().c_str() );
275  paths.pop_back();
276  }
277 }
void cleanExit ( char *  str,
  ... 
)

Definition at line 239 of file soundserver.segfault.cpp.

References errorv().

240 {
241 #ifdef HAVE_SDL
242  va_list ap;
243 
244  va_start( ap, str );
245  errorv( str, ap );
246  va_end( ap );
247  Mix_CloseAudio();
248  SDL_Quit();
249 #endif
250  exit( 1 );
251 }
void errorv ( char *  str,
va_list  ap 
)

Definition at line 216 of file soundserver.segfault.cpp.

References fprintf.

217 {
218 #ifdef HAVE_SDL
219  vfprintf( stderr, str, ap );
220 
221  fprintf( stderr, ": %s.\n", SDL_GetError() );
222 #endif
223 }
int GetMaxVolume ( )
int main ( int  argc,
char **  argv 
)

Definition at line 355 of file soundserver.segfault.cpp.

References cleanExit(), done, fadein, fadeout, fNET_AcceptFrom(), fNET_cleanup(), fNET_close(), fNET_fgetc(), fNET_startup(), GetMaxVolume(), i, Music::m, music_finished(), mysocket_write, PlayMusic(), Music::SetMasterVolume(), and TakeLotsOfPriority().

356 {
358  Music music;
359  int audio_rate, audio_channels,
360  //set this to any of 512,1024,2048,4096
361  //the higher it is, the more FPS shown and CPU needed
362  audio_buffers = 4096;
363 #ifdef HAVE_SDL
364  Uint16 audio_format;
365  //initialize SDL for audio and video
366  if (SDL_Init( SDL_INIT_AUDIO ) < 0)
367  cleanExit( "SDL_Init\n" );
368  Mix_HookMusicFinished( &music_finished );
369 #else
370 #ifdef __APPLE__
371  if ( !FSOUND_Init( 44100, 64, FSOUND_INIT_GLOBALFOCUS ) ) {
372  printf( "SOUND Error %d\n", FSOUND_GetError() );
373  exit( 1 );
374  }
375 #endif
376 #endif
377  fNET_startup();
378  GetMaxVolume();
379  //initialize sdl mixer, open up the audio device
380 #ifdef HAVE_SDL
381  if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, audio_buffers ) < 0)
382  cleanExit( "Mix_OpenAudio\n" );
383  //print out some info on the audio device and stream
384  Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels );
385  bits = audio_format&0xFF;
386 #endif
387  printf( "Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
388  bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers );
389 
390  //load the song
391  printf( "argv %d\n\n\n", argc );
392  if (argc != 3) {
393  for (int i = 0; i < 10 && mysocket_write == -1; i++) {
394  mysocket_write = fNET_AcceptFrom( 4364, "localhost" );
396  }
397  if (mysocket_write == -1)
398  return 1;
399  } else {
400  mysocket_write = open( argv[2], O_WRONLY|O_SHLOCK|O_CREAT, 0xffffffff );
401  mysocket_read = open( argv[1], O_RDONLY|O_SHLOCK|O_CREAT, 0xffffffff );
402  }
403  printf( "\n[CONNECTED]\n" );
404  char ministr[2] = {'\0', '\0'};
405  while (!done) {
406 //if ((Mix_PlayingMusic() || Mix_PausedMusic())&&(!done)) {
407  char arg;
408  std::string str;
409  arg = fNET_fgetc( mysocket_read );
410  printf( "%c", arg );
411  switch (arg)
412  {
413  case 'p':
414  case 'P':
415  arg = fNET_fgetc( mysocket_read );
416  while (arg != '\0' && arg != '\n') {
417  if (arg != '\r') {
418  ministr[0] = arg;
419  str += ministr;
420  }
421  arg = fNET_fgetc( mysocket_read );
422  }
423  printf( "%s", str.c_str() );
424  if ( (str != curmus || invalid_string)
425 #ifdef HAVE_SDL
426  || ( !Mix_PlayingMusic() )
427 #endif
428  ) {
429  music = PlayMusic( str.c_str(), music );
430  if (music.m) {
431  printf( "\n[PLAYING %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(), fadein, fadeout );
432  curmus = str;
433  invalid_string = false;
434  } else {
435  printf( "\n[UNABLE TO PLAY %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(), fadein, fadeout );
436  music_finished();
437  }
438  } else {
439  printf( "\n[%s WITH %d FADEIN AND %d FADEOUT IS ALREADY PLAYING]\n", str.c_str(), fadein, fadeout );
440  }
441  break;
442  case 'i':
443  case 'I':
444  arg = fNET_fgetc( mysocket_read );
445  while (arg != '\0' && arg != '\n') {
446  if (arg != '\r') {
447  ministr[0] = arg;
448  str += ministr;
449  }
450  arg = fNET_fgetc( mysocket_read );
451  }
452  printf( "%s", str.c_str() );
453  fadein = atoi( str.c_str() );
454  printf( "\n[SETTING FADEIN TO %d]\n", fadein );
455  break;
456  case 'o':
457  case 'O':
458  arg = fNET_fgetc( mysocket_read );
459  while (arg != '\0' && arg != '\n') {
460  if (arg != '\r') {
461  ministr[0] = arg;
462  str += ministr;
463  }
464  arg = fNET_fgetc( mysocket_read );
465  }
466  printf( "%s", str.c_str() );
467  fadeout = atoi( str.c_str() );
468  printf( "\n[SETTING FADEOUT TO %d]\n", fadeout );
469  break;
470  case 'v':
471  case 'V':
472  arg = fNET_fgetc( mysocket_read );
473  while (arg != '\0' && arg != '\n') {
474  if (arg != '\r') {
475  ministr[0] = arg;
476  str += ministr;
477  }
478  arg = fNET_fgetc( mysocket_read );
479  }
480  printf( "%s", str.c_str() );
481  volume = atof( str.c_str() );
482  printf( "\n[SETTING VOLUME TO %f]\n", volume );
483  music.SetMasterVolume( volume );
484  break;
485  case 't':
486  case 'T':
487  case '\0':
489  if (mysocket_read != mysocket_write)
490  fNET_close( mysocket_write );
491  done = true;
492  printf( "\n[TERMINATING MUSIC SERVER]\n" );
493  break;
494  }
495  }
496  //free & close
497  fNET_cleanup();
498 #ifdef HAVE_SDL
499  Mix_CloseAudio();
500  SDL_Quit();
501 #endif
502 
503  return 0;
504 }
void music_finished ( )

Definition at line 311 of file soundserver.segfault.cpp.

References fNET_Write().

312 {
313  if (sende) {
314  char data = 'e';
315  e_already_sent = true;
316  fNET_Write( mysocket_write, sizeof (char), &data );
317  printf( "\ne\n[SONG DONE]\n" );
318  invalid_string = true;
319  }
320 }
Music PlayMusic ( const char *  file,
Music oldmusic 
)

Definition at line 285 of file soundserver.segfault.cpp.

References changehome(), accountXML::file, Music::Load(), Music::m, and Music::Play().

286 {
287  Music music;
288  music.Load( file );
289  if (music.m == NULL) {
290  changehome( true, false );
291  music.Load( file );
292  changehome( false );
293  if (music.m == NULL) {
294  changehome( true, true );
295  music.Load( file );
296  changehome( false );
297  if (music.m == NULL)
298  return oldmusic;
299  }
300  }
301  sende = false;
302  music.Play( fadeout, fadein, oldmusic );
303 
304  sende = true;
305  curmus = file;
306  invalid_string = false;
307  return music;
308 }
void TakeLotsOfPriority ( )

Definition at line 321 of file soundserver.segfault.cpp.

References error.

Referenced by main().

322 {
323 #ifdef __MACH__
324 //Disable timesharing
325  {
326  kern_return_t error;
327  thread_extended_policy_data_t extendedPolicy;
328  thread_precedence_policy_data_t precedencePolicy;
329 
330  extendedPolicy.timeshare = 0;
331  error = thread_policy_set( mach_thread_self(),
332  THREAD_EXTENDED_POLICY,
333  (thread_policy_t) &extendedPolicy,
334  THREAD_EXTENDED_POLICY_COUNT );
335  if (error != KERN_SUCCESS) {
336 #if DEBUG
337  mach_error( "Couldn't set feeder thread's extended policy",
338  error );
339 #endif
340  }
341  precedencePolicy.importance = 1;
342  error = thread_policy_set( mach_thread_self(),
343  THREAD_PRECEDENCE_POLICY,
344  (thread_policy_t) &precedencePolicy,
345  THREAD_PRECEDENCE_POLICY_COUNT );
346  if (error != KERN_SUCCESS) {
347 #if DEBUG
348  mach_error( "Couldn't set feeder thread's precedence policy",
349  error );
350 #endif
351  }
352  }
353 #endif
354 }

Variable Documentation

int bits = 0

Definition at line 210 of file soundserver.segfault.cpp.

std::string curmus

Definition at line 283 of file soundserver.segfault.cpp.

int done = 0

Definition at line 210 of file soundserver.segfault.cpp.

struct Music fadein = 0
struct Music fadeout = 0
bool invalid_string = true

Definition at line 282 of file soundserver.segfault.cpp.

int mysocket_read = -1

Definition at line 309 of file soundserver.segfault.cpp.

Referenced by main().

int mysocket_write = -1

Definition at line 310 of file soundserver.segfault.cpp.

bool sende = true

Definition at line 281 of file soundserver.segfault.cpp.

float volume = 0

Definition at line 209 of file soundserver.segfault.cpp.