]> git.proxmox.com Git - qemu.git/blob - os-win32.c
Move find_datadir to OS specific files.
[qemu.git] / os-win32.c
1 /*
2 * os-win32.c
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 #include <windows.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <sys/time.h>
32 #include "config-host.h"
33 #include "sysemu.h"
34
35 /***********************************************************/
36 /* Polling handling */
37
38 typedef struct PollingEntry {
39 PollingFunc *func;
40 void *opaque;
41 struct PollingEntry *next;
42 } PollingEntry;
43
44 static PollingEntry *first_polling_entry;
45
46 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
47 {
48 PollingEntry **ppe, *pe;
49 pe = qemu_mallocz(sizeof(PollingEntry));
50 pe->func = func;
51 pe->opaque = opaque;
52 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
53 *ppe = pe;
54 return 0;
55 }
56
57 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
58 {
59 PollingEntry **ppe, *pe;
60 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
61 pe = *ppe;
62 if (pe->func == func && pe->opaque == opaque) {
63 *ppe = pe->next;
64 qemu_free(pe);
65 break;
66 }
67 }
68 }
69
70 /***********************************************************/
71 /* Wait objects support */
72 typedef struct WaitObjects {
73 int num;
74 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
75 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
76 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
77 } WaitObjects;
78
79 static WaitObjects wait_objects = {0};
80
81 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
82 {
83 WaitObjects *w = &wait_objects;
84
85 if (w->num >= MAXIMUM_WAIT_OBJECTS)
86 return -1;
87 w->events[w->num] = handle;
88 w->func[w->num] = func;
89 w->opaque[w->num] = opaque;
90 w->num++;
91 return 0;
92 }
93
94 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
95 {
96 int i, found;
97 WaitObjects *w = &wait_objects;
98
99 found = 0;
100 for (i = 0; i < w->num; i++) {
101 if (w->events[i] == handle)
102 found = 1;
103 if (found) {
104 w->events[i] = w->events[i + 1];
105 w->func[i] = w->func[i + 1];
106 w->opaque[i] = w->opaque[i + 1];
107 }
108 }
109 if (found)
110 w->num--;
111 }
112
113 void os_host_main_loop_wait(int *timeout)
114 {
115 int ret, ret2, i;
116 PollingEntry *pe;
117
118 /* XXX: need to suppress polling by better using win32 events */
119 ret = 0;
120 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
121 ret |= pe->func(pe->opaque);
122 }
123 if (ret == 0) {
124 int err;
125 WaitObjects *w = &wait_objects;
126
127 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
128 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
129 if (w->func[ret - WAIT_OBJECT_0])
130 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
131
132 /* Check for additional signaled events */
133 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
134
135 /* Check if event is signaled */
136 ret2 = WaitForSingleObject(w->events[i], 0);
137 if(ret2 == WAIT_OBJECT_0) {
138 if (w->func[i])
139 w->func[i](w->opaque[i]);
140 } else if (ret2 == WAIT_TIMEOUT) {
141 } else {
142 err = GetLastError();
143 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
144 }
145 }
146 } else if (ret == WAIT_TIMEOUT) {
147 } else {
148 err = GetLastError();
149 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
150 }
151 }
152
153 *timeout = 0;
154 }
155
156 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
157 {
158 exit(STATUS_CONTROL_C_EXIT);
159 return TRUE;
160 }
161
162 void os_setup_early_signal_handling(void)
163 {
164 /* Note: cpu_interrupt() is currently not SMP safe, so we force
165 QEMU to run on a single CPU */
166 HANDLE h;
167 DWORD mask, smask;
168 int i;
169
170 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
171
172 h = GetCurrentProcess();
173 if (GetProcessAffinityMask(h, &mask, &smask)) {
174 for(i = 0; i < 32; i++) {
175 if (mask & (1 << i))
176 break;
177 }
178 if (i != 32) {
179 mask = 1 << i;
180 SetProcessAffinityMask(h, mask);
181 }
182 }
183 }
184
185 /* Look for support files in the same directory as the executable. */
186 char *os_find_datadir(const char *argv0)
187 {
188 char *p;
189 char buf[MAX_PATH];
190 DWORD len;
191
192 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
193 if (len == 0) {
194 return NULL;
195 }
196
197 buf[len] = 0;
198 p = buf + len - 1;
199 while (p != buf && *p != '\\')
200 p--;
201 *p = 0;
202 if (access(buf, R_OK) == 0) {
203 return qemu_strdup(buf);
204 }
205 return NULL;
206 }