Skip to content

Commit

Permalink
WIP: port to gtk4 (compiles but doesn't work properly)
Browse files Browse the repository at this point in the history
- some icons are broken
- clearing osd window doesn't work
- gtk4_layer_shell crashes more often than not
  • Loading branch information
Ferdi265 committed Sep 15, 2024
1 parent 2748d3c commit b3e6f03
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 381 deletions.
502 changes: 219 additions & 283 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ toml = "0.8"
serde = "1"
serde_derive = "1"
# GUI Dependencies
gtk = "0.18.1"
gtk-layer-shell = "0.8.1"
gtk4 = "0.9.1"
gtk4-layer-shell = "0.4.0"
shrinkwraprs = "0.3.0"
cascade = "1.0.1"
pulse = { version = "2.26.0", package = "libpulse-binding" }
Expand Down
6 changes: 3 additions & 3 deletions src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mod brightness_backend;

use config::APPLICATION_NAME;
use global_utils::{handle_application_args, HandleLocalStatus};
use gtk::glib::{OptionArg, OptionFlags};
use gtk::{gio::ApplicationFlags, Application};
use gtk::{glib, prelude::*};
use gtk4::glib::{OptionArg, OptionFlags};
use gtk4::{gio::ApplicationFlags, Application};
use gtk4::{glib, prelude::*};
use std::env::args_os;
use std::path::PathBuf;
use zbus::{blocking::Connection, proxy};
Expand Down
2 changes: 1 addition & 1 deletion src/config/backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gtk::glib::system_config_dirs;
use gtk4::glib::system_config_dirs;
use serde_derive::Deserialize;
use std::error::Error;
use std::path::PathBuf;
Expand Down
4 changes: 2 additions & 2 deletions src/config/user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use gtk::glib::system_config_dirs;
use gtk::glib::user_config_dir;
use gtk4::glib::system_config_dirs;
use gtk4::glib::user_config_dir;
use serde_derive::Deserialize;
use std::error::Error;
use std::path::Path;
Expand Down
2 changes: 1 addition & 1 deletion src/global_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gtk::glib::{variant::DictEntry, Variant};
use gtk4::glib::{variant::DictEntry, Variant};

use crate::argtypes::ArgTypes;

Expand Down
52 changes: 29 additions & 23 deletions src/server/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use crate::global_utils::{handle_application_args, HandleLocalStatus};
use crate::osd_window::SwayosdWindow;
use crate::utils::{self, *};
use glib::{MainContext, ControlFlow::Continue, ControlFlow::Break};
use gtk::gio::SignalSubscriptionId;
use gtk::gio::{ApplicationFlags, BusNameWatcherFlags, BusType};
use gtk::glib::{clone, OptionArg, OptionFlags};
use gtk::prelude::*;
use gtk::*;
use gtk4::gio::prelude::*;
use gtk4::gio::SignalSubscriptionId;
use gtk4::gio::{ApplicationFlags, BusNameWatcherFlags, BusType};
use gtk4::glib::{clone, OptionArg, OptionFlags, variant::ToVariant};
use gtk4::prelude::*;
use gtk4::*;
use pulsectl::controllers::{SinkController, SourceController};
use std::cell::RefCell;
use std::rc::Rc;
Expand All @@ -20,7 +21,7 @@ use super::config::user::ServerConfig;
#[derive(Clone, Shrinkwrap)]
pub struct SwayOSDApplication {
#[shrinkwrap(main_field)]
app: gtk::Application,
app: gtk4::Application,
windows: Rc<RefCell<Vec<SwayosdWindow>>>,
}

Expand Down Expand Up @@ -77,7 +78,7 @@ impl SwayOSDApplication {
}

// Parse args
app.connect_handle_local_options(clone!(@strong osd_app => move |_app, args| {
app.connect_handle_local_options(clone!(#[strong] osd_app, move |_app, args| {
let actions = match handle_application_args(args.to_variant()) {
(HandleLocalStatus::SUCCESS | HandleLocalStatus::CONTINUE, actions) => actions,
(status @ HandleLocalStatus::FAILURE, _) => return status as i32,
Expand Down Expand Up @@ -109,7 +110,7 @@ impl SwayOSDApplication {
}));

// Listen to any Client actions
MainContext::default().spawn_local(clone!(@strong osd_app => async move {
MainContext::default().spawn_local(clone!(#[strong] osd_app, async move {
while let Ok((arg_type, data)) = action_receiver.recv().await {
Self::action_activated(&osd_app, arg_type, (!data.is_empty()).then_some(data));
}
Expand All @@ -118,7 +119,7 @@ impl SwayOSDApplication {

// Listen to the LibInput Backend and activate the Application action
let (sender, receiver) = async_channel::bounded::<(u16, i32)>(1);
MainContext::default().spawn_local(clone!(@strong osd_app => async move {
MainContext::default().spawn_local(clone!(#[strong] osd_app, async move {
while let Ok((key_code, state)) = receiver.recv().await {
let (arg_type, data): (ArgTypes, Option<String>) =
match evdev_rs::enums::int_to_ev_key(key_code as u32) {
Expand All @@ -143,7 +144,7 @@ impl SwayOSDApplication {
BusType::System,
DBUS_BACKEND_NAME,
BusNameWatcherFlags::NONE,
clone!(@strong sender, @strong signal_id => move |connection, _, _| {
clone!(#[strong] sender, #[strong] signal_id, move |connection, _, _| {
println!("Connecting to the SwayOSD LibInput Backend");
let mut mutex = match signal_id.lock() {
Ok(mut mutex) => match mutex.as_mut() {
Expand All @@ -159,7 +160,7 @@ impl SwayOSDApplication {
Some(config::DBUS_PATH),
None,
gio::DBusSignalFlags::NONE,
clone!(@strong sender => move |_, _, _, _, _, variant| {
clone!(#[strong] sender, move |_, _, _, _, _, variant| {
let key_code = variant.try_child_get::<u16>(0);
let state = variant.try_child_get::<i32>(1);
match (key_code, state) {
Expand All @@ -173,7 +174,7 @@ impl SwayOSDApplication {
}),
));
}),
clone!(@strong signal_id => move|connection, _| {
clone!(#[strong] signal_id, move|connection, _| {
eprintln!("SwayOSD LibInput Backend isn't available, waiting...");
match signal_id.lock() {
Ok(mut mutex) => if let Some(sig_id) = mutex.take() {
Expand Down Expand Up @@ -372,24 +373,28 @@ impl SwayOSDApplication {

let _self = self;

display.connect_opened(clone!(@strong _self => move |d| {
display.connect_opened(clone!(#[strong] _self, move |d| {
_self.init_windows(d);
}));

display.connect_closed(clone!(@strong _self => move |_d, is_error| {
display.connect_closed(clone!(#[strong] _self, move |_d, is_error| {
if is_error {
eprintln!("Display closed due to errors...");
}
_self.close_all_windows();
}));

display.connect_monitor_added(clone!(@strong _self => move |d, mon| {
_self.add_window(d, mon);
}));

display.connect_monitor_removed(clone!(@strong _self => move |d, _mon| {
_self.init_windows(d);
}));
display.monitors().connect_items_changed(clone!(#[strong] _self, move |monitors, position, removed, added| {
if removed != 0 {
_self.init_windows(&display);
} else if added != 0 {
for i in 0..added {
if let Some(mon) = monitors.item(position + i).and_then(|obj| obj.downcast::<gdk::Monitor>().ok()) {
_self.add_window(&display, &mon);
}
}
}
}));
}

fn add_window(&self, display: &gdk::Display, monitor: &gdk::Monitor) {
Expand All @@ -400,8 +405,9 @@ impl SwayOSDApplication {
fn init_windows(&self, display: &gdk::Display) {
self.close_all_windows();

for i in 0..display.n_monitors() {
let monitor: gdk::Monitor = match display.monitor(i) {
let monitors = display.monitors();
for i in 0..monitors.n_items() {
let monitor = match monitors.item(i).and_then(|obj| obj.downcast::<gdk::Monitor>().ok()) {
Some(x) => x,
_ => continue,
};
Expand Down
47 changes: 25 additions & 22 deletions src/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ extern crate cascade;
use application::SwayOSDApplication;
use argtypes::ArgTypes;
use config::{DBUS_PATH, DBUS_SERVER_NAME};
use gtk::prelude::*;
use gtk::{
gdk::Screen,
use gtk4::prelude::*;
use gtk4::{
gdk::Display,
gio::{self, Resource},
glib::Bytes,
traits::IconThemeExt,
CssProvider, IconTheme, StyleContext,
CssProvider, IconTheme,
};
use std::env::args_os;
use std::future::pending;
Expand Down Expand Up @@ -74,7 +73,7 @@ impl DbusServer {
const GRESOURCE_BASE_PATH: &str = "/org/erikreider/swayosd";

fn main() {
if gtk::init().is_err() {
if gtk4::init().is_err() {
eprintln!("failed to initialize GTK Application");
std::process::exit(1);
}
Expand All @@ -86,22 +85,25 @@ fn main() {
gio::resources_register(&res);

// Load the icon theme
let theme = IconTheme::default().expect("Could not get IconTheme");
let theme = IconTheme::default();
theme.add_resource_path(&format!("{}/icons", GRESOURCE_BASE_PATH));

// Load the CSS themes
let screen = Screen::default().expect("Failed getting the default screen");
let display = Display::default().expect("Failed getting the default screen");

// Load the provided default CSS theme
let provider = CssProvider::new();
provider.connect_parsing_error(|_provider, _section, error| {
eprintln!("Could not load default CSS stylesheet: {}", error);
});
match get_system_css_path() {
Some(path) => match provider.load_from_path(path.to_str().unwrap()) {
Ok(_) => StyleContext::add_provider_for_screen(
&screen,
Some(path) => {
provider.load_from_path(path.to_str().unwrap());
gtk4::style_context_add_provider_for_display(
&display,
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION as u32,
),
Err(error) => eprintln!("Could not load default CSS stylesheet: {}", error),
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION as u32,
);
},
None => eprintln!("Could not find the system CSS file..."),
}
Expand Down Expand Up @@ -139,14 +141,15 @@ fn main() {
// Try loading the users CSS theme
if let Some(user_config_path) = user_style_path(custom_user_css) {
let user_provider = CssProvider::new();
user_provider
.load_from_path(&user_config_path)
.expect("Failed loading user defined style.css");
StyleContext::add_provider_for_screen(
&screen,
&user_provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION as u32,
);
user_provider.connect_parsing_error(|_provider, _section, error| {
eprintln!("Failed loading user defined style.css: {}", error);
});
user_provider.load_from_path(&user_config_path);
gtk4::style_context_add_provider_for_display(
&display,
&user_provider,
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION as u32,
);
println!("Loaded user defined CSS file");
}

Expand Down
Loading

0 comments on commit b3e6f03

Please sign in to comment.