summaryrefslogtreecommitdiff
path: root/esdmgr.c
blob: 9cc39ab4e3d2255de7b0fdbd44a747c7fba62316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

#include <esd.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

/*********************************************************************/
/* print server into to stdout */
void esd_print_server_info( esd_server_info_t *server_info )
{
    printf( "server version = %d\n", server_info->version );
    printf( "server format  = 0x%08x\n", server_info->format );
    printf( "server rate    = %d\n", server_info->rate );
    return;
}

/*********************************************************************/
/* print player into to stdout */
void esd_print_player_info( esd_player_info_t *player_info )
{
    printf( "player %d name    = %s\n", 
	    player_info->source_id, player_info->name );
    printf( "player %d format  = 0x%08x\n", 
	    player_info->source_id, player_info->format );
    printf( "player %d rate    = %d\n", 
	    player_info->source_id, player_info->rate );
    printf( "player %d left    = %d\n", 
	    player_info->source_id, player_info->left_vol_scale );
    printf( "player %d right   = %d\n", 
	    player_info->source_id, player_info->right_vol_scale );
    return;
}

/*********************************************************************/
/* print server into to stdout */
void esd_print_sample_info( esd_sample_info_t *sample_info )
{
    printf( "sample %d name    = %s\n", 
	    sample_info->sample_id, sample_info->name );
    printf( "sample %d format  = 0x%08x\n", 
	    sample_info->sample_id, sample_info->format );
    printf( "sample %d rate    = %d\n", 
	    sample_info->sample_id, sample_info->rate );
    printf( "sample %d left    = %d\n", 
	    sample_info->sample_id, sample_info->left_vol_scale );
    printf( "sample %d right   = %d\n", 
	    sample_info->sample_id, sample_info->right_vol_scale );
    printf( "sample %d length  = %d\n", 
	    sample_info->sample_id, sample_info->length );
    return;
}

/*********************************************************************/
/* print all info to stdout */
void esd_print_all_info( esd_info_t *all_info )
{
    esd_player_info_t *player_info;
    esd_sample_info_t *sample_info;

    /* dump server info */
    esd_print_server_info( all_info->server );

    /* dump player info */
    for ( player_info = all_info->player_list 
	      ; player_info != NULL ; player_info = player_info->next )
    {
	esd_print_player_info( player_info );
    }

    /* dump sample info */
    for ( sample_info = all_info->sample_list 
	      ; sample_info != NULL ; sample_info = sample_info->next )
    {
	esd_print_sample_info( sample_info );
    }

    return;
}

/*********************************************************************/
/* retrieve server properties (sample rate, format, version number) */
esd_server_info_t *esd_get_server_info( int esd )
{
    int proto = ESD_PROTO_SERVER_INFO;
    int version = 0;

    /* allocate the server info structure */
    esd_server_info_t *server_info 
	= (esd_server_info_t *) malloc( sizeof(esd_server_info_t) );
    if ( !server_info ) return server_info;

    /* tell the server to cough up the info */
    write( esd, &proto, sizeof(proto) );
    if ( write( esd, &version, sizeof(version) ) != sizeof(version) ) {
	free( server_info );
	return NULL;
    }

    /* get the info from the server */
    read( esd, &server_info->version, sizeof(server_info->version) );
    read( esd, &server_info->rate, sizeof(server_info->rate) );
    if ( read( esd, &server_info->format, sizeof(server_info->format) )
	 != sizeof(server_info->format) ) {
	free( server_info );
	return NULL;
    }

    return server_info;
}

/*********************************************************************/
/* release all memory allocated for the server properties structure */
void esd_free_server_info( esd_server_info_t *server_info )
{
    free( server_info );
    return;
}

/*********************************************************************/
/* retrieve all information from server */
esd_info_t *esd_get_all_info( int esd )
{
    int proto = ESD_PROTO_ALL_INFO;
    int version = 0;
    esd_server_info_t *server_info;
    esd_player_info_t *player_info;
    esd_sample_info_t *sample_info;

    /* allocate the entire info structure, and set defaults to NULL */
    esd_info_t *info = (esd_info_t *) malloc( sizeof(esd_info_t) );
    info->player_list = NULL;
    info->sample_list = NULL;

    /* allocate the server info structure */
    server_info = (esd_server_info_t *) malloc( sizeof(esd_server_info_t) );
    if ( !server_info ) {
        esd_free_all_info( info );
        return NULL;
    }

    /* tell the server to cough up the info */
    write( esd, &proto, sizeof(proto) );
    if ( write( esd, &version, sizeof(version) ) != sizeof(version) ) {
	esd_free_all_info( info );
	free( server_info );
	return NULL;
    }

    /* get the server info */
    read( esd, &server_info->version, sizeof(server_info->version) );
    read( esd, &server_info->rate, sizeof(server_info->rate) );
    if ( read( esd, &server_info->format, sizeof(server_info->format) )
	 != sizeof(server_info->format) ) {
	esd_free_all_info( info );
	free( server_info );
	return NULL;
    }
    info->server = server_info;

    /* get the player info */
    do 
    {
	player_info = (esd_player_info_t *) malloc( sizeof(esd_player_info_t) );
	if ( !player_info ) {
	    esd_free_all_info( info );
	    return NULL;
	}

	read( esd, &player_info->source_id, sizeof(player_info->source_id) );
	read( esd, &player_info->name, ESD_NAME_MAX );
	player_info->name[ ESD_NAME_MAX - 1 ] = '\0';
	read( esd, &player_info->rate, sizeof(player_info->rate) );
	read( esd, &player_info->left_vol_scale, sizeof(player_info->left_vol_scale) );
	read( esd, &player_info->right_vol_scale, sizeof(player_info->right_vol_scale) );
	if ( read( esd, &player_info->format, sizeof(player_info->format) )
	     != sizeof(player_info->format) ) {
	    free( player_info );
	    esd_free_all_info( info );	    
	    return NULL;
	}
	
	if ( player_info->source_id > 0 ) {
	    player_info->next = info->player_list;
	    info->player_list = player_info;
	}

    } while( player_info->source_id > 0 );

    free( player_info );

    /* get the sample info */
    do 
    {
	sample_info = (esd_sample_info_t *) malloc( sizeof(esd_sample_info_t) );
	if ( !sample_info ) {
	    esd_free_all_info( info );
	    return NULL;
	}

	read( esd, &sample_info->sample_id, sizeof(sample_info->sample_id) );
	read( esd, &sample_info->name, ESD_NAME_MAX );
	sample_info->name[ ESD_NAME_MAX - 1 ] = '\0';
	read( esd, &sample_info->rate, sizeof(sample_info->rate) );
	read( esd, &sample_info->left_vol_scale, sizeof(sample_info->left_vol_scale) );
	read( esd, &sample_info->right_vol_scale, sizeof(sample_info->right_vol_scale) );
	read( esd, &sample_info->format, sizeof(sample_info->format) );
	if ( read( esd, &sample_info->length, sizeof(sample_info->length) )
	     != sizeof(sample_info->length) ) {
	    free( sample_info );
	    esd_free_all_info( info );	    
	    return NULL;
	}
	
	if ( sample_info->sample_id > 0 ) {
	    sample_info->next = info->sample_list;
	    info->sample_list = sample_info;
	}

    } while( sample_info->sample_id > 0 );

    free( sample_info );

    return info;
}

/*********************************************************************/
/* retrieve all information from server, and update until unsubsribed or closed */
esd_info_t *esd_subscribe_all_info( int esd )
{
    fprintf( stderr, "- esd_subscribe_all_info: not yet implemented!\n" );
    if(esd) {
	fprintf(stderr, "but you passed me something anyways\n");
    }
    return NULL;
}

/*********************************************************************/
/* call to update the info structure with new information, and call callbacks */
esd_info_t *esd_update_info( int esd, esd_info_t *info, 
			     esd_update_info_callbacks_t *callbacks )
{
    fprintf( stderr, "- esd_update_info: not yet implemented!\n" );
    if(esd)
	if(info) 
	    if(callbacks)
		fprintf(stderr, "but you passed me something anyways\n");
    return NULL;
}

/*********************************************************************/
/* call to update the info structure with new information, and call callbacks */
esd_info_t *esd_unsubscribe_info( int esd )
{
    fprintf( stderr, "- esd_unsubscribe_info: not yet implemented!\n" );
	if(esd)
		fprintf(stderr, "but you passed me something anyways\n");
    return NULL;
}

/*********************************************************************/
/* release all memory allocated for the esd info structure */
void esd_free_all_info( esd_info_t *info )
{
    esd_player_info_t *player_info, *next_player_info;
    esd_sample_info_t *sample_info, *next_sample_info;

    free( info->server );

    player_info = info->player_list;
    while ( player_info != NULL ) {
	next_player_info = player_info->next;
	free( player_info );
	player_info = next_player_info;
    }

    sample_info = info->sample_list;
    while ( sample_info != NULL ) {
	next_sample_info = sample_info->next;
	free( sample_info );
	sample_info = next_sample_info;
    }

    free( info );
    return;
}


/* reset the volume panning for a stream */
int esd_set_stream_pan( int esd, int stream_id, 
			int left_scale, int right_scale )
{
    int ok;
    int proto = ESD_PROTO_STREAM_PAN;

    /* send the stream panning information */
    if ( write( esd, &proto, sizeof(proto) ) != sizeof(proto) )
	return -1;
    if ( write( esd, &stream_id, sizeof(stream_id) ) != sizeof(stream_id) )
	return -1;
    if ( write( esd, &left_scale, sizeof(left_scale) ) != sizeof(left_scale) )
	return -1;
    if ( write( esd, &right_scale, sizeof(right_scale) ) != sizeof(right_scale) )
	return -1;

    /* flush the socket */
    /* fsync( sock ); */
    
    /* read the status back from the server */
    if ( read( esd, &ok, sizeof(ok) ) != sizeof(ok) )
	return -1;

    return ok;
}

/* reset the default volume panning for a sample */
int esd_set_default_sample_pan( int esd, int sample_id, 
				int left_scale, int right_scale )
{
    int ok;
    int proto = ESD_PROTO_SAMPLE_PAN;

    /* send the stream panning information */
    if ( write( esd, &proto, sizeof(proto) ) != sizeof(proto) )
	return -1;
    if ( write( esd, &sample_id, sizeof(sample_id) ) != sizeof(sample_id) )
	return -1;
    if ( write( esd, &left_scale, sizeof(left_scale) ) != sizeof(left_scale) )
	return -1;
    if ( write( esd, &right_scale, sizeof(right_scale) ) != sizeof(right_scale) )
	return -1;

    /* flush the socket */
    /* fsync( sock ); */
    
    /* read the status back from the server */
    if ( read( esd, &ok, sizeof(ok) ) != sizeof(ok) )
	return -1;

    return ok;
}

/* see if the server is in stnaby, autostandby, etc */
esd_standby_mode_t esd_get_standby_mode( int esd )
{
    int proto = ESD_PROTO_STANDBY_MODE, mode = ESM_ERROR, version = 0;

    /* diagnostic info */
    /*
    if ( getenv( "ESDBG" ) )
	printf( "esound getting standby mode\n" );
    */

    write( esd, &proto, sizeof(proto) );
    if ( write( esd, &version, sizeof(version) ) != sizeof(version) ) {
	return ESM_ERROR;
    }

    if ( read( esd, &mode, sizeof(mode) ) != sizeof(mode) )
	return ESM_ERROR;

    return mode;
}