BOOL FileExists(TCHAR *file)
{
	return FileOrDirExists(file, FALSE);
}
Example #2
0
// _type: 0=Post-Fader (Post-Pan), 1=Pre-FX, 2=deprecated, 3=Pre-Fader (Post-FX)
// _undoMsg: NULL=no undo
bool CueBuss(const char* _undoMsg, const char* _busName, int _type, bool _showRouting,
			 int _soloDefeat, char* _trTemplatePath, bool _sendToMaster, int* _hwOuts) 
{
	if (!SNM_CountSelectedTracks(NULL, false))
		return false;

	WDL_FastString tmplt;
	if (_trTemplatePath && (!FileOrDirExists(_trTemplatePath) || !LoadChunk(_trTemplatePath, &tmplt) || !tmplt.GetLength()))
	{
		char msg[SNM_MAX_PATH] = "";
		lstrcpyn(msg, __LOCALIZE("Cue buss not created!\nNo track template file defined","sws_DLG_149"), sizeof(msg));
		if (*_trTemplatePath)
			_snprintfSafe(msg, sizeof(msg), __LOCALIZE_VERFMT("Cue buss not created!\nTrack template not found (or empty): %s","sws_DLG_149"), _trTemplatePath);
		MessageBox(GetMainHwnd(), msg, __LOCALIZE("S&M - Error","sws_DLG_149"), MB_OK);
		return false;
	}

	bool updated = false;
	MediaTrack * cueTr = NULL;
	SNM_SendPatcher* p = NULL;
	for (int i=1; i <= GetNumTracks(); i++) // skip master
	{
		MediaTrack* tr = CSurf_TrackFromID(i, false);
		if (tr && *(int*)GetSetMediaTrackInfo(tr, "I_SELECTED", NULL))
		{
			GetSetMediaTrackInfo(tr, "I_SELECTED", &g_i0);

			// add the buss track, done once!
			if (!cueTr)
			{
				InsertTrackAtIndex(GetNumTracks(), false);
				TrackList_AdjustWindows(false);
				cueTr = CSurf_TrackFromID(GetNumTracks(), false);
				GetSetMediaTrackInfo(cueTr, "P_NAME", (void*)_busName);

				p = new SNM_SendPatcher(cueTr);

				if (tmplt.GetLength())
				{
					WDL_FastString chunk;
					MakeSingleTrackTemplateChunk(&tmplt, &chunk, true, true, false);
					ApplyTrackTemplate(cueTr, &chunk, false, false, p);
				}
				updated = true;
			}

			// add a send
			if (cueTr && p && tr != cueTr)
				AddReceiveWithVolPan(tr, cueTr, _type, p);
		}
	}

	if (cueTr && p)
	{
		// send to master/parent init
		if (!tmplt.GetLength())
		{
			// solo defeat
			if (_soloDefeat) {
				char one[2] = "1";
				updated |= (p->ParsePatch(SNM_SET_CHUNK_CHAR, 1, "TRACK", "MUTESOLO", 0, 3, one) > 0);
			}
			
			// master/parend send
			WDL_FastString mainSend;
			mainSend.SetFormatted(SNM_MAX_CHUNK_LINE_LENGTH, "MAINSEND %d 0", _sendToMaster?1:0);

			// adds hw outputs
			if (_hwOuts)
			{
				int monoHWCount=0; 
				while (GetOutputChannelName(monoHWCount)) monoHWCount++;

				bool cr = false;
				for(int i=0; i<SNM_MAX_HW_OUTS; i++)
				{
					if (_hwOuts[i])
					{
						if (!cr) {
							mainSend.Append("\n"); 
							cr = true;
						}
						if (_hwOuts[i] >= monoHWCount) 
							mainSend.AppendFormatted(32, "HWOUT %d ", (_hwOuts[i]-monoHWCount) | 1024);
						else
							mainSend.AppendFormatted(32, "HWOUT %d ", _hwOuts[i]-1);

						mainSend.Append("0 ");
						mainSend.AppendFormatted(20, "%.14f ", *(double*)GetConfigVar("defhwvol"));
						mainSend.Append("0.00000000000000 0 0 0 -1.00000000000000 -1\n");
					}
				}
				if (!cr)
					mainSend.Append("\n"); // hot
			}

			// patch both updates (no break keyword here: new empty track)
			updated |= p->ReplaceLine("TRACK", "MAINSEND", 1, 0, mainSend.Get());
		}

		p->Commit();
		delete p;

		if (updated)
		{
			GetSetMediaTrackInfo(cueTr, "I_SELECTED", &g_i1);
			UpdateTimeline();
			ScrollSelTrack(true, true);
			if (_showRouting) 
				Main_OnCommand(40293, 0);
			if (_undoMsg)
				Undo_OnStateChangeEx2(NULL, _undoMsg, UNDO_STATE_ALL, -1);
		}
	}
	return updated;
}
Example #3
0
BOOL CMortScriptApp::InitInstance()
{
    SetRegistryKey( L"Mort" );

	CParseCmdLine myCmdLine;
	myCmdLine.ParseCmdLine( m_lpCmdLine );

	TCHAR exe[MAX_PATH];
   ::GetModuleFileName(NULL,exe,MAX_PATH);

	ExecuteFileName = exe;

    CStr file = myCmdLine.Filename; // m_lpCmdLine;
    // file.TrimLeft( '\"' ); file.TrimRight( '\"' );

    if ( myCmdLine.RegOnly || file.IsEmpty() )
    {
        RegisterFileClass();
        RegisterFileType( L".jscr", L"JScripts" );
        RegisterFileType( L".mscr", L"JScripts" );
		if ( !myCmdLine.RegOnly )
		{
			MessageBox( NULL
					  , L".jscr and .mscr extensions registered.\nPlease run any .jscr/.mscr file or read the manual.\n\n"
						L"(c) Mirko Schenk 2005-2007"
					  , L"JScripts V" + CStr( VERSION_INFO )
					  , MB_OK|MB_SETFOREGROUND );
		}
    }
	else
	{
		if (   file.GetLength() >= 4
			/* file.GetLength() >= 8 && file.Right(8).CompareNoCase( L".mortrun" ) == 0
			|| file.GetLength() >= 5 && file.Right(5).CompareNoCase( L".mscr" ) == 0 */
		   )
		{
            CStr mutexName = file;
            mutexName.MakeLower();
#ifdef DESKTOP
			// Windows XP doesn't like some path characters in the mutex' name
			mutexName.Replace( ':', '_' );
			mutexName.Replace( '\\', '/' );
#endif
          MutexName = (LPCTSTR)mutexName;

	        HANDLE mutex = ::CreateMutex(NULL, FALSE, MutexName);

            if ( mutex!=NULL )
	        {
				int exists = ::GetLastError();
		        if ( exists == ERROR_ALREADY_EXISTS) 
		        {
					DWORD procId = GetRunningScriptProcId( file );

					if ( procId != NULL )
					{
						/*
						 CString msg;
						 msg.Format( L"Process ID: %08x", procId );
						 MessageBox( NULL, msg, L"Debug", MB_SETFOREGROUND );
						*/

						FindAppT findApp;
						findApp.procId = procId;
						findApp.hWnd   = NULL;
						::EnumWindows( FindApplicationWindowProc, (LPARAM)&findApp );
						if ( findApp.hWnd != NULL )
						{
							// msg.Format( L"Set foreground window: %08x", findApp.hWnd );
							// MessageBox( NULL, msg, L"Debug", MB_SETFOREGROUND );
							::SetForegroundWindow( findApp.hWnd );
						}
					}
					else
						exists = 0;

					//MessageBox( NULL, L"Process opened", L"Debug", MB_SETFOREGROUND );
					/*
						TCHAR procName[256];
				    	::GetModuleFileName((HMODULE)procId,procName,256);
						//MessageBox( NULL, procName, L"Debug", MB_SETFOREGROUND );
						if ( CString(procName).Right(14).CompareNoCase( L"MortScript.exe" ) == 0 )
						{
							int aw = MessageBox( NULL
											   , L"Script seems to be running. Cancel old script?"
											   , L"Script already running"
											   , MB_YESNO|MB_SETFOREGROUND
											   );
							if ( aw == IDYES )
							{
								RegWriteDW( HKEY_CURRENT_USER, L"Software\\JScripts\\Abort", MutexName, 1 );
								DWORD exitCode = 0;
								SetCursor(LoadStandardCursor(IDC_WAIT));
								for ( int i=0; i<=10; i++ )
								{
									Sleep(1000);
									if ( GetExitCodeProcess( hProc, &exitCode ) == FALSE )
									{
										//MessageBox( NULL, L"GetExitCode failed", L"Debug", MB_SETFOREGROUND );
										exitCode = 0;
										break;
									}
									else
									{
										if ( exitCode != STILL_ACTIVE )
										{
											//MessageBox( NULL, L"No longer active", L"Debug", MB_SETFOREGROUND );
											break;
										}
									}
								}
								SetCursor(LoadStandardCursor(IDC_ARROW)); 
								if ( exitCode == STILL_ACTIVE )
								{
									int aw = MessageBox( NULL
													   , L"Script seems to be hanging or busy. Terminate old script?"
													   , L"Script still running"
													   , MB_YESNO|MB_SETFOREGROUND
													   );
									if ( aw == IDYES )
									{
										TerminateProcess( hProc, 0 );
									}
								}
							}
						}
						else
						{
							exists = 0;
						}

						CloseHandle( hProc );
					}
					else
					{
						exists = 0;
					}
					*/
                }

                if ( exists != ERROR_ALREADY_EXISTS )
                {
	                HKEY    key;
	                if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, 0, &key ) == ERROR_SUCCESS )
	                {
                        RegDeleteValue( key, MutexName );
		                RegCloseKey( key );
	                }

					DWORD currProcId = GetCurrentProcessId();
					//CString dbg;
					//dbg.Format(L"ProcId: %d", GetCurrentProcessId());
					//MessageBox( NULL, dbg, L"Debug", MB_SETFOREGROUND );

					// Remove old script entries with same process id
	                if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, 0, &key ) == ERROR_SUCCESS )
	                {
						int   idx;
						TCHAR valName[MAX_PATH];
						DWORD valSize = MAX_PATH, type;
						DWORD value, valueSize = sizeof(DWORD);

						CStrArray oldProcesses;
						for ( idx = 0; RegEnumValue( key, idx, valName, &valSize, NULL, &type, (BYTE*)&value, &valueSize ) == ERROR_SUCCESS; idx++ )
						{
							if ( type == REG_DWORD && value == currProcId )
							{
								oldProcesses.Add( valName );
							}
							valSize = MAX_PATH;
							valueSize = sizeof(DWORD);
						}

						for ( idx = 0; idx < oldProcesses.GetSize(); idx++ )
						{
							RegDeleteValue( key, oldProcesses.GetAt(idx) );
						}

		                RegCloseKey( key );
					}

					RegWriteDW( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", MutexName, currProcId );

					if ( myCmdLine.WaitForFile > 0 && (long)FileOrDirExists( file, 0 ) == 0 )
					{
						CDlgWait wait(NULL);
						wait.AllowOK = TRUE;
						wait.Countdown = myCmdLine.WaitForFile;
						wait.Expression = L"FileExists(\"" + file + L"\")";
						wait.Title = L"JScripts";
						wait.m_Label = L"Script " + file + " not found, waiting for existance (storage card not initialized?)";
						wait.DoModal();
					}

					if ( myCmdLine.WaitForFile <= 0 || (long)FileOrDirExists( file, 0 ) == 1 )
					{
						g_hInst = theApp.m_hInstance;

						SYSTEMTIME now;
						GetLocalTime( &now );
						long seed = SystemTimeToUnixTime( now );
						srand( seed );

						AppPath = m_pszHelpFilePath;
						AppPath = AppPath.Left( AppPath.ReverseFind('\\') );

						ScriptAborted = CreateEvent( 0, TRUE, FALSE, CStr(MutexName)+L"ScriptAborted" );
					    StatusDialog = new CDlgStatus( file );
						//status.DoModal();

						CInterpreter interpreter;
						//Debug(file);

						//jwz:modi
						interpreter.RunFile( file );
						interpreter.Parser();
						//jwz:modi end

						if ( StatusWindow != NULL )
						{
							StatusDialog->ScriptFinished = TRUE;
							SendMessage( StatusWindow, WM_EXIT_STATUS, 0, 0 );
							WaitForSingleObject( StatusDialogFinished, INFINITE );
							CloseHandle( StatusDialogFinished );
						}

						delete StatusDialog;

						UnloadToolhelp();

						CloseHandle( ScriptAborted );
						ReleaseMutex( mutex );

						Variables.RemoveAll();
						for ( int i = 0; i<LocalVariablesStack.GetSize(); i++ )
							delete (CMapStrToValue*)LocalVariablesStack.GetAt(i);

						POSITION pos = FileHandles.GetStartPosition();
						CStr key; void *value;
						while ( pos != 0 )
						{
							FileHandles.GetNextAssoc( pos, key, value );
							if ( value != NULL )
							{
								delete (CFileInfo*)value;
							}
						}
						FileHandles.RemoveAll();

						if ( ChoiceFont != NULL ) DeleteObject( ChoiceFont );
						if ( StatusListFont != NULL ) DeleteObject( StatusListFont );
					}

					if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, KEY_WRITE, &key ) == ERROR_SUCCESS )
	                {
                        RegDeleteValue( key, MutexName );
		                RegCloseKey( key );
	                }
                }
                CloseHandle( mutex );
            }
			else
			{
				int error = ::GetLastError();
				MessageBox( NULL
						  , L"Error creating mutex"
						  , L"JScripts"
						  , MB_OK|MB_SETFOREGROUND );
			}
		}
		else
		{
			MessageBox( NULL
					  , L"Invalid file type for MortScript"
					  , L"JScripts"
					  , MB_OK|MB_SETFOREGROUND );
		}
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
Example #4
0
WDL_DLGRET CueBussDlgProc(HWND _hwnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam)
{
	if (INT_PTR r = SNM_HookThemeColorsMessage(_hwnd, _uMsg, _wParam, _lParam))
		return r;

	const char cWndPosKey[] = "CueBus Window Pos";
	switch(_uMsg)
	{
		case WM_INITDIALOG:
		{
			WDL_UTF8_HookComboBox(GetDlgItem(_hwnd, IDC_SNM_CUEBUS_TYPE));
			WDL_UTF8_HookComboBox(GetDlgItem(_hwnd, IDC_COMBO));
			for(int i=0; i < SNM_MAX_HW_OUTS; i++)
				WDL_UTF8_HookComboBox(GetDlgItem(_hwnd, IDC_SNM_CUEBUS_HWOUT1+i));

			RestoreWindowPos(_hwnd, cWndPosKey, false);
			char buf[16] = "";
			for(int i=0; i < SNM_MAX_CUE_BUSS_CONFS; i++)
				if (_snprintfStrict(buf,sizeof(buf),"%d",i+1) > 0)
					SendDlgItemMessage(_hwnd,IDC_COMBO,CB_ADDSTRING,0,(LPARAM)buf);
			SendDlgItemMessage(_hwnd,IDC_COMBO,CB_SETCURSEL,0,0);
			FillCueBussDlg(_hwnd);
			return 0;
		}
		break;

		case WM_CLOSE :
			g_cueBussHwnd = NULL; // for proper toggle state report, see openCueBussWnd()
			break;

		case WM_COMMAND :
			switch(LOWORD(_wParam))
			{
				case IDC_COMBO:
					if(HIWORD(_wParam) == CBN_SELCHANGE) // config id update?
					{ 
						int id = (int)SendDlgItemMessage(_hwnd, IDC_COMBO, CB_GETCURSEL, 0, 0);
						if (id != CB_ERR) {
							g_cueBussConfId = id;
							FillCueBussDlg();
						}
					}
					break;
				case IDOK:
					CueBuss(__LOCALIZE("Create cue buss from track selection","sws_undo"), g_cueBussConfId);
					return 0;
				case IDCANCEL:
					g_cueBussHwnd = NULL; // for proper toggle state report, see openCueBussWnd()
					ShowWindow(_hwnd, SW_HIDE);
					return 0;
				case IDC_FILES: {
					char curPath[SNM_MAX_PATH]="";
					GetDlgItemText(_hwnd, IDC_SNM_CUEBUS_TEMPLATE, curPath, sizeof(curPath));
					if (!*curPath || !FileOrDirExists(curPath))
						if (_snprintfStrict(curPath, sizeof(curPath), "%s%cTrackTemplates", GetResourcePath(), PATH_SLASH_CHAR) <= 0)
							*curPath = '\0';
					if (char* fn = BrowseForFiles(__LOCALIZE("S&M - Load track template","sws_DLG_149"), curPath, NULL, false, "REAPER Track Template (*.RTrackTemplate)\0*.RTrackTemplate\0")) {
						SetDlgItemText(_hwnd,IDC_SNM_CUEBUS_TEMPLATE,fn);
						free(fn);
						SaveCueBussSettings();
					}
					break;
				}
				case IDC_CHECK3: {
					bool templateEnable = (IsDlgButtonChecked(_hwnd, IDC_CHECK3) == 1);
					EnableWindow(GetDlgItem(_hwnd, IDC_SNM_CUEBUS_TEMPLATE), templateEnable);
					EnableWindow(GetDlgItem(_hwnd, IDC_FILES), templateEnable);
					EnableWindow(GetDlgItem(_hwnd, IDC_SNM_CUEBUS_NAME), !templateEnable);
					for(int k=0; k < SNM_MAX_HW_OUTS ; k++)
						EnableWindow(GetDlgItem(_hwnd, IDC_SNM_CUEBUS_HWOUT1+k), !templateEnable);
					EnableWindow(GetDlgItem(_hwnd, IDC_CHECK1), !templateEnable);
					EnableWindow(GetDlgItem(_hwnd, IDC_CHECK4), !templateEnable);
//					SetFocus(GetDlgItem(_hwnd, templateEnable ? IDC_SNM_CUEBUS_TEMPLATE : IDC_SNM_CUEBUS_NAME));
					SaveCueBussSettings();
					break;
				}
				case IDC_SNM_CUEBUS_SOLOGRP:
				case IDC_CHECK1:
				case IDC_CHECK2:
				case IDC_CHECK4:
					SaveCueBussSettings();
					break;
				case IDC_SNM_CUEBUS_TYPE:
				case IDC_SNM_CUEBUS_HWOUT1:
				case IDC_SNM_CUEBUS_HWOUT2:
				case IDC_SNM_CUEBUS_HWOUT3:
				case IDC_SNM_CUEBUS_HWOUT4:
				case IDC_SNM_CUEBUS_HWOUT5:
				case IDC_SNM_CUEBUS_HWOUT6:
				case IDC_SNM_CUEBUS_HWOUT7:
				case IDC_SNM_CUEBUS_HWOUT8:
					if(HIWORD(_wParam) == CBN_SELCHANGE)
						SaveCueBussSettings();
					break;
				case IDC_SNM_CUEBUS_TEMPLATE:
				case IDC_SNM_CUEBUS_NAME:
					if (HIWORD(_wParam)==EN_CHANGE)
						SaveCueBussSettings();
					break;
			}
			break;

		case WM_DESTROY:
			SaveWindowPos(_hwnd, cWndPosKey);
			break;
	}
	return 0;
}