Crossfire Client, Trunk  R20330
init.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, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
19 #include "config.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "client.h"
26 #include "common.h"
27 #include "sndproto.h"
28 
30 
34 static void init_sounds() {
35  FILE *fp;
36  char buf[512];
37  int i;
38 
39  /* Initialize by setting all sounds to NULL. */
40  for (i = 0; i < MAX_SOUNDS; i++) {
41  sounds[i].filename = NULL;
42  }
43 
44  /* Try to open the sound definitions file. */
45  printf("Loading sounds from '%s'...\n", g_getenv("CF_SOUND_DIR"));
46  fp = fopen(g_getenv("CF_SOUND_CONF"), "r");
47 
48  if (fp == NULL) {
49  fprintf(stderr, "Could not find sound definitions; aborting!\n");
50  exit(EXIT_FAILURE);
51  }
52 
53  /* Use 'i' as index tracker, so set it to zero. */
54  i = 0;
55 
56  /* Parse the sound definitions file, line by line. */
57  while (fgets(buf, sizeof(buf), fp) != NULL) {
58  char *line;
59  line = &buf[0];
60 
61  /* Ignore all lines that start with a comment or newline. */
62  if (buf[0] == '#' || buf[0] == '\n') {
63  continue;
64  }
65 
66  /* Trim the trailing newline if it exists (see CERT FIO36-C). */
67  char *newline;
68  newline = strchr(buf, '\n');
69 
70  if (newline != NULL) {
71  *newline = '\0';
72  }
73 
74  /* FIXME: No error checking; potential segfaults here. */
75  sounds[i].symbolic = g_strdup(strsep(&line, ":"));
76  sounds[i].volume = atoi(strsep(&line, ":"));
77  sounds[i].filename = g_strdup(strsep(&line, ":"));
78 
79  /* Move on to the next sound. */
80  i++;
81  }
82 
83  fclose(fp);
84 }
85 
94 int init() {
95  char path[MAXSOCKBUF];
96 
97  /* Sanity check for $HOME environmental variable. */
98  if (g_getenv("HOME") == NULL) {
99  fprintf(stderr, "Couldn't read $HOME environmental variable.\n"
100  "Please set it to something reasonable.\n");
101  return -1;
102  }
103 
104  /* Set $CF_SOUND_DIR to something reasonable, if not already set. */
105  if (g_setenv("CF_SOUND_DIR", CLIENT_SOUNDS_PATH, 0) != 0) {
106  perror("Couldn't set $CF_SOUND_DIR");
107  return -1;
108  }
109 
110  /* Set $CF_SOUND_CONF to something reasonable, if not already set. */
111  snprintf(path, sizeof(path), "%s/sounds.conf", g_getenv("CF_SOUND_DIR"));
112 
113  if (g_setenv("CF_SOUND_CONF", path, 0) != 0) {
114  perror("Couldn't set $CF_SOUND_CONF");
115  return -1;
116  }
117 
118  /* Initialize sound definitions. */
119  init_sounds();
120 
121  /* Initialize audio library. */
122  if (init_audio()) {
123  return -1;
124  }
125 
126  return 0;
127 }
unsigned char volume
Definition: common.h:15
int init()
Initialize sound server.
Definition: init.c:94
#define MAX_SOUNDS
Definition: common.h:8
int init_audio()
Initialize the sound subsystem.
Definition: cfsndserv.c:41
Sound_Info sounds[MAX_SOUNDS]
Definition: init.c:29
#define MAXSOCKBUF
Maximum size of a packet the client expects to get and that the server can send.
Definition: newclient.h:53
char * symbolic
Definition: common.h:14
Includes various dependencies header files needed by most everything.
#define CLIENT_SOUNDS_PATH
Definition: common.h:5
char * filename
Definition: common.h:13
static void init_sounds()
Load sound definitions from a file.
Definition: init.c:34