xref: /qemu/os-win32.c (revision a8d25326)
119113504SJes Sorensen /*
219113504SJes Sorensen  * os-win32.c
319113504SJes Sorensen  *
419113504SJes Sorensen  * Copyright (c) 2003-2008 Fabrice Bellard
519113504SJes Sorensen  * Copyright (c) 2010 Red Hat, Inc.
619113504SJes Sorensen  *
719113504SJes Sorensen  * Permission is hereby granted, free of charge, to any person obtaining a copy
819113504SJes Sorensen  * of this software and associated documentation files (the "Software"), to deal
919113504SJes Sorensen  * in the Software without restriction, including without limitation the rights
1019113504SJes Sorensen  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1119113504SJes Sorensen  * copies of the Software, and to permit persons to whom the Software is
1219113504SJes Sorensen  * furnished to do so, subject to the following conditions:
1319113504SJes Sorensen  *
1419113504SJes Sorensen  * The above copyright notice and this permission notice shall be included in
1519113504SJes Sorensen  * all copies or substantial portions of the Software.
1619113504SJes Sorensen  *
1719113504SJes Sorensen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1819113504SJes Sorensen  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919113504SJes Sorensen  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2019113504SJes Sorensen  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2119113504SJes Sorensen  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2219113504SJes Sorensen  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2319113504SJes Sorensen  * THE SOFTWARE.
2419113504SJes Sorensen  */
25*a8d25326SMarkus Armbruster 
26d38ea87aSPeter Maydell #include "qemu/osdep.h"
2719113504SJes Sorensen #include <windows.h>
280727b867SPaolo Bonzini #include <mmsystem.h>
29*a8d25326SMarkus Armbruster #include "qemu-common.h"
309c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
3159a5264bSJes Sorensen #include "qemu-options.h"
3219113504SJes Sorensen 
3319113504SJes Sorensen /***********************************************************/
340a1574bbSStefan Weil /* Functions missing in mingw */
350a1574bbSStefan Weil 
360a1574bbSStefan Weil int setenv(const char *name, const char *value, int overwrite)
370a1574bbSStefan Weil {
380a1574bbSStefan Weil     int result = 0;
390a1574bbSStefan Weil     if (overwrite || !getenv(name)) {
400a1574bbSStefan Weil         size_t length = strlen(name) + strlen(value) + 2;
417267c094SAnthony Liguori         char *string = g_malloc(length);
420a1574bbSStefan Weil         snprintf(string, length, "%s=%s", name, value);
430a1574bbSStefan Weil         result = putenv(string);
4491a9ecefSZhi Hui Li 
4591a9ecefSZhi Hui Li         /* Windows takes a copy and does not continue to use our string.
4691a9ecefSZhi Hui Li          * Therefore it can be safely freed on this platform.  POSIX code
4791a9ecefSZhi Hui Li          * typically has to leak the string because according to the spec it
4891a9ecefSZhi Hui Li          * becomes part of the environment.
4991a9ecefSZhi Hui Li          */
5091a9ecefSZhi Hui Li         g_free(string);
510a1574bbSStefan Weil     }
520a1574bbSStefan Weil     return result;
530a1574bbSStefan Weil }
540a1574bbSStefan Weil 
5569bd73b1SJes Sorensen static BOOL WINAPI qemu_ctrl_handler(DWORD type)
5669bd73b1SJes Sorensen {
57cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL);
58b75a0282SPavel Dovgaluk     /* Windows 7 kills application when the function returns.
59b75a0282SPavel Dovgaluk        Sleep here to give QEMU a try for closing.
60b75a0282SPavel Dovgaluk        Sleep period is 10000ms because Windows kills the program
61b75a0282SPavel Dovgaluk        after 10 seconds anyway. */
62b75a0282SPavel Dovgaluk     Sleep(10000);
63b75a0282SPavel Dovgaluk 
6469bd73b1SJes Sorensen     return TRUE;
6569bd73b1SJes Sorensen }
6669bd73b1SJes Sorensen 
670727b867SPaolo Bonzini static TIMECAPS mm_tc;
680727b867SPaolo Bonzini 
690727b867SPaolo Bonzini static void os_undo_timer_resolution(void)
700727b867SPaolo Bonzini {
710727b867SPaolo Bonzini     timeEndPeriod(mm_tc.wPeriodMin);
720727b867SPaolo Bonzini }
730727b867SPaolo Bonzini 
74fe98ac14SJes Sorensen void os_setup_early_signal_handling(void)
7569bd73b1SJes Sorensen {
7669bd73b1SJes Sorensen     SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
770727b867SPaolo Bonzini     timeGetDevCaps(&mm_tc, sizeof(mm_tc));
780727b867SPaolo Bonzini     timeBeginPeriod(mm_tc.wPeriodMin);
790727b867SPaolo Bonzini     atexit(os_undo_timer_resolution);
8069bd73b1SJes Sorensen }
816170540bSJes Sorensen 
826170540bSJes Sorensen /* Look for support files in the same directory as the executable.  */
8310f5bff6SFam Zheng char *os_find_datadir(void)
846170540bSJes Sorensen {
8510f5bff6SFam Zheng     return qemu_get_exec_dir();
866170540bSJes Sorensen }
8759a5264bSJes Sorensen 
886650b710SStefan Weil void os_set_line_buffering(void)
896650b710SStefan Weil {
906650b710SStefan Weil     setbuf(stdout, NULL);
916650b710SStefan Weil     setbuf(stderr, NULL);
926650b710SStefan Weil }
936650b710SStefan Weil 
9459a5264bSJes Sorensen /*
9559a5264bSJes Sorensen  * Parse OS specific command line options.
9659a5264bSJes Sorensen  * return 0 if option handled, -1 otherwise
9759a5264bSJes Sorensen  */
981217d6caSThomas Huth int os_parse_cmd_args(int index, const char *optarg)
9959a5264bSJes Sorensen {
1001217d6caSThomas Huth     return -1;
10159a5264bSJes Sorensen }
102