add plural forms for pt to gtk2_ardour/po/pt.po
[ardour.git] / gtk2_ardour / msvc / winmain.cc
1 /*
2     Copyright (C) 2001-2012 Paul Davis
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 int ardour_main (int argc, char *argv[]);
21
22 #if (defined(COMPILER_MSVC) && defined(NDEBUG) && !defined(RDC_BUILD))
23
24 #include <fcntl.h>
25 #include <shellapi.h>
26
27 bool IsAConsolePort (HANDLE handle)
28 {
29 DWORD mode;
30
31         return (GetConsoleMode(handle, &mode) != 0);
32 }
33
34 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
35 {
36 int   ret  = (-1);
37 char  szPathToProgram[768];
38 char* argv[256];
39
40         // Essential!!  Make sure that any files used by Ardour
41         //              will be created or opened in BINARY mode!
42         _fmode = O_BINARY;
43
44         GetModuleFileName (NULL, (LPSTR)szPathToProgram, (DWORD)sizeof(szPathToProgram));
45         argv[0] = new char[(strlen(szPathToProgram) + 1)];
46
47         if (argv[0])
48         {
49                 LPWSTR  lpwCmdLine         = 0;
50                 int     count, nArgs, argc = 1;
51                 size_t  argStringLen       = strlen(lpCmdLine);
52
53                 // Copy the program path to argv[0]
54                 strcpy (argv[0], szPathToProgram);
55
56                 // Parse the user's command line and add any parameters to argv
57                 if (argStringLen)
58                 {
59                         lpwCmdLine = new wchar_t[argStringLen+1];
60                         mbstowcs (lpwCmdLine, lpCmdLine, argStringLen+1);
61
62                         LPWSTR* pwArgv = CommandLineToArgvW ((LPCWSTR)lpwCmdLine, &nArgs);
63
64                         if (pwArgv && nArgs)
65                         {
66                                 for (count = 1; count <= nArgs; count++)
67                                 {
68                                         int argChars = wcslen (pwArgv[count-1]);
69                                         if (0 != (argv[count] = new char[(argChars+1)]))
70                                         {
71                                                 argc++;
72                                                 wcstombs (argv[count], pwArgv[count-1], argChars+1);
73
74                                                 // Append a NULL to the argv vector
75                                                 if (argc < 255)
76                                                         argv[count+1] = 0;
77                                         }
78                                 }
79                         }
80
81                         if (pwArgv)
82                                 LocalFree (pwArgv);
83                 }
84
85                 // If the user started Mixbus from a console, re-attach
86                 // to the console so we can see 'printf()' output etc.
87                 FILE  *pStdOut = 0, *pStdErr = 0;
88                 BOOL  bConsole = AttachConsole(ATTACH_PARENT_PROCESS);
89                 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
90
91                 if ((bConsole) && (IsAConsolePort(hStdOut)))
92                 {
93                         pStdOut = freopen( "CONOUT$", "w", stdout );
94                         pStdErr = freopen( "CONOUT$", "w", stderr );
95                 }
96
97                 ret = ardour_main (argc, argv);
98
99                 if (pStdOut)
100                         fclose (pStdOut);
101                 if (pStdErr)
102                         fclose (pStdErr);
103
104                 if (bConsole)
105                 {
106                         // Detach and free the console from our application
107                         INPUT_RECORD input_record;
108
109                         input_record.EventType = KEY_EVENT;
110                         input_record.Event.KeyEvent.bKeyDown = TRUE;
111                         input_record.Event.KeyEvent.dwControlKeyState = 0;
112                         input_record.Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
113                         input_record.Event.KeyEvent.wRepeatCount      = 1;
114                         input_record.Event.KeyEvent.wVirtualKeyCode   = VK_RETURN;
115                         input_record.Event.KeyEvent.wVirtualScanCode  = MapVirtualKey( VK_RETURN, 0 );
116
117                         DWORD written = 0;
118                         WriteConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &input_record, 1, &written );
119
120                         FreeConsole();
121                 }
122
123                 for (count = 0; count < argc; count++)
124                         delete[] argv[count];
125
126                 if (lpwCmdLine)
127                         delete[] lpwCmdLine;
128         }
129
130         return (ret);
131 }
132
133 #endif