Crossfire Client, Trunk  R20612
sound.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, see the
9  * 'LICENSE' and 'COPYING' files.
10  *
11  * The authors can be reached via e-mail to crossfire-devel@real-time.com
12  */
13 
21 #include "client.h"
22 
23 #include "client-vala.h"
24 
25 SoundServer* server;
26 
27 int init_sounds() {
28  server = sound_server_new();
29  return sound_server_spawn(server, sound_server);
30 }
31 
40 void Sound2Cmd(unsigned char *data, int len) {
41  gint8 x, y;
42  guint8 dir, vol, type, len_sound, len_source;
43  char *sound = NULL;
44  char *source = NULL;
45 
54  if (len < 8) {
56  "gtk-v2::Sound2Cmd", "Sound command too short: %d\n bytes", len);
57  return;
58  }
59 
60  x = data[0];
61  y = data[1];
62  dir = data[2];
63  vol = data[3];
64  type = data[4];
65  len_sound = data[5];
66  /*
67  * The minimum size of data is 1 for each byte in the command (7) plus the
68  * size of the sound string. If we do not have that, the data is bogus.
69  */
70  if (6 + len_sound + 1 > len) {
72  "gtk-v2::Sound2Cmd", "sound length check: %i len: %i\n",
73  len_sound, len);
74  return;
75  }
76 
77  len_source = data[6 + len_sound];
78  if (len_sound != 0) {
79  sound = (char *) data + 6;
80  data[6 + len_sound] = '\0';
81  }
82  /*
83  * The minimum size of data is 1 for each byte in the command (7) plus the
84  * size of the sound string, and the size of the source string.
85  */
86  if (6 + len_sound + 1 + len_source > len) {
88  "gtk-v2::Sound2Cmd", "source length check: %i len: %i\n",
89  len_source, len);
90  return;
91  }
92  /*
93  * Though it looks like there is potential for writing a null off the end
94  * of the buffer, there is always room for a null (see do_client()).
95  */
96  if (len_source != 0) {
97  source = (char *) data + 6 + len_sound + 1;
98  data[6 + len_sound + 1 + len_source] = '\0';
99  }
100 
101  LOG(LOG_DEBUG, "gtk-v2::Sound2Cmd",
102  "Playing sound2 x=%hhd y=%hhd dir=%hhd volume=%hhd type=%hhd",
103  x, y, dir, vol, type);
104  LOG(LOG_DEBUG, "gtk-v2::Sound2Cmd", " len_sound=%hhd sound=%s",
105  len_sound, sound);
106  LOG(LOG_DEBUG, "gtk-v2::Sound2Cmd", " len_source=%hhd source=%s",
107  len_source, source);
108  if (use_config[CONFIG_SOUND]) {
109  sound_server_play(server, x, y, dir, vol, type, sound, source);
110  }
111 }
112 
122 void MusicCmd(const char *data, int len) {
130  // Check for null terminator.
131  if (data[len]) {
132  LOG(LOG_ERROR, "gtk-v2::MusicCmd",
133  "Music command string is not null-terminated.");
134  return;
135  }
136  LOG(LOG_DEBUG, "gtk-v2::MusicCmd", "\"%s\"", data);
137  if (use_config[CONFIG_SOUND]) {
138  sound_server_play_music(server, data);
139  }
140 }
gint16 use_config[CONFIG_NUMS]
Definition: init.c:40
Warning that something might not work.
Definition: client.h:443
void LOG(LogLevel level, const char *origin, const char *format,...)
Definition: misc.c:109
void MusicCmd(const char *data, int len)
Definition: sound.c:122
int init_sounds()
Definition: sound.c:27
Warning that something definitely didn't work.
Definition: client.h:444
char * sound_server
Definition: client.c:51
#define CONFIG_SOUND
Definition: client.h:197
Useful debugging information.
Definition: client.h:441
SoundServer * server
Definition: sound.c:25
void Sound2Cmd(unsigned char *data, int len)
Definition: sound.c:40