diff --git a/kemu/.kateproject b/kemu/.kateproject new file mode 100644 index 00000000..aa91ca4c --- /dev/null +++ b/kemu/.kateproject @@ -0,0 +1,4 @@ +{ + "name": "kemu" , + "files": [ { "git": 1 } ] +}; \ No newline at end of file diff --git a/kemu/CMakeLists.txt b/kemu/CMakeLists.txt new file mode 100644 index 00000000..7604776f --- /dev/null +++ b/kemu/CMakeLists.txt @@ -0,0 +1,20 @@ +project(kemu) + +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +find_package(KDE4) +include(KDE4Defaults) + +include_directories(${KDE4_INCLUDES}) + +set(kemu_sources + main.cpp + kemumainwindow.cpp +) + +add_executable(kemu ${kemu_sources}) +target_link_libraries(kemu KDE4::kdeui KDE4::kfile) + +install(TARGETS kemu DESTINATION ${BIN_INSTALL_DIR}) +install(FILES kemuui.rc DESTINATION ${DATA_INSTALL_DIR}/kemu) +install(PROGRAMS kemu.desktop DESTINATION ${XDG_APPS_INSTALL_DIR}) diff --git a/kemu/kemu.desktop b/kemu/kemu.desktop new file mode 100644 index 00000000..008c89af --- /dev/null +++ b/kemu/kemu.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Icon=applications-engineering +Name=KEmu +GenericName=KEmu +Comment=Simple QEMU frontend for KDE +Exec=kemu --icon '%i' --caption '%c' %U +Terminal=false +Type=Application +Categories=Qt;KDE; diff --git a/kemu/kemu.ui b/kemu/kemu.ui new file mode 100644 index 00000000..b76ccead --- /dev/null +++ b/kemu/kemu.ui @@ -0,0 +1,250 @@ + + + KEmuWindow + + + + 0 + 0 + 800 + 600 + + + + KEmu + + + + + + + + + + + + Qt::Horizontal + + + QSizePolicy::Minimum + + + + 40 + 20 + + + + + + + + false + + + Start + + + + + + + + + + + + Qt::Horizontal + + + QSizePolicy::Minimum + + + + 40 + 20 + + + + + + + + + 300 + 16777215 + + + + + + + + + + + + + + Hard Drive image + + + + + + + + + + CD-ROM image + + + + + + + + + + + + + System type + + + + + + + Video output + + + + + + + + cirrus + + + + + vmware + + + + + qxl + + + + + xenfb + + + + + tcx + + + + + cg3 + + + + + virtio + + + + + + + + Random Access Memory + + + + + + + 32 + + + + + + + Kernel-based Virtual Machine + + + true + + + + + + + Qt::Vertical + + + + 471 + 373 + + + + + + + + + + + Extra arguments + + + + + + + + + + + + 0 + 0 + 800 + 20 + + + + + + + + KEditListWidget + QWidget +
keditlistwidget.h
+
+ + KLineEdit + QLineEdit +
klineedit.h
+
+ + KUrlRequester + QFrame +
kurlrequester.h
+
+ + KIntNumInput + QWidget +
knuminput.h
+
+
+ + +
diff --git a/kemu/kemumainwindow.cpp b/kemu/kemumainwindow.cpp new file mode 100644 index 00000000..0b2b5cc8 --- /dev/null +++ b/kemu/kemumainwindow.cpp @@ -0,0 +1,280 @@ +/* This file is part of the KDE libraries + Copyright (C) 2016 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kemumainwindow.h" +#include "ui_kemu.h" + +KEmuMainWindow::KEmuMainWindow(QWidget *parent, Qt::WindowFlags flags) + : KXmlGuiWindow(parent, flags), m_kemuui(new Ui_KEmuWindow) +{ + m_kemuui->setupUi(this); + m_kemuui->startStopButton->setText(i18n("Start")); + m_kemuui->startStopButton->setIcon(KIcon("system-run")); + + KAction *a = actionCollection()->addAction("harddisk_create", this, SLOT(createHardDrive())); + a->setText(i18n("Create Hard Disk image")); + a->setIcon(KIcon("hard-drive")); + a->setShortcut(KStandardShortcut::openNew()); + a->setWhatsThis(i18n("Create a new Hard Disk image for later use.")); + + KAction *b = actionCollection()->addAction("file_quit", this, SLOT(quit())); + b->setText(i18n("Quit")); + b->setIcon(KIcon("application-exit")); + b->setShortcut(KStandardShortcut::quit()); + b->setWhatsThis(i18n("Close the application.")); + + setupGUI(); + setAutoSaveSettings(); + + setWindowIcon(KIcon("applications-engineering")); + m_kemuui->groupBox->setEnabled(false); + connect(m_kemuui->machinesList->listView()->selectionModel(), + SIGNAL(selectionChanged(QItemSelection,QItemSelection)), + this, SLOT(machineChanged(QItemSelection,QItemSelection))); + connect(m_kemuui->machinesList, SIGNAL(added(QString)), this, SLOT(addMachine(QString))); + connect(m_kemuui->startStopButton, SIGNAL(clicked()), this, SLOT(startStopMachine())); + connect(m_kemuui->machinesList, SIGNAL(removed(QString)), this, SLOT(removeMachine(QString))); + + // TODO: more machine-type entries + static const QStringList qemuBins = QStringList() << "qemu-system-i386"; + foreach (const QString bin, qemuBins) { + if(!KStandardDirs::findExe(bin).isEmpty()) { + m_kemuui->systemComboBox->addItem(bin); + } + } + + m_settings = new QSettings("KEmu", "kemu"); + foreach(const QString machine, m_settings->childGroups()) { + if (m_settings->value(machine + "/enable") == true) { + m_kemuui->machinesList->insertItem(machine); + machineLoad(machine); + } else { + kDebug() << "garbage machine" << machine; + } + } + + QFile kvmdev("/dev/kvm"); + if (!kvmdev.exists()) { + const QString modprobeBin = KStandardDirs::findExe("modprobe"); + if (!modprobeBin.isEmpty()) { + QProcess modprobe(this); + modprobe.start(modprobeBin, QStringList() << "-b" << "kvm"); + modprobe.waitForFinished(); + if (!kvmdev.exists()) { + QMessageBox::warning(this, i18n("KVM not available"), i18n("KVM not available")); + } + } else { + kDebug() << "modprobe not found"; + } + } + + connect(m_kemuui->CDROMInput, SIGNAL(textChanged(QString)), this, SLOT(machineSave(QString))); + connect(m_kemuui->HardDriveInput, SIGNAL(textChanged(QString)), this, SLOT(machineSave(QString))); + connect(m_kemuui->systemComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(machineSave(QString))); + connect(m_kemuui->videoComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(machineSave(QString))); + connect(m_kemuui->RAMInput, SIGNAL(valueChanged(int)), this, SLOT(machineSave(int))); + connect(m_kemuui->KVMCheckBox, SIGNAL(stateChanged(int)), this, SLOT(machineSave(int))); + connect(m_kemuui->argumentsLineEdit, SIGNAL(textChanged(QString)), this, SLOT(machineSave(QString))); +} + +KEmuMainWindow::~KEmuMainWindow() +{ + saveAutoSaveSettings(); + m_settings->sync(); + delete m_settings; + foreach(QProcess* machineProcess, m_machines) { + const QString machine = m_machines.key(machineProcess); + if (machineProcess->state() == QProcess::Running) { + kDebug() << "stopping machine" << machine; + machineProcess->terminate(); + } + machineProcess->deleteLater(); + m_machines.remove(machine); + } +} + +void KEmuMainWindow::createHardDrive() +{ + // TODO: implement + QMessageBox::warning(this, "NOT IMPLEMENTED", "NOT IMPLEMENTED"); +} + +void KEmuMainWindow::quit() +{ + qApp->quit(); +} + +void KEmuMainWindow::machineSave(const QString ignored) +{ + QString machine = m_kemuui->machinesList->currentText(); + kDebug() << "saving machine" << machine; + m_settings->setValue(machine + "/cdrom", m_kemuui->CDROMInput->url().prettyUrl()); + m_settings->setValue(machine + "/harddrive", m_kemuui->HardDriveInput->url().prettyUrl()); + m_settings->setValue(machine + "/system", m_kemuui->systemComboBox->currentText()); + m_settings->setValue(machine + "/video", m_kemuui->videoComboBox->currentText()); + m_settings->setValue(machine + "/ram", m_kemuui->RAMInput->value()); + m_settings->setValue(machine + "/kvm", m_kemuui->KVMCheckBox->isChecked()); + m_settings->setValue(machine + "/args", m_kemuui->argumentsLineEdit->text()); + m_settings->sync(); +} + +void KEmuMainWindow::machineSave(int ignored) +{ + machineSave(QString()); +} + + +void KEmuMainWindow::machineLoad(const QString machine) +{ + kDebug() << "loading machine" << machine; + m_kemuui->CDROMInput->setUrl(m_settings->value(machine + "/cdrom").toUrl()); + m_kemuui->HardDriveInput->setUrl(m_settings->value(machine + "/harddrive").toUrl()); + const QString system = m_settings->value(machine + "/system").toString(); + const int systemIndex = m_kemuui->systemComboBox->findText(system); + m_kemuui->systemComboBox->setCurrentIndex(systemIndex); + const QString video = m_settings->value(machine + "/video", "virtio").toString(); + const int videoIndex = m_kemuui->videoComboBox->findText(video); + m_kemuui->videoComboBox->setCurrentIndex(videoIndex); + m_kemuui->RAMInput->setValue(m_settings->value(machine + "/ram", 32).toInt()); + m_kemuui->KVMCheckBox->setChecked(m_settings->value(machine + "/kvm", false).toBool()); + m_kemuui->argumentsLineEdit->setText(m_settings->value(machine + "/args").toString()); +} + +void KEmuMainWindow::machineChanged(QItemSelection ignored, QItemSelection ignored2) +{ + QString machine = m_kemuui->machinesList->currentText(); + if (!machine.isEmpty()) { + QFile kvmdev("/dev/kvm"); + m_kemuui->KVMCheckBox->setEnabled(kvmdev.exists()); + + m_kemuui->startStopButton->setEnabled(true); + m_kemuui->groupBox->setEnabled(true); + if (m_machines.contains(machine)) { + kDebug() << "machine is running" << machine; + QProcess* machineProcess = m_machines.value(machine); + if (machineProcess->state() == QProcess::Running) { + m_kemuui->startStopButton->setText(i18n("Stop")); + m_kemuui->startStopButton->setIcon(KIcon("system-shutdown")); + } else { + m_kemuui->startStopButton->setText(i18n("Start")); + m_kemuui->startStopButton->setIcon(KIcon("system-run")); + } + } + machineLoad(machine); + } else { + m_kemuui->startStopButton->setEnabled(false); + m_kemuui->groupBox->setEnabled(false); + } +} + +void KEmuMainWindow::machineFinished(int exitCode, QProcess::ExitStatus exitStatus) +{ + QProcess *machineProcess = qobject_cast(sender()); + disconnect(machineProcess, SIGNAL(finished(int,QProcess::ExitStatus)), + this, SLOT(machineFinished(int,QProcess::ExitStatus))); + if (exitCode != 0) { + QMessageBox::warning(this, i18n("QEMU error"), + i18n("An error occured:\n%1", QString(machineProcess->readAll()))); + } + m_kemuui->startStopButton->setText(i18n("Start")); + m_kemuui->startStopButton->setIcon(KIcon("system-run")); + const QString machine = m_machines.key(machineProcess); + m_machines.remove(machine); + machineProcess->deleteLater(); +} + +void KEmuMainWindow::addMachine(const QString machine) +{ + m_settings->setValue(machine + "/enable", true); + // TODO: set defaults? + m_settings->sync(); +} + +void KEmuMainWindow::startStopMachine() +{ + const QString machine = m_kemuui->machinesList->currentText(); + if (!machine.isEmpty()) { + if (m_machines.contains(machine)) { + kDebug() << "stopping machine" << machine; + QProcess* machineProcess = m_machines.take(machine); + machineProcess->terminate(); + machineProcess->deleteLater(); + m_kemuui->startStopButton->setText(i18n("Start")); + m_kemuui->startStopButton->setIcon(KIcon("system-run")); + } else { + kDebug() << "starting machine" << machine; + QStringList machineArgs; + const QString CDRom = m_kemuui->CDROMInput->url().prettyUrl(); + if (!CDRom.isEmpty()) { + machineArgs << "-cdrom" << CDRom; + } + const QString HDrive = m_kemuui->HardDriveInput->url().prettyUrl(); + if (!HDrive.isEmpty()) { + machineArgs << "-hda" << HDrive; + } + if (CDRom.isEmpty() && HDrive.isEmpty()) { + QMessageBox::warning(this, i18n("Requirements not met"), + i18n("Either CD-ROM or Hard-Drive image must be set")); + return; + } + machineArgs << "-vga" << m_kemuui->videoComboBox->currentText(); + machineArgs << "-m" << QByteArray::number(m_kemuui->RAMInput->value()); + if (m_kemuui->KVMCheckBox->isChecked()) { + machineArgs << "-enable-kvm"; + } + const QString extraArgs = m_kemuui->argumentsLineEdit->text(); + if (!extraArgs.isEmpty()) { + foreach (const QString argument, extraArgs.split(" ")) { + machineArgs << argument; + } + } + QProcess* machineProcess = new QProcess(this); + machineProcess->setProcessChannelMode(QProcess::MergedChannels); + machineProcess->start(m_kemuui->systemComboBox->currentText(), machineArgs); + machineProcess->waitForStarted(); + m_kemuui->startStopButton->setText(i18n("Stop")); + m_kemuui->startStopButton->setIcon(KIcon("system-shutdown")); + m_machines.insert(machine, machineProcess); + connect(machineProcess, SIGNAL(finished(int,QProcess::ExitStatus)), + this, SLOT(machineFinished(int,QProcess::ExitStatus))); + } + } +} + +void KEmuMainWindow::removeMachine(const QString machine) +{ + if (m_machines.contains(machine)) { + kDebug() << "stopping machine" << machine; + QProcess* machineProcess = m_machines.take(machine); + machineProcess->terminate(); + machineProcess->deleteLater(); + } + kDebug() << "removing machine" << machine; + m_settings->setValue(machine + "/enable", false); + m_settings->sync(); +} diff --git a/kemu/kemumainwindow.h b/kemu/kemumainwindow.h new file mode 100644 index 00000000..f7b6c9dc --- /dev/null +++ b/kemu/kemumainwindow.h @@ -0,0 +1,56 @@ +/* This file is part of the KDE libraries + Copyright (C) 2016 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KEMUMAINWINDOW_H +#define KEMUMAINWINDOW_H + +#include +#include +#include +#include + +class Ui_KEmuWindow; +class KEmuMainWindow: public KXmlGuiWindow +{ + Q_OBJECT +public: + KEmuMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0); + ~KEmuMainWindow(); + +public slots: + void createHardDrive(); + void quit(); + +private slots: + void machineLoad(const QString machine); + void machineSave(const QString ignored); + void machineSave(int ignored); + void machineChanged(QItemSelection ignored, QItemSelection ignored2); + void machineFinished(int exitCode, QProcess::ExitStatus exitStatus); + void addMachine(const QString machine); + void startStopMachine(); + void removeMachine(const QString machine); + +private: + + Ui_KEmuWindow *m_kemuui; + QSettings *m_settings; + QHash m_machines; +}; + +#endif // KEMUMAINWINDOW_H \ No newline at end of file diff --git a/kemu/kemuui.rc b/kemu/kemuui.rc new file mode 100644 index 00000000..20986a22 --- /dev/null +++ b/kemu/kemuui.rc @@ -0,0 +1,13 @@ + + + + &File + + + + + + + Main Toolbar + + diff --git a/kemu/main.cpp b/kemu/main.cpp new file mode 100644 index 00000000..f0cb32ac --- /dev/null +++ b/kemu/main.cpp @@ -0,0 +1,43 @@ +/* This file is part of the KDE libraries + Copyright (C) 2016 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include "kemumainwindow.h" + +int main(int argc, char** argv) +{ + KAboutData aboutData("kemu", 0, ki18n("KEmu"), + "1.0.0", ki18n("Simple QEMU frontend for KDE."), + KAboutData::License_GPL_V2, + ki18n("(c) 2016 Ivailo Monev"), + KLocalizedString(), + "http://github.com/fluxer/katana" + ); + + aboutData.addAuthor(ki18n("Ivailo Monev"), + ki18n("Maintainer"), + "xakepa10@gmail.com"); + aboutData.setProgramIconName(QLatin1String("applications-engineering")); + KCmdLineArgs::init(argc, argv, &aboutData); + KApplication *kemuapp = new KApplication(); + KEmuMainWindow *kemuwindow = new KEmuMainWindow(); + kemuwindow->show(); + return kemuapp->exec(); +}