summaryrefslogtreecommitdiff
path: root/plugins/BASS_interface/src/Main.cpp
blob: 2be38e40285ff6a85d9369f148270159752c4fb0 (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
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
/*
Copyright (C) 2010, 2011 tico-tico
*/

#include "bass_interface.h"

HINSTANCE hInst;
int hLangpack;

PLUGININFOEX pluginInfo={
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	MIID_BASSINT
};

static HANDLE hService;
static HANDLE hTBButton;
static HINSTANCE hBass = NULL;

extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
	hInst=hinstDLL;
	return TRUE;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion)
{
	return &pluginInfo;
}

#define MAXCHAN	5
static TCHAR CurrBassPath[MAX_PATH], tmp[MAX_PATH];
static int sndNSnd = 0, sndLimSnd;
static HSTREAM sndSSnd[MAXCHAN] = {0};

static WORD TimeWrd1, TimeWrd2, StatMask;
static BOOL QuietTime, Preview, EnPreview;
static int Volume;
static int device = -1;
static int newBass = 0;
static HWND ClistHWND;

static int OnPlaySnd(WPARAM wParam, LPARAM lParam)
{
	TCHAR* ptszFile = (TCHAR*) lParam; SYSTEMTIME systime; WORD currtime, currstat; BOOL doPlay = TRUE;

	GetLocalTime(&systime);
	currtime = MAKEWORD(systime.wMinute, systime.wHour);

	currstat = 1;
	switch (CallService(MS_CLIST_GETSTATUSMODE, 0, 0))
	{
		case ID_STATUS_OUTTOLUNCH:   currstat <<= 1;
		case ID_STATUS_ONTHEPHONE:   currstat <<= 1;
		case ID_STATUS_INVISIBLE:    currstat <<= 1;
		case ID_STATUS_FREECHAT:     currstat <<= 1;
		case ID_STATUS_DND:          currstat <<= 1;
		case ID_STATUS_OCCUPIED:     currstat <<= 1;
		case ID_STATUS_NA:           currstat <<= 1;
		case ID_STATUS_AWAY:         currstat <<= 1;
		case ID_STATUS_ONLINE:       currstat <<= 1;
	}

	if ( !DBGetContactSettingByte(NULL,"Skin","UseSound",0)) doPlay = FALSE;

	if ( QuietTime )
		if (
			((TimeWrd1 < TimeWrd2) && (TimeWrd1 <= currtime && currtime < TimeWrd2)) ||
			((TimeWrd2 < TimeWrd1) && (TimeWrd1 <= currtime || currtime < TimeWrd2))
				) doPlay = FALSE;
		
	if ( !(currstat & StatMask)) doPlay = FALSE;

	if ( Preview || (int)wParam==1 ) doPlay = TRUE;

	if ( !ptszFile ) doPlay = FALSE;

	if ( doPlay )
	{
		BASS_StreamFree(sndSSnd[sndNSnd]);
		sndSSnd[sndNSnd] = BASS_StreamCreateFile(FALSE, ptszFile, 0, 0, BASS_TCHAR | BASS_STREAM_AUTOFREE);
		BASS_ChannelPlay(sndSSnd[sndNSnd], FALSE);
		sndNSnd = (sndNSnd+1)%sndLimSnd;
	}

	Preview = FALSE;

	return 1;
}

#define SLIDER_MIN  0
#define SLIDER_MAX  100

INT_PTR CALLBACK OptionsProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_INITDIALOG:
			{
				int i; SYSTEMTIME systime = {0};

				TranslateDialogDefault(hwndDlg);

				SendDlgItemMessage(hwndDlg, IDC_MAXCHANNEL, CB_RESETCONTENT, 0, 0);
				for(i=1; i<=MAXCHAN; i++)
					SendDlgItemMessage(hwndDlg, IDC_MAXCHANNEL, CB_ADDSTRING, 0, (LPARAM)_itot(i, tmp, 10));

				SendDlgItemMessage(hwndDlg, IDC_MAXCHANNEL, CB_SETCURSEL, sndLimSnd - 1, 0);
				
				SendDlgItemMessage(hwndDlg, IDC_CURRPATH, WM_SETTEXT, 0, (LPARAM)CurrBassPath);

				SendDlgItemMessage(hwndDlg, IDC_VOLUME, TBM_SETRANGE, FALSE, MAKELONG(SLIDER_MIN,SLIDER_MAX));
				SendDlgItemMessage(hwndDlg, IDC_VOLUME, TBM_SETPOS, TRUE, Volume);
				SendDlgItemMessage(hwndDlg, IDC_VOLUME, TBM_SETPAGESIZE, 0, 5);

				systime.wHour = HIBYTE(TimeWrd1);
				systime.wMinute = LOBYTE(TimeWrd1);
				systime.wYear = 2000;
				systime.wMonth = 1;
				systime.wDay = 1;
				SendDlgItemMessage(hwndDlg, IDC_TIME1, DTM_SETFORMAT, 0, (LPARAM)_T("HH:mm"));
				SendDlgItemMessage(hwndDlg, IDC_TIME1, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&systime);
				systime.wHour = HIBYTE(TimeWrd2);
				systime.wMinute = LOBYTE(TimeWrd2);
				SendDlgItemMessage(hwndDlg, IDC_TIME2, DTM_SETFORMAT, 0, (LPARAM)_T("HH:mm"));
				SendDlgItemMessage(hwndDlg, IDC_TIME2, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&systime);
				
				CheckDlgButton(hwndDlg, IDC_PREVIEW, EnPreview ? BST_CHECKED : BST_UNCHECKED);

				for(i = IDC_CHECKBOX1; i < IDC_CHECKBOX10+1; i++)
				{
					if (StatMask & (1 << (i-IDC_CHECKBOX1))) 
						CheckDlgButton(hwndDlg, i, BST_CHECKED);
				}

				if (QuietTime)
				{
					EnableWindow(GetDlgItem(hwndDlg,IDC_TIME1), TRUE);
					EnableWindow(GetDlgItem(hwndDlg,IDC_TIME2), TRUE);
					CheckDlgButton(hwndDlg, IDC_QUIETTIME, BST_CHECKED);
				}
				if (hBass == NULL)
				{
					EnableWindow(GetDlgItem(hwndDlg, IDC_VOLUME), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_MAXCHANNEL), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_OUTDEVICE), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_QUIETTIME), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_PREVIEW), FALSE);

					for(i = IDC_CHECKBOX1; i < IDC_CHECKBOX10+1; i++)
					{
						EnableWindow(GetDlgItem(hwndDlg, i), FALSE);
					}
				}
				else
				{
					DBVARIANT dbv = {0}; BASS_DEVICEINFO info; DWORD bassver = BASS_GetVersion();

					mir_sntprintf(tmp, SIZEOF(tmp), TranslateT("un4seen's bass version: %d.%d.%d.%d"), bassver >> 24, (bassver >> 16) & 0xff, (bassver >> 8) & 0xff, bassver & 0xff);
					SendDlgItemMessage(hwndDlg, IDC_BASSVERSION, WM_SETTEXT, 0, (LPARAM)tmp);

					SendDlgItemMessage(hwndDlg, IDC_OUTDEVICE, CB_RESETCONTENT, 0, 0);
					SendDlgItemMessage(hwndDlg, IDC_OUTDEVICE, CB_ADDSTRING, 0, (LPARAM)TranslateT("--default device--"));
					SendDlgItemMessage(hwndDlg, IDC_OUTDEVICE, CB_SETCURSEL, 0, 0);
					DBGetContactSettingString(NULL, ModuleName, OPT_OUTDEVICE, &dbv);
					for (i=1; BASS_GetDeviceInfo(i+newBass, &info); i++)
					{
						SendDlgItemMessageA(hwndDlg, IDC_OUTDEVICE, CB_ADDSTRING, 0, (LPARAM)info.name);
						if (!lstrcmpA(dbv.pszVal, info.name))
							SendDlgItemMessage(hwndDlg, IDC_OUTDEVICE, CB_SETCURSEL, i, 0);
					}
					DBFreeVariant(&dbv);
				}

				return 1;
			}

		case WM_HSCROLL:
			if (hBass != NULL)				
				if (LOWORD(wParam) == SB_ENDSCROLL || LOWORD(wParam) == SB_THUMBTRACK)
				{
					BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, SendDlgItemMessage(hwndDlg, IDC_VOLUME, TBM_GETPOS, 0, 0) * 100);
					Preview = TRUE;
					if (EnPreview) SkinPlaySound("AlertMsg");
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
				}
			break;

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code)
			{
				case PSN_APPLY:
					if (hBass != NULL)
					{
						int i; SYSTEMTIME systime;

						SendDlgItemMessageA(hwndDlg, IDC_OUTDEVICE, WM_GETTEXT, sizeof(tmp), (LPARAM)tmp);
						DBWriteContactSettingString(NULL, ModuleName, OPT_OUTDEVICE, (char *)tmp);
						
						Volume = (DWORD)SendDlgItemMessage(hwndDlg, IDC_VOLUME, TBM_GETPOS, 0, 0);
						DBWriteContactSettingByte(NULL, ModuleName, OPT_VOLUME, Volume);

						sndLimSnd = SendDlgItemMessage(hwndDlg, IDC_MAXCHANNEL, CB_GETCURSEL, 0, 0) + 1;
						if ( sndLimSnd > MAXCHAN ) sndLimSnd = MAXCHAN;
						DBWriteContactSettingByte(NULL, ModuleName, OPT_MAXCHAN, sndLimSnd);

						QuietTime = IsDlgButtonChecked(hwndDlg, IDC_QUIETTIME) == BST_CHECKED;
						DBWriteContactSettingByte(NULL, ModuleName, OPT_QUIETTIME, QuietTime);
						
						SendDlgItemMessage(hwndDlg, IDC_TIME1, DTM_GETSYSTEMTIME, 0, (LPARAM)&systime);
						TimeWrd1 = MAKEWORD(systime.wMinute, systime.wHour);
						DBWriteContactSettingWord(NULL, ModuleName, OPT_TIME1, TimeWrd1);
						
						SendDlgItemMessage(hwndDlg, IDC_TIME2, DTM_GETSYSTEMTIME, 0, (LPARAM)&systime);
						TimeWrd2 = MAKEWORD(systime.wMinute, systime.wHour);
						DBWriteContactSettingWord(NULL, ModuleName, OPT_TIME2, TimeWrd2);
						
						EnPreview = IsDlgButtonChecked(hwndDlg, IDC_PREVIEW) == BST_CHECKED;
						DBWriteContactSettingByte(NULL, ModuleName, OPT_PREVIEW, EnPreview);

						StatMask = 0;
						for(i = IDC_CHECKBOX10; i > IDC_CHECKBOX1-1; i--)
						{
							StatMask <<= 1;
							if(IsDlgButtonChecked(hwndDlg, i) == BST_CHECKED)
								StatMask |= 1;
						}

						DBWriteContactSettingWord(NULL, ModuleName, OPT_STATUS, StatMask);

						device = SendDlgItemMessage(hwndDlg, IDC_OUTDEVICE, CB_GETCURSEL, 0, 0);
						if (device == 0)	device = -1;
						else				device += newBass;
						if (CallService(MS_TTB_GETBUTTONSTATE, (WPARAM)hTBButton, 0) == TTBST_RELEASED) {
							BASS_Free();
							BASS_Init(device, 44100, 0, ClistHWND, NULL);
							BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, Volume * 100 );
						}
					}
					return 1;

				case PSN_RESET:
					if (hBass != NULL)
					{
						BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, Volume * 100);
					}
					return 1;
					
				case DTN_DATETIMECHANGE:
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
					return 1;
			}
			break;

		case WM_COMMAND:
			switch(LOWORD(wParam)) 
			{
				case IDC_QUIETTIME:
					{
						BOOL b = IsDlgButtonChecked(hwndDlg, IDC_QUIETTIME) == BST_CHECKED;
						EnableWindow(GetDlgItem(hwndDlg,IDC_TIME1), b);
						EnableWindow(GetDlgItem(hwndDlg,IDC_TIME2), b);
					}
				case IDC_MAXCHANNEL:
				case IDC_OUTDEVICE:
				case IDC_CHECKBOX1:
				case IDC_CHECKBOX2:
				case IDC_CHECKBOX3:
				case IDC_CHECKBOX4:
				case IDC_CHECKBOX5:
				case IDC_CHECKBOX6:
				case IDC_CHECKBOX7:
				case IDC_CHECKBOX8:
				case IDC_CHECKBOX9:
				case IDC_CHECKBOX10:
				case IDC_PREVIEW:
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
					break;
				case IDC_GETBASS:
					CallService(MS_UTILS_OPENURL , 1, (LPARAM)"http://www.un4seen.com/");
					break;
			}
			break;
		
		//case WM_CLOSE:
		//	EndDialog(hwndDlg, 0);
	}
	return 0;
}

struct
{
	char* szDescr;
	char* szName;
	int   defIconID;
}
static const iconList[] =
{
	{ "Audio device is opened", "BASSSoundOnOffUp", IDI_BASSSoundOnOffUp },
	{ "Audio device is closed", "BASSSoundOnOffDown", IDI_BASSSoundOnOffDown }
};

static HANDLE hIconLibItem[SIZEOF(iconList)];

void InitIcons(void)
{
	int i; char szSettingName[100]; SKINICONDESC sid = {0}; TCHAR szFile[MAX_PATH];

	GetModuleFileName(hInst, szFile, SIZEOF(szFile));

	sid.cbSize = sizeof(SKINICONDESC);
	sid.ptszDefaultFile = szFile;
	sid.cx = sid.cy = 16;
	sid.pszName = szSettingName;
	sid.pszSection = ModuleName;
	sid.flags = SIDF_PATH_TCHAR;

	for (i = 0; i < SIZEOF(iconList); i++) 
	{
		mir_snprintf(szSettingName, SIZEOF(szSettingName), "%s_%s", ModuleName, iconList[i].szName);

		sid.pszDescription = (char*)iconList[i].szDescr;
		sid.iDefaultIndex = -iconList[i].defIconID;
		hIconLibItem[i] = Skin_AddIcon(&sid);
	}	
}

int OptionsInit(WPARAM wParam, LPARAM lParam)
{
	OPTIONSDIALOGPAGE odp = {0};
	odp.cbSize = sizeof(odp);
	odp.hInstance = hInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPTIONS);
	odp.pszTitle = LPGEN(ModuleName);
	odp.pfnDlgProc = OptionsProc;
	odp.pszGroup = LPGEN("Customize");
	odp.flags = ODPF_BOLDGROUPS;

	Options_AddPage(wParam, &odp);
	return 0;
}

INT_PTR BASSSoundOnOff(WPARAM wParam, LPARAM lParam)
{
	if (hBass != NULL) {
		BOOL opened = CallService(MS_TTB_GETBUTTONSTATE, (WPARAM)"BASSSoundOnOff", 0) == TTBST_RELEASED;

		if ( opened )		
		{
			BASS_Free();				// Close Device
		}
		else
		{
			BASS_Init(device, 44100, 0, ClistHWND, NULL);
			BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, Volume * 100 );
		}
		
		CallService(MS_TTB_SETBUTTONSTATE, (WPARAM)"BASSSoundOnOff", opened ? TTBST_PUSHED : TTBST_RELEASED);
		DBWriteContactSettingByte(NULL, ModuleName, OPT_DEVOPEN, !opened);
	}
	return 0;
}

int OnToolbarLoaded(WPARAM wParam, LPARAM lParam) 
{
	TTBButton tbb = {0};
	tbb.cbSize = sizeof(TTBButton);
	tbb.name = LPGEN("Open/close audio device");
	tbb.pszService = "BASSinterface/BASSSoundOnOff";
	tbb.pszTooltipUp = LPGEN("Audio device is opened");
	tbb.pszTooltipDn = LPGEN("Audio device is closed");
	tbb.hIconHandleUp = hIconLibItem[0];
	tbb.hIconHandleDn = hIconLibItem[1];
	tbb.dwFlags = TTBBF_SHOWTOOLTIP;
	hTBButton = TopToolbar_AddButton(&tbb);
	return 0;
}

int OnModulesLoaded(WPARAM wParam, LPARAM lParam) 
{
	DBVARIANT dbv = {0};

	if (DBGetContactSettingTString(NULL, ModuleName, OPT_BASSPATH, &dbv)) 
	{
		DBWriteContactSettingTString(NULL, ModuleName, OPT_BASSPATH, _T("Plugins\\Bass\\Bass.dll"));
		lstrcpy(tmp, _T("Plugins\\Bass\\Bass.dll"));
	}
	else
	{
		lstrcpy(tmp, dbv.ptszVal);
	}
	
	DBFreeVariant(&dbv);
	
	CallService(MS_UTILS_PATHTOABSOLUTET, (WPARAM)tmp, (LPARAM)CurrBassPath);
	hBass = LoadLibrary(CurrBassPath);

	if (hBass != NULL)
	{
		if (LOADBASSFUNCTION(BASS_Init) != NULL && LOADBASSFUNCTION(BASS_SetConfig) != NULL &&
			LOADBASSFUNCTION(BASS_ChannelPlay) != NULL && LOADBASSFUNCTION(BASS_StreamCreateFile) != NULL &&
			LOADBASSFUNCTION(BASS_GetVersion) != NULL && LOADBASSFUNCTION(BASS_StreamFree) != NULL &&
			LOADBASSFUNCTION(BASS_GetDeviceInfo) != NULL && LOADBASSFUNCTION(BASS_Free))
		{
			int i; BASS_DEVICEINFO info;

			newBass = (BASS_SetConfig(BASS_CONFIG_DEV_DEFAULT, TRUE) != 0); // will use new "Default" device			

			if (!DBGetContactSettingString(NULL, ModuleName, OPT_OUTDEVICE, &dbv))
			{
				for (i=1; BASS_GetDeviceInfo(i, &info); i++)
					if (!lstrcmpA(dbv.pszVal, info.name))
						device = i;
			}
			DBFreeVariant(&dbv);

			sndLimSnd = DBGetContactSettingByte(NULL, ModuleName, OPT_MAXCHAN, MAXCHAN);
			if ( sndLimSnd > MAXCHAN ) sndLimSnd = MAXCHAN;
			TimeWrd1 = DBGetContactSettingWord(NULL, ModuleName, OPT_TIME1, 0);
			TimeWrd2 = DBGetContactSettingWord(NULL, ModuleName, OPT_TIME2, 0);
			QuietTime = DBGetContactSettingByte(NULL, ModuleName, OPT_QUIETTIME, 0);
			EnPreview = DBGetContactSettingByte(NULL, ModuleName, OPT_PREVIEW, 0);

			StatMask = DBGetContactSettingWord(NULL, ModuleName, OPT_STATUS, 0x3ff);

			ClistHWND = (HWND)CallService("CLUI/GetHwnd", 0, 0);
			if (DBGetContactSettingByte(NULL, ModuleName, OPT_DEVOPEN, 1))
				BASS_Init(device, 44100, 0, ClistHWND, NULL);
			else
				CallService(MS_TTB_SETBUTTONSTATE, (WPARAM)hTBButton, TTBST_PUSHED);

			Volume = DBGetContactSettingByte(NULL, ModuleName, OPT_VOLUME, 33);
			BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, Volume * 100 );
			HookEvent(ME_SKIN_PLAYINGSOUND, OnPlaySnd);
		}
		else
		{
			FreeLibrary(hBass);
			hBass = NULL;
		}
	}

	HookEvent(ME_OPT_INITIALISE, OptionsInit);
	return 0;
}

int OnShutdown(WPARAM wParam, LPARAM lParam) 
{
	if(hBass != NULL)
	{
		BASS_Free();
		FreeLibrary(hBass);
	}

	return 0;
}

extern "C" int __declspec(dllexport) Load(void)
{
	mir_getLP(&pluginInfo);

	HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
	HookEvent(ME_SYSTEM_SHUTDOWN, OnShutdown);
	HookEvent(ME_TTB_MODULELOADED, OnToolbarLoaded);
	
	hService = CreateServiceFunction("BASSinterface/BASSSoundOnOff", BASSSoundOnOff);

	InitIcons();

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	DestroyServiceFunction(hService);
	return 0;
}