4 #include <wx/filename.h>
6 QemuVM::QemuVM(const wxString
& configfile
) : wxEvtHandler(){
8 this->socket
= new MonitorSocket();
9 this->state
= VM_STATE_POWERED_OFF
;
11 this->vncdisplay
= -1;
12 this->monitorport
= -1;
13 this->configfile
= configfile
;
15 if(!wxFileExists(configfile
)){
17 file
.Create(configfile
);
21 wxFileInputStream
stream(configfile
);
22 config
= new wxFileConfig(stream
);
29 int QemuVM::vncdisplays
= 10;
30 int QemuVM::monitorports
= 3000;
32 wxString
QemuVM::SendCommand(const wxString
& cmd
, bool block
){
33 this->socket
->Write(cmd
,block
);
35 return socket
->ReadAll();
40 wxString
QemuVM::GetTitle(){
41 return config
->Read(wxT("/vm/title"));
44 void QemuVM::SetTitle(const wxString
& title
){
45 config
->Write(wxT("/vm/title"),title
);
48 wxString
QemuVM::GetCmdlineArguments(){
49 return config
->Read(wxT("/vm/cmdline"));
52 void QemuVM::SetCmdlineArguments(const wxString
& args
){
53 config
->Write(wxT("/vm/cmdline"),args
);
56 wxString
QemuVM::GetConfigFile(){
60 void QemuVM::SaveConfig(){
61 this->SaveConfig(configfile
);
64 void QemuVM::SaveConfig(const wxString
& configfile
){
65 wxFileOutputStream
stream(configfile
);
69 MonitorSocket
*QemuVM::GetSocket(){
73 PipedProcess
*QemuVM::GetProcess(){
75 process
= new PipedProcess();
76 process
->AddEventHandler(this);
81 void QemuVM::AddEventHandler(wxEvtHandler
*handler
){
82 process
->AddEventHandler(handler
);
85 long QemuVM::PowerOn(){
89 if(state
== VM_STATE_PAUSED
){
94 wxString cmd
= wxT("qemu");
96 pid
= process
->Launch(cmd
,1000);
98 // give some time to start up
101 if(!socket
->Connect(wxT("localhost"),GetCurrentMonitorPort())){
104 state
= VM_STATE_RUNNING
;
108 void QemuVM::PowerOff(){
109 //SendCommand(wxT("system_powerdown"));
110 SendCommand(wxT("quit"));
111 state
= VM_STATE_POWERED_OFF
;
115 if(!process
|| pid
== -1)
117 if(wxProcess::Exists(pid
)){
118 if(wxKill(pid
,wxSIGKILL
) == 0){
119 state
= VM_STATE_POWERED_OFF
;
125 void QemuVM::Reset(){
129 SendCommand(wxT("system_reset"));
130 state
= VM_STATE_RUNNING
;
133 void QemuVM::Pause(){
137 SendCommand(wxT("stop"));
138 state
= VM_STATE_PAUSED
;
141 void QemuVM::Continue(){
145 SendCommand(wxT("cont"));
146 state
= VM_STATE_RUNNING
;
149 void QemuVM::Suspend(){
153 //SendCommand(wxT("savevm supended"));
154 SendCommand(wxT("stop"));
155 state
= VM_STATE_SUSPENDED
;
158 void QemuVM::Snapshot(){
161 SendCommand(wxT("savevm snapshot"));
162 config
->Write(wxT("/snapshots/snapshot"),wxT("snapshot"));
165 void QemuVM::Revert(){
168 SendCommand(wxT("loadvm snapshot"));
169 state
= VM_STATE_RUNNING
;
170 config
->DeleteEntry(wxT("/snapshots/snapshot"));
173 bool QemuVM::HasSnapshot(){
174 wxString snapshot
= config
->Read(wxT("/snapshots/snapshot"));
175 return !snapshot
.IsEmpty();
178 bool QemuVM::IsRunning(){
179 return state
== VM_STATE_RUNNING
;
182 bool QemuVM::IsPaused(){
183 return state
== VM_STATE_PAUSED
;
186 bool QemuVM::IsPoweredOff(){
187 return state
== VM_STATE_POWERED_OFF
;
190 void QemuVM::OnProcessTermination(wxCommandEvent
& WXUNUSED(event
)){
191 state
= VM_STATE_POWERED_OFF
;
195 wxString
QemuVM::GetHd(const int hd
){
196 wxString device
= wxString::Format(wxT("/devices/hd%c"),(char)('a'+hd
));
197 return config
->Read(device
);
200 void QemuVM::SetHd(const int hd
,const wxString
& image
){
201 wxString device
= wxString::Format(wxT("/devices/hd%c"),(char)('a'+hd
));
202 config
->Write(device
,image
);
205 wxString
QemuVM::GetCdRom(){
206 return config
->Read(wxT("/devices/cdrom"));
209 void QemuVM::SetCdRom(const wxString
& image
){
210 config
->Write(wxT("/devices/cdrom"),image
);
213 wxString
QemuVM::GetFd(const int fd
){
214 wxString device
= wxString::Format(wxT("/devices/fd%c"),(char)('a'+fd
));
215 return config
->Read(device
);
218 void QemuVM::SetFd(const int fd
,const wxString
& image
){
219 wxString device
= wxString::Format(wxT("/devices/fd%c"),(char)('a'+fd
));
220 config
->Write(device
,image
);
223 int QemuVM::GetMemory(){
224 return config
->Read(wxT("/devices/memory"),0L);
227 void QemuVM::SetMemory(const int memory
){
229 config
->Write(wxT("/devices/memory"),memory
);
232 int QemuVM::GetSmp(){
233 return config
->Read(wxT("/cpu/smp"),1L);
236 void QemuVM::SetSmp(const int smp
){
237 config
->Write(wxT("/cpu/smp"),smp
);
240 wxString
QemuVM::GetSMB(){
241 return config
->Read(wxT("/net/smb"));
244 void QemuVM::SetSMB(const wxString
& smb
){
246 config
->Write(wxT("/net/smb"),smb
);
248 config
->DeleteEntry(wxT("/net/smb"),true);
251 wxString
QemuVM::GetTFTP(){
252 return config
->Read(wxT("/net/tftp"));
255 void QemuVM::SetTFTP(const wxString
& tftp
){
257 config
->Write(wxT("/net/tftp"),tftp
);
259 config
->DeleteEntry(wxT("/net/tftp"),true);
262 wxString
QemuVM::GetBoot(){
263 return config
->Read(wxT("/vm/boot"));
266 void QemuVM::SetBoot(const wxString
& boot
){
267 config
->Write(wxT("/vm/boot"),boot
);
270 wxString
QemuVM::GetKeyboardLayout(){
271 return config
->Read(wxT("/devices/keyboard"),wxT("en-us"));
274 void QemuVM::SetKeyboardLayout(const wxString
& layout
){
275 config
->Write(wxT("/devices/keyboard"),layout
);
278 void QemuVM::ConnectCdRom(bool state
){
280 SendCommand(wxT("connect cdrom"));
282 SendCommand(wxT("disconnect cdrom"));
286 bool QemuVM::IsCdRomConnected(){
290 bool QemuVM::GetWin2kHack(){
291 wxString win2khack
= config
->Read(wxT("/misc/win2k-hack"));
292 return !win2khack
.Cmp(wxT("true"));
295 void QemuVM::SetWin2kHack(bool win2khack
){
296 config
->Write(wxT("/misc/win2k-hack"),win2khack
? wxT("true") : wxT("false"));
299 bool QemuVM::GetLocalTime(){
300 wxString localtime
= config
->Read(wxT("/misc/localtime"));
301 return !localtime
.Cmp(wxT("true"));
304 void QemuVM::SetLocalTime(bool localtime
){
305 config
->Write(wxT("/misc/localtime"),localtime
? wxT("true") : wxT("false"));
308 bool QemuVM::GetSnapshotMode(){
309 wxString mode
= config
->Read(wxT("/snapshot/snapshotmode"));
310 return !mode
.Cmp(wxT("true"));
313 void QemuVM::SetSnapshotMode(bool mode
){
314 config
->Write(wxT("/snapshot/snapshotmode"),mode
? wxT("true"): wxT("false"));
317 bool QemuVM::GetStdVga(){
318 wxString vga
= config
->Read(wxT("/misc/std-vga"));
319 return !vga
.Cmp(wxT("true"));
322 void QemuVM::SetStdVga(bool vga
){
323 config
->Write(wxT("/misc/std-vga"),vga
? wxT("true") : wxT("false"));
326 bool QemuVM::GetNoACPI(){
327 wxString acpi
= config
->Read(wxT("/misc/no-acpi"));
328 return !acpi
.Cmp(wxT("true"));
331 void QemuVM::SetNoACPI(bool acpi
){
332 config
->Write(wxT("/misc/no-acpi"),acpi
? wxT("true") : wxT("false"));
335 int QemuVM::GetCurrentMonitorPort(){
336 return this->monitorport
;
339 int QemuVM::GetMonitorPort(){
340 return config
->Read(wxT("/misc/monitorport"),-1L);
343 void QemuVM::SetMonitorPort(const int port
){
345 config
->Write(wxT("/misc/monitorport"),port
);
348 int QemuVM::GetCurrentVNCDisplay(){
349 return this->vncdisplay
;
352 int QemuVM::GetVNCDisplay(){
353 return config
->Read(wxT("/misc/vncdisplay"),-1L);
356 void QemuVM::SetVNCDisplay(const int display
){
358 config
->Write(wxT("/misc/vncdisplay"),display
);
361 wxString
QemuVM::GetCmdline(){
362 wxString cmdline
= wxT(" -monitor ");
363 cmdline
<< wxT(" tcp::");
366 cmdline
<< monitorport
;
367 else if((monitorport
= GetMonitorPort()) >= 0)
368 cmdline
<< monitorport
;
370 cmdline
<< (monitorport
= QemuVM::monitorports
++);
372 cmdline
<< wxT(",server ");
374 cmdline
<< wxT(" -usbdevice tablet -vnc :");
377 cmdline
<< vncdisplay
;
378 else if((vncdisplay
= GetVNCDisplay()) >= 0)
379 cmdline
<< vncdisplay
;
381 cmdline
<< (vncdisplay
= QemuVM::vncdisplays
++);
383 wxString hda
= GetHd(0);
385 cmdline
<< wxT(" -hda ") << hda
;
386 wxString hdb
= GetHd(1);
388 cmdline
<< wxT(" -hdb ") << hdb
;
389 wxString hdc
= GetHd(2);
390 wxString cdrom
= GetCdRom();
391 if(!hdc
.IsEmpty() && cdrom
.IsEmpty())
392 cmdline
<< wxT(" -hdc ") << hdc
;
393 wxString hdd
= GetHd(3);
395 cmdline
<< wxT(" -hdd ") << hdd
;
397 cmdline
<< wxT(" -cdrom ") << cdrom
;
399 wxString fda
= GetFd(0);
401 cmdline
<< wxT(" -fda ") << fda
;
402 wxString fdb
= GetFd(1);
404 cmdline
<< wxT(" -fdb ") << fdb
;
406 wxString boot
= GetBoot();
408 cmdline
<< wxT(" -boot ") << boot
;
410 const int memory
= GetMemory();
412 cmdline
<< wxT(" -m ") << memory
;
416 cmdline
<< wxT(" -smp ") << smp
;
418 wxString smb
= GetSMB();
420 cmdline
<< wxT(" -smb ") << smb
;
422 wxString tftp
= GetTFTP();
424 cmdline
<< wxT(" -tftp ") << tftp
;
427 cmdline
<< wxT(" -localtime ");
430 cmdline
<< wxT(" -win2k-hack ");
433 cmdline
<< wxT(" -std-vga ");
436 cmdline
<< wxT(" -no-acpi ");
438 if(GetSnapshotMode())
439 cmdline
<< wxT(" -snapshot ");
441 wxString keyboard
= GetKeyboardLayout();
442 if(!keyboard
.IsEmpty())
443 cmdline
<< wxT(" -k ") << keyboard
;
445 cmdline
<< wxT(" ") << GetCmdlineArguments();
449 wxString
QemuVM::GetPath(){
450 wxFileName
filename(this->configfile
);
451 return filename
.GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
);
454 long QemuVM::GetPID(){
458 void QemuVM::SendKey(const wxString
& key
){
459 SendCommand(wxT("sendkey ") + key
);
462 BEGIN_EVENT_TABLE(QemuVM
, wxEvtHandler
)
463 EVT_COMMAND(wxID_ANY
,EVT_PROCESS_TERMINATED
,QemuVM::OnProcessTermination
)