-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathi_midipipe.c
505 lines (422 loc) · 11.7 KB
/
i_midipipe.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//
// Copyright(C) 2013 James Haley et al.
// Copyright(C) 2017 Alex Mayfield
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// DESCRIPTION:
// Client Interface to Midi Server
//
#if _WIN32
#include <stdlib.h>
#include <sys/stat.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "i_midipipe.h"
#include "config.h"
#include "i_sound.h"
#include "i_timer.h"
#include "m_misc.h"
#include "net_packet.h"
#include "../midiproc/proto.h"
#if defined(_DEBUG)
#define DEBUGOUT(s) puts(s)
#else
#define DEBUGOUT(s)
#endif
//=============================================================================
//
// Public Data
//
// True if the midi proces was initialized at least once and has not been
// explicitly shut down. This remains true if the server is momentarily
// unreachable.
boolean midi_server_initialized = false;
// True if the current track is being handled via the MIDI server.
boolean midi_server_registered = false;
//=============================================================================
//
// Data
//
#define MIDIPIPE_MAX_WAIT 1000 // Max amount of ms to wait for expected data.
static HANDLE midi_process_in_reader; // Input stream for midi process.
static HANDLE midi_process_in_writer;
static HANDLE midi_process_out_reader; // Output stream for midi process.
static HANDLE midi_process_out_writer;
//=============================================================================
//
// Private functions
//
//
// FreePipes
//
// Free all pipes in use by this module.
//
static void FreePipes()
{
if (midi_process_in_reader != NULL)
{
CloseHandle(midi_process_in_reader);
midi_process_in_reader = NULL;
}
if (midi_process_in_writer != NULL)
{
CloseHandle(midi_process_in_writer);
midi_process_in_writer = NULL;
}
if (midi_process_out_reader != NULL)
{
CloseHandle(midi_process_out_reader);
midi_process_in_reader = NULL;
}
if (midi_process_out_writer != NULL)
{
CloseHandle(midi_process_out_writer);
midi_process_out_writer = NULL;
}
}
//
// UsingNativeMidi
//
// Enumerate all music decoders and return true if NATIVEMIDI is one of them.
//
// If this is the case, using the MIDI server is probably necessary. If not,
// we're likely using Timidity and thus don't need to start the server.
//
static boolean UsingNativeMidi()
{
int i;
int decoders = Mix_GetNumMusicDecoders();
for (i = 0; i < decoders; i++)
{
if (strcmp(Mix_GetMusicDecoder(i), "NATIVEMIDI") == 0)
{
return true;
}
}
return false;
}
//
// WritePipe
//
// Writes packet data to the subprocess' standard in.
//
static boolean WritePipe(net_packet_t *packet)
{
DWORD bytes_written;
BOOL ok = WriteFile(midi_process_in_writer, packet->data, packet->len,
&bytes_written, NULL);
return ok;
}
//
// ExpectPipe
//
// Expect the contents of a packet off of the subprocess' stdout. If the
// response is unexpected, or doesn't arrive within a specific amuont of time,
// assume the subprocess is in an unknown state.
//
static boolean ExpectPipe(net_packet_t *packet)
{
int start;
BOOL ok;
CHAR pipe_buffer[8192];
DWORD pipe_buffer_read = 0;
if (packet->len > sizeof(pipe_buffer))
{
// The size of the packet we're expecting is larger than our buffer
// size, so bail out now.
return false;
}
start = I_GetTimeMS();
do
{
// Wait until we see exactly the amount of data we expect on the pipe.
ok = PeekNamedPipe(midi_process_out_reader, NULL, 0, NULL,
&pipe_buffer_read, NULL);
if (!ok)
{
break;
}
else if (pipe_buffer_read < packet->len)
{
I_Sleep(1);
continue;
}
// Read precisely the number of bytes we're expecting, and no more.
ok = ReadFile(midi_process_out_reader, pipe_buffer, packet->len,
&pipe_buffer_read, NULL);
if (!ok || pipe_buffer_read != packet->len)
{
break;
}
// Compare our data buffer to the packet.
if (memcmp(packet->data, pipe_buffer, packet->len) != 0)
{
break;
}
return true;
// Continue looping as long as we don't exceed our maximum wait time.
} while (I_GetTimeMS() - start <= MIDIPIPE_MAX_WAIT);
// TODO: Deal with the wedged process?
return false;
}
//
// RemoveFileSpec
//
// A reimplementation of PathRemoveFileSpec that doesn't bring in Shlwapi
//
void RemoveFileSpec(TCHAR *path, size_t size)
{
TCHAR *fp = NULL;
fp = &path[size];
while (path <= fp && *fp != DIR_SEPARATOR)
{
fp--;
}
*(fp + 1) = '\0';
}
static boolean BlockForAck(void)
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(2);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_ACK);
ok = ExpectPipe(packet);
NET_FreePacket(packet);
return ok;
}
//=============================================================================
//
// Protocol Commands
//
//
// I_MidiPipe_RegisterSong
//
// Tells the MIDI subprocess to load a specific filename for playing. This
// function blocks until there is an acknowledgement from the server.
//
boolean I_MidiPipe_RegisterSong(char *filename)
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(64);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_REGISTER_SONG);
NET_WriteString(packet, filename);
ok = WritePipe(packet);
NET_FreePacket(packet);
midi_server_registered = false;
ok = ok && BlockForAck();
if (!ok)
{
DEBUGOUT("I_MidiPipe_RegisterSong failed");
return false;
}
midi_server_registered = true;
DEBUGOUT("I_MidiPipe_RegisterSong succeeded");
return true;
}
//
// I_MidiPipe_UnregisterSong
//
// Tells the MIDI subprocess to unload the current song.
//
void I_MidiPipe_UnregisterSong(void)
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(64);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_UNREGISTER_SONG);
ok = WritePipe(packet);
NET_FreePacket(packet);
ok = ok && BlockForAck();
if (!ok)
{
DEBUGOUT("I_MidiPipe_UnregisterSong failed");
return;
}
midi_server_registered = false;
DEBUGOUT("I_MidiPipe_UnregisterSong succeeded");
}
//
// I_MidiPipe_SetVolume
//
// Tells the MIDI subprocess to set a specific volume for the song.
//
void I_MidiPipe_SetVolume(int vol)
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(6);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_SET_VOLUME);
NET_WriteInt32(packet, vol);
ok = WritePipe(packet);
NET_FreePacket(packet);
ok = ok && BlockForAck();
if (!ok)
{
DEBUGOUT("I_MidiPipe_SetVolume failed");
return;
}
DEBUGOUT("I_MidiPipe_SetVolume succeeded");
}
//
// I_MidiPipe_PlaySong
//
// Tells the MIDI subprocess to play the currently loaded song.
//
void I_MidiPipe_PlaySong(int loops)
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(6);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_PLAY_SONG);
NET_WriteInt32(packet, loops);
ok = WritePipe(packet);
NET_FreePacket(packet);
ok = ok && BlockForAck();
if (!ok)
{
DEBUGOUT("I_MidiPipe_PlaySong failed");
return;
}
DEBUGOUT("I_MidiPipe_PlaySong succeeded");
}
//
// I_MidiPipe_StopSong
//
// Tells the MIDI subprocess to stop playing the currently loaded song.
//
void I_MidiPipe_StopSong()
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(2);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_STOP_SONG);
ok = WritePipe(packet);
NET_FreePacket(packet);
ok = ok && BlockForAck();
if (!ok)
{
DEBUGOUT("I_MidiPipe_StopSong failed");
return;
}
DEBUGOUT("I_MidiPipe_StopSong succeeded");
}
//
// I_MidiPipe_ShutdownServer
//
// Tells the MIDI subprocess to shutdown.
//
void I_MidiPipe_ShutdownServer()
{
boolean ok;
net_packet_t *packet;
packet = NET_NewPacket(2);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_SHUTDOWN);
ok = WritePipe(packet);
NET_FreePacket(packet);
ok = ok && BlockForAck();
FreePipes();
midi_server_initialized = false;
if (!ok)
{
DEBUGOUT("I_MidiPipe_ShutdownServer failed");
return;
}
DEBUGOUT("I_MidiPipe_ShutdownServer succeeded");
}
//=============================================================================
//
// Public Interface
//
//
// I_MidiPipeInitServer
//
// Start up the MIDI server.
//
boolean I_MidiPipe_InitServer()
{
TCHAR dirname[MAX_PATH + 1];
DWORD dirname_len;
char *module = NULL;
char *cmdline = NULL;
char params_buf[128];
SECURITY_ATTRIBUTES sec_attrs;
PROCESS_INFORMATION proc_info;
STARTUPINFO startup_info;
BOOL ok;
if (!UsingNativeMidi() || strlen(snd_musiccmd) > 0)
{
// If we're not using native MIDI, or if we're playing music through
// an exteranl program, we don't need to start the server.
return false;
}
// Get directory name
memset(dirname, 0, sizeof(dirname));
dirname_len = GetModuleFileName(NULL, dirname, MAX_PATH);
if (dirname_len == 0)
{
return false;
}
RemoveFileSpec(dirname, dirname_len);
// Define the module.
module = PROGRAM_PREFIX "midiproc.exe";
// Set up pipes
memset(&sec_attrs, 0, sizeof(SECURITY_ATTRIBUTES));
sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
sec_attrs.bInheritHandle = TRUE;
sec_attrs.lpSecurityDescriptor = NULL;
if (!CreatePipe(&midi_process_in_reader, &midi_process_in_writer, &sec_attrs, 0))
{
DEBUGOUT("Could not initialize midiproc stdin");
return false;
}
if (!SetHandleInformation(midi_process_in_writer, HANDLE_FLAG_INHERIT, 0))
{
DEBUGOUT("Could not disinherit midiproc stdin");
return false;
}
if (!CreatePipe(&midi_process_out_reader, &midi_process_out_writer, &sec_attrs, 0))
{
DEBUGOUT("Could not initialize midiproc stdout/stderr");
return false;
}
if (!SetHandleInformation(midi_process_out_reader, HANDLE_FLAG_INHERIT, 0))
{
DEBUGOUT("Could not disinherit midiproc stdin");
return false;
}
// Define the command line. Version, Sample Rate, and handles follow
// the executable name.
M_snprintf(params_buf, sizeof(params_buf), "%d %Iu %Iu",
snd_samplerate, (size_t) midi_process_in_reader, (size_t) midi_process_out_writer);
cmdline = M_StringJoin(module, " \"" PACKAGE_STRING "\"", " ", params_buf, NULL);
// Launch the subprocess
memset(&proc_info, 0, sizeof(proc_info));
memset(&startup_info, 0, sizeof(startup_info));
startup_info.cb = sizeof(startup_info);
ok = CreateProcess(TEXT(module), TEXT(cmdline), NULL, NULL, TRUE,
0, NULL, dirname, &startup_info, &proc_info);
if (!ok)
{
FreePipes();
free(cmdline);
return false;
}
// Since the server has these handles, we don't need them anymore.
CloseHandle(midi_process_in_reader);
midi_process_in_reader = NULL;
CloseHandle(midi_process_out_writer);
midi_process_out_writer = NULL;
midi_server_initialized = true;
return true;
}
#endif