-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.cpp
362 lines (295 loc) · 8.26 KB
/
monitor.cpp
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 <foobar2000.h>
#include "../helpers/window_placement_helper.h"
#include "config.h"
#include "resource.h"
#include "monitor.h"
#include <gme/Music_Emu.h>
critical_section lock;
class monitor_dialog * dialog = 0;
gme_t const* emu = 0;
bool changed_info = false;
pfc::string8 path;
pfc::array_t<const char *> voice_names;
bool changed_controls = false;
int mute_mask = 0;
static const GUID guid_cfg_placement = { 0x2ea726fb, 0x60b, 0x471c, { 0x99, 0x40, 0xdc, 0x9a, 0xf1, 0x4e, 0x51, 0x88 } };
static cfg_window_placement cfg_placement(guid_cfg_placement);
void monitor_start( gme_t * p_emu, const char * p_path )
{
insync( lock );
changed_info = true;
emu = p_emu;
path = p_path;
voice_names.set_size( p_emu->voice_count() );
for ( unsigned i = 0, j = p_emu->voice_count(); i < j; i++ )
voice_names [i] = p_emu->voice_name( i );
if ( cfg_control_override )
{
p_emu->set_tempo( static_cast<double> (cfg_control_tempo) / 10000 );
p_emu->mute_voices( mute_mask );
p_emu->ignore_silence();
}
}
void monitor_apply( gme_t * p_emu )
{
insync( lock );
bool enabled = !!cfg_control_override;
double t = enabled ? ( static_cast<double> (cfg_control_tempo) / 10000 ) : 1;
int mask = enabled ? mute_mask : 0;
p_emu->set_tempo( t );
p_emu->mute_voices( mask );
p_emu->ignore_silence( enabled );
}
void monitor_update( gme_t * p_emu )
{
insync( lock );
if ( emu == p_emu )
{
if ( changed_controls )
{
changed_controls = false;
bool enabled = !!cfg_control_override;
double t = enabled ? ( static_cast<double> (cfg_control_tempo) / 10000 ) : 1;
int mask = enabled ? mute_mask : 0;
p_emu->set_tempo( t );
p_emu->mute_voices( mask );
p_emu->ignore_silence( enabled );
}
}
}
void monitor_stop( const gme_t * p_emu )
{
insync( lock );
if ( emu == p_emu )
{
emu = 0;
changed_info = true;
path = "";
voice_names.set_size( 0 );
}
}
class monitor_dialog
{
HWND wnd;
static BOOL CALLBACK g_dialog_proc( HWND wnd, UINT msg, WPARAM wp, LPARAM lp )
{
monitor_dialog * ptr;
if ( msg == WM_INITDIALOG )
{
ptr = reinterpret_cast<monitor_dialog *> (lp);
uSetWindowLong( wnd, DWL_USER, lp );
}
else
{
ptr = reinterpret_cast<monitor_dialog *> ( uGetWindowLong( wnd, DWL_USER ) );
}
if ( ptr ) return ptr->dialog_proc( wnd, msg, wp, lp );
else return 0;
}
BOOL dialog_proc( HWND wnd, UINT msg, WPARAM wp, LPARAM lp )
{
switch ( msg )
{
case WM_INITDIALOG:
{
this->wnd = wnd;
uSendDlgItemMessage( wnd, IDC_OVERRIDE, BM_SETCHECK, cfg_control_override, 0 );
HWND w = GetDlgItem( wnd, IDC_TEMPO_SLIDER );
uSendMessage( w, TBM_SETRANGEMIN, FALSE, 2 );
uSendMessage( w, TBM_SETRANGEMAX, FALSE, 400 * 100 );
uSendMessage( w, TBM_SETPAGESIZE, 0, 1250 );
uSendMessage( w, TBM_SETPOS, TRUE, cfg_control_tempo );
/*for ( unsigned tick = 25 * 100; tick <= 400 * 100; tick += 25 * 100 )
uSendMessage( w, TBM_SETTIC, 0, tick );*/
{
insync( lock );
changed_info = false;
changed_controls = false;
update();
update_tempo();
}
SetTimer( wnd, 0, 100, 0 );
cfg_placement.on_window_creation(wnd);
modeless_dialog_manager::g_add( wnd );
}
return 1;
case WM_TIMER:
{
insync( lock );
if ( changed_info )
{
changed_info = false;
update();
}
}
break;
case WM_DESTROY:
{
modeless_dialog_manager::g_remove( wnd );
cfg_placement.on_window_destruction( wnd );
KillTimer( wnd, 0 );
uSetWindowLong( wnd, DWL_USER, 0 );
delete this;
dialog = 0;
}
break;
case WM_COMMAND:
if ( wp == IDCANCEL )
{
DestroyWindow( wnd );
}
else if ( wp == IDC_OVERRIDE )
{
insync( lock );
cfg_control_override = uSendMessage((HWND)lp,BM_GETCHECK,0,0);
BOOL enable = emu != 0 && cfg_control_override;
EnableWindow( GetDlgItem( wnd, IDC_TEMPO_CAPTION ), enable );
EnableWindow( GetDlgItem( wnd, IDC_TEMPO ), enable );
EnableWindow( GetDlgItem( wnd, IDC_TEMPO_SLIDER ), enable );
for ( unsigned i = 0, j = voice_names.get_size(); i < j; ++i )
{
EnableWindow( GetDlgItem( wnd, IDC_VOICE1 + i ), enable );
}
changed_controls = true;
}
else if ( wp == IDC_RESET )
{
insync( lock );
changed_controls = ( cfg_control_tempo != 100 * 100 ) || ( mute_mask != 0 );
cfg_control_tempo = 100 * 100;
mute_mask = 0;
if ( changed_controls )
{
uSendDlgItemMessage( wnd, IDC_TEMPO_SLIDER, TBM_SETPOS, TRUE, cfg_control_tempo );
update();
update_tempo();
}
}
else if ( wp - IDC_VOICE1 < 30 )
{
unsigned voice = wp - IDC_VOICE1;
unsigned mask = ~(1 << voice);
unsigned bit = uSendMessage((HWND)lp,BM_GETCHECK,0,0) ? 0 : ( 1 << voice );
insync( lock );
changed_controls = true;
mute_mask = ( mute_mask & mask ) | bit;
}
break;
case WM_HSCROLL:
{
unsigned t = uSendMessage((HWND)lp,TBM_GETPOS,0,0);
if ( t < 2 ) t = 2;
else if ( t > 400 * 100 ) t = 400 * 100;
insync( lock );
changed_controls = true;
cfg_control_tempo = t;
update_tempo();
}
break;
}
return 0;
}
void update()
{
pfc::string8 title;
if ( path.length() )
{
title = pfc::string_filename_ext( path );
title += " - ";
}
title += "Game Emu Player";
uSetWindowText( wnd, title );
BOOL enable = emu != 0 && cfg_control_override;
EnableWindow( GetDlgItem( wnd, IDC_TEMPO_CAPTION ), enable );
EnableWindow( GetDlgItem( wnd, IDC_TEMPO ), enable );
EnableWindow( GetDlgItem( wnd, IDC_TEMPO_SLIDER ), enable );
HWND w;
unsigned count = voice_names.get_size();
for ( unsigned i = 0; i < count; ++i )
{
w = GetDlgItem( wnd, IDC_VOICE1 + i );
uSendMessage( w, BM_SETCHECK, ! ( ( mute_mask >> i ) & 1 ) , 0 );
uSetWindowText( w, voice_names[ i ] );
EnableWindow( w, enable );
ShowWindow( w, SW_SHOWNA );
}
for ( unsigned i = count; i < 30; ++i )
{
w = GetDlgItem( wnd, IDC_VOICE1 + i );
uSendMessage( w, BM_SETCHECK, ! ( ( mute_mask >> i ) & 1 ) , 0 );
uSetWindowText( w, "" );
EnableWindow( w, 0 );
ShowWindow( w, SW_HIDE );
}
}
void update_tempo()
{
pfc::string8 text;
int tempo = cfg_control_tempo;
text << pfc::format_int( tempo / 100 ) << "." << pfc::format_int( tempo % 100, 2 ) << "%";
uSetDlgItemText( wnd, IDC_TEMPO, text );
}
public:
monitor_dialog( HWND parent )
{
wnd = 0;
if ( ! CreateDialogParam(core_api::get_my_instance(), MAKEINTRESOURCE(IDD_MONITOR), parent, g_dialog_proc, reinterpret_cast<LPARAM> (this) ) )
throw exception_win32( GetLastError() );
}
~monitor_dialog()
{
DestroyWindow( wnd );
}
};
class monitor_menu : public mainmenu_commands
{
virtual t_uint32 get_command_count()
{
return 1;
}
virtual GUID get_command(t_uint32 p_index)
{
// {ED5CA903-E477-48dd-9421-7E530B48FAF5}
static const GUID guid =
{ 0xed5ca903, 0xe477, 0x48dd, { 0x94, 0x21, 0x7e, 0x53, 0xb, 0x48, 0xfa, 0xf5 } };
return guid;
}
virtual void get_name(t_uint32 p_index,pfc::string_base & p_out)
{
p_out = "GEP control";
}
virtual bool get_description(t_uint32 p_index,pfc::string_base & p_out)
{
p_out = "Activates the Game Emu Player advanced controls window.";
return true;
}
virtual GUID get_parent()
{
return mainmenu_groups::view;
}
virtual bool get_display(t_uint32 p_index,pfc::string_base & p_text,t_uint32 & p_flags)
{
p_flags = 0;
get_name(p_index,p_text);
return true;
}
virtual void execute(t_uint32 p_index,service_ptr_t<service_base> p_callback)
{
if ( p_index == 0 && core_api::assert_main_thread() )
{
if ( !dialog )
{
try
{
dialog = new monitor_dialog( core_api::get_main_window() );
}
catch ( const std::exception & e )
{
dialog = 0;
console::error( e.what() );
}
}
}
}
};
static mainmenu_commands_factory_t <monitor_menu> g_mainmenu_commands_monitor_factory;