Use new vnc syntax (with a colon).
[qemu-gui.git] / vm.cpp
blob08699c28e4846c6e25e5166c1400b8b7a3c8009e
1 #include "vm.h"
2 #include "socket.h"
3 #include <wx/wx.h>
4 #include <wx/filename.h>
6 QemuVM::QemuVM(const wxString& configfile) : wxEvtHandler(){
7 this->process = NULL;
8 this->socket = new MonitorSocket();
9 this->state = VM_STATE_POWERED_OFF;
10 this->pid = -1;
11 this->vncdisplay = -1;
12 this->monitorport = -1;
13 this->configfile = configfile;
15 if(!wxFileExists(configfile)){
16 wxFile file;
17 file.Create(configfile);
18 file.Close();
21 wxFileInputStream stream(configfile);
22 config = new wxFileConfig(stream);
25 QemuVM::~QemuVM(){
26 delete this->config;
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);
34 if(block)
35 return socket->ReadAll();
36 else
37 return wxEmptyString;
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(){
57 return configfile;
60 void QemuVM::SaveConfig(){
61 this->SaveConfig(configfile);
64 void QemuVM::SaveConfig(const wxString& configfile){
65 wxFileOutputStream stream(configfile);
66 config->Save(stream);
69 MonitorSocket *QemuVM::GetSocket(){
70 return this->socket;
73 PipedProcess *QemuVM::GetProcess(){
74 if(!process){
75 process = new PipedProcess();
76 process->AddEventHandler(this);
78 return process;
81 void QemuVM::AddEventHandler(wxEvtHandler *handler){
82 process->AddEventHandler(handler);
85 long QemuVM::PowerOn(){
86 if(!process)
87 return -1;
89 if(state == VM_STATE_PAUSED){
90 Continue();
91 return pid;
94 wxString cmd = wxT("qemu");
95 cmd << GetCmdline();
96 pid = process->Launch(cmd,1000);
98 // give some time to start up
99 wxMilliSleep(500);
101 if(!socket->Connect(wxT("localhost"),GetCurrentMonitorPort())){
102 return -1;
104 state = VM_STATE_RUNNING;
105 return pid;
108 void QemuVM::PowerOff(){
109 //SendCommand(wxT("system_powerdown"));
110 SendCommand(wxT("quit"));
111 state = VM_STATE_POWERED_OFF;
114 void QemuVM::Kill(){
115 if(!process || pid == -1)
116 return;
117 if(wxProcess::Exists(pid)){
118 if(wxKill(pid,wxSIGKILL) == 0){
119 state = VM_STATE_POWERED_OFF;
120 pid = -1;
125 void QemuVM::Reset(){
126 if(!process)
127 return;
129 SendCommand(wxT("system_reset"));
130 state = VM_STATE_RUNNING;
133 void QemuVM::Pause(){
134 if(!process)
135 return;
137 SendCommand(wxT("stop"));
138 state = VM_STATE_PAUSED;
141 void QemuVM::Continue(){
142 if(!process)
143 return;
145 SendCommand(wxT("cont"));
146 state = VM_STATE_RUNNING;
149 void QemuVM::Suspend(){
150 if(!process)
151 return;
153 //SendCommand(wxT("savevm supended"));
154 SendCommand(wxT("stop"));
155 state = VM_STATE_SUSPENDED;
158 void QemuVM::Snapshot(){
159 if(!process)
160 return;
161 SendCommand(wxT("savevm snapshot"));
162 config->Write(wxT("/snapshots/snapshot"),wxT("snapshot"));
165 void QemuVM::Revert(){
166 if(!process)
167 return;
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;
192 pid = -1;
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){
228 if(memory != 0)
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){
245 if(!smb.IsEmpty())
246 config->Write(wxT("/net/smb"),smb);
247 else
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){
256 if(!tftp.IsEmpty())
257 config->Write(wxT("/net/tftp"),tftp);
258 else
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){
279 if(state){
280 SendCommand(wxT("connect cdrom"));
281 } else {
282 SendCommand(wxT("disconnect cdrom"));
286 bool QemuVM::IsCdRomConnected(){
287 return true;
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){
344 if(port > 0)
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){
357 if(display > 0)
358 config->Write(wxT("/misc/vncdisplay"),display);
361 wxString QemuVM::GetCmdline(){
362 wxString cmdline = wxT(" -monitor ");
363 cmdline << wxT(" tcp::");
365 if(monitorport >= 0)
366 cmdline << monitorport;
367 else if((monitorport = GetMonitorPort()) >= 0)
368 cmdline << monitorport;
369 else
370 cmdline << (monitorport = QemuVM::monitorports++);
372 cmdline << wxT(",server ");
374 cmdline << wxT(" -usbdevice tablet -vnc :");
376 if(vncdisplay >= 0)
377 cmdline << vncdisplay;
378 else if((vncdisplay = GetVNCDisplay()) >= 0)
379 cmdline << vncdisplay;
380 else
381 cmdline << (vncdisplay = QemuVM::vncdisplays++);
383 wxString hda = GetHd(0);
384 if(!hda.IsEmpty())
385 cmdline << wxT(" -hda ") << hda;
386 wxString hdb = GetHd(1);
387 if(!hdb.IsEmpty())
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);
394 if(!hdd.IsEmpty())
395 cmdline << wxT(" -hdd ") << hdd;
396 if(!cdrom.IsEmpty())
397 cmdline << wxT(" -cdrom ") << cdrom;
399 wxString fda = GetFd(0);
400 if(!fda.IsEmpty())
401 cmdline << wxT(" -fda ") << fda;
402 wxString fdb = GetFd(1);
403 if(!fdb.IsEmpty())
404 cmdline << wxT(" -fdb ") << fdb;
406 wxString boot = GetBoot();
407 if(!boot.IsEmpty())
408 cmdline << wxT(" -boot ") << boot;
410 const int memory = GetMemory();
411 if(memory != 0)
412 cmdline << wxT(" -m ") << memory;
414 int smp = GetSmp();
415 if(smp != 1)
416 cmdline << wxT(" -smp ") << smp;
418 wxString smb = GetSMB();
419 if(!smb.IsEmpty())
420 cmdline << wxT(" -smb ") << smb;
422 wxString tftp = GetTFTP();
423 if(!tftp.IsEmpty())
424 cmdline << wxT(" -tftp ") << tftp;
426 if(GetLocalTime())
427 cmdline << wxT(" -localtime ");
429 if(GetWin2kHack())
430 cmdline << wxT(" -win2k-hack ");
432 if(GetStdVga())
433 cmdline << wxT(" -std-vga ");
435 if(GetNoACPI())
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();
446 return cmdline;
449 wxString QemuVM::GetPath(){
450 wxFileName filename(this->configfile);
451 return filename.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
454 long QemuVM::GetPID(){
455 return this->pid;
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)
464 END_EVENT_TABLE()