Broken crap
This commit is contained in:
parent
de9afc9469
commit
7be3d97d7b
@ -11,6 +11,7 @@ gdk-sys = { git = "https://github.com/gtk-rs/sys" }
|
|||||||
gtk-sys = { git = "https://github.com/gtk-rs/sys" }
|
gtk-sys = { git = "https://github.com/gtk-rs/sys" }
|
||||||
glib = { git = "https://github.com/gtk-rs/glib.git" }
|
glib = { git = "https://github.com/gtk-rs/glib.git" }
|
||||||
glib-sys = { git = "https://github.com/gtk-rs/sys" }
|
glib-sys = { git = "https://github.com/gtk-rs/sys" }
|
||||||
|
gobject-sys = { git = "https://github.com/gtk-rs/sys" }
|
||||||
ffi = "^0.0.2"
|
ffi = "^0.0.2"
|
||||||
flexi_logger = "^0.5.1"
|
flexi_logger = "^0.5.1"
|
||||||
log = "^0.3.8"
|
log = "^0.3.8"
|
||||||
|
@ -1,11 +1,64 @@
|
|||||||
use gtk;
|
use gtk;
|
||||||
|
use audio::AlsaCard;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: destructors
|
||||||
|
|
||||||
// TODO: glade stuff, config, alsacard
|
// TODO: glade stuff, config, alsacard
|
||||||
pub struct AppS {
|
pub struct AppS {
|
||||||
/* we keep this to ensure the lifetime is across the whole application */
|
pub gui: Gui,
|
||||||
pub status_icon: gtk::StatusIcon,
|
pub acard: Rc<RefCell<AlsaCard>>,
|
||||||
|
}
|
||||||
pub builder_popup: gtk::Builder,
|
|
||||||
|
|
||||||
|
impl AppS {
|
||||||
|
pub fn new() -> AppS {
|
||||||
|
let builder_popup = gtk::Builder::new_from_string(include_str!(
|
||||||
|
"../data/ui/popup-window-vertical.glade"
|
||||||
|
));
|
||||||
|
return AppS {
|
||||||
|
gui: Gui::new(builder_popup),
|
||||||
|
acard: Rc::new(RefCell::new(
|
||||||
|
AlsaCard::new(None, Some(String::from("Master"))).unwrap(),
|
||||||
|
)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Gui {
|
||||||
|
pub status_icon: gtk::StatusIcon,
|
||||||
|
pub popup_window: PopupWindow,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Gui {
|
||||||
|
pub fn new(builder: gtk::Builder) -> Gui {
|
||||||
|
return Gui {
|
||||||
|
status_icon: gtk::StatusIcon::new_from_icon_name("pnmixer"),
|
||||||
|
popup_window: PopupWindow::new(builder),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct PopupWindow {
|
||||||
|
pub window: gtk::Window,
|
||||||
|
pub vol_scale_adj: gtk::Adjustment,
|
||||||
|
pub vol_scale: gtk::Scale,
|
||||||
|
pub mute_check: gtk::CheckButton,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl PopupWindow {
|
||||||
|
pub fn new(builder: gtk::Builder) -> PopupWindow {
|
||||||
|
return PopupWindow {
|
||||||
|
window: builder.get_object("popup_window").unwrap(),
|
||||||
|
vol_scale_adj: builder.get_object("vol_scale_adj").unwrap(),
|
||||||
|
vol_scale: builder.get_object("vol_scale").unwrap(),
|
||||||
|
mute_check: builder.get_object("mute_check").unwrap(),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
153
src/audio.rs
153
src/audio.rs
@ -11,6 +11,7 @@ use libc::size_t;
|
|||||||
use myalsa::*;
|
use myalsa::*;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::cell::Ref;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::u8;
|
use std::u8;
|
||||||
|
|
||||||
@ -23,15 +24,17 @@ pub struct AlsaCard {
|
|||||||
pub mixer: Mixer,
|
pub mixer: Mixer,
|
||||||
pub selem_id: SelemId,
|
pub selem_id: SelemId,
|
||||||
pub watch_ids: Vec<u32>,
|
pub watch_ids: Vec<u32>,
|
||||||
last_action_timestamp: RefCell<i64>,
|
pub last_action_timestamp: RefCell<i64>,
|
||||||
|
pub handlers: RefCell<Vec<Box<Fn(&AlsaCard, AudioSignal, AudioUser)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* TODO: AlsaCard cleanup */
|
/* TODO: AlsaCard cleanup */
|
||||||
impl AlsaCard {
|
impl AlsaCard {
|
||||||
pub fn new(card_name: Option<String>,
|
pub fn new(
|
||||||
elem_name: Option<String>)
|
card_name: Option<String>,
|
||||||
-> Result<AlsaCard> {
|
elem_name: Option<String>,
|
||||||
|
) -> Result<AlsaCard> {
|
||||||
let card = {
|
let card = {
|
||||||
match card_name {
|
match card_name {
|
||||||
Some(name) => get_alsa_card_by_name(name)?,
|
Some(name) => get_alsa_card_by_name(name)?,
|
||||||
@ -39,26 +42,34 @@ impl AlsaCard {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mixer = get_mixer(&card)?;
|
let mixer = get_mixer(&card)?;
|
||||||
let selem_id =
|
let selem_id = get_selem_by_name(
|
||||||
get_selem_by_name(&mixer,
|
&mixer,
|
||||||
elem_name.unwrap_or(String::from("Master")))
|
elem_name.unwrap_or(String::from("Master")),
|
||||||
.unwrap()
|
).unwrap()
|
||||||
.get_id();
|
.get_id();
|
||||||
let vec_pollfd = PollDescriptors::get(&mixer)?;
|
let vec_pollfd = PollDescriptors::get(&mixer)?;
|
||||||
|
|
||||||
/* TODO: callback is registered here, which must be unregistered
|
let mut acard = AlsaCard {
|
||||||
* when the mixer is destroyed!!
|
|
||||||
* poll descriptors must be unwatched too */
|
|
||||||
let watch_ids = watch_poll_descriptors(vec_pollfd, &mixer);
|
|
||||||
|
|
||||||
return Ok(AlsaCard {
|
|
||||||
_cannot_construct: (),
|
_cannot_construct: (),
|
||||||
card: card,
|
card: card,
|
||||||
mixer: mixer,
|
mixer: mixer,
|
||||||
selem_id: selem_id,
|
selem_id: selem_id,
|
||||||
watch_ids: watch_ids,
|
watch_ids: vec![],
|
||||||
last_action_timestamp: RefCell::new(0),
|
last_action_timestamp: RefCell::new(0),
|
||||||
});
|
handlers: RefCell::new(vec![]),
|
||||||
|
};
|
||||||
|
|
||||||
|
/* TODO: callback is registered here, which must be unregistered
|
||||||
|
* when the mixer is destroyed!!
|
||||||
|
* poll descriptors must be unwatched too */
|
||||||
|
let watch_ids = watch_poll_descriptors(vec_pollfd, &acard);
|
||||||
|
// acard.watch_ids = watch_ids;
|
||||||
|
|
||||||
|
// println!("Watch IDs: {:?}", acard.watch_ids);
|
||||||
|
println!("Last_Timestamp: {}", acard.last_action_timestamp.borrow());
|
||||||
|
|
||||||
|
|
||||||
|
return Ok(acard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -75,8 +86,12 @@ impl AlsaCard {
|
|||||||
|
|
||||||
|
|
||||||
pub fn set_vol(&self, new_vol: f64, user: AudioUser) -> Result<()> {
|
pub fn set_vol(&self, new_vol: f64, user: AudioUser) -> Result<()> {
|
||||||
|
{
|
||||||
let mut rc = self.last_action_timestamp.borrow_mut();
|
let mut rc = self.last_action_timestamp.borrow_mut();
|
||||||
*rc = glib::get_real_time();
|
*rc = glib::get_monotonic_time();
|
||||||
|
println!("glib::get_monotonic_time: {}", glib::get_real_time());
|
||||||
|
}
|
||||||
|
println!("Now timestamp: {}", self.last_action_timestamp.borrow());
|
||||||
// TODO invoke handlers, make use of user
|
// TODO invoke handlers, make use of user
|
||||||
return set_vol(&self.selem(), new_vol);
|
return set_vol(&self.selem(), new_vol);
|
||||||
}
|
}
|
||||||
@ -94,32 +109,76 @@ impl AlsaCard {
|
|||||||
|
|
||||||
pub fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> {
|
pub fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> {
|
||||||
let mut rc = self.last_action_timestamp.borrow_mut();
|
let mut rc = self.last_action_timestamp.borrow_mut();
|
||||||
*rc = glib::get_real_time();
|
*rc = glib::get_monotonic_time();
|
||||||
// TODO invoke handlers, make use of user
|
// TODO invoke handlers, make use of user
|
||||||
return set_mute(&self.selem(), mute);
|
return set_mute(&self.selem(), mute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn on_alsa_event(&self, alsa_event: AlsaEvent) {
|
fn on_alsa_event(&self, alsa_event: AlsaEvent) {
|
||||||
let last: i64 = *self.last_action_timestamp.borrow();
|
// let last: i64 = *Ref::clone(&self.last_action_timestamp.borrow());
|
||||||
let now: i64 = glib::get_monotonic_time();
|
|
||||||
let delay = now - last;
|
// if last != 0 {
|
||||||
if delay < 1000000 {
|
// let now: i64 = glib::get_monotonic_time();
|
||||||
return;
|
// let delay: i64 = now - last;
|
||||||
}
|
// if delay < 1000000 {
|
||||||
|
// println!("Too short: {} and {}", now, last);
|
||||||
|
// println!("Delay: {}", delay);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// *self.last_action_timestamp.borrow_mut() = 0;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
/* external change */
|
/* external change */
|
||||||
match alsa_event {
|
match alsa_event {
|
||||||
// TODO: invoke handlers with AudioUserUnknown
|
// TODO: invoke handlers with AudioUserUnknown
|
||||||
AlsaEvent::AlsaCardError => println!("AlsaCardError"),
|
AlsaEvent::AlsaCardError => println!("AlsaCardError"),
|
||||||
AlsaEvent::AlsaCardDiconnected => println!("AlsaCardDiconnected"),
|
AlsaEvent::AlsaCardDiconnected => println!("AlsaCardDiconnected"),
|
||||||
AlsaEvent::AlsaCardValuesChanged => println!("AlsaCardValuesChanged"),
|
AlsaEvent::AlsaCardValuesChanged => {
|
||||||
|
println!("AlsaCardValuesChanged");
|
||||||
|
self.invoke_handlers(
|
||||||
|
self::AudioSignal::AudioValuesChanged,
|
||||||
|
self::AudioUser::AudioUserUnknown,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn invoke_handlers(&self, signal: AudioSignal, user: AudioUser) {
|
||||||
|
|
||||||
|
let mut vec = vec![1,2,3];
|
||||||
|
|
||||||
|
for v in &vec {
|
||||||
|
println!("Elem: {}", v);
|
||||||
|
}
|
||||||
|
|
||||||
|
let handlers = self.handlers.borrow();
|
||||||
|
let x: &Vec<Box<Fn(&AlsaCard, AudioSignal, AudioUser)>> = &*handlers;
|
||||||
|
println!("Vec size: {}", handlers.capacity());
|
||||||
|
// for handler in x {
|
||||||
|
// // let unboxed = handler.as_ref();
|
||||||
|
// // unboxed(&self, signal, user);
|
||||||
|
// println!("Gogo");
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn connect_handler(
|
||||||
|
&self,
|
||||||
|
cb: Box<Fn(&AlsaCard, AudioSignal, AudioUser)>,
|
||||||
|
) {
|
||||||
|
println!("Vec size before: {}", self.handlers.borrow().capacity());
|
||||||
|
|
||||||
|
self.handlers.borrow_mut().push(cb);
|
||||||
|
println!("Vec size after: {}", self.handlers.borrow().capacity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub enum AudioUser {
|
pub enum AudioUser {
|
||||||
AudioUserUnknown,
|
AudioUserUnknown,
|
||||||
AudioUserPopup,
|
AudioUserPopup,
|
||||||
@ -128,7 +187,8 @@ pub enum AudioUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum AudioSignal {
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum AudioSignal {
|
||||||
AudioNoCard,
|
AudioNoCard,
|
||||||
AudioCardInitialized,
|
AudioCardInitialized,
|
||||||
AudioCardCleanedUp,
|
AudioCardCleanedUp,
|
||||||
@ -137,17 +197,19 @@ enum AudioSignal {
|
|||||||
AudioValuesChanged,
|
AudioValuesChanged,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum AlsaEvent {
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum AlsaEvent {
|
||||||
AlsaCardError,
|
AlsaCardError,
|
||||||
AlsaCardDiconnected,
|
AlsaCardDiconnected,
|
||||||
AlsaCardValuesChanged,
|
AlsaCardValuesChanged,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn watch_poll_descriptors(polls: Vec<pollfd>, mixer: &Mixer) -> Vec<c_uint> {
|
fn watch_poll_descriptors(polls: Vec<pollfd>, acard: &AlsaCard) -> Vec<c_uint> {
|
||||||
let mut watch_ids: Vec<c_uint> = vec![];
|
let mut watch_ids: Vec<c_uint> = vec![];
|
||||||
let mixer_ptr =
|
let acard_ptr =
|
||||||
unsafe { mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(mixer) };
|
unsafe { mem::transmute::<&AlsaCard, &glib_sys::gpointer>(acard) };
|
||||||
for poll in polls {
|
for poll in polls {
|
||||||
unsafe {
|
unsafe {
|
||||||
let gioc: *mut glib_sys::GIOChannel =
|
let gioc: *mut glib_sys::GIOChannel =
|
||||||
@ -158,24 +220,31 @@ fn watch_poll_descriptors(polls: Vec<pollfd>, mixer: &Mixer) -> Vec<c_uint> {
|
|||||||
glib_sys::G_IO_IN.bits() | glib_sys::G_IO_ERR.bits(),
|
glib_sys::G_IO_IN.bits() | glib_sys::G_IO_ERR.bits(),
|
||||||
).unwrap(),
|
).unwrap(),
|
||||||
Some(watch_cb),
|
Some(watch_cb),
|
||||||
*mixer_ptr as glib_sys::gpointer,
|
*acard_ptr,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("Handler size in watch_poll_descriptors: {}", acard.handlers.borrow().capacity());
|
||||||
return watch_ids;
|
return watch_ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel,
|
extern "C" fn watch_cb(
|
||||||
|
chan: *mut glib_sys::GIOChannel,
|
||||||
cond: glib_sys::GIOCondition,
|
cond: glib_sys::GIOCondition,
|
||||||
data: glib_sys::gpointer)
|
data: glib_sys::gpointer,
|
||||||
-> glib_sys::gboolean {
|
) -> glib_sys::gboolean {
|
||||||
|
|
||||||
let mixer = data as *mut alsa_sys::snd_mixer_t;
|
let acard =
|
||||||
|
unsafe { mem::transmute::<&glib_sys::gpointer, &AlsaCard>(&data) };
|
||||||
|
println!("Handler size in watch_cb: {}", acard.handlers.borrow().capacity());
|
||||||
|
let mixer = unsafe {
|
||||||
|
mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(&acard.mixer)
|
||||||
|
};
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
alsa_sys::snd_mixer_handle_events(mixer);
|
alsa_sys::snd_mixer_handle_events(*mixer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if cond == glib_sys::G_IO_ERR {
|
if cond == glib_sys::G_IO_ERR {
|
||||||
@ -186,13 +255,14 @@ extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel,
|
|||||||
let mut buf: Vec<u8> = vec![0; 256];
|
let mut buf: Vec<u8> = vec![0; 256];
|
||||||
|
|
||||||
while sread > 0 {
|
while sread > 0 {
|
||||||
let stat: glib_sys::GIOStatus =
|
let stat: glib_sys::GIOStatus = unsafe {
|
||||||
unsafe {
|
glib_sys::g_io_channel_read_chars(
|
||||||
glib_sys::g_io_channel_read_chars(chan,
|
chan,
|
||||||
buf.as_mut_ptr() as *mut u8,
|
buf.as_mut_ptr() as *mut u8,
|
||||||
256,
|
256,
|
||||||
&mut sread as *mut size_t,
|
&mut sread as *mut size_t,
|
||||||
ptr::null_mut())
|
ptr::null_mut(),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
match stat {
|
match stat {
|
||||||
@ -209,5 +279,8 @@ extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel,
|
|||||||
|
|
||||||
// TODO: handle alsa events, pass to 'on_alsa_event'
|
// TODO: handle alsa events, pass to 'on_alsa_event'
|
||||||
|
|
||||||
|
println!("on_alsa_event triggering");
|
||||||
|
acard.on_alsa_event(AlsaEvent::AlsaCardValuesChanged);
|
||||||
|
|
||||||
return true as glib_sys::gboolean;
|
return true as glib_sys::gboolean;
|
||||||
}
|
}
|
||||||
|
@ -79,3 +79,46 @@ macro_rules! try_r {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! try_e {
|
||||||
|
($expr:expr) => {
|
||||||
|
try_er!($expr, ())
|
||||||
|
};
|
||||||
|
($expr:expr, $fmt:expr, $($arg:tt)+) => {
|
||||||
|
try_er!($expr, (), $fmt, $(arg)+)
|
||||||
|
};
|
||||||
|
($expr:expr, $fmt:expr) => {
|
||||||
|
try_er!($expr, (), $fmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! try_er {
|
||||||
|
($expr:expr, $ret:expr) => (match $expr {
|
||||||
|
::std::result::Result::Ok(val) => val,
|
||||||
|
::std::result::Result::Err(err) => {
|
||||||
|
err!("{:?}", err);
|
||||||
|
return $ret;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
($expr:expr, $ret:expr, $fmt:expr) => (match $expr {
|
||||||
|
std::result::Result::Ok(val) => val,
|
||||||
|
std::result::Result::Err(err) => {
|
||||||
|
err!("Original error: {:?}", err);
|
||||||
|
err!($fmt);
|
||||||
|
return $ret;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
($expr:expr, $ret:expr, $fmt:expr, $($arg:tt)+) => (match $expr {
|
||||||
|
std::result::Result::Ok(val) => val,
|
||||||
|
std::result::Result::Err(err) => {
|
||||||
|
err!("Original error: {:?}", err);
|
||||||
|
err!(format!($fmt, $(arg)+));
|
||||||
|
return $ret;
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
16
src/main.rs
16
src/main.rs
@ -15,12 +15,11 @@ extern crate gdk;
|
|||||||
extern crate gdk_sys;
|
extern crate gdk_sys;
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
extern crate glib_sys;
|
extern crate glib_sys;
|
||||||
|
extern crate gobject_sys;
|
||||||
extern crate gtk;
|
extern crate gtk;
|
||||||
extern crate gtk_sys;
|
extern crate gtk_sys;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use app_state::*;
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -33,21 +32,14 @@ mod ui_entry;
|
|||||||
mod ui_popup_window;
|
mod ui_popup_window;
|
||||||
mod ui_tray_icon;
|
mod ui_tray_icon;
|
||||||
|
|
||||||
use audio::AlsaCard;
|
use app_state::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
gtk::init().unwrap();
|
gtk::init().unwrap();
|
||||||
|
|
||||||
let ref apps = AppS {
|
let apps = Rc::new(AppS::new());
|
||||||
status_icon: gtk::StatusIcon::new_from_icon_name("pnmixer"),
|
|
||||||
builder_popup: gtk::Builder::new_from_string(include_str!("../data/ui/popup-window-vertical.glade")),
|
|
||||||
};
|
|
||||||
|
|
||||||
let acard = Rc::new(RefCell::new(AlsaCard::new(None,
|
|
||||||
Some(String::from("Master")))
|
|
||||||
.unwrap()));
|
|
||||||
|
|
||||||
flexi_logger::LogOptions::new()
|
flexi_logger::LogOptions::new()
|
||||||
.log_to_file(false)
|
.log_to_file(false)
|
||||||
@ -55,7 +47,7 @@ fn main() {
|
|||||||
.init(Some("info".to_string()))
|
.init(Some("info".to_string()))
|
||||||
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
|
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
|
||||||
|
|
||||||
ui_entry::init(apps, acard);
|
ui_entry::init(apps);
|
||||||
|
|
||||||
gtk::main();
|
gtk::main();
|
||||||
}
|
}
|
||||||
|
@ -56,9 +56,7 @@ pub fn get_selems(mixer: &Mixer) -> Map<alsa::mixer::Iter, fn(Elem) -> Selem> {
|
|||||||
|
|
||||||
pub fn get_selem_by_name(mixer: &Mixer, name: String) -> Result<Selem> {
|
pub fn get_selem_by_name(mixer: &Mixer, name: String) -> Result<Selem> {
|
||||||
for selem in get_selems(mixer) {
|
for selem in get_selems(mixer) {
|
||||||
let n = selem.get_id()
|
let n = selem.get_id().get_name().map(|y| String::from(y))?;
|
||||||
.get_name()
|
|
||||||
.map(|y| String::from(y))?;
|
|
||||||
|
|
||||||
if n == name {
|
if n == name {
|
||||||
return Ok(selem);
|
return Ok(selem);
|
||||||
@ -99,7 +97,9 @@ pub fn set_vol(selem: &Selem, new_vol: f64) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let range = selem.get_playback_volume_range();
|
let range = selem.get_playback_volume_range();
|
||||||
selem.set_playback_volume_all(percent_to_vol(new_vol, range))?;
|
selem.set_playback_volume_all(
|
||||||
|
percent_to_vol(new_vol, range),
|
||||||
|
)?;
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::AlsaCard;
|
use audio::{AlsaCard, AudioSignal, AudioUser};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use ui_popup_window::*;
|
use ui_popup_window::*;
|
||||||
@ -7,8 +7,21 @@ use ui_tray_icon::*;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn init<'a>(appstate: &'a AppS, rc_acard: Rc<RefCell<AlsaCard>>) {
|
pub fn init(appstate: Rc<AppS>) {
|
||||||
|
let s1 = appstate.clone();
|
||||||
|
let s2 = appstate.clone();
|
||||||
|
|
||||||
init_tray_icon(&appstate);
|
appstate.acard.borrow().connect_handler(
|
||||||
init_popup_window(&appstate, rc_acard);
|
Box::new(|_, s, u| {
|
||||||
|
println!("In der closure");
|
||||||
|
match (s, u) {
|
||||||
|
(AudioSignal::AudioValuesChanged, AudioUser::AudioUserUnknown) => {
|
||||||
|
println!("Gaga");
|
||||||
|
}
|
||||||
|
_ => println!("Nix"),
|
||||||
|
}}),
|
||||||
|
);
|
||||||
|
|
||||||
|
init_tray_icon(s1);
|
||||||
|
init_popup_window(s2);
|
||||||
}
|
}
|
||||||
|
@ -1,82 +1,90 @@
|
|||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::AlsaCard;
|
|
||||||
use audio::AudioUser::*;
|
use audio::AudioUser::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use gdk::DeviceExt;
|
use gdk::DeviceExt;
|
||||||
use gdk::{GrabOwnership, GrabStatus, BUTTON_PRESS_MASK, KEY_PRESS_MASK};
|
use gdk::{GrabOwnership, GrabStatus, BUTTON_PRESS_MASK, KEY_PRESS_MASK};
|
||||||
use gdk;
|
use gdk;
|
||||||
use gdk_sys::{GDK_KEY_Escape, GDK_CURRENT_TIME};
|
use gdk_sys::{GDK_KEY_Escape, GDK_CURRENT_TIME};
|
||||||
|
use glib;
|
||||||
|
use glib_sys;
|
||||||
|
use gobject_sys::{G_SIGNAL_MATCH_ID, G_SIGNAL_MATCH_DATA};
|
||||||
|
use gobject_sys;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk;
|
use gtk;
|
||||||
use std::cell::RefCell;
|
use std::mem;
|
||||||
|
use std::ptr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn init_popup_window(appstate: &AppS, rc_acard: Rc<RefCell<AlsaCard>>) {
|
pub fn init_popup_window(appstate: Rc<AppS>) {
|
||||||
|
let mut toggle_signal = 0;
|
||||||
|
|
||||||
|
/* mute_check.connect_toggled */
|
||||||
|
{
|
||||||
|
let _appstate = appstate.clone();
|
||||||
|
let mute_check = &appstate.clone().gui.popup_window.mute_check;
|
||||||
|
toggle_signal = mute_check.connect_toggled(
|
||||||
|
move |_| on_mute_check_toggled(&_appstate),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/* popup_window.connect_show */
|
/* popup_window.connect_show */
|
||||||
{
|
{
|
||||||
let popup_window: gtk::Window =
|
let _appstate = appstate.clone();
|
||||||
appstate.builder_popup.get_object("popup_window").unwrap();
|
let popup_window = &appstate.clone().gui.popup_window.window;
|
||||||
let vol_scale_adj: gtk::Adjustment =
|
popup_window.connect_show(
|
||||||
appstate.builder_popup.get_object("vol_scale_adj").unwrap();
|
move |w| on_popup_window_show(w, &_appstate, toggle_signal),
|
||||||
let mute_check: gtk::CheckButton =
|
);
|
||||||
appstate.builder_popup.get_object("mute_check").unwrap();
|
|
||||||
let vol_scale: gtk::Scale =
|
|
||||||
appstate.builder_popup.get_object("vol_scale").unwrap();
|
|
||||||
|
|
||||||
let card = rc_acard.clone();
|
|
||||||
popup_window.connect_show(move |w| {
|
|
||||||
let acard = card.borrow();
|
|
||||||
|
|
||||||
let cur_vol = try_w!(acard.vol());
|
|
||||||
println!("Cur vol: {}", cur_vol);
|
|
||||||
set_slider(&vol_scale_adj, cur_vol);
|
|
||||||
|
|
||||||
let muted = acard.get_mute();
|
|
||||||
update_mute_check(&mute_check, muted);
|
|
||||||
|
|
||||||
vol_scale.grab_focus();
|
|
||||||
try_w!(grab_devices(w));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vol_scale_adj.connect_value_changed */
|
/* vol_scale_adj.connect_value_changed */
|
||||||
{
|
{
|
||||||
let vol_scale_adj: Rc<gtk::Adjustment> =
|
let _appstate = appstate.clone();
|
||||||
Rc::new(appstate.builder_popup
|
let vol_scale_adj = &appstate.clone().gui.popup_window.vol_scale_adj;
|
||||||
.get_object("vol_scale_adj")
|
vol_scale_adj.connect_value_changed(
|
||||||
.unwrap());
|
move |_| on_vol_scale_value_changed(&_appstate),
|
||||||
|
);
|
||||||
let card = rc_acard.clone();
|
|
||||||
let vol_scale = vol_scale_adj.clone();
|
|
||||||
vol_scale_adj.connect_value_changed(move |_| {
|
|
||||||
let acard = card.borrow();
|
|
||||||
let val = vol_scale.get_value();
|
|
||||||
|
|
||||||
try_w!(acard.set_vol(val, AudioUserPopup));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/* mute_check.connect_toggled */
|
|
||||||
{
|
|
||||||
let mute_check: gtk::CheckButton =
|
|
||||||
appstate.builder_popup.get_object("mute_check").unwrap();
|
|
||||||
|
|
||||||
let card = rc_acard.clone();
|
|
||||||
mute_check.connect_toggled(move |_| {
|
|
||||||
let acard = card.borrow();
|
|
||||||
|
|
||||||
let muted = try_w!(acard.get_mute());
|
|
||||||
let _ = try_w!(acard.set_mute(!muted, AudioUserPopup));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* popup_window.connect_event */
|
/* popup_window.connect_event */
|
||||||
{
|
{
|
||||||
let popup_window: gtk::Window =
|
let _appstate = appstate.clone();
|
||||||
appstate.builder_popup.get_object("popup_window").unwrap();
|
let popup_window = &appstate.clone().gui.popup_window.window;
|
||||||
popup_window.connect_event(move |w, e| {
|
popup_window.connect_event(move |w, e| {
|
||||||
|
on_popup_window_event(w, e, &_appstate)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn on_popup_window_show(
|
||||||
|
window: >k::Window,
|
||||||
|
appstate: &AppS,
|
||||||
|
toggle_signal: u64,
|
||||||
|
) {
|
||||||
|
let acard = appstate.acard.borrow();
|
||||||
|
let popup_window = &appstate.gui.popup_window;
|
||||||
|
|
||||||
|
let cur_vol = try_w!(acard.vol());
|
||||||
|
println!("Cur vol: {}", cur_vol);
|
||||||
|
set_slider(&popup_window.vol_scale_adj, cur_vol);
|
||||||
|
|
||||||
|
let muted = acard.get_mute();
|
||||||
|
update_mute_check(&appstate, toggle_signal, muted);
|
||||||
|
|
||||||
|
popup_window.vol_scale.grab_focus();
|
||||||
|
// try_w!(grab_devices(window));
|
||||||
|
|
||||||
|
println!("Handler size in on_popup_window_show: {}", acard.handlers.borrow().capacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn on_popup_window_event(
|
||||||
|
w: >k::Window,
|
||||||
|
e: &gdk::Event,
|
||||||
|
appstate: &AppS,
|
||||||
|
) -> gtk::Inhibit {
|
||||||
match gdk::Event::get_event_type(e) {
|
match gdk::Event::get_event_type(e) {
|
||||||
gdk::EventType::GrabBroken => w.hide(),
|
gdk::EventType::GrabBroken => w.hide(),
|
||||||
gdk::EventType::KeyPress => {
|
gdk::EventType::KeyPress => {
|
||||||
@ -102,12 +110,52 @@ pub fn init_popup_window(appstate: &AppS, rc_acard: Rc<RefCell<AlsaCard>>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Inhibit(false);
|
return Inhibit(false);
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn update_mute_check(check_button: >k::CheckButton, muted: Result<bool>) {
|
fn on_vol_scale_value_changed(appstate: &AppS) {
|
||||||
|
let acard = appstate.acard.borrow();
|
||||||
|
|
||||||
|
let val = appstate.gui.popup_window.vol_scale.get_value();
|
||||||
|
|
||||||
|
try_w!(acard.set_vol(val, AudioUserPopup));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn on_mute_check_toggled(appstate: &AppS) {
|
||||||
|
let acard = appstate.acard.borrow();
|
||||||
|
|
||||||
|
let muted = try_w!(acard.get_mute());
|
||||||
|
let _ = try_w!(acard.set_mute(!muted, AudioUserPopup));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn update_mute_check(appstate: &AppS, toggle_signal: u64, muted: Result<bool>) {
|
||||||
|
let check_button = &appstate.gui.popup_window.mute_check;
|
||||||
|
// let check_button_ptr = unsafe {
|
||||||
|
// mem::transmute::<>k::CheckButton, &*mut gobject_sys::GObject>(
|
||||||
|
// check_button,
|
||||||
|
// )
|
||||||
|
// };
|
||||||
|
|
||||||
|
// g_signal_handlers_block_matched() doesn't work in gtk-rs
|
||||||
|
glib::signal_handler_block(check_button, toggle_signal);
|
||||||
|
// let n_blocked = unsafe {
|
||||||
|
// gobject_sys::g_signal_handlers_block_matched(
|
||||||
|
// *check_button_ptr,
|
||||||
|
// G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA,
|
||||||
|
// toggle_signal as u32,
|
||||||
|
// 0,
|
||||||
|
// ptr::null_mut(),
|
||||||
|
// ptr::null_mut(),
|
||||||
|
// ptr::null_mut(),
|
||||||
|
// )
|
||||||
|
// };
|
||||||
|
|
||||||
|
// if n_blocked != 1 {
|
||||||
|
// error!("Wrong number of blocked handlers: {}", n_blocked);
|
||||||
|
// }
|
||||||
|
|
||||||
match muted {
|
match muted {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
check_button.set_active(val);
|
check_button.set_active(val);
|
||||||
@ -120,6 +168,20 @@ fn update_mute_check(check_button: >k::CheckButton, muted: Result<bool>) {
|
|||||||
check_button.set_tooltip_text("Soundcard has no mute switch");
|
check_button.set_tooltip_text("Soundcard has no mute switch");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glib::signal_handler_unblock(check_button, toggle_signal);
|
||||||
|
|
||||||
|
// unsafe {
|
||||||
|
// gobject_sys::g_signal_handlers_unblock_matched(
|
||||||
|
// *check_button_ptr,
|
||||||
|
// G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA,
|
||||||
|
// toggle_signal as u32,
|
||||||
|
// 0,
|
||||||
|
// ptr::null_mut(),
|
||||||
|
// ptr::null_mut(),
|
||||||
|
// ptr::null_mut(),
|
||||||
|
// );
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -134,32 +196,40 @@ fn grab_devices(window: >k::Window) -> Result<()> {
|
|||||||
let gdk_window = window.get_window().ok_or("No window?!")?;
|
let gdk_window = window.get_window().ok_or("No window?!")?;
|
||||||
|
|
||||||
/* Grab the mouse */
|
/* Grab the mouse */
|
||||||
let m_grab_status =
|
let m_grab_status = device.grab(
|
||||||
device.grab(&gdk_window,
|
&gdk_window,
|
||||||
GrabOwnership::None,
|
GrabOwnership::None,
|
||||||
true,
|
true,
|
||||||
BUTTON_PRESS_MASK,
|
BUTTON_PRESS_MASK,
|
||||||
None,
|
None,
|
||||||
GDK_CURRENT_TIME as u32);
|
GDK_CURRENT_TIME as u32,
|
||||||
|
);
|
||||||
|
|
||||||
if m_grab_status != GrabStatus::Success {
|
if m_grab_status != GrabStatus::Success {
|
||||||
warn!("Could not grab {}",
|
warn!(
|
||||||
device.get_name().unwrap_or(String::from("UNKNOWN DEVICE")));
|
"Could not grab {}",
|
||||||
|
device.get_name().unwrap_or(String::from("UNKNOWN DEVICE"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Grab the keyboard */
|
/* Grab the keyboard */
|
||||||
let k_dev = device.get_associated_device()
|
let k_dev = device.get_associated_device().ok_or(
|
||||||
.ok_or("Couldn't get associated device")?;
|
"Couldn't get associated device",
|
||||||
|
)?;
|
||||||
|
|
||||||
let k_grab_status = k_dev.grab(&gdk_window,
|
let k_grab_status = k_dev.grab(
|
||||||
|
&gdk_window,
|
||||||
GrabOwnership::None,
|
GrabOwnership::None,
|
||||||
true,
|
true,
|
||||||
KEY_PRESS_MASK,
|
KEY_PRESS_MASK,
|
||||||
None,
|
None,
|
||||||
GDK_CURRENT_TIME as u32);
|
GDK_CURRENT_TIME as u32,
|
||||||
|
);
|
||||||
if k_grab_status != GrabStatus::Success {
|
if k_grab_status != GrabStatus::Success {
|
||||||
warn!("Could not grab {}",
|
warn!(
|
||||||
k_dev.get_name().unwrap_or(String::from("UNKNOWN DEVICE")));
|
"Could not grab {}",
|
||||||
|
k_dev.get_name().unwrap_or(String::from("UNKNOWN DEVICE"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
use app_state::*;
|
use app_state::*;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn init_tray_icon(appstate: &AppS) {
|
pub fn init_tray_icon(appstate: Rc<AppS>) {
|
||||||
|
|
||||||
let ref tray_icon = appstate.status_icon;
|
let tray_icon = &appstate.clone().gui.status_icon;
|
||||||
|
tray_icon.connect_activate(
|
||||||
|
move |_| on_tray_icon_activate(&appstate.clone()),
|
||||||
|
);
|
||||||
|
tray_icon.set_visible(true);
|
||||||
|
}
|
||||||
|
|
||||||
let popup_window: gtk::Window =
|
|
||||||
appstate.builder_popup.get_object("popup_window").unwrap();
|
|
||||||
|
|
||||||
tray_icon.connect_activate(move |_| if popup_window.get_visible() {
|
fn on_tray_icon_activate(appstate: &AppS) {
|
||||||
|
let popup_window = &appstate.gui.popup_window.window;
|
||||||
|
|
||||||
|
if popup_window.get_visible() {
|
||||||
popup_window.hide();
|
popup_window.hide();
|
||||||
} else {
|
} else {
|
||||||
popup_window.show_now();
|
popup_window.show_now();
|
||||||
});
|
}
|
||||||
tray_icon.set_visible(true);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user