summaryrefslogtreecommitdiff
path: root/gtk2_ardour/msvc/winmain.cc
blob: b9dd7d9b01386d876b2056715761edea9ef008bb (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
/*
 * Copyright (C) 2014 John Emmas <john@creativepost.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

int ardour_main (int argc, char *argv[]);

#if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))

#include <fcntl.h>
#include <shellapi.h>

bool IsAConsolePort (HANDLE handle)
{
DWORD mode;

	return (GetConsoleMode(handle, &mode) != 0);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int   ret  = (-1);
char  szPathToProgram[768];
char* argv[256];

	// Essential!!  Make sure that any files used by Ardour
	//              will be created or opened in BINARY mode!
	_fmode = O_BINARY;

	GetModuleFileName (NULL, (LPSTR)szPathToProgram, (DWORD)sizeof(szPathToProgram));
	argv[0] = new char[(strlen(szPathToProgram) + 1)];

	if (argv[0])
	{
		LPWSTR  lpwCmdLine         = 0;
		int     count, nArgs, argc = 1;
		size_t  argStringLen       = strlen(lpCmdLine);

		// Copy the program path to argv[0]
		strcpy (argv[0], szPathToProgram);

		// Parse the user's command line and add any parameters to argv
		if (argStringLen)
		{
			lpwCmdLine = new wchar_t[argStringLen+1];
			mbstowcs (lpwCmdLine, lpCmdLine, argStringLen+1);

			LPWSTR* pwArgv = CommandLineToArgvW ((LPCWSTR)lpwCmdLine, &nArgs);

			if (pwArgv && nArgs)
			{
				for (count = 1; count <= nArgs; count++)
				{
					int argChars = wcslen (pwArgv[count-1]);
					if (0 != (argv[count] = new char[(argChars+1)]))
					{
						argc++;
						wcstombs (argv[count], pwArgv[count-1], argChars+1);

						// Append a NULL to the argv vector
						if (argc < 255)
							argv[count+1] = 0;
					}
				}
			}

			if (pwArgv)
				LocalFree (pwArgv);
		}

		// If the user started Mixbus from a console, re-attach
		// to the console so we can see 'printf()' output etc.
		FILE  *pStdOut = 0, *pStdErr = 0;
		BOOL  bConsole = AttachConsole(ATTACH_PARENT_PROCESS);
		HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

		if ((bConsole) && (IsAConsolePort(hStdOut)))
		{
			pStdOut = freopen( "CONOUT$", "w", stdout );
			pStdErr = freopen( "CONOUT$", "w", stderr );
		}

		ret = ardour_main (argc, argv);

		if (pStdOut)
			fclose (pStdOut);
		if (pStdErr)
			fclose (pStdErr);

		if (bConsole)
		{
			// Detach and free the console from our application
			INPUT_RECORD input_record;

			input_record.EventType = KEY_EVENT;
			input_record.Event.KeyEvent.bKeyDown = TRUE;
			input_record.Event.KeyEvent.dwControlKeyState = 0;
			input_record.Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
			input_record.Event.KeyEvent.wRepeatCount      = 1;
			input_record.Event.KeyEvent.wVirtualKeyCode   = VK_RETURN;
			input_record.Event.KeyEvent.wVirtualScanCode  = MapVirtualKey( VK_RETURN, 0 );

			DWORD written = 0;
			WriteConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &input_record, 1, &written );

			FreeConsole();
		}

		for (count = 0; count < argc; count++)
			delete[] argv[count];

		if (lpwCmdLine)
			delete[] lpwCmdLine;
	}

	return (ret);
}

#endif