Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / msvc / winmain.cc
1 /*
2  * Copyright (C) 2014 John Emmas <john@creativepost.co.uk>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 int ardour_main (int argc, char *argv[]);
20
21 #if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
22
23 #include <fcntl.h>
24 #include <shellapi.h>
25
26 bool IsAConsolePort (HANDLE handle)
27 {
28 DWORD mode;
29
30         return (GetConsoleMode(handle, &mode) != 0);
31 }
32
33 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
34 {
35 int   ret  = (-1);
36 char  szPathToProgram[768];
37 char* argv[256];
38
39         // Essential!!  Make sure that any files used by Ardour
40         //              will be created or opened in BINARY mode!
41         _fmode = O_BINARY;
42
43         GetModuleFileName (NULL, (LPSTR)szPathToProgram, (DWORD)sizeof(szPathToProgram));
44         argv[0] = new char[(strlen(szPathToProgram) + 1)];
45
46         if (argv[0])
47         {
48                 LPWSTR  lpwCmdLine         = 0;
49                 int     count, nArgs, argc = 1;
50                 size_t  argStringLen       = strlen(lpCmdLine);
51
52                 // Copy the program path to argv[0]
53                 strcpy (argv[0], szPathToProgram);
54
55                 // Parse the user's command line and add any parameters to argv
56                 if (argStringLen)
57                 {
58                         lpwCmdLine = new wchar_t[argStringLen+1];
59                         mbstowcs (lpwCmdLine, lpCmdLine, argStringLen+1);
60
61                         LPWSTR* pwArgv = CommandLineToArgvW ((LPCWSTR)lpwCmdLine, &nArgs);
62
63                         if (pwArgv && nArgs)
64                         {
65                                 for (count = 1; count <= nArgs; count++)
66                                 {
67                                         int argChars = wcslen (pwArgv[count-1]);
68                                         if (0 != (argv[count] = new char[(argChars+1)]))
69                                         {
70                                                 argc++;
71                                                 wcstombs (argv[count], pwArgv[count-1], argChars+1);
72
73                                                 // Append a NULL to the argv vector
74                                                 if (argc < 255)
75                                                         argv[count+1] = 0;
76                                         }
77                                 }
78                         }
79
80                         if (pwArgv)
81                                 LocalFree (pwArgv);
82                 }
83
84                 // If the user started Mixbus from a console, re-attach
85                 // to the console so we can see 'printf()' output etc.
86                 FILE  *pStdOut = 0, *pStdErr = 0;
87                 BOOL  bConsole = AttachConsole(ATTACH_PARENT_PROCESS);
88                 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
89
90                 if ((bConsole) && (IsAConsolePort(hStdOut)))
91                 {
92                         pStdOut = freopen( "CONOUT$", "w", stdout );
93                         pStdErr = freopen( "CONOUT$", "w", stderr );
94                 }
95
96                 ret = ardour_main (argc, argv);
97
98                 if (pStdOut)
99                         fclose (pStdOut);
100                 if (pStdErr)
101                         fclose (pStdErr);
102
103                 if (bConsole)
104                 {
105                         // Detach and free the console from our application
106                         INPUT_RECORD input_record;
107
108                         input_record.EventType = KEY_EVENT;
109                         input_record.Event.KeyEvent.bKeyDown = TRUE;
110                         input_record.Event.KeyEvent.dwControlKeyState = 0;
111                         input_record.Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
112                         input_record.Event.KeyEvent.wRepeatCount      = 1;
113                         input_record.Event.KeyEvent.wVirtualKeyCode   = VK_RETURN;
114                         input_record.Event.KeyEvent.wVirtualScanCode  = MapVirtualKey( VK_RETURN, 0 );
115
116                         DWORD written = 0;
117                         WriteConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &input_record, 1, &written );
118
119                         FreeConsole();
120                 }
121
122                 for (count = 0; count < argc; count++)
123                         delete[] argv[count];
124
125                 if (lpwCmdLine)
126                         delete[] lpwCmdLine;
127         }
128
129         return (ret);
130 }
131
132 #endif