Initial commit

This commit is contained in:
Marc-André Lureau
2021-01-23 20:03:56 +04:00
commit edaffdb868
43 changed files with 1656 additions and 0 deletions
+170
View File
@@ -0,0 +1,170 @@
use crate::config;
use crate::window::QemuApplicationWindow;
use gio::ApplicationFlags;
use glib::clone;
use glib::WeakRef;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gdk, gio, glib};
use gtk_macros::action;
use log::{debug, info};
use once_cell::sync::OnceCell;
use std::env;
use qemu_display_listener::Console;
use zbus::Connection;
mod imp {
use super::*;
use glib::subclass;
#[derive(Debug)]
pub struct QemuApplication {
pub window: OnceCell<WeakRef<QemuApplicationWindow>>,
pub conn: OnceCell<Connection>,
}
impl ObjectSubclass for QemuApplication {
const NAME: &'static str = "QemuApplication";
type Type = super::QemuApplication;
type ParentType = gtk::Application;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;
glib::object_subclass!();
fn new() -> Self {
Self {
window: OnceCell::new(),
conn: OnceCell::new(),
}
}
}
impl ObjectImpl for QemuApplication {}
impl gio::subclass::prelude::ApplicationImpl for QemuApplication {
fn activate(&self, app: &Self::Type) {
debug!("GtkApplication<QemuApplication>::activate");
let priv_ = QemuApplication::from_instance(app);
if let Some(window) = priv_.window.get() {
let window = window.upgrade().unwrap();
window.show();
window.present();
return;
}
app.set_resource_base_path(Some("/org/qemu/gtk4/"));
app.setup_css();
let conn = Connection::new_session().expect("Failed to connect");
let console = Console::new(&conn, 0).expect("Failed to get the console");
self.conn.set(conn).expect("Connection already set.");
let window = QemuApplicationWindow::new(app, console);
self.window
.set(window.downgrade())
.expect("Window already set.");
app.setup_gactions();
app.setup_accels();
app.get_main_window().present();
}
fn startup(&self, app: &Self::Type) {
debug!("GtkApplication<QemuApplication>::startup");
self.parent_startup(app);
}
}
impl GtkApplicationImpl for QemuApplication {}
}
glib::wrapper! {
pub struct QemuApplication(ObjectSubclass<imp::QemuApplication>)
@extends gio::Application, gtk::Application, @implements gio::ActionMap, gio::ActionGroup;
}
impl QemuApplication {
pub fn new() -> Self {
glib::Object::new(&[
("application-id", &Some(config::APP_ID)),
("flags", &ApplicationFlags::empty()),
])
.expect("Application initialization failed...")
}
fn get_main_window(&self) -> QemuApplicationWindow {
let priv_ = imp::QemuApplication::from_instance(self);
priv_.window.get().unwrap().upgrade().unwrap()
}
fn setup_gactions(&self) {
// Quit
action!(
self,
"quit",
clone!(@weak self as app => move |_, _| {
// This is needed to trigger the delete event
// and saving the window state
app.get_main_window().close();
app.quit();
})
);
// About
action!(
self,
"about",
clone!(@weak self as app => move |_, _| {
app.show_about_dialog();
})
);
}
// Sets up keyboard shortcuts
fn setup_accels(&self) {
self.set_accels_for_action("app.quit", &["<primary>q"]);
self.set_accels_for_action("win.show-help-overlay", &["<primary>question"]);
}
fn setup_css(&self) {
let provider = gtk::CssProvider::new();
provider.load_from_resource("/org/qemu/gtk4/style.css");
if let Some(display) = gdk::Display::get_default() {
gtk::StyleContext::add_provider_for_display(
&display,
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
}
}
fn show_about_dialog(&self) {
let dialog = gtk::AboutDialogBuilder::new()
.program_name("QEMU Gtk")
.logo_icon_name(config::APP_ID)
.license_type(gtk::License::MitX11)
.website("https://gitlab.com/qemu-project/qemu/")
.version(config::VERSION)
.transient_for(&self.get_main_window())
.modal(true)
.authors(vec!["QEMU developpers".into()])
.artists(vec!["QEMU developpers".into()])
.build();
dialog.show();
}
pub fn run(&self) {
info!("QEMU Gtk ({})", config::APP_ID);
info!("Version: {} ({})", config::VERSION, config::PROFILE);
info!("Datadir: {}", config::PKGDATADIR);
let args: Vec<String> = env::args().collect();
ApplicationExtManual::run(self, &args);
}
}
+7
View File
@@ -0,0 +1,7 @@
pub const APP_ID: &str = @APP_ID@;
pub const GETTEXT_PACKAGE: &str = @GETTEXT_PACKAGE@;
pub const LOCALEDIR: &str = @LOCALEDIR@;
pub const PKGDATADIR: &str = @PKGDATADIR@;
pub const PROFILE: &str = @PROFILE@;
pub const RESOURCES_FILE: &str = concat!(@PKGDATADIR@, "/resources.gresource");
pub const VERSION: &str = @VERSION@;
+32
View File
@@ -0,0 +1,32 @@
#[allow(clippy::new_without_default)]
mod application;
#[rustfmt::skip]
mod config;
mod window;
use application::QemuApplication;
use config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};
use gettextrs::*;
use gtk::gio;
fn main() {
// Initialize logger, debug is carried out via debug!, info!, and warn!.
pretty_env_logger::init();
// Prepare i18n
setlocale(LocaleCategory::LcAll, "");
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
textdomain(GETTEXT_PACKAGE);
gtk::glib::set_application_name("QEMU Gtk");
gtk::glib::set_prgname(Some("qemu-gtk4"));
gtk::init().expect("Unable to start GTK4");
let res = gio::Resource::load(RESOURCES_FILE).expect("Could not load gresource file");
gio::resources_register(&res);
let app = QemuApplication::new();
app.run();
}
+45
View File
@@ -0,0 +1,45 @@
global_conf = configuration_data()
global_conf.set_quoted('APP_ID', application_id)
global_conf.set_quoted('PKGDATADIR', pkgdatadir)
global_conf.set_quoted('PROFILE', profile)
global_conf.set_quoted('VERSION', version + version_suffix)
global_conf.set_quoted('GETTEXT_PACKAGE', gettext_package)
global_conf.set_quoted('LOCALEDIR', localedir)
config = configure_file(
input: 'config.rs.in',
output: 'config.rs',
configuration: global_conf
)
# Copy the config.rs output to the source directory.
run_command(
'cp',
meson.build_root() / 'src' / 'config.rs',
meson.source_root() / 'src' / 'config.rs',
check: true
)
sources = files(
'application.rs',
'config.rs',
'main.rs',
'window.rs',
)
custom_target(
'cargo-build',
build_by_default: true,
input: sources,
output: meson.project_name(),
console: true,
install: true,
install_dir: bindir,
depends: resources,
command: [
cargo_script,
meson.build_root(),
meson.source_root(),
'@OUTPUT@',
profile,
meson.project_name(),
]
)
+140
View File
@@ -0,0 +1,140 @@
use crate::application::QemuApplication;
use crate::config::{APP_ID, PROFILE};
use glib::clone;
use glib::signal::Inhibit;
use gtk::subclass::prelude::*;
use gtk::{self, prelude::*};
use gtk::{gio, glib, CompositeTemplate};
use log::warn;
use qemu_display_listener::Console;
mod imp {
use super::*;
use glib::subclass;
#[derive(Debug, CompositeTemplate)]
#[template(resource = "/org/qemu/gtk4/window.ui")]
pub struct QemuApplicationWindow {
#[template_child]
pub headerbar: TemplateChild<gtk::HeaderBar>,
#[template_child]
pub label: TemplateChild<gtk::Label>,
pub settings: gio::Settings,
}
impl ObjectSubclass for QemuApplicationWindow {
const NAME: &'static str = "QemuApplicationWindow";
type Type = super::QemuApplicationWindow;
type ParentType = gtk::ApplicationWindow;
type Interfaces = ();
type Instance = subclass::simple::InstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;
glib::object_subclass!();
fn new() -> Self {
Self {
headerbar: TemplateChild::default(),
label: TemplateChild::default(),
settings: gio::Settings::new(APP_ID),
}
}
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
}
// You must call `Widget`'s `init_template()` within `instance_init()`.
fn instance_init(obj: &glib::subclass::InitializingObject<Self::Type>) {
obj.init_template();
}
}
impl ObjectImpl for QemuApplicationWindow {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
let builder = gtk::Builder::from_resource("/org/qemu/gtk4/shortcuts.ui");
let shortcuts = builder.get_object("shortcuts").unwrap();
obj.set_help_overlay(Some(&shortcuts));
// Devel Profile
if PROFILE == "Devel" {
obj.get_style_context().add_class("devel");
}
// load latest window state
obj.load_window_size();
}
}
impl WindowImpl for QemuApplicationWindow {
// save window state on delete event
fn close_request(&self, obj: &Self::Type) -> Inhibit {
if let Err(err) = obj.save_window_size() {
warn!("Failed to save window state, {}", &err);
}
Inhibit(false)
}
}
impl WidgetImpl for QemuApplicationWindow {}
impl ApplicationWindowImpl for QemuApplicationWindow {}
}
glib::wrapper! {
pub struct QemuApplicationWindow(ObjectSubclass<imp::QemuApplicationWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, @implements gio::ActionMap, gio::ActionGroup;
}
impl QemuApplicationWindow {
pub fn new(app: &QemuApplication, console: Console) -> Self {
let window: Self = glib::Object::new(&[]).expect("Failed to create QemuApplicationWindow");
window.set_application(Some(app));
// Set icons for shell
gtk::Window::set_default_icon_name(APP_ID);
let rx = console
.glib_listen()
.expect("Failed to listen to the console");
rx.attach(
None,
clone!(@weak window as win => move |t| {
let label = &imp::QemuApplicationWindow::from_instance(&win).label;
label.set_text(&format!("{:?}", t));
Continue(true)
}),
);
window
}
pub fn save_window_size(&self) -> Result<(), glib::BoolError> {
let settings = &imp::QemuApplicationWindow::from_instance(self).settings;
let size = self.get_default_size();
settings.set_int("window-width", size.0)?;
settings.set_int("window-height", size.1)?;
settings.set_boolean("is-maximized", self.is_maximized())?;
Ok(())
}
fn load_window_size(&self) {
let settings = &imp::QemuApplicationWindow::from_instance(self).settings;
let width = settings.get_int("window-width");
let height = settings.get_int("window-height");
let is_maximized = settings.get_boolean("is-maximized");
self.set_default_size(width, height);
if is_maximized {
self.maximize();
}
}
}