midimap: Handle MIDI running status.
[wine.git] / programs / winevdm / winevdm.c
blob5b5f72db2530d7a41eec2be8d2f400ee11e8f6e7
1 /*
2 * Wine virtual DOS machine
4 * Copyright 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wine/winbase16.h"
30 #include "winuser.h"
31 #include "wincon.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
36 #define DOSBOX "dosbox"
38 /*** PIF file structures ***/
39 #include "pshpack1.h"
41 /* header of a PIF file */
42 typedef struct {
43 BYTE unk1[2]; /* 0x00 */
44 CHAR windowtitle[ 30 ]; /* 0x02 seems to be padded with blanks*/
45 WORD memmax; /* 0x20 */
46 WORD memmin; /* 0x22 */
47 CHAR program[63]; /* 0x24 seems to be zero terminated */
48 BYTE hdrflags1; /* 0x63 various flags:
49 * 02 286: text mode selected
50 * 10 close window at exit
52 BYTE startdrive; /* 0x64 */
53 char startdir[64]; /* 0x65 */
54 char optparams[64]; /* 0xa5 seems to be zero terminated */
55 BYTE videomode; /* 0xe5 */
56 BYTE unkn2; /* 0xe6 ?*/
57 BYTE irqlow; /* 0xe7 */
58 BYTE irqhigh; /* 0xe8 */
59 BYTE rows; /* 0xe9 */
60 BYTE cols; /* 0xea */
61 BYTE winY; /* 0xeb */
62 BYTE winX; /* 0xec */
63 WORD unkn3; /* 0xed 7??? */
64 CHAR unkn4[64]; /* 0xef */
65 CHAR unkn5[64]; /* 0x12f */
66 BYTE hdrflags2; /* 0x16f */
67 BYTE hdrflags3; /* 0x170 */
68 } pifhead_t;
70 /* record header: present on every record */
71 typedef struct {
72 CHAR recordname[16]; /* zero terminated */
73 WORD posofnextrecord; /* file offset, 0xffff if last */
74 WORD startofdata; /* file offset */
75 WORD sizeofdata; /* data is expected to follow directly */
76 } recordhead_t;
78 /* 386 -enhanced mode- record */
79 typedef struct {
80 WORD memmax; /* memory desired, overrides the pif header*/
81 WORD memmin; /* memory required, overrides the pif header*/
82 WORD prifg; /* foreground priority */
83 WORD pribg; /* background priority */
84 WORD emsmax; /* EMS memory limit */
85 WORD emsmin; /* EMS memory required */
86 WORD xmsmax; /* XMS memory limit */
87 WORD xmsmin; /* XMS memory required */
88 WORD optflags; /* option flags:
89 * 0008 full screen
90 * 0004 exclusive
91 * 0002 background
92 * 0001 close when active
94 WORD memflags; /* various memory flags*/
95 WORD videoflags; /* video flags:
96 * 0010 text
97 * 0020 med. res. graphics
98 * 0040 hi. res. graphics
100 WORD hotkey[9]; /* Hot key info */
101 CHAR optparams[64]; /* optional params, replaces those in the pif header */
102 } pif386rec_t;
104 #include "poppack.h"
106 /***********************************************************************
107 * start_dosbox
109 static void start_dosbox( const char *appname, const char *args )
111 const WCHAR *config_dir = _wgetenv( L"WINECONFIGDIR" );
112 WCHAR path[MAX_PATH], config[MAX_PATH];
113 HANDLE file;
114 char *p, *prefix, *buffer, app[MAX_PATH];
115 int i;
116 NTSTATUS ret = STATUS_OBJECT_NAME_NOT_FOUND;
117 DWORD written, drives = GetLogicalDrives();
119 if (!config_dir || !(prefix = wine_get_unix_file_name( config_dir ))) return;
120 if (!GetTempPathW( MAX_PATH, path )) return;
121 if (!GetTempFileNameW( path, L"cfg", 0, config )) return;
122 if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
123 if (!GetShortPathNameA( appname, app, MAX_PATH )) return;
124 GetShortPathNameW( path, path, MAX_PATH );
125 file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
126 if (file == INVALID_HANDLE_VALUE) return;
128 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
129 sizeof("mount -z c") + sizeof("config -securemode") +
130 26 * (strlen(prefix) + sizeof("mount c /dosdevices/c:")) +
131 4 * lstrlenW( path ) +
132 6 + strlen( app ) + strlen( args ) + 20 );
133 p = buffer;
134 p += sprintf( p, "[autoexec]\n" );
135 for (i = 25; i >= 0; i--)
136 if (!(drives & (1 << i)))
138 p += sprintf( p, "mount -z %c\n", 'a' + i );
139 break;
141 for (i = 0; i <= 25; i++)
143 if (!(drives & (1 << i))) continue;
144 p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, prefix, 'a' + i );
146 p += sprintf( p, "%c:\ncd ", path[0] );
147 p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * lstrlenW(path), NULL, NULL ) - 1;
148 p += sprintf( p, "\nconfig -securemode\n" );
149 p += sprintf( p, "%s %s\n", app, args );
150 p += sprintf( p, "exit\n" );
151 if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
153 const char *args[5];
154 char *config_file = wine_get_unix_file_name( config );
155 args[0] = DOSBOX;
156 args[1] = "-userconf";
157 args[2] = "-conf";
158 args[3] = config_file;
159 args[4] = NULL;
160 ret = __wine_unix_spawnvp( (char **)args, TRUE );
162 CloseHandle( file );
163 DeleteFileW( config );
164 HeapFree( GetProcessHeap(), 0, buffer );
165 if (FAILED(ret)) MESSAGE( "winevdm: %s is a DOS application, you need to install DOSBox.\n", appname );
166 ExitProcess( ret );
170 /***********************************************************************
171 * read_pif_file
172 *pif386rec_tu
173 * Read a pif file and return the header and possibly the 286 (real mode)
174 * record or 386 (enhanced mode) record. Returns FALSE if the file is
175 * invalid otherwise TRUE.
177 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
178 char *optparams, char *startdir, int *closeonexit, int *textmode)
180 DWORD nread;
181 LARGE_INTEGER filesize;
182 recordhead_t rhead;
183 BOOL found386rec = FALSE;
184 pif386rec_t pif386rec;
185 pifhead_t pifheader;
186 if( !GetFileSizeEx( hFile, &filesize) ||
187 filesize.QuadPart < (sizeof(pifhead_t) + sizeof(recordhead_t))) {
188 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
189 return FALSE;
191 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
192 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
193 return FALSE;
194 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
195 wine_dbgstr_a(pifheader.program),
196 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
197 wine_dbgstr_a(pifheader.startdir),
198 wine_dbgstr_a(pifheader.optparams));
199 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
200 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
201 pifheader.videomode);
202 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
203 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
204 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
205 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
206 WINE_ERR("Invalid pif file: magic string not found\n");
207 return FALSE;
209 /* now process the following records */
210 while( 1) {
211 WORD nextrecord = rhead.posofnextrecord;
212 if( (nextrecord & 0x8000) ||
213 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
214 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
215 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
216 return FALSE;
217 if( !rhead.recordname[0]) continue; /* deleted record */
218 WINE_TRACE("reading record %s size %d next 0x%x\n",
219 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
220 rhead.posofnextrecord );
221 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
222 found386rec = TRUE;
223 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
224 WINE_TRACE("386rec: memory req'd %d des'd %d EMS req'd %d des'd %d XMS req'd %d des'd %d\n",
225 pif386rec.memmin, pif386rec.memmax,
226 pif386rec.emsmin, pif386rec.emsmax,
227 pif386rec.xmsmin, pif386rec.xmsmax);
228 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
229 pif386rec.optflags, pif386rec.memflags,
230 pif386rec.videoflags);
231 WINE_TRACE("386rec: optional parameters %s\n",
232 wine_dbgstr_a(pif386rec.optparams));
235 /* prepare the return data */
236 lstrcpynA( progname, pifheader.program, sizeof(pifheader.program)+1);
237 lstrcpynA( title, pifheader.windowtitle, sizeof(pifheader.windowtitle)+1);
238 if( found386rec)
239 lstrcpynA( optparams, pif386rec.optparams, sizeof( pif386rec.optparams)+1);
240 else
241 lstrcpynA( optparams, pifheader.optparams, sizeof(pifheader.optparams)+1);
242 lstrcpynA( startdir, pifheader.startdir, sizeof(pifheader.startdir)+1);
243 *closeonexit = pifheader.hdrflags1 & 0x10;
244 *textmode = found386rec ? pif386rec.videoflags & 0x0010
245 : pifheader.hdrflags1 & 0x0002;
246 return TRUE;
249 /***********************************************************************
250 * pif_cmd
252 * execute a pif file.
254 static VOID pif_cmd( char *filename, char *cmdline)
256 HANDLE hFile;
257 char progpath[MAX_PATH];
258 char buf[308];
259 char progname[64];
260 char title[31];
261 char optparams[65];
262 char startdir[65];
263 char *p;
264 int closeonexit;
265 int textmode;
266 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
267 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
269 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
270 return;
272 if( !read_pif_file( hFile, progname, title, optparams, startdir,
273 &closeonexit, &textmode)) {
274 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
275 CloseHandle( hFile);
276 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
277 filename);
278 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
279 SetLastError( ERROR_BAD_FORMAT);
280 return;
282 CloseHandle( hFile);
283 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
284 WINE_FIXME(".bat programs in pif files are not supported.\n");
285 /* first change dir, so the search below can start from there */
286 if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
287 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
288 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
289 filename);
290 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
292 /* search for the program */
293 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
294 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
295 filename);
296 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
297 SetLastError( ERROR_FILE_NOT_FOUND);
298 return;
300 if( textmode)
301 if( AllocConsole())
302 SetConsoleTitleA( title) ;
303 /* if no arguments on the commandline, use them from the pif file */
304 if( !cmdline[0] && optparams[0])
305 cmdline = optparams;
306 /* FIXME: do something with:
307 * - close on exit
308 * - graphic modes
309 * - hot key's
310 * - etc.
312 start_dosbox( progpath, cmdline );
315 /***********************************************************************
316 * build_command_line
318 * Build the command line of a process from the argv array.
319 * Copied from ENV_BuildCommandLine.
321 static char *build_command_line( char **argv )
323 int len;
324 char *p, **arg, *cmd_line;
326 len = 0;
327 for (arg = argv; *arg; arg++)
329 BOOL has_space;
330 int bcount;
331 char* a;
333 has_space=FALSE;
334 bcount=0;
335 a=*arg;
336 if( !*a ) has_space=TRUE;
337 while (*a!='\0') {
338 if (*a=='\\') {
339 bcount++;
340 } else {
341 if (*a==' ' || *a=='\t') {
342 has_space=TRUE;
343 } else if (*a=='"') {
344 /* doubling of '\' preceding a '"',
345 * plus escaping of said '"'
347 len+=2*bcount+1;
349 bcount=0;
351 a++;
353 len+=(a-*arg)+1 /* for the separating space */;
354 if (has_space)
355 len+=2; /* for the quotes */
358 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
359 return NULL;
361 p = cmd_line;
362 *p++ = (len < 256) ? len : 255;
363 for (arg = argv; *arg; arg++)
365 BOOL has_space,has_quote;
366 char* a;
368 /* Check for quotes and spaces in this argument */
369 has_space=has_quote=FALSE;
370 a=*arg;
371 if( !*a ) has_space=TRUE;
372 while (*a!='\0') {
373 if (*a==' ' || *a=='\t') {
374 has_space=TRUE;
375 if (has_quote)
376 break;
377 } else if (*a=='"') {
378 has_quote=TRUE;
379 if (has_space)
380 break;
382 a++;
385 /* Now transfer it to the command line */
386 if (has_space)
387 *p++='"';
388 if (has_quote) {
389 int bcount;
391 bcount=0;
392 a=*arg;
393 while (*a!='\0') {
394 if (*a=='\\') {
395 *p++=*a;
396 bcount++;
397 } else {
398 if (*a=='"') {
399 int i;
401 /* Double all the '\\' preceding this '"', plus one */
402 for (i=0;i<=bcount;i++)
403 *p++='\\';
404 *p++='"';
405 } else {
406 *p++=*a;
408 bcount=0;
410 a++;
412 } else {
413 strcpy(p,*arg);
414 p+=strlen(*arg);
416 if (has_space)
417 *p++='"';
418 *p++=' ';
420 if (len) p--; /* remove last space */
421 *p = '\0';
422 return cmd_line;
426 /***********************************************************************
427 * usage
429 static void usage(void)
431 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
432 ExitProcess(1);
436 /***********************************************************************
437 * main
439 int main( int argc, char *argv[] )
441 DWORD count;
442 HINSTANCE16 instance;
443 LOADPARAMS16 params;
444 WORD showCmd[2];
445 char buffer[MAX_PATH];
446 STARTUPINFOA info;
447 char *cmdline, *appname, **first_arg;
448 char *p;
450 if (!argv[1]) usage();
452 if (!strcmp( argv[1], "--app-name" ))
454 if (!(appname = argv[2])) usage();
455 first_arg = argv + 3;
457 else
459 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
461 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
462 ExitProcess(1);
464 appname = buffer;
465 first_arg = argv + 1;
468 if (*first_arg) first_arg++; /* skip program name */
469 cmdline = build_command_line( first_arg );
471 if (WINE_TRACE_ON(winevdm))
473 int i;
474 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
475 WINE_TRACE( "appname = '%s'\n", appname );
476 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
477 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
480 GetStartupInfoA( &info );
481 showCmd[0] = 2;
482 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
484 params.hEnvironment = 0;
485 params.cmdLine = MapLS( cmdline );
486 params.showCmd = MapLS( showCmd );
487 params.reserved = 0;
489 RestoreThunkLock(1); /* grab the Win16 lock */
491 /* some programs assume mmsystem is always present */
492 LoadLibrary16( "gdi.exe" );
493 LoadLibrary16( "user.exe" );
494 LoadLibrary16( "mmsystem.dll" );
496 if ((instance = LoadModule16( appname, &params )) < 32)
498 if (instance == 11)
500 /* first see if it is a .pif file */
501 if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
502 pif_cmd( appname, cmdline + 1);
503 else
504 /* try DOS format */
505 start_dosbox( appname, cmdline + 1 );
508 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
509 switch (instance)
511 case 2: WINE_MESSAGE("file not found\n" ); break;
512 case 11: WINE_MESSAGE("invalid program file\n" ); break;
513 default: WINE_MESSAGE("error=%d\n", instance ); break;
515 ExitProcess(instance);
518 /* wait forever; the process will be killed when the last task exits */
519 ReleaseThunkLock( &count );
520 Sleep( INFINITE );
521 return 0;