1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20     w32g_utl.c: written by Daisuke Aoki <dai@y7.net>
21                            Masanao Izumo <mo@goice.co.jp>
22 */
23 
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif /* HAVE_CONFIG_H */
28 #include "interface.h"
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #ifndef NO_STRING_H
33 #include <string.h>
34 #else
35 #include <strings.h>
36 #endif
37 #include <ctype.h>
38 
39 #include "timidity.h"
40 #include "common.h"
41 #include "instrum.h"
42 #include "playmidi.h"
43 #include "readmidi.h"
44 #include "reverb.h"
45 #include "output.h"
46 #include "controls.h"
47 #include "recache.h"
48 #include "tables.h"
49 #ifdef SUPPORT_SOUNDSPEC
50 #include "soundspec.h"
51 #endif /* SUPPORT_SOUNDSPEC */
52 #include "wrd.h"
53 #include "w32g.h"
54 #include "w32g_utl.h"
55 #include <sys/stat.h>
56 #include "strtab.h"
57 #include "url.h"
58 
59 extern int opt_default_mid;
60 extern int effect_lr_mode;
61 extern int effect_lr_delay_msec;
62 extern char def_instr_name[];
63 extern int opt_control_ratio;
64 extern char *opt_aq_max_buff;
65 extern char *opt_aq_fill_buff;
66 extern int opt_aq_fill_buff_free_needed;
67 extern int opt_evil_mode;
68 extern int opt_buffer_fragments;
69 extern int32 opt_output_rate;
70 extern int PlayerLanguage;
71 extern volatile int data_block_bits;
72 extern volatile int data_block_num;
73 extern int DocWndIndependent;
74 extern int DocWndAutoPopup;
75 extern int SeachDirRecursive;
76 extern int IniFileAutoSave;
77 extern int SecondMode;
78 extern int AutoloadPlaylist;
79 extern int AutosavePlaylist;
80 char DefaultPlaylistName[] = "default.pls";
81 char DefaultPlaylistPath[1024];
82 
83 extern int PosSizeSave;
84 
85 //*****************************************************************************/
86 // ini
87 
88 // INI file
89 CHAR *INI_INVALID = "INVALID PARAMETER";
90 CHAR *INI_SEC_PLAYER = "PLAYER";
91 CHAR *INI_SEC_TIMIDITY = "TIMIDITY";
92 #define INI_MAXLEN 1024
93 
94 int
IniGetKeyInt32(char * section,char * key,int32 * n)95 IniGetKeyInt32(char *section, char *key,int32 *n)
96 {
97   CHAR buffer[INI_MAXLEN];
98   GetPrivateProfileString
99     (section,key,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
100   if(strcasecmp(buffer,INI_INVALID)){
101     *n =atol(buffer);
102     return 0;
103   } else
104     return 1;
105 }
106 
107 int
IniGetKeyInt(char * section,char * key,int * n)108 IniGetKeyInt(char *section, char *key,int *n)
109 {
110   CHAR buffer[INI_MAXLEN];
111   GetPrivateProfileString
112     (section,key,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
113   if(strcasecmp(buffer,INI_INVALID)){
114     *n =atoi(buffer);
115     return 0;
116   } else
117     return 1;
118 }
119 
120 int
IniGetKeyInt8(char * section,char * key,int8 * n)121 IniGetKeyInt8(char *section, char *key,int8 *n)
122 {
123   CHAR buffer[INI_MAXLEN];
124   GetPrivateProfileString
125     (section,key,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
126   if(strcasecmp(buffer,INI_INVALID)){
127     *n = (int8)atoi(buffer);
128     return 0;
129   } else
130     return 1;
131 }
132 
133 int
IniGetKeyInt32Array(char * section,char * key,int32 * n,int arraysize)134 IniGetKeyInt32Array(char *section, char *key, int32 *n, int arraysize)
135 {
136   int i;
137   int ret = 0;
138   CHAR buffer[INI_MAXLEN];
139   char keybuffer[INI_MAXLEN];
140   for(i=0;i<arraysize;i++){
141     sprintf(keybuffer,"%s%d",key,i);
142     GetPrivateProfileString
143       (section,keybuffer,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
144     if(strcasecmp(buffer,INI_INVALID))
145       n[i] =atol(buffer);
146     else
147       ret++;
148   }
149   return ret;
150 }
151 
152 int
IniGetKeyIntArray(char * section,char * key,int * n,int arraysize)153 IniGetKeyIntArray(char *section, char *key, int *n, int arraysize)
154 {
155   int i;
156   int ret = 0;
157   CHAR buffer[INI_MAXLEN];
158   char keybuffer[INI_MAXLEN];
159   for(i=0;i<arraysize;i++){
160     sprintf(keybuffer,"%s%d",key,i);
161     GetPrivateProfileString
162       (section,keybuffer,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
163     if(strcasecmp(buffer,INI_INVALID))
164       n[i] =atol(buffer);
165     else
166       ret++;
167   }
168   return ret;
169 }
170 
171 int
IniGetKeyFloat(char * section,char * key,FLOAT_T * n)172 IniGetKeyFloat(char *section, char *key, FLOAT_T *n)
173 {
174     CHAR buffer[INI_MAXLEN];
175     GetPrivateProfileString(section, key, INI_INVALID, buffer,
176 			    INI_MAXLEN-1, IniFile);
177     if(strcasecmp(buffer, INI_INVALID))
178     {
179 	*n = (FLOAT_T)atof(buffer);
180 	return 0;
181     }
182     else
183 	return 1;
184 }
185 
186 int
IniGetKeyChar(char * section,char * key,char * c)187 IniGetKeyChar(char *section, char *key, char *c)
188 {
189   char buffer[64];
190   if(IniGetKeyStringN(section,key,buffer,60))
191     return 1;
192   else {
193     *c = buffer[0];
194     return 0;
195   }
196 }
197 
198 int
IniGetKeyString(char * section,char * key,char * str)199 IniGetKeyString(char *section, char *key,char *str)
200 {
201   CHAR buffer[INI_MAXLEN];
202   GetPrivateProfileString
203     (section,key,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
204   if(strcasecmp(buffer,INI_INVALID)){
205     strcpy(str,buffer);
206     return 0;
207   } else
208     return 1;
209 }
210 
211 int
IniGetKeyStringN(char * section,char * key,char * str,int size)212 IniGetKeyStringN(char *section, char *key,char *str, int size)
213 {
214   CHAR buffer[INI_MAXLEN];
215   GetPrivateProfileString
216     (section,key,INI_INVALID,buffer,INI_MAXLEN-1,IniFile);
217   if(strcasecmp(buffer,INI_INVALID)){
218     strncpy(str,buffer,size);
219     return 0;
220   }	else
221     return 1;
222 }
223 
224 int
IniPutKeyInt32(char * section,char * key,int32 * n)225 IniPutKeyInt32(char *section, char *key,int32 *n)
226 {
227   CHAR buffer[INI_MAXLEN];
228   sprintf(buffer,"%ld",*n);
229   WritePrivateProfileString
230     (section,key,buffer,IniFile);
231   return 0;
232 }
233 
234 int
IniPutKeyInt(char * section,char * key,int * n)235 IniPutKeyInt(char *section, char *key,int *n)
236 {
237   CHAR buffer[INI_MAXLEN];
238   sprintf(buffer,"%ld",*n);
239   WritePrivateProfileString
240     (section,key,buffer,IniFile);
241   return 0;
242 }
243 
244 int
IniPutKeyInt8(char * section,char * key,int8 * n)245 IniPutKeyInt8(char *section, char *key,int8 *n)
246 {
247   CHAR buffer[INI_MAXLEN];
248   sprintf(buffer,"%ld",(int)(*n));
249   WritePrivateProfileString
250     (section,key,buffer,IniFile);
251   return 0;
252 }
253 
254 int
IniPutKeyInt32Array(char * section,char * key,int32 * n,int arraysize)255 IniPutKeyInt32Array(char *section, char *key, int32 *n, int arraysize)
256 {
257   int i;
258   CHAR buffer[INI_MAXLEN];
259   CHAR keybuffer[INI_MAXLEN];
260   for(i=0;i<arraysize;i++){
261     sprintf(buffer,"%ld",n[i]);
262     sprintf(keybuffer,"%s%d",key,i);
263     WritePrivateProfileString(section,keybuffer,buffer,IniFile);
264   }
265   return 0;
266 }
267 
268 int
IniPutKeyIntArray(char * section,char * key,int * n,int arraysize)269 IniPutKeyIntArray(char *section, char *key, int *n, int arraysize)
270 {
271   int i;
272   CHAR buffer[INI_MAXLEN];
273   CHAR keybuffer[INI_MAXLEN];
274   for(i=0;i<arraysize;i++){
275     sprintf(buffer,"%ld",n[i]);
276     sprintf(keybuffer,"%s%d",key,i);
277     WritePrivateProfileString(section,keybuffer,buffer,IniFile);
278   }
279   return 0;
280 }
281 
282 int
IniPutKeyChar(char * section,char * key,char * c)283 IniPutKeyChar(char *section, char *key, char *c)
284 {
285   char buffer[64];
286   sprintf(buffer,"%c",*c);
287   return IniPutKeyStringN(section,key,buffer,60);
288 }
289 
290 int
IniPutKeyString(char * section,char * key,char * str)291 IniPutKeyString(char *section, char *key, char *str)
292 {
293   WritePrivateProfileString(section,key,str,IniFile);
294   return 0;
295 }
296 
297 int
IniPutKeyStringN(char * section,char * key,char * str,int size)298 IniPutKeyStringN(char *section, char *key, char *str, int size)
299 {
300   WritePrivateProfileString(section,key,str,IniFile);
301   return 0;
302 }
303 
304 int
IniPutKeyFloat(char * section,char * key,FLOAT_T n)305 IniPutKeyFloat(char *section, char *key,FLOAT_T n)
306 {
307     CHAR buffer[INI_MAXLEN];
308     sprintf(buffer,"%f", (double)n);
309     WritePrivateProfileString(section, key, buffer, IniFile);
310     return 0;
311 }
312 
IniFlush(void)313 void IniFlush(void)
314 {
315 	WritePrivateProfileString(NULL,NULL,NULL,IniFile);
316 }
317 
318 // LoadIniFile() , SaveIniFile()
319 // ***************************************************************************
320 // Setting
321 
322 #define SetFlag(flag) (!!(flag))
323 
SetValue(int32 value,int32 min,int32 max)324 static long SetValue(int32 value, int32 min, int32 max)
325 {
326   int32 v = value;
327   if(v < min) v = min;
328   else if( v > max) v = max;
329   return v;
330 }
331 
332 void
ApplySettingPlayer(SETTING_PLAYER * sp)333 ApplySettingPlayer(SETTING_PLAYER *sp)
334 {
335   InitMinimizeFlag = SetFlag(sp->InitMinimizeFlag);
336   DebugWndStartFlag = SetFlag(sp->DebugWndStartFlag);
337   ConsoleWndStartFlag = SetFlag(sp->ConsoleWndStartFlag);
338   ListWndStartFlag = SetFlag(sp->ListWndStartFlag);
339   TracerWndStartFlag = SetFlag(sp->TracerWndStartFlag);
340   DocWndStartFlag = SetFlag(sp->DocWndStartFlag);
341   WrdWndStartFlag = SetFlag(sp->WrdWndStartFlag);
342   DebugWndFlag = SetFlag(sp->DebugWndFlag);
343   ConsoleWndFlag = SetFlag(sp->ConsoleWndFlag);
344   ListWndFlag = SetFlag(sp->ListWndFlag);
345   TracerWndFlag = SetFlag(sp->TracerWndFlag);
346   DocWndFlag = SetFlag(sp->DocWndFlag);
347   WrdWndFlag = SetFlag(sp->WrdWndFlag);
348   SoundSpecWndFlag = SetFlag(sp->SoundSpecWndFlag);
349   SubWindowMax = SetValue(sp->SubWindowMax,1,10);
350   strncpy(ConfigFile,sp->ConfigFile,MAXPATH + 31);
351   ConfigFile[MAXPATH + 31] = '\0';
352   strncpy(PlaylistFile,sp->PlaylistFile,MAXPATH + 31);
353   PlaylistFile[MAXPATH + 31] = '\0';
354   strncpy(PlaylistHistoryFile,sp->PlaylistHistoryFile,MAXPATH + 31);
355   PlaylistHistoryFile[MAXPATH + 31] = '\0';
356   strncpy(MidiFileOpenDir,sp->MidiFileOpenDir,MAXPATH + 31);
357   MidiFileOpenDir[MAXPATH + 31] = '\0';
358   strncpy(ConfigFileOpenDir,sp->ConfigFileOpenDir,MAXPATH + 31);
359   ConfigFileOpenDir[MAXPATH + 31] = '\0';
360   strncpy(PlaylistFileOpenDir,sp->PlaylistFileOpenDir,MAXPATH + 31);
361   PlaylistFileOpenDir[MAXPATH + 31] = '\0';
362   PlayerThreadPriority = sp->PlayerThreadPriority;
363   GUIThreadPriority = sp->GUIThreadPriority;
364   TraceGraphicFlag = SetFlag(sp->TraceGraphicFlag);
365   // fonts ...
366   SystemFontSize = sp->SystemFontSize;
367   PlayerFontSize = sp->PlayerFontSize;
368   WrdFontSize = sp->WrdFontSize;
369   DocFontSize = sp->DocFontSize;
370   ListFontSize = sp->ListFontSize;
371   TracerFontSize = sp->TracerFontSize;
372   strncpy(SystemFont,sp->SystemFont,255);
373   SystemFont[255] = '\0';
374   strncpy(PlayerFont,sp->PlayerFont,255);
375   PlayerFont[255] = '\0';
376   strncpy(WrdFont,sp->WrdFont,255);
377   WrdFont[255] = '\0';
378   strncpy(DocFont,sp->DocFont,255);
379   DocFont[255] = '\0';
380   strncpy(ListFont,sp->ListFont,255);
381   ListFont[255] = '\0';
382   strncpy(TracerFont,sp->TracerFont,255);
383   TracerFont[255] = '\0';
384   // Apply font functions ...
385 
386   DocMaxSize = sp->DocMaxSize;
387   strncpy(DocFileExt,sp->DocFileExt,255);
388   DocFileExt[255] = '\0';
389   PlayerLanguage = sp->PlayerLanguage;
390   DocWndIndependent = sp->DocWndIndependent;
391   DocWndAutoPopup = sp->DocWndAutoPopup;
392   SeachDirRecursive = sp->SeachDirRecursive;
393   IniFileAutoSave = sp->IniFileAutoSave;
394   SecondMode = sp->SecondMode;
395   AutoloadPlaylist = sp->AutoloadPlaylist;
396   AutosavePlaylist = sp->AutosavePlaylist;
397   PosSizeSave = sp->PosSizeSave;
398 }
399 
400 void
SaveSettingPlayer(SETTING_PLAYER * sp)401 SaveSettingPlayer(SETTING_PLAYER *sp)
402 {
403   sp->InitMinimizeFlag = SetFlag(InitMinimizeFlag);
404   sp->DebugWndStartFlag = SetFlag(DebugWndStartFlag);
405   sp->ConsoleWndStartFlag = SetFlag(ConsoleWndStartFlag);
406   sp->ListWndStartFlag = SetFlag(ListWndStartFlag);
407   sp->TracerWndStartFlag = SetFlag(TracerWndStartFlag);
408   sp->DocWndStartFlag = SetFlag(DocWndStartFlag);
409   sp->WrdWndStartFlag = SetFlag(WrdWndStartFlag);
410   sp->DebugWndFlag = SetFlag(DebugWndFlag);
411   sp->ConsoleWndFlag = SetFlag(ConsoleWndFlag);
412   sp->ListWndFlag = SetFlag(ListWndFlag);
413   sp->TracerWndFlag = SetFlag(TracerWndFlag);
414   sp->DocWndFlag = SetFlag(DocWndFlag);
415   sp->WrdWndFlag = SetFlag(WrdWndFlag);
416   sp->SoundSpecWndFlag = SetFlag(SoundSpecWndFlag);
417   sp->SubWindowMax = SetValue(SubWindowMax,1,10);
418   strncpy(sp->ConfigFile,ConfigFile,MAXPATH + 31);
419   (sp->ConfigFile)[MAXPATH + 31] = '\0';
420   strncpy(sp->PlaylistFile,PlaylistFile,MAXPATH + 31);
421   (sp->PlaylistFile)[MAXPATH + 31] = '\0';
422   strncpy(sp->PlaylistHistoryFile,PlaylistHistoryFile,MAXPATH + 31);
423   (sp->PlaylistHistoryFile)[MAXPATH + 31] = '\0';
424   strncpy(sp->MidiFileOpenDir,MidiFileOpenDir,MAXPATH + 31);
425   (sp->MidiFileOpenDir)[MAXPATH + 31] = '\0';
426   strncpy(sp->ConfigFileOpenDir,ConfigFileOpenDir,MAXPATH + 31);
427   (sp->ConfigFileOpenDir)[MAXPATH + 31] = '\0';
428   strncpy(sp->PlaylistFileOpenDir,PlaylistFileOpenDir,MAXPATH + 31);
429   (sp->PlaylistFileOpenDir)[MAXPATH + 31] = '\0';
430   sp->PlayerThreadPriority = PlayerThreadPriority;
431   sp->GUIThreadPriority = GUIThreadPriority;
432   sp->WrdGraphicFlag = SetFlag(WrdGraphicFlag);
433   sp->TraceGraphicFlag = SetFlag(TraceGraphicFlag);
434   // fonts ...
435   sp->SystemFontSize = SystemFontSize;
436   sp->PlayerFontSize = PlayerFontSize;
437   sp->WrdFontSize = WrdFontSize;
438   sp->DocFontSize = DocFontSize;
439   sp->ListFontSize = ListFontSize;
440   sp->TracerFontSize = TracerFontSize;
441   strncpy(sp->SystemFont,SystemFont,255);
442   sp->SystemFont[255] = '\0';
443   strncpy(sp->PlayerFont,PlayerFont,255);
444   sp->PlayerFont[255] = '\0';
445   strncpy(sp->WrdFont,WrdFont,255);
446   sp->WrdFont[255] = '\0';
447   strncpy(sp->DocFont,DocFont,255);
448   DocFont[255] = '\0';
449   strncpy(sp->ListFont,ListFont,255);
450   sp->ListFont[255] = '\0';
451   strncpy(sp->TracerFont,TracerFont,255);
452   sp->TracerFont[255] = '\0';
453   sp->DocMaxSize = DocMaxSize;
454   strncpy(sp->DocFileExt,DocFileExt,255);
455   sp->DocFileExt[255] = '\0';
456   sp->PlayerLanguage = PlayerLanguage;
457   sp->DocWndIndependent = DocWndIndependent;
458   sp->DocWndAutoPopup = DocWndAutoPopup;
459   sp->SeachDirRecursive = SeachDirRecursive;
460   sp->IniFileAutoSave = IniFileAutoSave;
461   sp->SecondMode = SecondMode;
462   sp->AutoloadPlaylist = AutoloadPlaylist;
463   sp->AutosavePlaylist = AutosavePlaylist;
464   sp->PosSizeSave = PosSizeSave;
465 }
466 
467 extern int set_play_mode(char *cp);
468 extern int set_tim_opt(int c, char *optarg);
469 extern int set_ctl(char *cp);
470 extern int set_wrd(char *w);
471 
472 #if defined(__W32__) && defined(SMFCONV)
473 extern int opt_rcpcv_dll;
474 #endif /* SMFCONV */
475 extern char *wrdt_open_opts;
476 
is_device_output_ID(int id)477 static int is_device_output_ID(int id)
478 {
479     return id == 'd' || id == 'n' || id == 'e';
480 }
481 
482 #ifdef IA_W32G_SYN
483 extern int w32g_syn_id_port[];
484 extern int syn_AutoStart;
485 extern DWORD processPriority;
486 extern DWORD syn_ThreadPriority;
487 extern int w32g_syn_port_num;
488 extern int volatile stream_max_compute;
489 #endif
490 
491 void
ApplySettingTiMidity(SETTING_TIMIDITY * st)492 ApplySettingTiMidity(SETTING_TIMIDITY *st)
493 {
494     int i;
495     char buffer[INI_MAXLEN];
496 
497     /* Player must be stopped.
498      * DANGER to apply settings while playing.
499      */
500     amplification = SetValue(st->amplification, 0, MAX_AMPLIFICATION);
501     antialiasing_allowed = SetFlag(st->antialiasing_allowed);
502     if(st->buffer_fragments == -1)
503 	opt_buffer_fragments = -1;
504     else
505 	opt_buffer_fragments = SetValue(st->buffer_fragments, 3, 1000);
506     default_drumchannels = st->default_drumchannels;
507     default_drumchannel_mask = st->default_drumchannel_mask;
508     opt_modulation_wheel = SetFlag(st->opt_modulation_wheel);
509     opt_portamento = SetFlag(st->opt_portamento);
510     opt_nrpn_vibrato = SetFlag(st->opt_nrpn_vibrato);
511     opt_channel_pressure = SetFlag(st->opt_channel_pressure);
512     opt_trace_text_meta_event = SetFlag(st->opt_trace_text_meta_event);
513     opt_overlap_voice_allow = SetFlag(st->opt_overlap_voice_allow);
514     opt_default_mid = st->opt_default_mid;
515     default_tonebank = st->default_tonebank;
516     special_tonebank = st->special_tonebank;
517     effect_lr_mode = st->effect_lr_mode;
518     effect_lr_delay_msec = st->effect_lr_delay_msec;
519     opt_reverb_control = st->opt_reverb_control;
520     opt_chorus_control = st->opt_chorus_control;
521 	opt_surround_chorus = st->opt_surround_chorus;
522     noise_sharp_type = st->noise_sharp_type;
523     opt_evil_mode = st->opt_evil_mode;
524 	opt_tva_attack = st->opt_tva_attack;
525 	opt_tva_decay = st->opt_tva_decay;
526 	opt_tva_release = st->opt_tva_release;
527 	opt_delay_control = st->opt_delay_control;
528 	opt_default_module = st->opt_default_module;
529 	opt_lpf_def = st->opt_lpf_def;
530 	opt_drum_effect = st->opt_drum_effect;
531 	opt_modulation_envelope = st->opt_modulation_envelope;
532 	opt_pan_delay = st->opt_pan_delay;
533 	opt_eq_control = st->opt_eq_control;
534 	opt_insertion_effect = st->opt_insertion_effect;
535     adjust_panning_immediately = SetFlag(st->adjust_panning_immediately);
536     fast_decay = SetFlag(st->fast_decay);
537 #ifdef SUPPORT_SOUNDSPEC
538     view_soundspec_flag = SetFlag(st->view_soundspec_flag);
539     spectrigram_update_sec = st->spectrigram_update_sec;
540 #endif
541     for(i = 0; i < MAX_CHANNELS; i++)
542 	default_program[i] = st->default_program[i];
543     set_ctl(st->opt_ctl);
544     opt_realtime_playing = SetFlag(st->opt_realtime_playing);
545     reduce_voice_threshold = st->reduce_voice_threshold;
546     if (*st->opt_playmode != '\0')
547 	set_play_mode(st->opt_playmode);
548     strncpy(OutputName,st->OutputName,MAXPATH);
549 #if 0
550     if(OutputName[0] && !is_device_output_ID(play_mode->id_character))
551 		play_mode->name = strdup(OutputName);		// ���������[�N���邩�ȁH �͂����Ƃ��߂����B
552 #else
553     if(OutputName[0] && !is_device_output_ID(play_mode->id_character))
554 		play_mode->name = OutputName;
555 #endif
556     strncpy(w32g_output_dir,st->OutputDirName,MAXPATH);
557 	w32g_output_dir[MAXPATH-1] = '\0';
558 	w32g_auto_output_mode = st->auto_output_mode;
559     opt_output_rate = st->output_rate;
560     if(st->output_rate)
561 	play_mode->rate = SetValue(st->output_rate,
562 				   MIN_OUTPUT_RATE, MAX_OUTPUT_RATE);
563     else if(play_mode->rate == 0)
564 	play_mode->rate = DEFAULT_RATE;
565     voices = st->voices;
566 	if( voices > max_voices) max_voices = voices;
567 	auto_reduce_polyphony = st->auto_reduce_polyphony;
568     quietchannels = st->quietchannels;
569     temper_type_mute = st->temper_type_mute;
570     if(opt_aq_max_buff)
571 	free(opt_aq_max_buff);
572     if(opt_aq_fill_buff && opt_aq_fill_buff_free_needed)
573 	free(opt_aq_fill_buff);
574     strcpy(buffer, st->opt_qsize);
575     opt_aq_max_buff = buffer;
576     opt_aq_fill_buff = strchr(opt_aq_max_buff, '/');
577     *opt_aq_fill_buff++ = '\0';
578     opt_aq_max_buff = safe_strdup(opt_aq_max_buff);
579     opt_aq_fill_buff = safe_strdup(opt_aq_fill_buff);
580     modify_release = SetValue(st->modify_release, 0, MAX_MREL);
581     allocate_cache_size = st->allocate_cache_size;
582 	key_adjust = st->key_adjust;
583 	opt_force_keysig = st->opt_force_keysig;
584 	opt_pure_intonation = st->opt_pure_intonation;
585 	opt_init_keysig = st->opt_init_keysig;
586     if(output_text_code)
587 	free(output_text_code);
588     output_text_code = safe_strdup(st->output_text_code);
589     free_instruments_afterwards = st->free_instruments_afterwards;
590     set_wrd(st->opt_wrd);
591 #if defined(__W32__) && defined(SMFCONV)
592     opt_rcpcv_dll = st->opt_rcpcv_dll;
593 #endif /* SMFCONV */
594 
595     opt_control_ratio = st->control_ratio;
596     if(opt_control_ratio)
597 	control_ratio = SetValue(opt_control_ratio, 1, MAX_CONTROL_RATIO);
598     else
599     {
600 	control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
601 	control_ratio = SetValue(control_ratio, 1, MAX_CONTROL_RATIO);
602     }
603 	opt_drum_power = SetValue(st->opt_drum_power, 0, MAX_AMPLIFICATION);
604 	opt_amp_compensation = st->opt_amp_compensation;
605     data_block_bits = st->data_block_bits;
606     data_block_num = st->data_block_num;
607 
608 #ifdef IA_W32G_SYN
609 	for ( i = 0; i < MAX_PORT; i ++ ) {
610 		w32g_syn_id_port[i] = st->SynIDPort[i];
611 	}
612 	syn_AutoStart = st->syn_AutoStart;
613 	processPriority = st->processPriority;
614 	syn_ThreadPriority = st->syn_ThreadPriority;
615 	w32g_syn_port_num = st->SynPortNum;
616 	stream_max_compute = st->SynShTime;
617 #endif
618 }
619 
620 void
SaveSettingTiMidity(SETTING_TIMIDITY * st)621 SaveSettingTiMidity(SETTING_TIMIDITY *st)
622 {
623     int i, j;
624 
625     st->amplification = SetValue(amplification, 0, MAX_AMPLIFICATION);
626     st->antialiasing_allowed = SetFlag(antialiasing_allowed);
627     st->buffer_fragments = opt_buffer_fragments;
628     st->control_ratio = SetValue(opt_control_ratio, 0, MAX_CONTROL_RATIO);
629     st->default_drumchannels = default_drumchannels;
630     st->default_drumchannel_mask = default_drumchannel_mask;
631     st->opt_modulation_wheel = SetFlag(opt_modulation_wheel);
632     st->opt_portamento = SetFlag(opt_portamento);
633     st->opt_nrpn_vibrato = SetFlag(opt_nrpn_vibrato);
634     st->opt_channel_pressure = SetFlag(opt_channel_pressure);
635     st->opt_trace_text_meta_event = SetFlag(opt_trace_text_meta_event);
636     st->opt_overlap_voice_allow = SetFlag(opt_overlap_voice_allow);
637     st->opt_default_mid = opt_default_mid;
638     st->default_tonebank = default_tonebank;
639     st->special_tonebank = special_tonebank;
640     st->effect_lr_mode = effect_lr_mode;
641     st->effect_lr_delay_msec = effect_lr_delay_msec;
642     st->opt_reverb_control = opt_reverb_control;
643     st->opt_chorus_control = opt_chorus_control;
644 	st->opt_surround_chorus = opt_surround_chorus;
645 	st->opt_tva_attack = opt_tva_attack;
646 	st->opt_tva_decay = opt_tva_decay;
647 	st->opt_tva_release = opt_tva_release;
648 	st->opt_delay_control = opt_delay_control;
649 	st->opt_default_module = opt_default_module;
650 	st->opt_lpf_def = opt_lpf_def;
651 	st->opt_drum_effect = opt_drum_effect;
652 	st->opt_modulation_envelope = opt_modulation_envelope;
653 	st->opt_pan_delay = opt_pan_delay;
654 	st->opt_eq_control = opt_eq_control;
655 	st->opt_insertion_effect = opt_insertion_effect;
656     st->noise_sharp_type = noise_sharp_type;
657     st->opt_evil_mode = SetFlag(opt_evil_mode);
658     st->adjust_panning_immediately = SetFlag(adjust_panning_immediately);
659     st->fast_decay = SetFlag(fast_decay);
660 #ifdef SUPPORT_SOUNDSPEC
661     st->view_soundspec_flag = SetFlag(view_soundspec_flag);
662     st->spectrigram_update_sec = spectrigram_update_sec;
663 #endif
664     for(i = 0; i < MAX_CHANNELS; i++)
665     {
666 	if(def_instr_name[0])
667 	    st->default_program[i] = SPECIAL_PROGRAM;
668 	else
669 	    st->default_program[i] = default_program[i];
670     }
671     j = 0;
672     st->opt_ctl[j++] = ctl->id_character;
673     for(i = 1; i < ctl->verbosity; i++)
674 	st->opt_ctl[j++] = 'v';
675     for(i = 1; i > ctl->verbosity; i--)
676 	st->opt_ctl[j++] = 'q';
677     if(ctl->trace_playing)
678 	st->opt_ctl[j++] = 't';
679     if(ctl->flags & CTLF_LIST_LOOP)
680 	st->opt_ctl[j++] = 'l';
681     if(ctl->flags & CTLF_LIST_RANDOM)
682 	st->opt_ctl[j++] = 'r';
683     if(ctl->flags & CTLF_LIST_SORT)
684 	st->opt_ctl[j++] = 's';
685     if(ctl->flags & CTLF_AUTOSTART)
686 	st->opt_ctl[j++] = 'a';
687     if(ctl->flags & CTLF_AUTOEXIT)
688 	st->opt_ctl[j++] = 'x';
689     if(ctl->flags & CTLF_DRAG_START)
690 	st->opt_ctl[j++] = 'd';
691     if(ctl->flags & CTLF_AUTOUNIQ)
692 	st->opt_ctl[j++] = 'u';
693     if(ctl->flags & CTLF_AUTOREFINE)
694 	st->opt_ctl[j++] = 'R';
695     if(ctl->flags & CTLF_NOT_CONTINUE)
696 	st->opt_ctl[j++] = 'C';
697     st->opt_ctl[j] = '\0';
698     st->opt_realtime_playing = SetFlag(opt_realtime_playing);
699     st->reduce_voice_threshold = reduce_voice_threshold;
700     j = 0;
701     st->opt_playmode[j++] = play_mode->id_character;
702     st->opt_playmode[j++] = ((play_mode->encoding & PE_MONO) ? 'M' : 'S');
703     st->opt_playmode[j++] = ((play_mode->encoding & PE_SIGNED) ? 's' : 'u');
704 	if (play_mode->encoding & PE_24BIT) {st->opt_playmode[j++] = '2';}
705 	else if (play_mode->encoding & PE_16BIT) {st->opt_playmode[j++] = '1';}
706 	else {st->opt_playmode[j++] = '8';}
707     if(play_mode->encoding & PE_ULAW)
708 	st->opt_playmode[j++] = 'U';
709     else if(play_mode->encoding & PE_ALAW)
710 	st->opt_playmode[j++] = 'A';
711     else
712 	st->opt_playmode[j++] = 'l';
713     if(play_mode->encoding & PE_BYTESWAP)
714 	st->opt_playmode[j++] = 'x';
715     st->opt_playmode[j] = '\0';
716     strncpy(st->OutputName,OutputName,sizeof(st->OutputName)-1);
717 	strncpy(st->OutputDirName,w32g_output_dir,MAXPATH);
718 	st->OutputDirName[MAXPATH-1] = '\0';
719 	st->auto_output_mode = w32g_auto_output_mode;
720     st->voices = voices;
721 	st->auto_reduce_polyphony = auto_reduce_polyphony;
722     st->quietchannels = quietchannels;
723     st->temper_type_mute = temper_type_mute;
724     snprintf(st->opt_qsize,sizeof(st->opt_qsize),"%s/%s",
725 	     opt_aq_max_buff,opt_aq_fill_buff);
726     st->modify_release = SetValue(modify_release, 0, MAX_MREL);
727     st->allocate_cache_size = allocate_cache_size;
728 	st->opt_drum_power = SetValue(opt_drum_power, 0, MAX_AMPLIFICATION);
729 	st->opt_amp_compensation = opt_amp_compensation;
730 	st->key_adjust = key_adjust;
731 	st->opt_force_keysig = opt_force_keysig;
732 	st->opt_pure_intonation = opt_pure_intonation;
733 	st->opt_init_keysig = opt_init_keysig;
734     st->output_rate = opt_output_rate;
735     if(st->output_rate == 0)
736     {
737 	st->output_rate = play_mode->rate;
738 	if(st->output_rate == 0)
739 	    st->output_rate = DEFAULT_RATE;
740     }
741     st->output_rate = SetValue(st->output_rate,MIN_OUTPUT_RATE,MAX_OUTPUT_RATE);
742     strncpy(st->output_text_code,output_text_code,sizeof(st->output_text_code)-1);
743     st->free_instruments_afterwards = free_instruments_afterwards;
744     st->opt_wrd[0] = wrdt->id;
745     if(wrdt_open_opts)
746 	strncpy(st->opt_wrd + 1, wrdt_open_opts, sizeof(st->opt_wrd) - 2);
747     else
748 	st->opt_wrd[1] = '\0';
749 #if defined(__W32__) && defined(SMFCONV)
750     st->opt_rcpcv_dll = opt_rcpcv_dll;
751 #endif /* SMFCONV */
752   st->data_block_bits = data_block_bits;
753   st->data_block_num = data_block_num;
754 
755 #ifdef IA_W32G_SYN
756 	for ( i = 0; i < MAX_PORT; i ++ ) {
757 		st->SynIDPort[i] = w32g_syn_id_port[i];
758 	}
759 	st->syn_AutoStart = syn_AutoStart;
760 	st->processPriority = processPriority;
761 	st->syn_ThreadPriority = syn_ThreadPriority;
762 	st->SynPortNum = w32g_syn_port_num;
763 	st->SynShTime = stream_max_compute;
764 #endif
765 }
766 
767 
768 
769 
770 
771 
772 //****************************************************************************/
773 // ini & config
774 
775 static char S_IniFile[MAXPATH + 32];
776 static char S_timidity_window_inifile[MAXPATH + 32];
777 static char S_timidity_output_inifile[MAXPATH + 32];
778 static char S_ConfigFile[MAXPATH + 32];
779 static char S_PlaylistFile[MAXPATH + 32];
780 static char S_PlaylistHistoryFile[MAXPATH + 32];
781 static char S_MidiFileOpenDir[MAXPATH + 32];
782 static char S_ConfigFileOpenDir[MAXPATH + 32];
783 static char S_PlaylistFileOpenDir[MAXPATH + 32];
784 static char S_DocFileExt[256];
785 static char S_OutputName[MAXPATH + 32];
786 char *OutputName;
787 static char S_w32g_output_dir[1024 + 32];
788 static char S_SystemFont[256];
789 static char S_PlayerFont[256];
790 static char S_WrdFont[256];
791 static char S_DocFont[256];
792 static char S_ListFont[256];
793 static char S_TracerFont[256];
794 char *SystemFont = S_SystemFont;
795 char *PlayerFont = S_PlayerFont;
796 char *WrdFont = S_WrdFont;
797 char *DocFont = S_DocFont;
798 char *ListFont = S_ListFont;
799 char *TracerFont = S_TracerFont;
800 
801 //static HFONT hSystemFont = 0;
802 //static HFONT hPlayerFont = 0;
803 //static HFONT hWrdFont = 0;
804 //static HFONT hDocFont = 0;
805 //static HFONT hListFont = 0;
806 //static HFONT hTracerFont = 0;
807 int SystemFontSize = 9;
808 int PlayerFontSize = 16;
809 int WrdFontSize = 16;
810 int DocFontSize = 16;
811 int ListFontSize = 16;
812 int TracerFontSize = 16;
813 
814 #define DEFAULT_DOCFILEEXT "doc;txt;hed"
815 
816 SETTING_PLAYER *sp_default=NULL, *sp_current=NULL, *sp_temp=NULL;
817 SETTING_TIMIDITY *st_default=NULL, *st_current=NULL, *st_temp=NULL;
818 char *timidity_window_inifile;
819 char *timidity_output_inifile;
820 
821 #ifdef AU_GOGO
822 extern int gogo_ConfigDialogInfoInit(void);
823 extern int gogo_ConfigDialogInfoSaveINI(void);
824 extern int gogo_ConfigDialogInfoLoadINI(void);
825 #endif
826 #ifdef AU_VORBIS
827 extern int vorbis_ConfigDialogInfoInit(void);
828 extern int vorbis_ConfigDialogInfoSaveINI(void);
829 extern int vorbis_ConfigDialogInfoLoadINI(void);
830 #endif
831 
w32g_uninitialize(void)832 void w32g_uninitialize(void)
833 {
834 	if(sp_default != NULL) free(sp_default);
835 	if(st_default != NULL) free(st_default);
836 	if(sp_current != NULL) free(sp_current);
837 	if(st_current != NULL) free(st_current);
838 	if(sp_temp != NULL) free(sp_temp);
839 	if(st_temp != NULL) free(st_temp);
840 }
841 
w32g_initialize(void)842 void w32g_initialize(void)
843 {
844     char buffer[MAXPATH + 1024];
845     char *p;
846 
847     hInst = GetModuleHandle(0);
848 
849     IniFile = S_IniFile;
850     ConfigFile = S_ConfigFile;
851     PlaylistFile = S_PlaylistFile;
852     PlaylistHistoryFile = S_PlaylistHistoryFile;
853     MidiFileOpenDir = S_MidiFileOpenDir;
854     ConfigFileOpenDir = S_ConfigFileOpenDir;
855     PlaylistFileOpenDir = S_PlaylistFileOpenDir;
856     DocFileExt = S_DocFileExt;
857     OutputName = S_OutputName;
858 	w32g_output_dir = S_w32g_output_dir;
859     switch (PRIMARYLANGID(GetUserDefaultLangID()))
860     {
861     case LANG_JAPANESE:
862 	PlayerLanguage = LANGUAGE_JAPANESE;
863 	break;
864     default:
865 	PlayerLanguage = LANGUAGE_ENGLISH;
866 	break;
867     }
868 
869     IniFile[0] = '\0';
870     ConfigFile[0] = '\0';
871     PlaylistFile[0] = '\0';
872     PlaylistHistoryFile[0] = '\0';
873     MidiFileOpenDir[0] = '\0';
874     ConfigFileOpenDir[0] = '\0';
875     PlaylistFileOpenDir[0] = '\0';
876     OutputName[0] = '\0';
877 	w32g_output_dir[0] = '\0';
878 
879     strcpy(DocFileExt,DEFAULT_DOCFILEEXT);
880     strcpy(SystemFont,"�l�r ����");
881     strcpy(PlayerFont,"�l�r ����");
882     strcpy(WrdFont,"�l�r ����");
883     strcpy(DocFont,"�l�r ����");
884     strcpy(ListFont,"�l�r ����");
885     strcpy(TracerFont,"�l�r ����");
886 
887     if(GetModuleFileName(hInst, buffer, MAXPATH))
888     {
889 	if((p = pathsep_strrchr(buffer)) != NULL)
890 	{
891 	    p++;
892 	    *p = '\0';
893 	}
894 	else
895 	{
896 	    buffer[0] = '.';
897 	    buffer[1] = PATH_SEP;
898 	    buffer[2] = '\0';
899 	}
900     }
901     else
902     {
903 	buffer[0] = '.';
904 	buffer[1] = PATH_SEP;
905 	buffer[2] = '\0';
906     }
907 	// timpp32g.ini
908     strncpy(IniFile, buffer, MAXPATH);
909     IniFile[MAXPATH] = '\0';
910     strcat(IniFile,"timpp32g.ini");
911 	// timidity_window.ini
912 	timidity_window_inifile = S_timidity_window_inifile;
913 	strncpy(timidity_window_inifile, buffer, 200);
914     timidity_window_inifile[200] = '\0';
915     strcat(timidity_window_inifile,"timidity_window.ini");
916 	// timidity_output.ini
917 	timidity_output_inifile = S_timidity_output_inifile;
918 	strncpy(timidity_output_inifile, buffer, 200);
919     timidity_output_inifile[200] = '\0';
920     strcat(timidity_output_inifile,"timidity_output.ini");
921   // default playlist
922 	strncpy(DefaultPlaylistPath, buffer, 200);
923   DefaultPlaylistPath[200] = '\0';
924   strcat(DefaultPlaylistPath,DefaultPlaylistName);
925 
926 	//
927     st_default = (SETTING_TIMIDITY *)safe_malloc(sizeof(SETTING_TIMIDITY));
928     sp_default = (SETTING_PLAYER *)safe_malloc(sizeof(SETTING_PLAYER));
929     st_current = (SETTING_TIMIDITY *)safe_malloc(sizeof(SETTING_TIMIDITY));
930     sp_current = (SETTING_PLAYER *)safe_malloc(sizeof(SETTING_PLAYER));
931     st_temp = (SETTING_TIMIDITY *)safe_malloc(sizeof(SETTING_TIMIDITY));
932     sp_temp = (SETTING_PLAYER *)safe_malloc(sizeof(SETTING_PLAYER));
933 
934 	{
935 		DWORD dwRes;
936 #ifdef AU_GOGO
937 		gogo_ConfigDialogInfoInit();
938 #endif
939 #ifdef AU_VORBIS
940 		vorbis_ConfigDialogInfoInit();
941 #endif
942 		dwRes = GetFileAttributes(timidity_output_inifile);
943 		if(dwRes==0xFFFFFFFF || dwRes & FILE_ATTRIBUTE_DIRECTORY){
944 #ifdef AU_GOGO
945 			gogo_ConfigDialogInfoSaveINI();
946 #endif
947 #ifdef AU_VORBIS
948 			vorbis_ConfigDialogInfoSaveINI();
949 #endif
950 		} else {
951 #ifdef AU_GOGO
952 			gogo_ConfigDialogInfoLoadINI();
953 #endif
954 #ifdef AU_VORBIS
955 			vorbis_ConfigDialogInfoLoadINI();
956 #endif
957  		}
958  	}
959 
960     SaveSettingPlayer(sp_current);
961     SaveSettingTiMidity(st_current);
962     if(IniVersionCheck())
963     {
964 	LoadIniFile(sp_current, st_current);
965 	ApplySettingPlayer(sp_current);
966 	ApplySettingTiMidity(st_current);
967 	w32g_has_ini_file = 1;
968     }
969     else
970     {
971 	sprintf(buffer,
972 "Ini file is not found, or old format is found.\n"
973 "Do you want to initialize the ini file?\n\n"
974 "Ini file path: %s",
975 		IniFile);
976 
977 	if(MessageBox(0, buffer, "TiMidity Notice", MB_YESNO) == IDYES)
978 	{
979 	    SaveIniFile(sp_current, st_current);
980 	    w32g_has_ini_file = 1;
981 	}
982 	else
983 	{
984 	    w32g_has_ini_file = 0;
985 	}
986     }
987 
988     memcpy(sp_default, sp_current, sizeof(SETTING_PLAYER));
989     memcpy(st_default, st_current, sizeof(SETTING_TIMIDITY));
990 
991     memcpy(sp_temp, sp_current, sizeof(SETTING_PLAYER));
992     memcpy(st_temp, st_current, sizeof(SETTING_TIMIDITY));
993 
994 	wrdt=wrdt_list[0];
995 
996 #ifndef IA_W32G_SYN
997     w32g_i_init();
998 #endif
999 }
1000 
IniVersionCheck(void)1001 int IniVersionCheck(void)
1002 {
1003     char version[INI_MAXLEN];
1004     if(IniGetKeyStringN(INI_SEC_PLAYER,"IniVersion",version,sizeof(version)-1) == 0 &&
1005        strcmp(version, IniVersion) == 0)
1006 	return 1; // UnChanged
1007     return 0;
1008 }
1009 
BitBltRect(HDC dst,HDC src,RECT * rc)1010 void BitBltRect(HDC dst, HDC src, RECT *rc)
1011 {
1012     BitBlt(dst, rc->left, rc->top,
1013 	   rc->right - rc->left, rc->bottom - rc->top,
1014 	   src, rc->left, rc->top, SRCCOPY);
1015 }
1016 
1017 #if 0
1018 /*
1019  * TmColor
1020  */
1021 TmColors tm_colors[TMCC_SIZE];
1022 
1023 static COLORREF WeakHalfColor(COLORREF fc, COLORREF bc)
1024 {
1025     return fc*1/3 + bc*2/3;
1026 }
1027 
1028 static COLORREF HalfColor(COLORREF fc, COLORREF bc)
1029 {
1030     return fc*1/6 + bc*5/6;
1031 }
1032 
1033 void TmInitColor(void)
1034 {
1035     int i;
1036 
1037     tm_colors[TMCC_BLACK].color	= TMCCC_BLACK;
1038     tm_colors[TMCC_WHITE].color	= TMCCC_WHITE;
1039     tm_colors[TMCC_RED].color	= TMCCC_RED;
1040     tm_colors[TMCC_BACK].color	= TMCCC_BACK;
1041     tm_colors[TMCC_LOW].color	= TMCCC_LOW;
1042     tm_colors[TMCC_MIDDLE].color= TMCCC_MIDDLE;
1043     tm_colors[TMCC_HIGH].color	= TMCCC_HIGH;
1044 
1045     tm_colors[TMCC_FORE_HALF].color = HalfColor(TMCCC_FORE,TMCCC_BACK);
1046     tm_colors[TMCC_FORE_WEAKHALF].color = WeakHalfColor(TMCCC_FORE,TMCCC_BACK);
1047     tm_colors[TMCC_LOW_HALF].color = HalfColor(TMCCC_LOW,TMCCC_BACK);
1048     tm_colors[TMCC_MIDDLE_HALF].color = HalfColor(TMCCC_MIDDLE,TMCCC_BACK);
1049     tm_colors[TMCC_HIGH_HALF].color = HalfColor(TMCCC_HIGH,TMCCC_BACK);
1050 
1051     for(i = 0; i < TMCC_SIZE; i++)
1052     {
1053 	tm_colors[i].pen = CreatePen(PS_SOLID, 1, tm_colors[i].color);
1054 	tm_colors[i].brush = CreateSolidBrush(tm_colors[i].color);
1055     }
1056 }
1057 
1058 void TmFreeColor(void)
1059 {
1060     int i;
1061     for(i = 0; i < TMCC_SIZE; i++)
1062     {
1063 	if(tm_colors[i].pen != NULL)
1064 	{
1065 	    DeleteObject(tm_colors[i].pen);
1066 	    DeleteObject(tm_colors[i].brush);
1067 	    tm_colors[i].pen = NULL;
1068 	}
1069     }
1070 }
1071 
1072 void TmFillRect(HDC hdc, RECT *rc, int color)
1073 {
1074     HPEN hPen = tm_colors[color].pen;
1075     HBRUSH hBrush = tm_colors[color].brush;
1076     HGDIOBJ hgdiobj_hpen, hgdiobj_hbrush;
1077 
1078     hgdiobj_hpen = SelectObject(hdc, hPen);
1079     hgdiobj_hbrush = SelectObject(hdc, hBrush);
1080     Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1081     SelectObject(hdc, hgdiobj_hpen);
1082     SelectObject(hdc, hgdiobj_hbrush);
1083 }
1084 #endif
1085 
1086 #ifndef S_ISDIR
1087 #define S_ISDIR(mode)   (((mode)&0xF000) == 0x4000)
1088 #endif /* S_ISDIR */
is_directory(char * path)1089 int is_directory(char *path)
1090 {
1091 	struct stat st;
1092 	if(*path == '@') /* special identifire for playlist file */
1093 		return 0;
1094 	if(stat(path, &st) != -1)
1095 		return S_ISDIR(st.st_mode);
1096 	return GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY;
1097 }
1098 
1099 /* Return: 0: - not modified
1100  *         1: - modified
1101  */
directory_form(char * buffer)1102 int directory_form(char *buffer)
1103 {
1104 	int len;
1105 
1106 	len = strlen(buffer);
1107 	if(len == 0 || buffer[len - 1] == PATH_SEP)
1108 		return 0;
1109 	if(IS_PATH_SEP(buffer[len - 1]))
1110 		len--;
1111 	buffer[len++] = PATH_SEP;
1112 	buffer[len] = '\0';
1113 	return 1;
1114 }
1115 
1116 /* Return: 0: - not modified
1117  *         1: - modified
1118  */
nodirectory_form(char * buffer)1119 int nodirectory_form(char *buffer)
1120 {
1121 	char *lastp = buffer + strlen(buffer);
1122 	char *p = lastp;
1123 
1124 	while(p > buffer && IS_PATH_SEP(*(p - 1)))
1125 		p--;
1126 	if(p == lastp)
1127 		return 0;
1128 	*p = '\0';
1129 	return 1;
1130 }
1131 
SettingCtlFlag(SETTING_TIMIDITY * st,int c,int onoff)1132 void SettingCtlFlag(SETTING_TIMIDITY *st, int c, int onoff)
1133 {
1134     int n;
1135     char *opt;
1136 
1137     opt = st->opt_ctl + 1;
1138     n = strlen(opt);
1139     if(onoff)
1140     {
1141 	if(strchr(opt, c) != NULL)
1142 	    return; /* Already set */
1143 	opt[n++] = c;
1144 	opt[n] = '\0';
1145     }
1146     else
1147     {
1148 	char *p;
1149 	if((p = strchr(opt, c)) == NULL)
1150 	    return; /* Already removed */
1151 	while(*(p + 1))
1152 	{
1153 	    *p = *(p + 1);
1154 	    p++;
1155 	}
1156 	*p = '\0';
1157     }
1158 }
1159 
1160 
1161 
1162 
IsAvailableFilename(char * filename)1163 int IsAvailableFilename(char *filename)
1164 {
1165 	char *p = strrchr(filename,'.');
1166 	if(p == NULL)
1167 		return 0;
1168 	if(	strcasecmp(p,".lzh")==0 ||
1169 		strcasecmp(p,".zip")==0 ||
1170 		strcasecmp(p,".gz")==0	||
1171 		strcasecmp(p,".mid")==0 ||
1172 		strcasecmp(p,".rcp")==0 ||
1173 		strcasecmp(p,".r36")==0 ||
1174 		strcasecmp(p,".g18")==0 ||
1175 		strcasecmp(p,".g36")==0 ||
1176 		strcasecmp(p,".mod")==0 ||
1177 //		strcasecmp(p,".hqx")==0 ||
1178 		strcasecmp(p,".tar")==0 ||
1179 		strcasecmp(p,".tgz")==0 ||
1180 		strcasecmp(p,".lha")==0 ||
1181 		strcasecmp(p,".mime")==0 ||
1182 		strcasecmp(p,".smf")==0)
1183 		return 1;
1184 //	if(url_check_type(filename)!=-1)
1185 //		return 1;
1186 	return 0;
1187 }
1188 
1189 /* ScanDirectoryFiles() works like UNIX find. */
1190 #define SCANDIR_MAX_DEPTH 32
ScanDirectoryFiles(char * basedir,int (* file_proc)(char * pathname,void * user_val),void * user_val)1191 void ScanDirectoryFiles(char *basedir,
1192 						int (* file_proc)(char *pathname, /* (const) */
1193 										  void *user_val),
1194 						void *user_val)
1195 {
1196 	char baselen;
1197 	URL dir;
1198 
1199 	static int depth = 0;
1200     static int stop_flag;	/* Stop scanning if true */
1201     static int error_disp;	/* Whether error is displayed or not */
1202 	static char pathbuf[MAXPATH]; /* pathname buffer */
1203 
1204 	if(depth == 0) /* Initialize variables at first recursive */
1205 	{
1206 		stop_flag = 0;
1207 		error_disp = 0;
1208 		strcpy(pathbuf, basedir);
1209 	}
1210 	else if(depth > SCANDIR_MAX_DEPTH) /* Avoid infinite recursive */
1211 	{
1212 		if(!error_disp)
1213 		{
1214 			/* Display this message at once */
1215 			ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
1216 					  "%s: Directory is too deep",
1217 					  basedir);
1218 			error_disp = 1;
1219 		}
1220 		return; /* Skip scanning this directory */
1221 	}
1222 
1223 	directory_form(pathbuf);
1224 	baselen = strlen(pathbuf);
1225 	if(baselen > sizeof(pathbuf) - 16)
1226 	{
1227 		/* Ignore too long file name */
1228 		return;
1229 	}
1230 
1231 	if((dir = url_dir_open(pathbuf)) == NULL)
1232 	{
1233 	    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Can't open directory", pathbuf);
1234 		return;
1235 	}
1236 
1237 	if(file_proc(pathbuf, user_val))
1238 	{
1239 	    stop_flag = 1; /* Terminate */
1240 		return;
1241 	}
1242 
1243 	while(!stop_flag &&
1244 		  url_gets(dir, pathbuf + baselen, sizeof(pathbuf) - baselen - 1))
1245 	{
1246 		if(strcmp(pathbuf + baselen, ".") == 0 ||
1247 		   strcmp(pathbuf + baselen, "..") == 0)
1248 			continue;
1249 		if(file_proc(pathbuf, user_val))
1250 		{
1251 			stop_flag = 1; /* Terminate */
1252 			break;
1253 		}
1254 		if(is_directory(pathbuf))
1255 		{
1256 			/* into subdirectory */
1257 			depth++;
1258 			ScanDirectoryFiles(pathbuf, file_proc, user_val);
1259 			depth--;
1260 		}
1261 	}
1262 	url_close(dir);
1263 }
1264 
1265 #define EXPANDDIR_MAX_SIZE  100000	/* Limit of total bytes of the file names */
expand_dir_proc(char * filename,void * v)1266 static int expand_dir_proc(char *filename, void *v)
1267 {
1268 	void **user_val = (void **)v;
1269 	StringTable *st = (StringTable *)user_val[0];
1270 	int *total_size = (int *)user_val[1];
1271 	char *startdir  = (char *)user_val[2];
1272 
1273 	if(IsAvailableFilename(filename))
1274 	{
1275 		if(*total_size > EXPANDDIR_MAX_SIZE)
1276 		{
1277 			ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: There are too many files.",
1278 					  startdir);
1279 			return 1; /* Terminate */
1280 		}
1281 		put_string_table(st, filename, strlen(filename));
1282 		*total_size += strlen(filename);
1283 	}
1284 	return 0;
1285 }
1286 
FilesExpandDir(int * p_nfiles,char ** files)1287 char **FilesExpandDir(int *p_nfiles, char **files)
1288 {
1289 	StringTable st;
1290 	int i;
1291 
1292 	init_string_table(&st);
1293 	for(i = 0; i < *p_nfiles; i++)
1294 	{
1295 		void *user_val[3];
1296 		int total_size;
1297 
1298 		total_size = 0;
1299 		user_val[0] = &st;
1300 		user_val[1] = &total_size;
1301 		user_val[2] = files[i];
1302 
1303 		if(is_directory(files[i]))
1304 			ScanDirectoryFiles(files[i], expand_dir_proc, user_val);
1305 		else
1306 		{
1307 			int len = strlen(files[i]);
1308 			put_string_table(&st, files[i], len);
1309 		}
1310 	}
1311 	*p_nfiles = st.nstring;
1312     return make_string_array(&st);
1313 
1314 }
1315 
1316 #ifndef IA_W32G_SYN
w32gLoadDefaultPlaylist(void)1317 int w32gLoadDefaultPlaylist(void)
1318 {
1319 	if(AutoloadPlaylist) {
1320     w32g_lock_open_file = 1;
1321     w32g_send_rc(RC_EXT_LOAD_PLAYLIST, (ptr_size_t)DefaultPlaylistPath);
1322 	}
1323 	return 0;
1324 }
1325 
w32gSaveDefaultPlaylist(void)1326 int w32gSaveDefaultPlaylist(void)
1327 {
1328 	if(AutosavePlaylist) {
1329     w32g_lock_open_file = 1;
1330     w32g_send_rc(RC_EXT_SAVE_PLAYLIST, (ptr_size_t)DefaultPlaylistPath);
1331 	}
1332 	return 0;
1333 }
1334 #endif
1335 
1336 #ifndef WIN32GCC
get_filename(char * src,char * dest)1337 static char *get_filename(char *src, char *dest)
1338 {
1339 	char *p = src;
1340 	char *start = NULL;
1341 	int quot_flag = 0;
1342 	if(p == NULL)
1343 		return NULL;
1344 	for(;;){
1345 		if(*p != ' ' && *p != '\0' && start == NULL)
1346 			start = p;
1347 		if(*p == '\'' || *p == '\"'){
1348 			if(quot_flag){
1349 				if(p - start != 0)
1350 					strncpy(dest, start, p - start);
1351 				dest[p-start] = '\0';
1352 				p++;
1353 				return p;
1354 			} else {
1355 				quot_flag = !quot_flag;
1356 				p++;
1357 				start = p;
1358 				continue;
1359 			}
1360 		}
1361 		if(*p == '\0' || (*p == ' ' && !quot_flag)){
1362 			if(start == NULL)
1363 				return NULL;
1364 			if(p - start != 0)
1365 				strncpy(dest, start, p - start);
1366 			dest[p-start] = '\0';
1367 			if(*p != '\0')
1368 				p++;
1369 			return p;
1370 		}
1371 		p++;
1372 	}
1373 }
1374 
CmdLineToArgv(LPSTR lpCmdLine,int * pArgc,CHAR *** pArgv)1375 void CmdLineToArgv(LPSTR lpCmdLine, int *pArgc, CHAR ***pArgv)
1376 {
1377 	LPSTR p = lpCmdLine , buffer, lpsRes;
1378 	int i, max = -1, inc = 16;
1379 	int buffer_size;
1380 
1381 	*pArgv = NULL;
1382 	buffer_size = strlen(lpCmdLine) + 1024;
1383 //	buffer = safe_malloc(sizeof(CHAR) * buffer_size + 1);
1384 	buffer = (LPSTR)malloc(sizeof(CHAR) * buffer_size + 1);
1385 	strcpy(buffer, lpCmdLine);
1386 
1387 	for(i=0;;i++)
1388 	{
1389 	if(i > max){
1390 		max += inc;
1391 //		*pArgv = (CHAR **)safe_realloc(*pArgv, sizeof(CHAR *) * (max + 1));
1392 		*pArgv = (CHAR **)realloc(*pArgv, sizeof(CHAR *) * (max + 2));
1393 	}
1394 	if(i==0){
1395 		GetModuleFileName(NULL,buffer,buffer_size);
1396 		lpsRes = p;
1397 	} else
1398 		lpsRes = get_filename(p,buffer);
1399 	if(lpsRes != NULL){
1400 //		(*pArgv)[i] = (CHAR *)safe_malloc(sizeof(CHAR) * strlen(buffer) + 1);
1401 		(*pArgv)[i] = (CHAR *)malloc(sizeof(CHAR) * strlen(buffer) + 1);
1402 		strcpy((*pArgv)[i],buffer);
1403 		p = lpsRes;
1404 	} else {
1405 		*pArgc = i;
1406 		free(buffer);
1407 		return;
1408 	}
1409 	}
1410 }
1411 #endif /* !WIN32GCC */
1412