OSDN Git Service

[playmidi] Fix buffer overflow when more than 32 voices get selected
[timidity41/timidity41.git] / kbtimsetup / kbtimsetup.c
1
2
3
4 #include "timidity.c"
5
6 #include <tchar.h>
7 #include "w32g.h"
8 #include "w32g_res.h"
9 #include "w32g_utl.h"
10 #include "w32g_pref.h"
11
12
13 // \83G\83\89\81[\89ñ\94ð\97p (w32g_c.c, w32g_i.c, w32g_ut2.c \93\99\82Ì\91ã\91Ö
14 int PlayerLanguage = LANGUAGE_JAPANESE; //LANGUAGE_ENGLISH;
15 int IniFileAutoSave = 1;
16 char *IniFile;
17 char *ConfigFile;
18 char *PlaylistFile;
19 char *PlaylistHistoryFile;
20 char *MidiFileOpenDir;
21 char *ConfigFileOpenDir;
22 char *PlaylistFileOpenDir;
23 int SecondMode = 0;
24 BOOL PosSizeSave = TRUE;
25 int DocMaxSize;
26 char *DocFileExt;
27 int AutoloadPlaylist = 0;
28 int AutosavePlaylist = 0;
29 int SeachDirRecursive = 0;
30 int DocWndIndependent = 0;
31 int DocWndAutoPopup = 0;
32 int TraceGraphicFlag;
33 int PlayerThreadPriority;
34 int MidiPlayerThreadPriority;
35 int MainThreadPriority;
36 int GUIThreadPriority;
37 int TracerThreadPriority;
38 int WrdThreadPriority;
39 int SubWindowMax = 6;
40 int InitMinimizeFlag = 0;
41 int main_panel_update_time = 10;
42 int DebugWndStartFlag = 1;
43 int ConsoleWndStartFlag = 0;
44 int ListWndStartFlag = 0;
45 int TracerWndStartFlag = 0;
46 int DocWndStartFlag = 0;
47 int WrdWndStartFlag = 0;
48 int SoundSpecWndStartFlag = 0;
49 int DebugWndFlag = 1;
50 int ConsoleWndFlag = 1;
51 int ListWndFlag = 1;
52 int TracerWndFlag = 0;
53 int DocWndFlag = 1;
54 int WrdWndFlag = 0;
55 int SoundSpecWndFlag = 0;
56 int WrdGraphicFlag;
57 int TraceGraphicFlag;
58 char *w32g_output_dir = NULL;
59 int w32g_auto_output_mode = 0;
60 int w32g_lock_open_file = 0;
61 WRDTracer w32g_wrdt_mode;
62 int playlist_max = 1;
63 int playlist_max_ini = 1;
64 int ConsoleClearFlag = 0;
65 // HWND
66 HWND hMainWnd = 0;
67 HWND hDebugWnd = 0;
68 HWND hConsoleWnd = 0;
69 HWND hTracerWnd = 0;
70 HWND hDocWnd = 0;
71 HWND hListWnd = 0;
72 HWND hWrdWnd = 0;
73 HWND hSoundSpecWnd = 0;
74 HWND hDebugEditWnd = 0;
75 HWND hDocEditWnd = 0;
76 HWND hUrlWnd = 0;
77 // Process.
78 HANDLE hProcess = 0;
79 // Main Thread.
80 HANDLE hMainThread = 0;
81 HANDLE hPlayerThread = 0;
82 HANDLE hMainThreadInfo = 0;
83 DWORD dwMainThreadID = 0;
84
85
86
87
88 void TracerWndApplyQuietChannel(ChannelBitMask quietchannels_){}
89 void w32g_restart(void){}
90 void w32g_send_rc(int rc, ptr_size_t value){}
91 void w32g_i_init(void){}
92 void w32g_ext_control_main_thread(int rc, ptr_size_t value){}
93
94 // Control funcitons
95 static int ctl_open(int using_stdin, int using_stdout){}
96 static void ctl_close(void){}
97 static int ctl_pass_playing_list(int number_of_files, char *list_of_files[]){}
98 static void ctl_event(CtlEvent *e){}
99 static int ctl_read(ptr_size_t *valp){}
100 static int cmsg(int type, int verbosity_level, char *fmt, ...){}
101 #define ctl w32gui_control_mode
102 #define CTL_STATUS_UPDATE -98
103 ControlMode ctl=
104 {
105     "Win32 GUI interface", 'w',
106     "w32gui",
107     1,1,0,
108     CTLF_AUTOSTART | CTLF_DRAG_START,
109     ctl_open,
110     ctl_close,
111     ctl_pass_playing_list,
112     ctl_read,
113     NULL,
114     cmsg,
115     ctl_event
116 };
117
118
119 // \82±\82±\82©\82çsetup\82Ì\83\81\83C\83\93\95\94\95ª
120
121 char IniPath[FILEPATH_MAX] = "";
122
123
124 // w32_utl.c \82Å\8eg\97p
125 void get_ini_path(char *ini)
126 {
127         if(ini)
128                 strcpy(ini, (const char *)&IniPath);
129 }
130
131
132 // timidity_start_initialize()
133 void config_gui_start_initialize(void)
134 {
135         int i;
136     static int drums[] = DEFAULT_DRUMCHANNELS;
137     static int is_first = 1;
138
139 #if defined(__FreeBSD__) && !defined(__alpha__)
140     fp_except_t fpexp;
141 #elif defined(__NetBSD__) || defined(__OpenBSD__)
142     fp_except fpexp;
143 #endif
144 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
145     fpexp = fpgetmask();
146     fpsetmask(fpexp & ~(FP_X_INV|FP_X_DZ));
147 #endif
148     if(!output_text_code)
149                 output_text_code = safe_strdup(OUTPUT_TEXT_CODE);
150     if(!opt_aq_max_buff)
151                 opt_aq_max_buff = safe_strdup("5.0");
152     if(!opt_aq_fill_buff)
153                 opt_aq_fill_buff = safe_strdup("100%"); 
154     if(!opt_reduce_voice_threshold)
155                 opt_reduce_voice_threshold = safe_strdup("75%");
156     if(!opt_reduce_quality_threshold)
157                 opt_reduce_quality_threshold = safe_strdup("99%");
158     if(!opt_reduce_polyphony_threshold)
159                 opt_reduce_polyphony_threshold = safe_strdup("85%");
160
161     /* Check the byte order */
162     i = 1;
163 #ifdef LITTLE_ENDIAN
164     if(*(char *)&i != 1)
165 #else
166     if(*(char *)&i == 1)
167 #endif
168     {
169                 fprintf(stderr, "Byte order is miss configured.\n");
170                 exit(1);
171     }
172     for(i = 0; i < MAX_CHANNELS; i++)
173                 memset(&(channel[i]), 0, sizeof(Channel));
174     CLEAR_CHANNELMASK(quietchannels);
175     CLEAR_CHANNELMASK(default_drumchannels);
176     for(i = 0; drums[i] > 0; i++)
177                 SET_CHANNELMASK(default_drumchannels, drums[i] - 1);
178 #if MAX_CHANNELS > 16
179     for(i = 16; i < MAX_CHANNELS; i++)
180                 if(IS_SET_CHANNELMASK(default_drumchannels, i & 0xF))
181                         SET_CHANNELMASK(default_drumchannels, i);
182 #endif
183     if(program_name == NULL)
184                 program_name = "TiMidity";
185     uudecode_unquote_html = 1;
186     for(i = 0; i < MAX_CHANNELS; i++){
187                 default_program[i] = DEFAULT_PROGRAM;
188                 special_program[i] = -1;
189                 memset(channel[i].drums, 0, sizeof(channel[i].drums));
190     }
191     if(play_mode == NULL)
192                 play_mode = &null_play_mode;
193
194 }
195
196 // main() start
197 void config_gui_main(void)
198 {
199 #if defined(TIMIDITY_LEAK_CHECK)
200         _CrtSetDbgFlag(CRTDEBUGFLAGS);
201 #endif
202         OverrideSFSettingLoad();
203 #if defined(VST_LOADER_ENABLE)
204         if (hVSTHost == NULL) {         
205                 // ini(=kpi)\82Ì\83f\83B\83\8c\83N\83g\83\8a\82É\82 \82é timvstwrap.dll
206                 int i, last = 0;
207                 char WrapPath[FILEPATH_MAX] = "";
208                 for(i = 0; i < FILEPATH_MAX; i++){
209                         if(IniPath[i] == '\0')
210                                 break;
211                         else if(IniPath[i] == '\\')
212                                 last = i;
213                 }
214                 if(last){
215                         int j = 0;
216                         for(i = 0; i < last; i++){
217                                 WrapPath[j++] = IniPath[i];
218                         }
219                         WrapPath[j] = '\0';
220 #ifdef _WIN64
221                         strcat(WrapPath, "\\timvstwrap_x64.dll\0");
222 #else
223                         strcat(WrapPath, "\\timvstwrap.dll\0");
224 #endif
225                 }
226                 if(WrapPath[0] != 0)
227                         hVSTHost = LoadLibrary(WrapPath);
228                 if (hVSTHost == NULL){
229 #ifdef _WIN64
230                         hVSTHost = LoadLibrary("timvstwrap_x64.dll");
231 #else
232                         hVSTHost = LoadLibrary("timvstwrap.dll");
233 #endif
234                 }
235
236                 if (hVSTHost != NULL) {
237                         ((vst_open)GetProcAddress(hVSTHost, "vstOpen"))();
238                 }
239         }
240 #endif  
241 #if defined(__W32__) && !defined(WINDRV)
242         (void)w32_reset_dll_directory();
243 #endif
244         // timidity_start_initialize()
245         config_gui_start_initialize();
246 }
247
248 // main() end
249 void config_gui_main_close(void)
250 {
251         int i;
252 #ifdef SUPPORT_SOCKET
253         safe_free(url_user_agent);
254         url_user_agent = NULL;
255         safe_free(url_http_proxy_host);
256         url_http_proxy_host = NULL;
257         safe_free(url_ftp_proxy_host);
258         url_ftp_proxy_host = NULL;
259         safe_free(user_mailaddr);
260         user_mailaddr = NULL;
261 #endif
262 #ifdef IA_DYNAMIC
263         safe_free(dynamic_lib_root);
264         dynamic_lib_root = NULL;
265 #endif
266         safe_free(pcm_alternate_file);
267         pcm_alternate_file = NULL;
268         safe_free(opt_output_name);
269         opt_output_name = NULL;
270         safe_free(opt_aq_max_buff);
271         opt_aq_max_buff = NULL;
272         safe_free(opt_aq_fill_buff);
273         opt_aq_fill_buff = NULL;
274         safe_free(opt_reduce_voice_threshold);
275         opt_reduce_voice_threshold = NULL;
276         safe_free(opt_reduce_quality_threshold);
277         opt_reduce_quality_threshold = NULL;
278         safe_free(opt_reduce_polyphony_threshold);
279         opt_reduce_polyphony_threshold = NULL;
280         safe_free(output_text_code);
281         output_text_code = NULL;
282         safe_free(wrdt_open_opts);
283         wrdt_open_opts = NULL;
284         free_soft_queue();
285         free_audio_bucket();
286         free_instruments(0);
287         free_soundfonts();
288         free_cache_data();
289         free_freq_data();
290         free_wrd();
291         free_readmidi();
292         free_playmidi();
293         free_mix_c();
294         free_global_mblock();
295         tmdy_free_config();
296         //free_reverb_buffer();
297         free_effect_buffers();
298 #ifdef INT_SYNTH
299         free_int_synth();
300 #endif // INT_SYNTH
301         free_voices();
302         uninitialize_resampler_coeffs();
303         for (i = 0; i < MAX_CHANNELS; i++)
304                 free_drum_effect(i);
305 #ifdef VST_LOADER_ENABLE
306         if (hVSTHost != NULL) {
307                 // only load , no save
308                 ((vst_close)GetProcAddress(hVSTHost,"vstClose"))();
309                 FreeLibrary(hVSTHost);
310                 hVSTHost = NULL;
311         }
312 #endif
313 }
314
315
316
317
318 HINSTANCE hInst = NULL;
319 HWND hConfigWnd = NULL;
320
321 void set_config_hwnd(HWND hwnd)
322 {
323         if(hwnd)
324                 hConfigWnd = hwnd;
325 }
326
327 // Create Window
328 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
329 {
330         HICON *hIcon;
331
332         hInst = hInstance;
333         hIcon = (HICON *)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON_TIMIDITY), IMAGE_ICON, 16, 16, 0);
334         if ( hConfigWnd != NULL ) {
335                 DestroyWindow ( hConfigWnd );
336                 hConfigWnd = NULL;
337         }       
338         // Create Window
339         PrefWndCreate(NULL, 0); // set hConfigWnd       
340         if (hIcon!=NULL)
341                 SendMessage(hConfigWnd, WM_SETICON,FALSE, (LPARAM)hIcon);
342         {  // Set the title of the main window again.
343                 char buffer[256];
344                 SendMessage( hConfigWnd, WM_GETTEXT, (WPARAM)255, (LPARAM)buffer);
345                 SendMessage( hConfigWnd, WM_SETTEXT, (WPARAM)0, (LPARAM)buffer);
346         }
347         if (!hConfigWnd)
348                 return FALSE;
349         ShowWindow(hConfigWnd, nCmdShow);
350         UpdateWindow(hConfigWnd);
351         return TRUE;
352 }
353
354 int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
355 {
356         static int init_flg = 0;
357         MSG msg;
358         HACCEL hAccelTable;
359         
360         if(init_flg)
361                 return FALSE;
362         init_flg = 1;
363         if(lpCmdLine[0] == 0){
364                 // \8bó\82Ì\8fê\8d\87 kbtimsetup.exe\82Ì\83f\83B\83\8c\83N\83g\83\8a\82É\82 \82é kbtim.ini
365                 char buffer[FILEPATH_MAX] = {0};
366                 char *p;
367                 HMODULE hInst = GetModuleHandle(0);             
368                 if(GetModuleFileName(hInst, buffer, FILEPATH_MAX - 1)){
369                         if((p = pathsep_strrchr(buffer)) != NULL){
370                                 p++;
371                                 *p = '\0';
372                         }else{
373                                 buffer[0] = '.';
374                                 buffer[1] = PATH_SEP;
375                                 buffer[2] = '\0';
376                         }
377                 }else{
378                         buffer[0] = '.';
379                         buffer[1] = PATH_SEP;
380                         buffer[2] = '\0';
381                 }
382                 strncpy(IniPath, buffer, FILEPATH_MAX);
383                 IniPath[FILEPATH_MAX - 1] = '\0';
384                 strcat(IniPath,"kbtim.ini");
385         }else{
386                 // ini\8eæ\93¾ \83v\83\89\83O\83C\83\93\82Æ\83t\83@\83C\83\8b\96¼\82ª\93¯\82\82Å\8ag\92£\8eq\82ª INI   
387                 int i, last = 0;
388                 for(i = 0; i < FILEPATH_MAX; i++){
389                         if(lpCmdLine[i] == '\0')
390                                 break;
391                         else if(lpCmdLine[i] == '.')
392                                 last = i;
393                 }
394                 if(last){
395                         int j = 0;
396                         for(i = 0; i < last; i++){
397                                 if(lpCmdLine[i] != '"')
398                                         IniPath[j++] = lpCmdLine[i];
399                         }
400                         IniPath[j] = '\0';
401                         strcat(IniPath, ".ini\0");
402                 }
403         }
404         if (!InitInstance (hInstance, nCmdShow)) // Create Window
405                 return FALSE;
406 #if 0 // accel
407         hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CONFIG_GUI));     
408         while (GetMessage(&msg, NULL, 0, 0)){ // message loop
409                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){
410                         TranslateMessage(&msg);
411                         DispatchMessage(&msg);
412                 }
413         }
414 #else
415         while( GetMessage(&msg,NULL,0,0) ){ // message loop
416                 TranslateMessage(&msg);
417                 DispatchMessage(&msg);
418         }
419 #endif
420         return (int) msg.wParam;
421 }
422
423