Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
c7a200085b | |||
cd0b9bea93 | |||
29332b939e | |||
83381a9ffa | |||
176816ff83 | |||
05e8c772c2 | |||
31eb47a26b | |||
2b9b382883 | |||
c543bb8ce5 | |||
6639d4a577 | |||
ef2eb24328 | |||
7904c00aa8 | |||
01a8a57235 | |||
910baff12a | |||
8cc453e6cd | |||
a64923b818 | |||
3613f8831b | |||
506ad72015 | |||
72cc33ac7d | |||
d8f1a794ac | |||
6fc9d80ab0 | |||
9cc6921194 | |||
a47bede4a7 | |||
6251a65764 | |||
a9145628a8 | |||
590f803cd9 | |||
02c60b23ea | |||
686f5a0f4c | |||
687a3f7393 | |||
ee847ff896 | |||
b7beebc20f |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ target
|
|||||||
Cargo.lock
|
Cargo.lock
|
||||||
*.bk
|
*.bk
|
||||||
*~
|
*~
|
||||||
|
core
|
||||||
|
15
.travis.yml
15
.travis.yml
@ -7,25 +7,30 @@ rust:
|
|||||||
- stable
|
- stable
|
||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
sources:
|
|
||||||
- sourceline: 'ppa:gnome3-team/gnome3-staging'
|
|
||||||
- sourceline: 'ppa:gnome3-team/gnome3'
|
|
||||||
packages:
|
packages:
|
||||||
- libasound2-dev
|
- libasound2-dev
|
||||||
- libgdk-pixbuf2.0-dev
|
- libgdk-pixbuf2.0-dev
|
||||||
- libglib2.0-dev
|
- libglib2.0-dev
|
||||||
- libgtk-3-dev
|
- libgtk-3-dev
|
||||||
- libnotify-dev
|
- libnotify-dev
|
||||||
- libpng-dev
|
|
||||||
- libx11-dev
|
- libx11-dev
|
||||||
- pkg-config
|
- pkg-config
|
||||||
|
# for build.sh
|
||||||
|
- gperf
|
||||||
|
- libegl1-mesa-dev
|
||||||
|
- libfreetype6-dev
|
||||||
|
- libmount-dev
|
||||||
|
- libpng-dev
|
||||||
|
- libxml-parser-perl
|
||||||
|
- libxtst-dev
|
||||||
|
- xutils-dev
|
||||||
before_script:
|
before_script:
|
||||||
- |
|
- |
|
||||||
pip install 'travis-cargo<0.2' --user &&
|
pip install 'travis-cargo<0.2' --user &&
|
||||||
export PATH=$HOME/.local/bin:$PATH
|
export PATH=$HOME/.local/bin:$PATH
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
travis-cargo build
|
./.travis/build.sh
|
||||||
# travis-cargo test &&
|
# travis-cargo test &&
|
||||||
# travis-cargo --only stable doc
|
# travis-cargo --only stable doc
|
||||||
# after_success:
|
# after_success:
|
||||||
|
15
.travis/build.sh
Executable file
15
.travis/build.sh
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -x
|
||||||
|
set -e
|
||||||
|
|
||||||
|
BUNDLE="gtk-3.18.1-2"
|
||||||
|
WD="$PWD"
|
||||||
|
cd "$HOME"
|
||||||
|
git clone https://github.com/gkoz/gtk-bootstrap.git
|
||||||
|
cd gtk-bootstrap
|
||||||
|
./bootstrap.sh "$WD/.travis/manifest.txt"
|
||||||
|
cd "$WD"
|
||||||
|
export PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig"
|
||||||
|
|
||||||
|
travis-cargo build
|
13
.travis/manifest.txt
Normal file
13
.travis/manifest.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
https://dbus.freedesktop.org/releases/dbus/dbus-1.11.14.tar.gz
|
||||||
|
https://launchpad.net/intltool/trunk/0.51.0/+download/intltool-0.51.0.tar.gz
|
||||||
|
https://github.com/anholt/libepoxy/releases/download/1.4.3/libepoxy-1.4.3.tar.xz
|
||||||
|
https://www.freedesktop.org/software/fontconfig/release/fontconfig-2.12.4.tar.gz
|
||||||
|
https://www.freedesktop.org/software/harfbuzz/release/harfbuzz-1.4.6.tar.bz2
|
||||||
|
https://download.gnome.org/sources/glib/2.53/glib-2.53.3.tar.xz
|
||||||
|
https://download.gnome.org/sources/atk/2.25/atk-2.25.2.tar.xz
|
||||||
|
https://download.gnome.org/sources/at-spi2-core/2.25/at-spi2-core-2.25.4.tar.xz
|
||||||
|
https://download.gnome.org/sources/at-spi2-atk/2.25/at-spi2-atk-2.25.3.tar.xz
|
||||||
|
https://www.cairographics.org/releases/cairo-1.14.10.tar.xz
|
||||||
|
https://download.gnome.org/sources/pango/1.40/pango-1.40.6.tar.xz
|
||||||
|
https://download.gnome.org/sources/gdk-pixbuf/2.36/gdk-pixbuf-2.36.6.tar.xz
|
||||||
|
https://download.gnome.org/sources/gtk+/3.22/gtk+-3.22.16.tar.xz
|
114
Cargo.toml
114
Cargo.toml
@ -1,61 +1,83 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "pnmixer-rs"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Julian Ospald <hasufell@posteo.de>"]
|
authors = ["Julian Ospald <hasufell@posteo.de>"]
|
||||||
|
categories = [ "multimedia::audio" ]
|
||||||
[lib]
|
description = "Volume mixer for the system tray"
|
||||||
name = "pnmixerlib"
|
documentation = "https://hasufell.github.io/pnmixer-rust/pnmixerlib/"
|
||||||
path = "src/lib.rs"
|
homepage = "https://github.com/hasufell/pnmixer-rust"
|
||||||
doc = true
|
keywords = [ "alsa", "gtk", "mixer" ]
|
||||||
|
license = "GPL-3.0"
|
||||||
|
name = "pnmixer-rs"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "https://github.com/hasufell/pnmixer-rust"
|
||||||
|
version = "0.1.2"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
doc = false
|
||||||
name = "pnmixer-rs"
|
name = "pnmixer-rs"
|
||||||
path = "src/bin.rs"
|
path = "src/bin.rs"
|
||||||
doc = false
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
alsa = "^0.1.8"
|
alsa = "0.2.0"
|
||||||
alsa-sys = "^0.1.1"
|
alsa-sys = "0.1.1"
|
||||||
error-chain = { git = "https://github.com/hasufell/error-chain.git", branch = "PR-from-error" }
|
error-chain = "0.11.0"
|
||||||
ffi = "^0.0.2"
|
ffi = "0.0.2"
|
||||||
flexi_logger = "^0.5.1"
|
flexi_logger = "0.6.11"
|
||||||
gdk-pixbuf = { git = "https://github.com/gtk-rs/gdk-pixbuf.git" }
|
gdk-pixbuf = "0.3.0"
|
||||||
gdk-pixbuf-sys = { git = "https://github.com/gtk-rs/sys" }
|
gdk-pixbuf-sys = "0.5.0"
|
||||||
gdk-sys = { git = "https://github.com/gtk-rs/sys" }
|
gdk-sys = "0.5.0"
|
||||||
gio = { git = "https://github.com/gtk-rs/gio.git" }
|
getopts = "0.2.17"
|
||||||
glib = { git = "https://github.com/gtk-rs/glib.git" }
|
gio = "0.3.0"
|
||||||
glib-sys = { git = "https://github.com/gtk-rs/sys" }
|
glib = "0.4.1"
|
||||||
gobject-sys = { git = "https://github.com/gtk-rs/sys" }
|
glib-sys = "0.5.0"
|
||||||
gtk-sys = { git = "https://github.com/gtk-rs/sys" }
|
gobject-sys = "0.5.0"
|
||||||
libc = "^0.2.23"
|
gtk-sys = "0.5.0"
|
||||||
log = "^0.3.8"
|
lazy_static = "1.0.0"
|
||||||
serde = "^1.0.9"
|
libc = "0.2.36"
|
||||||
serde_derive = "^1.0.9"
|
libnotify = { version = "1.0.2", optional = true }
|
||||||
toml = "^0.4.2"
|
log = "0.4.1"
|
||||||
which = "*"
|
png = "0.11.0"
|
||||||
xdg = "*"
|
serde = "1.0.27"
|
||||||
libnotify = { git = "https://github.com/hasufell/rust-libnotify.git", branch = "git-deps", optional = true }
|
serde_derive = "1.0.27"
|
||||||
png = "^0.9.0"
|
toml = "0.4.5"
|
||||||
|
w_result = "0.1.2"
|
||||||
[dependencies.gtk]
|
which = "1.0.3"
|
||||||
git = "https://github.com/gtk-rs/gtk.git"
|
xdg = "2.1.0"
|
||||||
features = [ "v3_10", "v3_12", "v3_22" ]
|
|
||||||
|
|
||||||
[dependencies.gdk]
|
[dependencies.gdk]
|
||||||
git = "https://github.com/gtk-rs/gdk.git"
|
features = [
|
||||||
features = [ "v3_10", "v3_12", "v3_22" ]
|
"v3_10",
|
||||||
|
"v3_12",
|
||||||
|
"v3_22",
|
||||||
|
]
|
||||||
|
version = "0.7.0"
|
||||||
|
|
||||||
[profile.dev]
|
[dependencies.gtk]
|
||||||
opt-level = 0 # controls the `--opt-level` the compiler builds with
|
features = [
|
||||||
debug = true # controls whether the compiler passes `-C debuginfo`
|
"v3_10",
|
||||||
# a value of `true` is equivalent to `2`
|
"v3_12",
|
||||||
rpath = false # controls whether the compiler passes `-C rpath`
|
"v3_22",
|
||||||
lto = false # controls `-C lto` for binaries and staticlibs
|
]
|
||||||
debug-assertions = false # controls whether debug assertions are enabled
|
version = "0.3.0"
|
||||||
codegen-units = 1 # controls whether the compiler passes `-C codegen-units`
|
|
||||||
# `codegen-units` is ignored when `lto = true`
|
|
||||||
panic = 'unwind' # panic strategy (`-C panic=...`), can also be 'abort'
|
|
||||||
|
|
||||||
|
[dependencies.x11]
|
||||||
|
features = [
|
||||||
|
"xlib",
|
||||||
|
"xtst",
|
||||||
|
]
|
||||||
|
version = "2.17.2"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
notify = ["libnotify"]
|
notify = ["libnotify"]
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
doc = true
|
||||||
|
name = "pnmixerlib"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
[profile.dev]
|
||||||
|
codegen-units = 1
|
||||||
|
debug = true
|
||||||
|
debug-assertions = false
|
||||||
|
lto = false
|
||||||
|
opt-level = 0
|
||||||
|
panic = "unwind"
|
||||||
|
rpath = false
|
||||||
|
12
README.md
12
README.md
@ -1,3 +1,4 @@
|
|||||||
|
[![Latest Version](https://img.shields.io/crates/v/pnmixer-rs.svg)](https://crates.io/crates/pnmixer-rs)
|
||||||
[![Build Status](https://travis-ci.org/hasufell/pnmixer-rust.svg)](https://travis-ci.org/hasufell/pnmixer-rust)
|
[![Build Status](https://travis-ci.org/hasufell/pnmixer-rust.svg)](https://travis-ci.org/hasufell/pnmixer-rust)
|
||||||
[![Join the chat at https://gitter.im/hasufell/pnmixer-rust](https://badges.gitter.im/hasufell/pnmixer-rust.svg)](https://gitter.im/hasufell/pnmixer-rust?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[![Join the chat at https://gitter.im/hasufell/pnmixer-rust](https://badges.gitter.im/hasufell/pnmixer-rust.svg)](https://gitter.im/hasufell/pnmixer-rust?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
[![Documentation (master)](https://img.shields.io/badge/documentation-master-yellow.svg)](https://hasufell.github.io/pnmixer-rust/pnmixerlib/)
|
[![Documentation (master)](https://img.shields.io/badge/documentation-master-yellow.svg)](https://hasufell.github.io/pnmixer-rust/pnmixerlib/)
|
||||||
@ -27,7 +28,7 @@ the cloned repository:
|
|||||||
cargo install
|
cargo install
|
||||||
```
|
```
|
||||||
|
|
||||||
Features
|
Features/Differences
|
||||||
--------
|
--------
|
||||||
|
|
||||||
Additonal features compared to [nicklan/pnmixer](https://github.com/nicklan/pnmixer):
|
Additonal features compared to [nicklan/pnmixer](https://github.com/nicklan/pnmixer):
|
||||||
@ -40,17 +41,16 @@ Removed features:
|
|||||||
* normalize volume
|
* normalize volume
|
||||||
* slider orientation of volume popup
|
* slider orientation of volume popup
|
||||||
* settings for displaying text volume on volume popup
|
* settings for displaying text volume on volume popup
|
||||||
|
* gtk+:2 support
|
||||||
|
|
||||||
Behavior
|
Differences:
|
||||||
--------
|
|
||||||
|
|
||||||
Pretty much the same. Differences are:
|
|
||||||
|
|
||||||
* volume slider is shown even when volume is muted
|
* volume slider is shown even when volume is muted
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
----
|
----
|
||||||
|
|
||||||
- [ ] [hotkey support](https://github.com/hasufell/pnmixer-rust/issues/5)
|
- [x] [hotkey support](https://github.com/hasufell/pnmixer-rust/issues/5)
|
||||||
- [ ] [translation](https://github.com/hasufell/pnmixer-rust/issues/4)
|
- [ ] [translation](https://github.com/hasufell/pnmixer-rust/issues/4)
|
||||||
- [X] [documentation](https://github.com/hasufell/pnmixer-rust/issues/3)
|
- [X] [documentation](https://github.com/hasufell/pnmixer-rust/issues/3)
|
||||||
|
- [ ] [PulseAudio support](https://github.com/hasufell/pnmixer-rust/issues/11)
|
||||||
|
188
data/ui/hotkey-dialog.glade
Normal file
188
data/ui/hotkey-dialog.glade
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!-- Generated with glade 3.18.3 -->
|
||||||
|
<interface>
|
||||||
|
<requires lib="gtk+" version="3.12"/>
|
||||||
|
<object class="GtkDialog" id="hotkey_dialog">
|
||||||
|
<property name="width_request">300</property>
|
||||||
|
<property name="height_request">200</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="border_width">5</property>
|
||||||
|
<property name="title">Set xxx HotKey</property>
|
||||||
|
<property name="modal">True</property>
|
||||||
|
<property name="destroy_with_parent">True</property>
|
||||||
|
<property name="icon_name">input-keyboard</property>
|
||||||
|
<property name="type_hint">dialog</property>
|
||||||
|
<child internal-child="vbox">
|
||||||
|
<object class="GtkBox" id="dialog-vbox1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="spacing">2</property>
|
||||||
|
<child internal-child="action_area">
|
||||||
|
<object class="GtkButtonBox" id="dialog-action_area1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="layout_style">end</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkButton" id="button1">
|
||||||
|
<property name="use_action_appearance">False</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="receives_default">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="box4">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="halign">center</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkImage" id="image4">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="icon_name">gtk-cancel</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label2">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="margin_start">5</property>
|
||||||
|
<property name="label" translatable="yes">Cancel</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">False</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="pack_type">end</property>
|
||||||
|
<property name="position">3</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="hbox1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkImage" id="image1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="margin_start">10</property>
|
||||||
|
<property name="margin_end">10</property>
|
||||||
|
<property name="pixel_size">57</property>
|
||||||
|
<property name="icon_name">input-keyboard</property>
|
||||||
|
<property name="icon_size">3</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">False</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="vbox1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">Defining HotKey</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="hotkey_reset_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">(press <Ctrl>C to reset)</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkHSeparator" id="hseparator1">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="instruction_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label">Press new HotKey for xxx</property>
|
||||||
|
<property name="use_markup">True</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="key_pressed_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">True</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">4</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<action-widgets>
|
||||||
|
<action-widget response="0">button1</action-widget>
|
||||||
|
</action-widgets>
|
||||||
|
</object>
|
||||||
|
</interface>
|
@ -926,10 +926,229 @@
|
|||||||
</packing>
|
</packing>
|
||||||
</child>
|
</child>
|
||||||
<child>
|
<child>
|
||||||
<placeholder/>
|
<object class="GtkBox" id="hotkeys_vbox">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="valign">start</property>
|
||||||
|
<property name="margin_start">5</property>
|
||||||
|
<property name="margin_end">5</property>
|
||||||
|
<property name="margin_top">5</property>
|
||||||
|
<property name="margin_bottom">5</property>
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkFrame" id="frame8">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label_xalign">0</property>
|
||||||
|
<property name="shadow_type">none</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkBox" id="vbox6">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="margin_start">12</property>
|
||||||
|
<property name="orientation">vertical</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkCheckButton" id="hotkeys_enable_check">
|
||||||
|
<property name="label" translatable="yes">Enable HotKeys</property>
|
||||||
|
<property name="use_action_appearance">False</property>
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="receives_default">False</property>
|
||||||
|
<property name="halign">start</property>
|
||||||
|
<property name="draw_indicator">True</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">False</property>
|
||||||
|
<property name="padding">5</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkGrid" id="hotkeys_grid">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="row_spacing">12</property>
|
||||||
|
<property name="column_spacing">12</property>
|
||||||
|
<property name="column_homogeneous">True</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label20">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="halign">start</property>
|
||||||
|
<property name="label" translatable="yes">Command</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label7">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="halign">start</property>
|
||||||
|
<property name="label" translatable="yes">Mute/Unmute:</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label8">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="halign">start</property>
|
||||||
|
<property name="label" translatable="yes">Volume Up:</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label18">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="halign">start</property>
|
||||||
|
<property name="label" translatable="yes">Volume Down:</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">3</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label21">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">HotKey</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">1</property>
|
||||||
|
<property name="top_attach">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkEventBox" id="hotkeys_mute_eventbox">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="hotkeys_mute_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">(None)</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">1</property>
|
||||||
|
<property name="top_attach">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkEventBox" id="hotkeys_up_eventbox">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="hotkeys_up_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">(None)</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">1</property>
|
||||||
|
<property name="top_attach">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkEventBox" id="hotkeys_down_eventbox">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="hotkeys_down_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">(None)</property>
|
||||||
|
<attributes>
|
||||||
|
<attribute name="weight" value="bold"/>
|
||||||
|
</attributes>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">1</property>
|
||||||
|
<property name="top_attach">3</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel" id="label19">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">Double-click a HotKey to assign a new HotKey</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="left_attach">0</property>
|
||||||
|
<property name="top_attach">4</property>
|
||||||
|
<property name="width">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child type="label">
|
||||||
|
<object class="GtkLabel" id="label24">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="margin_bottom">5</property>
|
||||||
|
<property name="label" translatable="yes"><b>HotKey Settings</b></property>
|
||||||
|
<property name="use_markup">True</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">False</property>
|
||||||
|
<property name="padding">5</property>
|
||||||
|
<property name="position">0</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="position">3</property>
|
||||||
|
</packing>
|
||||||
</child>
|
</child>
|
||||||
<child type="tab">
|
<child type="tab">
|
||||||
<placeholder/>
|
<object class="GtkLabel" id="hotkeys_label">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">HotKeys</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="position">3</property>
|
||||||
|
<property name="tab_fill">False</property>
|
||||||
|
</packing>
|
||||||
</child>
|
</child>
|
||||||
</object>
|
</object>
|
||||||
<packing>
|
<packing>
|
||||||
|
127
src/app_state.rs
127
src/app_state.rs
@ -1,13 +1,16 @@
|
|||||||
//! Global application state.
|
//! Global application state.
|
||||||
|
|
||||||
|
|
||||||
use audio::{Audio, AudioUser};
|
use audio::alsa::backend::*;
|
||||||
|
use audio::frontend::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use gtk;
|
use gtk;
|
||||||
|
use hotkeys::Hotkeys;
|
||||||
use prefs::*;
|
use prefs::*;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use support_audio::*;
|
use std::rc::Rc;
|
||||||
use ui_entry::Gui;
|
use support::audio::*;
|
||||||
|
use ui::entry::Gui;
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
use notif::*;
|
use notif::*;
|
||||||
@ -16,53 +19,80 @@ use notif::*;
|
|||||||
|
|
||||||
// TODO: destructors
|
// TODO: destructors
|
||||||
/// The global application state struct.
|
/// The global application state struct.
|
||||||
pub struct AppS {
|
pub struct AppS<T>
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
_cant_construct: (),
|
_cant_construct: (),
|
||||||
/// Mostly static GUI state.
|
/// Mostly static GUI state.
|
||||||
pub gui: Gui,
|
pub gui: Gui,
|
||||||
/// Audio state.
|
/// Audio state.
|
||||||
pub audio: Audio,
|
pub audio: Rc<T>,
|
||||||
/// Preferences state.
|
/// Preferences state.
|
||||||
pub prefs: RefCell<Prefs>,
|
pub prefs: RefCell<Prefs>,
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
/// Notification state.
|
/// Notification state. In case of initialization failure, this
|
||||||
pub notif: Notif,
|
/// is set to `None`.
|
||||||
|
pub notif: Option<Notif>,
|
||||||
|
/// Hotkey state.
|
||||||
|
pub hotkeys: RefCell<Box<Hotkeys<T>>>, // Gets an Rc to Audio.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl AppS {
|
/// Create a new application state using the `AlsaBackend`.
|
||||||
|
pub fn new_alsa_appstate() -> AppS<AlsaBackend> {
|
||||||
|
let prefs = RefCell::new(unwrap_error!(Prefs::new(), None));
|
||||||
|
|
||||||
|
let card_name = prefs.borrow().device_prefs.card.clone();
|
||||||
|
let chan_name = prefs.borrow().device_prefs.channel.clone();
|
||||||
|
let audio = Rc::new(unwrap_error!(
|
||||||
|
AlsaBackend::new(Some(card_name), Some(chan_name)),
|
||||||
|
None
|
||||||
|
));
|
||||||
|
return AppS::new(prefs, audio);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl<T> AppS<T>
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
/// Create an application state instance. There should really only be one.
|
/// Create an application state instance. There should really only be one.
|
||||||
pub fn new() -> AppS {
|
pub fn new(prefs: RefCell<Prefs>, audio: Rc<T>) -> Self {
|
||||||
let builder_popup_window =
|
let builder_popup_window =
|
||||||
gtk::Builder::new_from_string(include_str!(concat!(env!("CARGO_MANIFEST_DIR"),
|
gtk::Builder::new_from_string(include_str!(concat!(
|
||||||
"/data/ui/popup-window.glade")));
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
|
"/data/ui/popup-window.glade"
|
||||||
|
)));
|
||||||
let builder_popup_menu =
|
let builder_popup_menu =
|
||||||
gtk::Builder::new_from_string(include_str!(concat!(env!("CARGO_MANIFEST_DIR"),
|
gtk::Builder::new_from_string(include_str!(concat!(
|
||||||
"/data/ui/popup-menu.glade")));
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
let prefs = RefCell::new(Prefs::new().unwrap());
|
"/data/ui/popup-menu.glade"
|
||||||
|
)));
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: better error handling
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
let notif = result_warn!(Notif::new(&prefs.borrow()), None).ok();
|
||||||
|
|
||||||
|
let hotkeys =
|
||||||
|
unwrap_error!(
|
||||||
|
wresult_warn!(Hotkeys::new(&prefs.borrow(), audio.clone()), None),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
let gui =
|
let gui =
|
||||||
Gui::new(builder_popup_window, builder_popup_menu, &prefs.borrow());
|
Gui::new(builder_popup_window, builder_popup_menu, &prefs.borrow());
|
||||||
|
|
||||||
let card_name = prefs.borrow()
|
|
||||||
.device_prefs
|
|
||||||
.card
|
|
||||||
.clone();
|
|
||||||
let chan_name = prefs.borrow()
|
|
||||||
.device_prefs
|
|
||||||
.channel
|
|
||||||
.clone();
|
|
||||||
// TODO: better error handling
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
let notif = Notif::new(&prefs.borrow()).unwrap();
|
|
||||||
|
|
||||||
return AppS {
|
return AppS {
|
||||||
_cant_construct: (),
|
_cant_construct: (),
|
||||||
gui,
|
gui,
|
||||||
audio: Audio::new(Some(card_name), Some(chan_name)).unwrap(),
|
audio,
|
||||||
prefs,
|
prefs,
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
notif,
|
notif,
|
||||||
};
|
hotkeys: RefCell::new(hotkeys),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -71,32 +101,37 @@ impl AppS {
|
|||||||
/// Update the tray icon state.
|
/// Update the tray icon state.
|
||||||
pub fn update_tray_icon(&self) -> Result<()> {
|
pub fn update_tray_icon(&self) -> Result<()> {
|
||||||
debug!("Update tray icon!");
|
debug!("Update tray icon!");
|
||||||
return self.gui.tray_icon.update_all(&self.prefs.borrow(),
|
return self.gui.tray_icon.update_all(
|
||||||
&self.audio,
|
&self.prefs.borrow(),
|
||||||
None);
|
self.audio.as_ref(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the Popup Window state.
|
/// Update the Popup Window state.
|
||||||
pub fn update_popup_window(&self) -> Result<()> {
|
pub fn update_popup_window(&self) -> Result<()> {
|
||||||
debug!("Update PopupWindow!");
|
debug!("Update PopupWindow!");
|
||||||
return self.gui.popup_window.update(&self.audio);
|
return self.gui.popup_window.update(self.audio.as_ref());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
/// Update the notification state.
|
/// Update the notification state.
|
||||||
pub fn update_notify(&self) -> Result<()> {
|
pub fn update_notify(&self) {
|
||||||
return self.notif.reload(&self.prefs.borrow());
|
match self.notif {
|
||||||
|
Some(ref n) => n.reload(&self.prefs.borrow()),
|
||||||
|
None => {
|
||||||
|
warn!("Notification system not unitialized, skipping update")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "notify"))]
|
#[cfg(not(feature = "notify"))]
|
||||||
/// Update the notification state.
|
/// Update the notification state.
|
||||||
pub fn update_notify(&self) -> Result<()> {
|
pub fn update_notify(&self) {}
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Update the audio state.
|
/// Update the audio state.
|
||||||
pub fn update_audio(&self, user: AudioUser) -> Result<()> {
|
pub fn update_audio(&self, user: AudioUser) -> Result<()> {
|
||||||
return audio_reload(&self.audio, &self.prefs.borrow(), user);
|
return audio_reload(self.audio.as_ref(), &self.prefs.borrow(), user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the config file.
|
/// Update the config file.
|
||||||
@ -104,4 +139,10 @@ impl AppS {
|
|||||||
let prefs = self.prefs.borrow_mut();
|
let prefs = self.prefs.borrow_mut();
|
||||||
return prefs.store_config();
|
return prefs.store_config();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update hotkey state.
|
||||||
|
pub fn update_hotkeys(&self) -> Result<()> {
|
||||||
|
let prefs = self.prefs.borrow();
|
||||||
|
return self.hotkeys.borrow_mut().reload(&prefs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
448
src/audio.rs
448
src/audio.rs
@ -1,448 +0,0 @@
|
|||||||
#![allow(missing_docs)] // enums
|
|
||||||
|
|
||||||
//! High-level audio subsystem.
|
|
||||||
//!
|
|
||||||
//! This is the middleman between the low-level audio backend (alsa),
|
|
||||||
//! and the high-level ui code.
|
|
||||||
//! This abstraction layer allows the high-level code to be completely unaware
|
|
||||||
//! of the underlying audio implementation, may it be alsa or whatever.
|
|
||||||
|
|
||||||
|
|
||||||
use alsa_card::*;
|
|
||||||
use errors::*;
|
|
||||||
use glib;
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::cell::Ref;
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::f64;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use support_alsa::*;
|
|
||||||
use support_audio::*;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
/// The volume level of the current audio configuration.
|
|
||||||
pub enum VolLevel {
|
|
||||||
Muted,
|
|
||||||
Low,
|
|
||||||
Medium,
|
|
||||||
High,
|
|
||||||
Off,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// An audio user, used to determine from where a signal originated.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub enum AudioUser {
|
|
||||||
Unknown,
|
|
||||||
Popup,
|
|
||||||
TrayIcon,
|
|
||||||
Hotkeys,
|
|
||||||
PrefsWindow,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// An audio signal. This will be used to connect callbacks to the
|
|
||||||
/// audio system and react appropriately.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub enum AudioSignal {
|
|
||||||
NoCard,
|
|
||||||
CardInitialized,
|
|
||||||
CardCleanedUp,
|
|
||||||
CardDisconnected,
|
|
||||||
CardError,
|
|
||||||
ValuesChanged,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
/// Convenience struct to make handling this madness easier.
|
|
||||||
pub struct Handlers {
|
|
||||||
inner: Rc<RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl Handlers {
|
|
||||||
fn new() -> Handlers {
|
|
||||||
return Handlers { inner: Rc::new(RefCell::new(vec![])) };
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn borrow(&self) -> Ref<Vec<Box<Fn(AudioSignal, AudioUser)>>> {
|
|
||||||
return self.inner.borrow();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn add_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
|
||||||
self.inner.borrow_mut().push(cb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// High-level Audio struct, which could theoretically be backend
|
|
||||||
/// agnostic.
|
|
||||||
pub struct Audio {
|
|
||||||
_cannot_construct: (),
|
|
||||||
/// The alsa card.
|
|
||||||
pub acard: RefCell<Box<AlsaCard>>,
|
|
||||||
/// Last timestamp of an internal action we triggered, e.g.
|
|
||||||
/// by setting the volume or the mute state.
|
|
||||||
pub last_action_timestamp: Rc<RefCell<i64>>,
|
|
||||||
/// A set of handlers that react to audio signals. We can
|
|
||||||
/// connect to these.
|
|
||||||
pub handlers: Handlers,
|
|
||||||
/// The step at which to increase/decrease the volume.
|
|
||||||
/// This value is basically from the preferences.
|
|
||||||
pub scroll_step: Cell<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl Audio {
|
|
||||||
/// Create a new Audio instance. This tries very hard to get
|
|
||||||
/// a working configuration from the backend.
|
|
||||||
/// ## `card_name`
|
|
||||||
/// If a card name is provided, it will be tried. If `None` is provided
|
|
||||||
/// or the given card name does not exist or is not playable, any other
|
|
||||||
/// playable card is tried.
|
|
||||||
/// ## `elem_name`
|
|
||||||
/// If an elem name is provided, it will be tried. If `None` is provided
|
|
||||||
/// or the given elem name does not exist or is not playable, any other
|
|
||||||
/// playable elem is tried.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// `Ok(Audio)` on success, `Err(error)` otherwise.
|
|
||||||
pub fn new(card_name: Option<String>,
|
|
||||||
elem_name: Option<String>)
|
|
||||||
-> Result<Audio> {
|
|
||||||
|
|
||||||
let handlers = Handlers::new();
|
|
||||||
let last_action_timestamp = Rc::new(RefCell::new(0));
|
|
||||||
|
|
||||||
let cb = {
|
|
||||||
let myhandler = handlers.clone();
|
|
||||||
let ts = last_action_timestamp.clone();
|
|
||||||
Rc::new(move |event| {
|
|
||||||
on_alsa_event(&mut *ts.borrow_mut(),
|
|
||||||
&myhandler.borrow(),
|
|
||||||
event)
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
let acard = AlsaCard::new(card_name, elem_name, cb);
|
|
||||||
|
|
||||||
/* additionally dispatch signals */
|
|
||||||
if acard.is_err() {
|
|
||||||
invoke_handlers(&handlers.borrow(),
|
|
||||||
AudioSignal::NoCard,
|
|
||||||
AudioUser::Unknown);
|
|
||||||
} else {
|
|
||||||
invoke_handlers(&handlers.borrow(),
|
|
||||||
AudioSignal::CardInitialized,
|
|
||||||
AudioUser::Unknown);
|
|
||||||
}
|
|
||||||
|
|
||||||
let audio = Audio {
|
|
||||||
_cannot_construct: (),
|
|
||||||
acard: RefCell::new(acard?),
|
|
||||||
last_action_timestamp: last_action_timestamp.clone(),
|
|
||||||
handlers: handlers.clone(),
|
|
||||||
scroll_step: Cell::new(5),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(audio);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Switches the current alsa card. Behaves the same way in regards to
|
|
||||||
/// `card_name` and `elem_name` as the `Audio::new()` method.
|
|
||||||
/// ## `user`
|
|
||||||
/// Where the card switch originates from.
|
|
||||||
pub fn switch_acard(&self,
|
|
||||||
card_name: Option<String>,
|
|
||||||
elem_name: Option<String>,
|
|
||||||
user: AudioUser)
|
|
||||||
-> Result<()> {
|
|
||||||
debug!("Switching cards");
|
|
||||||
debug!("Old card name: {}",
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.card_name()
|
|
||||||
.unwrap());
|
|
||||||
debug!("Old chan name: {}",
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.chan_name()
|
|
||||||
.unwrap());
|
|
||||||
let cb = self.acard
|
|
||||||
.borrow()
|
|
||||||
.cb
|
|
||||||
.clone();
|
|
||||||
{
|
|
||||||
let mut ac = self.acard.borrow_mut();
|
|
||||||
*ac = AlsaCard::new(card_name, elem_name, cb)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
invoke_handlers(&self.handlers.borrow(),
|
|
||||||
AudioSignal::CardInitialized,
|
|
||||||
user);
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Current volume.
|
|
||||||
pub fn vol(&self) -> Result<f64> {
|
|
||||||
let alsa_vol = self.acard
|
|
||||||
.borrow()
|
|
||||||
.get_vol()?;
|
|
||||||
return vol_to_percent(alsa_vol, self.acard.borrow().get_volume_range());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Current volume level, nicely usable for e.g. selecting from a set
|
|
||||||
/// of images.
|
|
||||||
pub fn vol_level(&self) -> VolLevel {
|
|
||||||
let muted = self.get_mute().unwrap_or(false);
|
|
||||||
if muted {
|
|
||||||
return VolLevel::Muted;
|
|
||||||
}
|
|
||||||
let cur_vol = try_r!(self.vol(), VolLevel::Muted);
|
|
||||||
match cur_vol {
|
|
||||||
0. => return VolLevel::Off,
|
|
||||||
0.0...33.0 => return VolLevel::Low,
|
|
||||||
0.0...66.0 => return VolLevel::Medium,
|
|
||||||
0.0...100.0 => return VolLevel::High,
|
|
||||||
_ => return VolLevel::Off,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Set the current volume.
|
|
||||||
/// ## `new_vol`
|
|
||||||
/// Set the volume to this value.
|
|
||||||
/// ## `user`
|
|
||||||
/// Where the card switch originates from.
|
|
||||||
/// ## `dir`
|
|
||||||
/// The "direction" of the volume change, e.g. is it a decrease
|
|
||||||
/// or increase. This helps with rounding problems.
|
|
||||||
/// ## `auto_unmute`
|
|
||||||
/// Whether to automatically unmute if the volume changes.
|
|
||||||
pub fn set_vol(&self,
|
|
||||||
new_vol: f64,
|
|
||||||
user: AudioUser,
|
|
||||||
dir: VolDir,
|
|
||||||
auto_unmute: bool)
|
|
||||||
-> Result<()> {
|
|
||||||
{
|
|
||||||
let mut rc = self.last_action_timestamp.borrow_mut();
|
|
||||||
*rc = glib::get_monotonic_time();
|
|
||||||
}
|
|
||||||
|
|
||||||
let alsa_vol = percent_to_vol(new_vol,
|
|
||||||
self.acard.borrow().get_volume_range(),
|
|
||||||
dir)?;
|
|
||||||
|
|
||||||
/* only invoke handlers etc. if volume did actually change */
|
|
||||||
{
|
|
||||||
let old_alsa_vol =
|
|
||||||
percent_to_vol(self.vol()?,
|
|
||||||
self.acard.borrow().get_volume_range(),
|
|
||||||
dir)?;
|
|
||||||
|
|
||||||
if old_alsa_vol == alsa_vol {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* auto-unmute */
|
|
||||||
if auto_unmute && self.has_mute() && self.get_mute()? {
|
|
||||||
self.set_mute(false, user)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("Setting vol on card {:?} and chan {:?} to {:?} by user {:?}",
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.card_name()
|
|
||||||
.unwrap(),
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.chan_name()
|
|
||||||
.unwrap(),
|
|
||||||
new_vol,
|
|
||||||
user);
|
|
||||||
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.set_vol(alsa_vol)?;
|
|
||||||
|
|
||||||
invoke_handlers(&self.handlers.borrow(),
|
|
||||||
AudioSignal::ValuesChanged,
|
|
||||||
user);
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Increase the volume. The step to increasy by is taken from
|
|
||||||
/// `self.scroll_step`.
|
|
||||||
/// ## `user`
|
|
||||||
/// Where the card switch originates from.
|
|
||||||
pub fn increase_vol(&self,
|
|
||||||
user: AudioUser,
|
|
||||||
auto_unmute: bool)
|
|
||||||
-> Result<()> {
|
|
||||||
let old_vol = self.vol()?;
|
|
||||||
let new_vol = old_vol + (self.scroll_step.get() as f64);
|
|
||||||
|
|
||||||
return self.set_vol(new_vol, user, VolDir::Up, auto_unmute);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Decrease the volume. The step to decrease by is taken from
|
|
||||||
/// `self.scroll_step`.
|
|
||||||
/// ## `user`
|
|
||||||
/// Where the card switch originates from.
|
|
||||||
pub fn decrease_vol(&self,
|
|
||||||
user: AudioUser,
|
|
||||||
auto_unmute: bool)
|
|
||||||
-> Result<()> {
|
|
||||||
let old_vol = self.vol()?;
|
|
||||||
let new_vol = old_vol - (self.scroll_step.get() as f64);
|
|
||||||
|
|
||||||
return self.set_vol(new_vol, user, VolDir::Down, auto_unmute);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Whether the current audio configuration can be muted.
|
|
||||||
pub fn has_mute(&self) -> bool {
|
|
||||||
return self.acard.borrow().has_mute();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the mute state of the current audio configuration.
|
|
||||||
pub fn get_mute(&self) -> Result<bool> {
|
|
||||||
return self.acard.borrow().get_mute();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Set the mute state of the current audio configuration.
|
|
||||||
pub fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> {
|
|
||||||
let mut rc = self.last_action_timestamp.borrow_mut();
|
|
||||||
*rc = glib::get_monotonic_time();
|
|
||||||
|
|
||||||
debug!("Setting mute to {} on card {:?} and chan {:?} by user {:?}",
|
|
||||||
mute,
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.card_name()
|
|
||||||
.unwrap(),
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.chan_name()
|
|
||||||
.unwrap(),
|
|
||||||
user);
|
|
||||||
|
|
||||||
self.acard
|
|
||||||
.borrow()
|
|
||||||
.set_mute(mute)?;
|
|
||||||
|
|
||||||
invoke_handlers(&self.handlers.borrow(),
|
|
||||||
AudioSignal::ValuesChanged,
|
|
||||||
user);
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Toggle the mute state of the current audio configuration.
|
|
||||||
pub fn toggle_mute(&self, user: AudioUser) -> Result<()> {
|
|
||||||
let muted = self.get_mute()?;
|
|
||||||
return self.set_mute(!muted, user);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Connect a signal handler to the audio subsystem. This can
|
|
||||||
/// be done from anywhere, e.g. in the UI code to react to
|
|
||||||
/// certain signals. Multiple handlers for the same signals are fine,
|
|
||||||
/// they will be executed in order.
|
|
||||||
pub fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
|
||||||
self.handlers.add_handler(cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the current card name.
|
|
||||||
pub fn card_name(&self) -> Result<String> {
|
|
||||||
return self.acard.borrow().card_name();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the currently playable channel names.
|
|
||||||
pub fn playable_chan_names(&self) -> Vec<String> {
|
|
||||||
return get_playable_selem_names(&self.acard.borrow().mixer);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the current active channel name.
|
|
||||||
pub fn chan_name(&self) -> Result<String> {
|
|
||||||
return self.acard.borrow().chan_name();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Invokes the registered handlers.
|
|
||||||
fn invoke_handlers(handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
|
||||||
signal: AudioSignal,
|
|
||||||
user: AudioUser) {
|
|
||||||
debug!("Invoking handlers for signal {:?} by user {:?}",
|
|
||||||
signal,
|
|
||||||
user);
|
|
||||||
if handlers.is_empty() {
|
|
||||||
debug!("No handler found");
|
|
||||||
} else {
|
|
||||||
debug!("Executing handlers")
|
|
||||||
}
|
|
||||||
for handler in handlers {
|
|
||||||
let unboxed = handler.as_ref();
|
|
||||||
unboxed(signal, user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// The callback for alsa events that is passed to the alsa subsystem.
|
|
||||||
/// This is the bridge between low-level alsa events and "high-level"
|
|
||||||
/// audio system signals.
|
|
||||||
fn on_alsa_event(last_action_timestamp: &mut i64,
|
|
||||||
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
|
||||||
alsa_event: AlsaEvent) {
|
|
||||||
let last: i64 = *last_action_timestamp;
|
|
||||||
|
|
||||||
if last != 0 {
|
|
||||||
let now: i64 = glib::get_monotonic_time();
|
|
||||||
let delay: i64 = now - last;
|
|
||||||
if delay < 1000000 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
debug!("Discarding last time stamp, too old");
|
|
||||||
*last_action_timestamp = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* external change */
|
|
||||||
match alsa_event {
|
|
||||||
AlsaEvent::AlsaCardError => {
|
|
||||||
invoke_handlers(handlers,
|
|
||||||
self::AudioSignal::CardError,
|
|
||||||
self::AudioUser::Unknown);
|
|
||||||
}
|
|
||||||
AlsaEvent::AlsaCardDiconnected => {
|
|
||||||
invoke_handlers(handlers,
|
|
||||||
self::AudioSignal::CardDisconnected,
|
|
||||||
self::AudioUser::Unknown);
|
|
||||||
}
|
|
||||||
AlsaEvent::AlsaCardValuesChanged => {
|
|
||||||
invoke_handlers(handlers,
|
|
||||||
self::AudioSignal::ValuesChanged,
|
|
||||||
self::AudioUser::Unknown);
|
|
||||||
}
|
|
||||||
e => warn!("Unhandled alsa event: {:?}", e),
|
|
||||||
}
|
|
||||||
}
|
|
356
src/audio/alsa/backend.rs
Normal file
356
src/audio/alsa/backend.rs
Normal file
@ -0,0 +1,356 @@
|
|||||||
|
#![allow(illegal_floating_point_literal_pattern)]
|
||||||
|
|
||||||
|
//! Alsa audio backend subsystem.
|
||||||
|
//!
|
||||||
|
//! This mod mainly defines the `AlsaBackend` struct.
|
||||||
|
|
||||||
|
|
||||||
|
use alsa_lib::mixer::SelemChannelId::*;
|
||||||
|
use audio::alsa::card::*;
|
||||||
|
use audio::frontend::*;
|
||||||
|
use errors::*;
|
||||||
|
use glib;
|
||||||
|
use std::cell::Cell;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use support::alsa::*;
|
||||||
|
use support::audio::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// Alsa implementation of the `AudioFrontend`.
|
||||||
|
pub struct AlsaBackend {
|
||||||
|
_cannot_construct: (),
|
||||||
|
/// The alsa card.
|
||||||
|
pub acard: RefCell<Box<AlsaCard>>,
|
||||||
|
/// Last timestamp of an internal action we triggered, e.g.
|
||||||
|
/// by setting the volume or the mute state.
|
||||||
|
pub last_action_timestamp: Rc<RefCell<i64>>,
|
||||||
|
/// A set of handlers that react to audio signals. We can
|
||||||
|
/// connect to these.
|
||||||
|
pub handlers: Handlers,
|
||||||
|
/// The step at which to increase/decrease the volume.
|
||||||
|
/// This value is basically from the preferences.
|
||||||
|
pub scroll_step: Cell<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl AlsaBackend {
|
||||||
|
/// Creates the `AlsaBackend`, containing an `AlsaCard`
|
||||||
|
/// and additional information.
|
||||||
|
pub fn new(
|
||||||
|
card_name: Option<String>,
|
||||||
|
elem_name: Option<String>,
|
||||||
|
) -> Result<AlsaBackend> {
|
||||||
|
|
||||||
|
|
||||||
|
let last_action_timestamp = Rc::new(RefCell::new(0));
|
||||||
|
let handlers = Handlers::new();
|
||||||
|
|
||||||
|
let cb = {
|
||||||
|
let myhandler = handlers.clone();
|
||||||
|
let ts = last_action_timestamp.clone();
|
||||||
|
Rc::new(move |event| {
|
||||||
|
on_alsa_event(&mut *ts.borrow_mut(), &myhandler.borrow(), event)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let acard = AlsaCard::new(card_name, elem_name, cb);
|
||||||
|
|
||||||
|
if acard.is_err() {
|
||||||
|
invoke_handlers(
|
||||||
|
&handlers.borrow(),
|
||||||
|
AudioSignal::NoCard,
|
||||||
|
AudioUser::Unknown,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
invoke_handlers(
|
||||||
|
&handlers.borrow(),
|
||||||
|
AudioSignal::CardInitialized,
|
||||||
|
AudioUser::Unknown,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let alsa_backend = AlsaBackend {
|
||||||
|
_cannot_construct: (),
|
||||||
|
acard: RefCell::new(acard?),
|
||||||
|
last_action_timestamp: last_action_timestamp.clone(),
|
||||||
|
handlers,
|
||||||
|
scroll_step: Cell::new(5),
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok(alsa_backend);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Gets the volume range of the currently selected card configuration.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `(min, max)`
|
||||||
|
fn get_volume_range(&self) -> (i64, i64) {
|
||||||
|
let acard = self.acard.borrow();
|
||||||
|
let selem = acard.selem();
|
||||||
|
return selem.get_playback_volume_range();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl AudioFrontend for AlsaBackend {
|
||||||
|
fn switch_card(
|
||||||
|
&self,
|
||||||
|
card_name: Option<String>,
|
||||||
|
elem_name: Option<String>,
|
||||||
|
user: AudioUser,
|
||||||
|
) -> Result<()> {
|
||||||
|
debug!("Switching cards");
|
||||||
|
debug!("Old card name: {}", self.card_name().unwrap());
|
||||||
|
debug!("Old chan name: {}", self.chan_name().unwrap());
|
||||||
|
let cb = self.acard.borrow().cb.clone();
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut ac = self.acard.borrow_mut();
|
||||||
|
*ac = AlsaCard::new(card_name, elem_name, cb)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("New card name: {}", self.card_name().unwrap());
|
||||||
|
debug!("New chan name: {}", self.chan_name().unwrap());
|
||||||
|
|
||||||
|
invoke_handlers(
|
||||||
|
&self.handlers.borrow(),
|
||||||
|
AudioSignal::CardInitialized,
|
||||||
|
user,
|
||||||
|
);
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn card_name(&self) -> Result<String> {
|
||||||
|
return Ok(self.acard.borrow().card.get_name()?);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn chan_name(&self) -> Result<String> {
|
||||||
|
let n = self.acard.borrow().selem_id.get_name().map(
|
||||||
|
|y| String::from(y),
|
||||||
|
)?;
|
||||||
|
return Ok(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn playable_chan_names(&self) -> Vec<String> {
|
||||||
|
return get_playable_selem_names(&self.acard.borrow().mixer);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_vol(&self) -> Result<f64> {
|
||||||
|
let acard = self.acard.borrow();
|
||||||
|
let selem = acard.selem();
|
||||||
|
let volume = selem.get_playback_volume(FrontRight)?;
|
||||||
|
|
||||||
|
return vol_to_percent(volume, self.get_volume_range());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_vol(
|
||||||
|
&self,
|
||||||
|
new_vol: f64,
|
||||||
|
user: AudioUser,
|
||||||
|
dir: VolDir,
|
||||||
|
auto_unmute: bool,
|
||||||
|
) -> Result<()> {
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut rc = self.last_action_timestamp.borrow_mut();
|
||||||
|
*rc = glib::get_monotonic_time();
|
||||||
|
}
|
||||||
|
|
||||||
|
let alsa_vol = percent_to_vol(new_vol, self.get_volume_range(), dir)?;
|
||||||
|
|
||||||
|
/* only invoke handlers etc. if volume did actually change */
|
||||||
|
{
|
||||||
|
let old_alsa_vol =
|
||||||
|
percent_to_vol(self.get_vol()?, self.get_volume_range(), dir)?;
|
||||||
|
|
||||||
|
if old_alsa_vol == alsa_vol {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* auto-unmute */
|
||||||
|
if auto_unmute && self.has_mute() && self.get_mute()? {
|
||||||
|
self.set_mute(false, user)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Setting vol on card {:?} and chan {:?} to {:?} by user {:?}",
|
||||||
|
self.card_name().unwrap(),
|
||||||
|
self.chan_name().unwrap(),
|
||||||
|
new_vol,
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
self.acard.borrow().selem().set_playback_volume_all(
|
||||||
|
alsa_vol,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
invoke_handlers(
|
||||||
|
&self.handlers.borrow(),
|
||||||
|
AudioSignal::ValuesChanged,
|
||||||
|
user,
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn vol_level(&self) -> VolLevel {
|
||||||
|
let muted = self.get_mute().unwrap_or(false);
|
||||||
|
if muted {
|
||||||
|
return VolLevel::Muted;
|
||||||
|
}
|
||||||
|
let cur_vol = try_r!(self.get_vol(), VolLevel::Muted);
|
||||||
|
match cur_vol {
|
||||||
|
0. => return VolLevel::Off,
|
||||||
|
0.0...33.0 => return VolLevel::Low,
|
||||||
|
0.0...66.0 => return VolLevel::Medium,
|
||||||
|
0.0...100.0 => return VolLevel::High,
|
||||||
|
_ => return VolLevel::Off,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increase_vol(&self, user: AudioUser, auto_unmute: bool) -> Result<()> {
|
||||||
|
let old_vol = self.get_vol()?;
|
||||||
|
let new_vol = old_vol + (self.scroll_step.get() as f64);
|
||||||
|
|
||||||
|
return self.set_vol(new_vol, user, VolDir::Up, auto_unmute);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrease_vol(&self, user: AudioUser, auto_unmute: bool) -> Result<()> {
|
||||||
|
let old_vol = self.get_vol()?;
|
||||||
|
let new_vol = old_vol - (self.scroll_step.get() as f64);
|
||||||
|
|
||||||
|
return self.set_vol(new_vol, user, VolDir::Down, auto_unmute);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_mute(&self) -> bool {
|
||||||
|
let acard = self.acard.borrow();
|
||||||
|
let selem = acard.selem();
|
||||||
|
return selem.has_playback_switch();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mute(&self) -> Result<bool> {
|
||||||
|
let acard = self.acard.borrow();
|
||||||
|
let selem = acard.selem();
|
||||||
|
let val = selem.get_playback_switch(FrontRight)?;
|
||||||
|
return Ok(val == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> {
|
||||||
|
let mut rc = self.last_action_timestamp.borrow_mut();
|
||||||
|
*rc = glib::get_monotonic_time();
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Setting mute to {} on card {:?} and chan {:?} by user {:?}",
|
||||||
|
mute,
|
||||||
|
self.card_name().unwrap(),
|
||||||
|
self.chan_name().unwrap(),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
let acard = self.acard.borrow();
|
||||||
|
let selem = acard.selem();
|
||||||
|
/* true -> mute, false -> unmute */
|
||||||
|
let _ = selem.set_playback_switch_all(!mute as i32)?;
|
||||||
|
|
||||||
|
invoke_handlers(
|
||||||
|
&self.handlers.borrow(),
|
||||||
|
AudioSignal::ValuesChanged,
|
||||||
|
user,
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn toggle_mute(&self, user: AudioUser) -> Result<()> {
|
||||||
|
let muted = self.get_mute()?;
|
||||||
|
return self.set_mute(!muted, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
||||||
|
self.handlers.add_handler(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_scroll_step(&self, scroll_step: u32) {
|
||||||
|
self.scroll_step.set(scroll_step);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_scroll_step(&self) -> u32 {
|
||||||
|
return self.scroll_step.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Invokes the registered handlers.
|
||||||
|
fn invoke_handlers(
|
||||||
|
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
||||||
|
signal: AudioSignal,
|
||||||
|
user: AudioUser,
|
||||||
|
) {
|
||||||
|
debug!(
|
||||||
|
"Invoking handlers for signal {:?} by user {:?}",
|
||||||
|
signal,
|
||||||
|
user
|
||||||
|
);
|
||||||
|
if handlers.is_empty() {
|
||||||
|
debug!("No handler found");
|
||||||
|
} else {
|
||||||
|
debug!("Executing handlers")
|
||||||
|
}
|
||||||
|
for handler in handlers {
|
||||||
|
let unboxed = handler.as_ref();
|
||||||
|
unboxed(signal, user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// The callback for alsa events that is passed to the alsa subsystem.
|
||||||
|
/// This is the bridge between low-level alsa events and "high-level"
|
||||||
|
/// audio system signals.
|
||||||
|
fn on_alsa_event(
|
||||||
|
last_action_timestamp: &mut i64,
|
||||||
|
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
||||||
|
alsa_event: AlsaEvent,
|
||||||
|
) {
|
||||||
|
let last: i64 = *last_action_timestamp;
|
||||||
|
|
||||||
|
if last != 0 {
|
||||||
|
let now: i64 = glib::get_monotonic_time();
|
||||||
|
let delay: i64 = now - last;
|
||||||
|
if delay < 1000000 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
debug!("Discarding last time stamp, too old");
|
||||||
|
*last_action_timestamp = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* external change */
|
||||||
|
match alsa_event {
|
||||||
|
AlsaEvent::AlsaCardError => {
|
||||||
|
invoke_handlers(
|
||||||
|
handlers,
|
||||||
|
self::AudioSignal::CardError,
|
||||||
|
self::AudioUser::Unknown,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
AlsaEvent::AlsaCardDiconnected => {
|
||||||
|
invoke_handlers(
|
||||||
|
handlers,
|
||||||
|
self::AudioSignal::CardDisconnected,
|
||||||
|
self::AudioUser::Unknown,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
AlsaEvent::AlsaCardValuesChanged => {
|
||||||
|
invoke_handlers(
|
||||||
|
handlers,
|
||||||
|
self::AudioSignal::ValuesChanged,
|
||||||
|
self::AudioUser::Unknown,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,10 +5,9 @@
|
|||||||
//! No other struct should directly interact with the alsa bindings.
|
//! No other struct should directly interact with the alsa bindings.
|
||||||
|
|
||||||
|
|
||||||
use alsa::card::Card;
|
use alsa_lib::card::Card;
|
||||||
use alsa::mixer::SelemChannelId::*;
|
use alsa_lib::mixer::{Mixer, Selem, SelemId};
|
||||||
use alsa::mixer::{Mixer, Selem, SelemId};
|
use alsa_lib::poll::PollDescriptors;
|
||||||
use alsa::poll::PollDescriptors;
|
|
||||||
use alsa_sys;
|
use alsa_sys;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use glib_sys;
|
use glib_sys;
|
||||||
@ -20,7 +19,7 @@ use std::mem;
|
|||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::u8;
|
use std::u8;
|
||||||
use support_alsa::*;
|
use support::alsa::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -67,16 +66,15 @@ impl AlsaCard {
|
|||||||
/// If an elem name is provided, it will be tried. If `None` is provided
|
/// If an elem name is provided, it will be tried. If `None` is provided
|
||||||
/// or the given elem name does not exist or is not playable, any other
|
/// or the given elem name does not exist or is not playable, any other
|
||||||
/// playable elem is tried.
|
/// playable elem is tried.
|
||||||
/// ## `cb`
|
|
||||||
/// Callback for the various `AlsaEvent`s.
|
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// `Ok(Box<AlsaCard>)` on success, `Err(error)` otherwise.
|
/// `Ok(Box<AlsaCard>)` on success, `Err(error)` otherwise.
|
||||||
pub fn new(card_name: Option<String>,
|
pub fn new(
|
||||||
elem_name: Option<String>,
|
card_name: Option<String>,
|
||||||
cb: Rc<Fn(AlsaEvent)>)
|
elem_name: Option<String>,
|
||||||
-> Result<Box<AlsaCard>> {
|
cb: Rc<Fn(AlsaEvent)>,
|
||||||
|
) -> Result<Box<AlsaCard>> {
|
||||||
let card = {
|
let card = {
|
||||||
match card_name {
|
match card_name {
|
||||||
Some(name) => {
|
Some(name) => {
|
||||||
@ -85,7 +83,9 @@ impl AlsaCard {
|
|||||||
if alsa_card_has_playable_selem(&default) {
|
if alsa_card_has_playable_selem(&default) {
|
||||||
default
|
default
|
||||||
} else {
|
} else {
|
||||||
warn!("Default alsa card not playabla, trying others");
|
warn!(
|
||||||
|
"Default alsa card not playabla, trying others"
|
||||||
|
);
|
||||||
get_first_playable_alsa_card()?
|
get_first_playable_alsa_card()?
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -93,8 +93,10 @@ impl AlsaCard {
|
|||||||
match mycard {
|
match mycard {
|
||||||
Ok(card) => card,
|
Ok(card) => card,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
warn!("Card {} not playable, trying others",
|
warn!(
|
||||||
name);
|
"Card {} not playable, trying others",
|
||||||
|
name
|
||||||
|
);
|
||||||
get_first_playable_alsa_card()?
|
get_first_playable_alsa_card()?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,9 +108,10 @@ impl AlsaCard {
|
|||||||
let mixer = get_mixer(&card)?;
|
let mixer = get_mixer(&card)?;
|
||||||
|
|
||||||
let selem_id = {
|
let selem_id = {
|
||||||
let requested_selem =
|
let requested_selem = get_playable_selem_by_name(
|
||||||
get_playable_selem_by_name(&mixer,
|
&mixer,
|
||||||
elem_name.unwrap_or(String::from("Master")));
|
elem_name.unwrap_or(String::from("Master")),
|
||||||
|
);
|
||||||
match requested_selem {
|
match requested_selem {
|
||||||
Ok(s) => s.get_id(),
|
Ok(s) => s.get_id(),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@ -121,110 +124,35 @@ impl AlsaCard {
|
|||||||
let vec_pollfd = PollDescriptors::get(&mixer)?;
|
let vec_pollfd = PollDescriptors::get(&mixer)?;
|
||||||
|
|
||||||
let acard = Box::new(AlsaCard {
|
let acard = Box::new(AlsaCard {
|
||||||
_cannot_construct: (),
|
_cannot_construct: (),
|
||||||
card: card,
|
card,
|
||||||
mixer: mixer,
|
mixer,
|
||||||
selem_id: selem_id,
|
selem_id,
|
||||||
watch_ids: Cell::new(vec![]),
|
watch_ids: Cell::new(vec![]),
|
||||||
cb: cb,
|
cb,
|
||||||
});
|
});
|
||||||
|
|
||||||
let watch_ids = AlsaCard::watch_poll_descriptors(vec_pollfd,
|
let watch_ids =
|
||||||
acard.as_ref());
|
AlsaCard::watch_poll_descriptors(vec_pollfd, acard.as_ref());
|
||||||
acard.watch_ids.set(watch_ids);
|
acard.watch_ids.set(watch_ids);
|
||||||
|
|
||||||
return Ok(acard);
|
return Ok(acard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Get the name of the alsa card.
|
|
||||||
pub fn card_name(&self) -> Result<String> {
|
|
||||||
return self.card.get_name().from_err();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the name of the channel.
|
|
||||||
pub fn chan_name(&self) -> Result<String> {
|
|
||||||
let n = self.selem_id
|
|
||||||
.get_name()
|
|
||||||
.map(|y| String::from(y))?;
|
|
||||||
return Ok(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the `Selem`, looked up by the `SelemId`.
|
/// Get the `Selem`, looked up by the `SelemId`.
|
||||||
pub fn selem(&self) -> Selem {
|
pub fn selem(&self) -> Selem {
|
||||||
return self.mixer.find_selem(&self.selem_id).unwrap();
|
let selem_id = &self.selem_id;
|
||||||
|
let selem = self.mixer.find_selem(selem_id);
|
||||||
|
return selem.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Get the current volume. The returned value corresponds to the
|
|
||||||
/// volume range and might need to be interpreted (such as converting
|
|
||||||
/// to percentage). This always gets
|
|
||||||
/// the volume of the `FrontRight` channel, because the seems to be
|
|
||||||
/// the safest bet.
|
|
||||||
pub fn get_vol(&self) -> Result<i64> {
|
|
||||||
let selem = self.selem();
|
|
||||||
let volume = selem.get_playback_volume(FrontRight);
|
|
||||||
|
|
||||||
return volume.from_err();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Sets the volume of the current card configuration.
|
|
||||||
/// ## `new_vol`
|
|
||||||
/// The volume corresponding to the volume range of the `Selem`. This
|
|
||||||
/// might need to be translated properly first from other formats
|
|
||||||
/// (like percentage).
|
|
||||||
pub fn set_vol(&self, new_vol: i64) -> Result<()> {
|
|
||||||
let selem = self.selem();
|
|
||||||
return selem.set_playback_volume_all(new_vol).from_err();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Gets the volume range of the currently selected card configuration.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// `(min, max)`
|
|
||||||
pub fn get_volume_range(&self) -> (i64, i64) {
|
|
||||||
let selem = self.selem();
|
|
||||||
return selem.get_playback_volume_range();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Whether the current card configuration can be muted.
|
|
||||||
pub fn has_mute(&self) -> bool {
|
|
||||||
let selem = self.selem();
|
|
||||||
return selem.has_playback_switch();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the mute state of the current card configuration.
|
|
||||||
pub fn get_mute(&self) -> Result<bool> {
|
|
||||||
let selem = self.selem();
|
|
||||||
let val = selem.get_playback_switch(FrontRight)?;
|
|
||||||
return Ok(val == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Set the mute state of the current card configuration.
|
|
||||||
/// ## `mute`
|
|
||||||
/// Passing `true` here means the card will be muted.
|
|
||||||
pub fn set_mute(&self, mute: bool) -> Result<()> {
|
|
||||||
let selem = self.selem();
|
|
||||||
/* true -> mute, false -> unmute */
|
|
||||||
let _ = selem.set_playback_switch_all(!mute as i32)?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Watch the given alsa card poll descriptors and
|
/// Watch the given alsa card poll descriptors and
|
||||||
/// return the corresponding watch IDs for saving
|
/// return the corresponding watch IDs for saving
|
||||||
/// in the `AlsaCard` struct.
|
/// in the `AlsaCard` struct.
|
||||||
fn watch_poll_descriptors(polls: Vec<pollfd>,
|
fn watch_poll_descriptors(
|
||||||
acard: &AlsaCard)
|
polls: Vec<pollfd>,
|
||||||
-> Vec<c_uint> {
|
acard: &AlsaCard,
|
||||||
|
) -> Vec<c_uint> {
|
||||||
let mut watch_ids: Vec<c_uint> = vec![];
|
let mut watch_ids: Vec<c_uint> = vec![];
|
||||||
let acard_ptr =
|
let acard_ptr =
|
||||||
unsafe { mem::transmute::<&AlsaCard, glib_sys::gpointer>(acard) };
|
unsafe { mem::transmute::<&AlsaCard, glib_sys::gpointer>(acard) };
|
||||||
@ -248,7 +176,6 @@ impl AlsaCard {
|
|||||||
return watch_ids;
|
return watch_ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Unwatch the given poll descriptors.
|
/// Unwatch the given poll descriptors.
|
||||||
fn unwatch_poll_descriptors(watch_ids: &Vec<u32>) {
|
fn unwatch_poll_descriptors(watch_ids: &Vec<u32>) {
|
||||||
for watch_id in watch_ids {
|
for watch_id in watch_ids {
|
||||||
@ -270,10 +197,11 @@ impl Drop for AlsaCard {
|
|||||||
|
|
||||||
|
|
||||||
/// The C callback function registered in `watch_poll_descriptors()`.
|
/// The C callback function registered in `watch_poll_descriptors()`.
|
||||||
extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel,
|
extern "C" fn watch_cb(
|
||||||
cond: glib_sys::GIOCondition,
|
chan: *mut glib_sys::GIOChannel,
|
||||||
data: glib_sys::gpointer)
|
cond: glib_sys::GIOCondition,
|
||||||
-> glib_sys::gboolean {
|
data: glib_sys::gpointer,
|
||||||
|
) -> glib_sys::gboolean {
|
||||||
|
|
||||||
let acard =
|
let acard =
|
||||||
unsafe { mem::transmute::<glib_sys::gpointer, &AlsaCard>(data) };
|
unsafe { mem::transmute::<glib_sys::gpointer, &AlsaCard>(data) };
|
||||||
@ -293,18 +221,18 @@ 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 {
|
||||||
glib_sys::G_IO_STATUS_AGAIN => {
|
glib_sys::G_IO_STATUS_AGAIN => {
|
||||||
debug!("G_IO_STATUS_AGAIN");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
glib_sys::G_IO_STATUS_NORMAL => {
|
glib_sys::G_IO_STATUS_NORMAL => {
|
||||||
@ -316,7 +244,9 @@ extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel,
|
|||||||
error!("GIO error has occurred");
|
error!("GIO error has occurred");
|
||||||
cb(AlsaEvent::AlsaCardError);
|
cb(AlsaEvent::AlsaCardError);
|
||||||
}
|
}
|
||||||
_ => warn!("Unknown status from g_io_channel_read_chars()"),
|
_ => {
|
||||||
|
error!("Internal error: unknown status!");
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return true as glib_sys::gboolean;
|
return true as glib_sys::gboolean;
|
||||||
}
|
}
|
4
src/audio/alsa/mod.rs
Normal file
4
src/audio/alsa/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
//! The alsa backend subsystem.
|
||||||
|
|
||||||
|
pub mod backend;
|
||||||
|
pub mod card;
|
167
src/audio/frontend.rs
Normal file
167
src/audio/frontend.rs
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
#![allow(missing_docs)] // enums
|
||||||
|
|
||||||
|
//! High-level audio subsystem.
|
||||||
|
//!
|
||||||
|
//! This is the middleman between the low-level audio backend (alsa),
|
||||||
|
//! and the high-level ui code.
|
||||||
|
//! This abstraction layer allows the high-level code to be completely unaware
|
||||||
|
//! of the underlying audio implementation, may it be alsa or whatever.
|
||||||
|
|
||||||
|
|
||||||
|
use errors::*;
|
||||||
|
use std::cell::Ref;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::f64;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use support::audio::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
/// The volume level of the current audio configuration.
|
||||||
|
pub enum VolLevel {
|
||||||
|
Muted,
|
||||||
|
Low,
|
||||||
|
Medium,
|
||||||
|
High,
|
||||||
|
Off,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// An audio user, used to determine from where a signal originated.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum AudioUser {
|
||||||
|
Unknown,
|
||||||
|
Popup,
|
||||||
|
TrayIcon,
|
||||||
|
Hotkeys,
|
||||||
|
PrefsWindow,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// An audio signal. This will be used to connect callbacks to the
|
||||||
|
/// audio system and react appropriately.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum AudioSignal {
|
||||||
|
NoCard,
|
||||||
|
CardInitialized,
|
||||||
|
CardCleanedUp,
|
||||||
|
CardDisconnected,
|
||||||
|
CardError,
|
||||||
|
ValuesChanged,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
/// Convenience struct to make handling this madness easier.
|
||||||
|
pub struct Handlers {
|
||||||
|
inner: Rc<RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Handlers {
|
||||||
|
pub fn new() -> Handlers {
|
||||||
|
return Handlers { inner: Rc::new(RefCell::new(vec![])) };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn borrow(&self) -> Ref<Vec<Box<Fn(AudioSignal, AudioUser)>>> {
|
||||||
|
return self.inner.borrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn add_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
||||||
|
self.inner.borrow_mut().push(cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// This is the audio frontend, which can be implemented by different backends,
|
||||||
|
/// e.g. Alsa or PulseAudio. The high-level UI code only calls these
|
||||||
|
/// functions, never the underlying backend functions. The backend
|
||||||
|
/// implementation must ensure proper state and consistency, especially
|
||||||
|
/// wrt handlers and switching the card.
|
||||||
|
pub trait AudioFrontend {
|
||||||
|
/// Switches the current card. Must invoke handlers.
|
||||||
|
/// ## `user`
|
||||||
|
/// Where the card switch originates from.
|
||||||
|
fn switch_card(
|
||||||
|
&self,
|
||||||
|
card_name: Option<String>,
|
||||||
|
elem_name: Option<String>,
|
||||||
|
user: AudioUser,
|
||||||
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Current volume. Between 0 and 100.
|
||||||
|
/// This always gets the volume of the `FrontRight` channel, because that
|
||||||
|
/// seems to be the safest bet.
|
||||||
|
fn get_vol(&self) -> Result<f64>;
|
||||||
|
|
||||||
|
/// Set the current volume. Must invoke handlers.
|
||||||
|
/// ## `new_vol`
|
||||||
|
/// Set the volume to this value. From 0 to 100.
|
||||||
|
/// ## `user`
|
||||||
|
/// Where the card switch originates from.
|
||||||
|
/// ## `dir`
|
||||||
|
/// The "direction" of the volume change, e.g. is it a decrease
|
||||||
|
/// or increase. This helps with rounding problems.
|
||||||
|
/// ## `auto_unmute`
|
||||||
|
/// Whether to automatically unmute if the volume changes.
|
||||||
|
fn set_vol(
|
||||||
|
&self,
|
||||||
|
new_vol: f64,
|
||||||
|
user: AudioUser,
|
||||||
|
dir: VolDir,
|
||||||
|
auto_unmute: bool,
|
||||||
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Current volume level, nicely usable for e.g. selecting from a set
|
||||||
|
/// of images.
|
||||||
|
fn vol_level(&self) -> VolLevel;
|
||||||
|
|
||||||
|
/// Increase the volume. The step to increasy by is taken from
|
||||||
|
/// `self.scroll_step`.
|
||||||
|
/// ## `user`
|
||||||
|
/// Where the card switch originates from.
|
||||||
|
fn increase_vol(&self, user: AudioUser, auto_unmute: bool) -> Result<()>;
|
||||||
|
|
||||||
|
/// Decrease the volume. The step to decrease by is taken from
|
||||||
|
/// `self.scroll_step`.
|
||||||
|
/// ## `user`
|
||||||
|
/// Where the card switch originates from.
|
||||||
|
fn decrease_vol(&self, user: AudioUser, auto_unmute: bool) -> Result<()>;
|
||||||
|
|
||||||
|
/// Whether the current audio configuration can be muted.
|
||||||
|
fn has_mute(&self) -> bool;
|
||||||
|
|
||||||
|
/// Get the mute state of the current audio configuration.
|
||||||
|
fn get_mute(&self) -> Result<bool>;
|
||||||
|
|
||||||
|
/// Set the mute state of the current audio configuration.
|
||||||
|
/// Must invoke handlers.
|
||||||
|
fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()>;
|
||||||
|
|
||||||
|
/// Toggle the mute state of the current audio configuration.
|
||||||
|
fn toggle_mute(&self, user: AudioUser) -> Result<()>;
|
||||||
|
|
||||||
|
/// Connect a signal handler to the audio subsystem. This can
|
||||||
|
/// be done from anywhere, e.g. in the UI code to react to
|
||||||
|
/// certain signals. Multiple handlers for the same signals are fine,
|
||||||
|
/// they will be executed in order.
|
||||||
|
fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>);
|
||||||
|
|
||||||
|
/// Get the current card name.
|
||||||
|
fn card_name(&self) -> Result<String>;
|
||||||
|
|
||||||
|
/// Get the currently playable channel names.
|
||||||
|
fn playable_chan_names(&self) -> Vec<String>;
|
||||||
|
|
||||||
|
/// Get the current active channel name.
|
||||||
|
fn chan_name(&self) -> Result<String>;
|
||||||
|
|
||||||
|
/// Set the scroll step.
|
||||||
|
fn set_scroll_step(&self, scroll_step: u32);
|
||||||
|
|
||||||
|
/// Get the scroll step.
|
||||||
|
fn get_scroll_step(&self) -> u32;
|
||||||
|
}
|
4
src/audio/mod.rs
Normal file
4
src/audio/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
//! The audio subsystem with its frontend and backends.
|
||||||
|
|
||||||
|
pub mod alsa;
|
||||||
|
pub mod frontend;
|
74
src/bin.rs
74
src/bin.rs
@ -1,34 +1,78 @@
|
|||||||
#![feature(alloc_system)]
|
#![feature(alloc_system)]
|
||||||
extern crate alloc_system;
|
extern crate alloc_system;
|
||||||
|
extern crate getopts;
|
||||||
|
|
||||||
extern crate pnmixerlib;
|
extern crate pnmixerlib;
|
||||||
|
|
||||||
use pnmixerlib::*;
|
use pnmixerlib::*;
|
||||||
|
|
||||||
use app_state::*;
|
use app_state::*;
|
||||||
#[cfg(feature = "notify")]
|
use getopts::Options;
|
||||||
use libnotify::*;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
gtk::init().unwrap();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
// TODO: error handling
|
let mut opts = Options::new();
|
||||||
#[cfg(feature = "notify")]
|
opts.optflag("h", "help", "Show help");
|
||||||
init("PNMixer-rs").unwrap();
|
opts.optopt("", "log-to-file",
|
||||||
|
"Log files to the specified dir instead of stderr",
|
||||||
|
"DIRECTORY");
|
||||||
|
opts.optflagopt("l", "log-level",
|
||||||
|
"Set the log level (trace/debug/info/warn/error/off)",
|
||||||
|
"LEVEL");
|
||||||
|
|
||||||
flexi_logger::LogOptions::new()
|
let matches = match opts.parse(&args[1..]) {
|
||||||
.log_to_file(false)
|
Ok(m) => { m }
|
||||||
// ... your configuration options go here ...
|
Err(f) => { panic!(f.to_string()) }
|
||||||
.init(Some("pnmixer=debug".to_string()))
|
};
|
||||||
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
|
|
||||||
|
|
||||||
let apps = Rc::new(AppS::new());
|
if matches.opt_present("h") {
|
||||||
|
print_usage(opts);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ui_entry::init(apps);
|
let log_dir = matches.opt_str("log-to-file");
|
||||||
|
let log_level = matches.opt_default("log-level", "debug").map(|s| {
|
||||||
|
match s.to_lowercase().as_str() {
|
||||||
|
"trace" => flexi_logger::LevelFilter::Trace,
|
||||||
|
"debug" => flexi_logger::LevelFilter::Debug,
|
||||||
|
"info" => flexi_logger::LevelFilter::Info,
|
||||||
|
"warn" => flexi_logger::LevelFilter::Warn,
|
||||||
|
"error" => flexi_logger::LevelFilter::Error,
|
||||||
|
"off" => flexi_logger::LevelFilter::Off,
|
||||||
|
_ => flexi_logger::LevelFilter::Debug,
|
||||||
|
}
|
||||||
|
}).unwrap_or(flexi_logger::LevelFilter::Off);
|
||||||
|
|
||||||
|
let mut flogger = flexi_logger::Logger::with(
|
||||||
|
flexi_logger::LogSpecification::default(log_level).build());
|
||||||
|
|
||||||
|
if let Some(dir) = log_dir {
|
||||||
|
flogger = flogger.log_to_file().directory(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
flogger
|
||||||
|
.start()
|
||||||
|
.unwrap_or_else(|e|{panic!("Logger initialization failed with {}",e)});
|
||||||
|
|
||||||
|
|
||||||
|
gtk::init()
|
||||||
|
.unwrap_or_else(|e| panic!("Gtk initialization failed with {}", e));
|
||||||
|
|
||||||
|
let apps = Rc::new(new_alsa_appstate());
|
||||||
|
|
||||||
|
ui::entry::init(apps);
|
||||||
|
|
||||||
gtk::main();
|
gtk::main();
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
// TODO: clean deallocation?
|
||||||
uninit();
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn print_usage(opts: Options) {
|
||||||
|
let brief = format!("Usage: pnmixer-rs [options]");
|
||||||
|
print!("{}", opts.usage(&brief));
|
||||||
}
|
}
|
||||||
|
131
src/errors.rs
131
src/errors.rs
@ -1,6 +1,7 @@
|
|||||||
#![allow(missing_docs)]
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
use alsa;
|
use alsa_lib;
|
||||||
|
use glib;
|
||||||
use png;
|
use png;
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std;
|
use std;
|
||||||
@ -10,10 +11,18 @@ use toml;
|
|||||||
|
|
||||||
error_chain! {
|
error_chain! {
|
||||||
foreign_links {
|
foreign_links {
|
||||||
Alsa(alsa::Error);
|
Alsa(alsa_lib::Error);
|
||||||
IO(std::io::Error);
|
IO(std::io::Error);
|
||||||
Toml(toml::de::Error);
|
Toml(toml::de::Error);
|
||||||
Png(png::DecodingError);
|
Png(png::DecodingError);
|
||||||
|
GlibError(glib::Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
errors {
|
||||||
|
GtkResponseCancel(t: String) {
|
||||||
|
description("User hit cancel")
|
||||||
|
display("User hit cancel: {}", t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,3 +116,121 @@ macro_rules! try_e {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Unwraps a `Result<T, E>` by yielding a value of the samet ype
|
||||||
|
/// for either case.
|
||||||
|
macro_rules! unwrap_any {
|
||||||
|
($expr:expr, $fmt_ok:expr, $fmt_err:expr) => (match $expr {
|
||||||
|
::std::result::Result::Ok(val) => $fmt_ok,
|
||||||
|
::std::result::Result::Err(err) => $fmt_err,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Warns on err and yields `()` without returning the function.
|
||||||
|
macro_rules! just_warn {
|
||||||
|
($expr:expr) => (match $expr {
|
||||||
|
::std::result::Result::Ok(_) => (),
|
||||||
|
::std::result::Result::Err(err) => {
|
||||||
|
warn!("{:?}", err);
|
||||||
|
()
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Present a gtk error dialog with given message.
|
||||||
|
/// Provides only a close button.
|
||||||
|
macro_rules! error_dialog {
|
||||||
|
($msg:expr, $parent:expr) => {
|
||||||
|
{
|
||||||
|
use gtk::DialogExt;
|
||||||
|
use gtk::prelude::GtkWindowExt;
|
||||||
|
use gtk::WidgetExt;
|
||||||
|
|
||||||
|
let parent: Option<>k::Window> = $parent;
|
||||||
|
let dialog = gtk::MessageDialog::new(parent,
|
||||||
|
gtk::DialogFlags::DESTROY_WITH_PARENT,
|
||||||
|
gtk::MessageType::Error,
|
||||||
|
gtk::ButtonsType::Close,
|
||||||
|
$msg);
|
||||||
|
dialog.set_title("PNMixer-rs Error");
|
||||||
|
|
||||||
|
dialog.run();
|
||||||
|
dialog.destroy();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Present a gtk error dialog with the error from the `Result` type,
|
||||||
|
/// if any.
|
||||||
|
/// Provides only a close button.
|
||||||
|
macro_rules! result_warn {
|
||||||
|
($expr:expr, $parent:expr) => (match $expr {
|
||||||
|
::std::result::Result::Ok(v) => Ok(v),
|
||||||
|
::std::result::Result::Err(err) => {
|
||||||
|
use std::error::Error;
|
||||||
|
let warn_string = format!("{}{}", err.description(),
|
||||||
|
err.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from("")));
|
||||||
|
warn!("{}", warn_string);
|
||||||
|
error_dialog!(warn_string.as_str(), $parent);
|
||||||
|
Err(err)
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Convert `WResult` to `Result`. All warnings are printed via the `log`
|
||||||
|
/// crate and are shown via Gtk dialogs.
|
||||||
|
macro_rules! wresult_warn {
|
||||||
|
($expr:expr, $parent:expr) => (match $expr {
|
||||||
|
::w_result::WResult::WOk(t, ws) => {
|
||||||
|
use std::error::Error;
|
||||||
|
for w in ws {
|
||||||
|
let warn_string = format!("{}{}", w.description(),
|
||||||
|
w.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from("")));
|
||||||
|
warn!("{}", warn_string);
|
||||||
|
error_dialog!(warn_string.as_str(), $parent);
|
||||||
|
}
|
||||||
|
Ok(t)
|
||||||
|
},
|
||||||
|
::w_result::WResult::WErr(err) => Err(err),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// If there is an error in the expression, push it to
|
||||||
|
/// the given mutable warning vector.
|
||||||
|
macro_rules! push_warning {
|
||||||
|
($expr:expr, $vec:ident) => (match $expr {
|
||||||
|
Err(e) => $vec.push(e),
|
||||||
|
_ => ()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// If there is a value in the Result type, unwrap it, otherwise error-log
|
||||||
|
/// the error, show it via gtk dialog and exit the whole program.
|
||||||
|
macro_rules! unwrap_error {
|
||||||
|
($expr:expr, $parent:expr) => (match $expr {
|
||||||
|
::std::result::Result::Ok(v) => v,
|
||||||
|
::std::result::Result::Err(err) => {
|
||||||
|
use std::error::Error;
|
||||||
|
let err_string = format!("{}{}", err.description(),
|
||||||
|
err.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from("")));
|
||||||
|
|
||||||
|
error!("{}", err_string);
|
||||||
|
error_dialog!(err_string.as_str(), $parent);
|
||||||
|
::std::process::exit(1);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
189
src/hotkey.rs
Normal file
189
src/hotkey.rs
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
//! The hotkey subsystem.
|
||||||
|
//!
|
||||||
|
//! This file defines what's a hotkey and deals with the low-level XKBlib and
|
||||||
|
//! Gtk/Gdk.
|
||||||
|
|
||||||
|
|
||||||
|
use errors::*;
|
||||||
|
use gdk;
|
||||||
|
use gdk_sys;
|
||||||
|
use gtk;
|
||||||
|
use libc::c_int;
|
||||||
|
use libc::c_uint;
|
||||||
|
use support::gdk_x11::*;
|
||||||
|
use x11;
|
||||||
|
|
||||||
|
|
||||||
|
/// `xmodmap -pm`
|
||||||
|
/// List of key modifiers which will be ignored whenever
|
||||||
|
/// we check whether the defined hotkeys have been pressed.
|
||||||
|
lazy_static! {
|
||||||
|
static ref KEY_MASKS: Vec<c_uint> = vec![
|
||||||
|
gdk_sys::GdkModifierType::empty().bits(), // No Modkey
|
||||||
|
gdk_sys::GDK_MOD2_MASK.bits(), // Numlock
|
||||||
|
gdk_sys::GDK_LOCK_MASK.bits(), // Capslock
|
||||||
|
gdk_sys::GDK_MOD2_MASK.bits() | gdk_sys::GDK_LOCK_MASK.bits(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
/// A hotkey, described by the underlying gdk/X11 representation.
|
||||||
|
pub struct Hotkey {
|
||||||
|
/// The hardware keycode.
|
||||||
|
pub code: gdk::key,
|
||||||
|
/// The modifier keys and mouse button that have been pressed
|
||||||
|
/// in addition to the main key (e.g. Numlock/Capslock).
|
||||||
|
/// This is the raw bit representation and can be converted to
|
||||||
|
/// `gtk::ModifierType` via `self.mods()`.
|
||||||
|
pub mod_bits: u32, // Makes serialize/deserialize easier
|
||||||
|
/// X key symbol.
|
||||||
|
pub sym: u64,
|
||||||
|
/// Gtk Accelerator string.
|
||||||
|
pub gtk_accel: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hotkey {
|
||||||
|
/// Creates a new hotkey and grabs it.
|
||||||
|
pub fn new(code: gdk::key, mods: gdk::ModifierType) -> Result<Hotkey> {
|
||||||
|
let display = gdk_x11_get_default_xdisplay();
|
||||||
|
let mod_bits = mods.bits();
|
||||||
|
let sym =
|
||||||
|
unsafe { x11::xlib::XkbKeycodeToKeysym(display, code as u8, 0, 0) };
|
||||||
|
let gtk_accel = gtk::accelerator_name(sym as u32, mods).ok_or(
|
||||||
|
"Could net get accelerator name",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let hotkey = Hotkey {
|
||||||
|
code,
|
||||||
|
mod_bits,
|
||||||
|
sym,
|
||||||
|
gtk_accel,
|
||||||
|
};
|
||||||
|
|
||||||
|
hotkey.grab()?;
|
||||||
|
|
||||||
|
return Ok(hotkey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new hotkey from an accelerator string and grabs it.
|
||||||
|
pub fn new_from_accel(accel: &str) -> Result<Hotkey> {
|
||||||
|
let (code, mods) = hotkey_accel_to_code(accel);
|
||||||
|
return Hotkey::new(code, mods);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Grab a key manually. Should be paired with a ungrab() call.
|
||||||
|
pub fn grab(&self) -> Result<()> {
|
||||||
|
let display = gdk_x11_get_default_xdisplay();
|
||||||
|
|
||||||
|
/* Init error handling */
|
||||||
|
let old_hdlr = unsafe {
|
||||||
|
GRAB_ERROR = 0;
|
||||||
|
x11::xlib::XSetErrorHandler(Some(grab_error_handler))
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Grab the key */
|
||||||
|
for key in KEY_MASKS.iter() {
|
||||||
|
unsafe {
|
||||||
|
x11::xlib::XGrabKey(
|
||||||
|
display,
|
||||||
|
self.code,
|
||||||
|
self.mod_bits | key,
|
||||||
|
gdk_x11_get_default_root_xwindow(),
|
||||||
|
1,
|
||||||
|
x11::xlib::GrabModeAsync,
|
||||||
|
x11::xlib::GrabModeAsync,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Synchronize X */
|
||||||
|
unsafe {
|
||||||
|
x11::xlib::XFlush(display);
|
||||||
|
x11::xlib::XSync(display, false as c_int);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Restore error handler */
|
||||||
|
unsafe {
|
||||||
|
x11::xlib::XSetErrorHandler(old_hdlr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check for error */
|
||||||
|
unsafe {
|
||||||
|
ensure!(GRAB_ERROR == 0, "Error grabbing");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ungrab a key manually. Should be paired with a grab() call.
|
||||||
|
pub fn ungrab(&self) {
|
||||||
|
let display = gdk_x11_get_default_xdisplay();
|
||||||
|
|
||||||
|
for key in KEY_MASKS.iter() {
|
||||||
|
unsafe {
|
||||||
|
x11::xlib::XUngrabKey(
|
||||||
|
display,
|
||||||
|
self.code,
|
||||||
|
self.mod_bits | key,
|
||||||
|
gdk_x11_get_default_root_xwindow(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the keycode we got (minus modifiers like
|
||||||
|
/// numlock/capslock) matches the hotkey.
|
||||||
|
/// Thus numlock + o will match o.
|
||||||
|
pub fn matches(&self, code: gdk::key, mods: gdk::ModifierType) -> bool {
|
||||||
|
if code != self.code {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for key in KEY_MASKS.iter() {
|
||||||
|
if (self.mod_bits | key) == mods.bits() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Hotkey {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
debug!("Ungrabbing hotkey");
|
||||||
|
self.ungrab();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Translate a Gtk Accelerator string to a key code and mods.
|
||||||
|
pub fn hotkey_accel_to_code(accel: &str) -> (gdk::key, gdk::ModifierType) {
|
||||||
|
let display = gdk_x11_get_default_xdisplay();
|
||||||
|
let (sym, mods) = gtk::accelerator_parse(accel);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
if sym != 0 {
|
||||||
|
return (
|
||||||
|
x11::xlib::XKeysymToKeycode(display, sym as u64) as i32,
|
||||||
|
mods,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return (-1, mods);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static mut GRAB_ERROR: u8 = 0;
|
||||||
|
|
||||||
|
extern "C" fn grab_error_handler(
|
||||||
|
_: *mut x11::xlib::Display,
|
||||||
|
_: *mut x11::xlib::XErrorEvent,
|
||||||
|
) -> c_int {
|
||||||
|
warn!("Error while grabbing hotkey");
|
||||||
|
unsafe {
|
||||||
|
GRAB_ERROR = 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
301
src/hotkeys.rs
Normal file
301
src/hotkeys.rs
Normal file
@ -0,0 +1,301 @@
|
|||||||
|
//! The hotkeys subsystem.
|
||||||
|
//!
|
||||||
|
//! This handles the PNMixer-rs specific hotkeys as a whole,
|
||||||
|
//! including communication with Xlib and intercepting key presses
|
||||||
|
//! before they can be interpreted by Gtk/Gdk.
|
||||||
|
|
||||||
|
|
||||||
|
use audio::frontend::*;
|
||||||
|
use errors::*;
|
||||||
|
use errors;
|
||||||
|
use gdk;
|
||||||
|
use gdk_sys;
|
||||||
|
use glib::translate::*;
|
||||||
|
use glib_sys;
|
||||||
|
use hotkey::*;
|
||||||
|
use prefs::*;
|
||||||
|
use std::mem;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use support::gdk_x11;
|
||||||
|
use w_result::*;
|
||||||
|
use x11;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// The possible Hotkeys for manipulating the volume.
|
||||||
|
pub struct Hotkeys<T>
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
enabled: bool,
|
||||||
|
mute_key: Option<Hotkey>,
|
||||||
|
up_key: Option<Hotkey>,
|
||||||
|
down_key: Option<Hotkey>,
|
||||||
|
|
||||||
|
// need this to access audio in 'key_filter'
|
||||||
|
audio: Rc<T>,
|
||||||
|
auto_unmute: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Hotkeys<T>
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
/// Creates the hotkeys subsystem and binds the hotkeys.
|
||||||
|
pub fn new(
|
||||||
|
prefs: &Prefs,
|
||||||
|
audio: Rc<T>,
|
||||||
|
) -> WResult<Box<Hotkeys<T>>, errors::Error, errors::Error> {
|
||||||
|
debug!("Creating hotkeys control");
|
||||||
|
let mut hotkeys = Box::new(Hotkeys {
|
||||||
|
enabled: false,
|
||||||
|
mute_key: None,
|
||||||
|
up_key: None,
|
||||||
|
down_key: None,
|
||||||
|
audio: audio,
|
||||||
|
auto_unmute: prefs.behavior_prefs.unmute_on_vol_change,
|
||||||
|
});
|
||||||
|
let mut warn = vec![];
|
||||||
|
push_warning!(hotkeys.reload(prefs), warn);
|
||||||
|
|
||||||
|
/* bind hotkeys */
|
||||||
|
let data_ptr = unsafe {
|
||||||
|
mem::transmute::<&Hotkeys<T>, glib_sys::gpointer>(hotkeys.as_ref())
|
||||||
|
};
|
||||||
|
hotkeys_add_filter(Some(key_filter::<T>), data_ptr);
|
||||||
|
return WOk(hotkeys, warn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reload the Hotkeys from the preferences.
|
||||||
|
/// If hotkeys are disabled, just sets all members to `None`.
|
||||||
|
/// This has to be called each time the preferences are modified.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `Ok(())` on success, otherwise `Err(str)` if some of the hotkeys
|
||||||
|
/// could not be grabbed, where `str` is a String that can be
|
||||||
|
/// presented via e.g. `run_error_dialog()`.
|
||||||
|
pub fn reload(&mut self, prefs: &Prefs) -> Result<()> {
|
||||||
|
self.enabled = prefs.hotkey_prefs.enable_hotkeys;
|
||||||
|
self.mute_key = None;
|
||||||
|
self.up_key = None;
|
||||||
|
self.down_key = None;
|
||||||
|
|
||||||
|
/* Return if hotkeys are disabled */
|
||||||
|
if self.enabled == false {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let hotkey_prefs = &prefs.hotkey_prefs;
|
||||||
|
let new_hotkey = |keyname: &Option<String>| -> (Option<Hotkey>, bool) {
|
||||||
|
match keyname {
|
||||||
|
&Some(ref k) => {
|
||||||
|
let hotkey = Hotkey::new_from_accel(k.as_str());
|
||||||
|
if hotkey.as_ref().is_err() {
|
||||||
|
(None, true)
|
||||||
|
} else {
|
||||||
|
(Some(hotkey.unwrap()), false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
&None => (None, false), // no actual error, just no key
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Setup mute hotkey */
|
||||||
|
let (m_unmute_key, mute_err) =
|
||||||
|
new_hotkey(&hotkey_prefs.mute_unmute_key);
|
||||||
|
if let Some(key) = m_unmute_key {
|
||||||
|
self.mute_key = Some(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Setup volume uphotkey */
|
||||||
|
let (m_up_key, up_err) = new_hotkey(&hotkey_prefs.vol_up_key);
|
||||||
|
if let Some(key) = m_up_key {
|
||||||
|
self.up_key = Some(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Setup volume down hotkey */
|
||||||
|
let (m_down_key, down_err) = new_hotkey(&hotkey_prefs.vol_down_key);
|
||||||
|
if let Some(key) = m_down_key {
|
||||||
|
self.down_key = Some(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if mute_err || up_err || down_err {
|
||||||
|
bail!("Could not grab the following hotkeys:\n{}{}{}",
|
||||||
|
if mute_err { "(Mute/Unmute)\n" } else { "" },
|
||||||
|
if up_err { "(Volume Up)\n" } else { "" },
|
||||||
|
if down_err { "(Volume Down)\n" } else { "" },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bind hotkeys manually. Should be paired with an `unbind()` call.
|
||||||
|
pub fn bind(&self) {
|
||||||
|
debug!("Bind hotkeys");
|
||||||
|
|
||||||
|
if let Some(ref key) = self.mute_key {
|
||||||
|
if key.grab().is_err() {
|
||||||
|
warn!("Could not grab mute key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref key) = self.up_key {
|
||||||
|
if key.grab().is_err() {
|
||||||
|
warn!("Could not grab mute key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref key) = self.down_key {
|
||||||
|
if key.grab().is_err() {
|
||||||
|
warn!("Could not grab mute key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let data_ptr =
|
||||||
|
unsafe { mem::transmute::<&Hotkeys<T>, glib_sys::gpointer>(self) };
|
||||||
|
hotkeys_add_filter(Some(key_filter::<T>), data_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unbind hotkeys manually. Should be paired with a `bind()` call.
|
||||||
|
pub fn unbind(&self) {
|
||||||
|
debug!("Unbind hotkeys");
|
||||||
|
|
||||||
|
if let Some(ref key) = self.mute_key {
|
||||||
|
key.ungrab();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref key) = self.up_key {
|
||||||
|
key.ungrab();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref key) = self.down_key {
|
||||||
|
key.ungrab();
|
||||||
|
}
|
||||||
|
|
||||||
|
let data_ptr =
|
||||||
|
unsafe { mem::transmute::<&Hotkeys<T>, glib_sys::gpointer>(self) };
|
||||||
|
hotkeys_remove_filter(Some(key_filter::<T>), data_ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for Hotkeys<T>
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
fn drop(&mut self) {
|
||||||
|
debug!("Freeing hotkeys");
|
||||||
|
self.mute_key = None;
|
||||||
|
self.up_key = None;
|
||||||
|
self.down_key = None;
|
||||||
|
|
||||||
|
let data_ptr = unsafe {
|
||||||
|
mem::transmute::<&mut Hotkeys<T>, glib_sys::gpointer>(self)
|
||||||
|
};
|
||||||
|
|
||||||
|
hotkeys_remove_filter(Some(key_filter::<T>), data_ptr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Attaches the `key_filter()` function as a filter
|
||||||
|
/// to the root window, so it will intercept window events.
|
||||||
|
fn hotkeys_add_filter(
|
||||||
|
function: gdk_sys::GdkFilterFunc,
|
||||||
|
data: glib_sys::gpointer,
|
||||||
|
) {
|
||||||
|
// TODO: all the unwrapping :/
|
||||||
|
let window = gdk_x11::gdk_x11_window_foreign_new_for_display(
|
||||||
|
&mut gdk::Display::get_default().unwrap(),
|
||||||
|
gdk_x11::gdk_x11_get_default_root_xwindow(),
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
gdk_sys::gdk_window_add_filter(window.to_glib_none().0, function, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Removes the previously attached `key_filter()` function from
|
||||||
|
/// the root window.
|
||||||
|
fn hotkeys_remove_filter(
|
||||||
|
function: gdk_sys::GdkFilterFunc,
|
||||||
|
data: glib_sys::gpointer,
|
||||||
|
) {
|
||||||
|
// TODO: all the unwrapping :/
|
||||||
|
let window = gdk_x11::gdk_x11_window_foreign_new_for_display(
|
||||||
|
&mut gdk::Display::get_default().unwrap(),
|
||||||
|
gdk_x11::gdk_x11_get_default_root_xwindow(),
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
gdk_sys::gdk_window_remove_filter(
|
||||||
|
window.to_glib_none().0,
|
||||||
|
function,
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// This function is called before Gtk/Gdk can respond
|
||||||
|
/// to any(!) window event and handles pressed hotkeys.
|
||||||
|
extern "C" fn key_filter<T>(
|
||||||
|
gdk_xevent: *mut gdk_sys::GdkXEvent,
|
||||||
|
_: *mut gdk_sys::GdkEvent,
|
||||||
|
data: glib_sys::gpointer,
|
||||||
|
) -> gdk_sys::GdkFilterReturn
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
let xevent = gdk_xevent as *mut x11::xlib::XKeyEvent;
|
||||||
|
|
||||||
|
let hotkeys: &Hotkeys<T> =
|
||||||
|
unsafe { mem::transmute::<glib_sys::gpointer, &Hotkeys<T>>(data) };
|
||||||
|
let mute_key = &hotkeys.mute_key;
|
||||||
|
let up_key = &hotkeys.up_key;
|
||||||
|
let down_key = &hotkeys.down_key;
|
||||||
|
let audio = &hotkeys.audio;
|
||||||
|
|
||||||
|
let xevent_type = unsafe { (*xevent).type_ };
|
||||||
|
if xevent_type == x11::xlib::KeyPress {
|
||||||
|
return gdk_sys::GDK_FILTER_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
let xevent_key = unsafe { (*xevent).keycode };
|
||||||
|
let xevent_state = unsafe { (*xevent).state };
|
||||||
|
|
||||||
|
|
||||||
|
if mute_key.as_ref().is_some() &&
|
||||||
|
mute_key.as_ref().unwrap().matches(
|
||||||
|
xevent_key as i32,
|
||||||
|
gdk::ModifierType::from_bits(xevent_state)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
{
|
||||||
|
just_warn!(audio.toggle_mute(AudioUser::Hotkeys));
|
||||||
|
} else if up_key.as_ref().is_some() &&
|
||||||
|
up_key.as_ref().unwrap().matches(
|
||||||
|
xevent_key as i32,
|
||||||
|
gdk::ModifierType::from_bits(
|
||||||
|
xevent_state,
|
||||||
|
).unwrap(),
|
||||||
|
)
|
||||||
|
{
|
||||||
|
just_warn!(audio.increase_vol(AudioUser::Hotkeys, hotkeys.auto_unmute));
|
||||||
|
|
||||||
|
} else if down_key.as_ref().is_some() &&
|
||||||
|
down_key.as_ref().unwrap().matches(
|
||||||
|
xevent_key as i32,
|
||||||
|
gdk::ModifierType::from_bits(
|
||||||
|
xevent_state,
|
||||||
|
).unwrap(),
|
||||||
|
)
|
||||||
|
{
|
||||||
|
just_warn!(audio.decrease_vol(AudioUser::Hotkeys, hotkeys.auto_unmute));
|
||||||
|
}
|
||||||
|
|
||||||
|
return gdk_sys::GDK_FILTER_CONTINUE;
|
||||||
|
}
|
65
src/lib.rs
65
src/lib.rs
@ -34,57 +34,54 @@ extern crate alloc_system;
|
|||||||
|
|
||||||
pub extern crate flexi_logger;
|
pub extern crate flexi_logger;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub extern crate error_chain;
|
extern crate error_chain;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
pub extern crate toml;
|
extern crate toml;
|
||||||
pub extern crate serde;
|
extern crate serde;
|
||||||
|
|
||||||
pub extern crate alsa;
|
extern crate alsa as alsa_lib;
|
||||||
pub extern crate alsa_sys;
|
extern crate alsa_sys;
|
||||||
pub extern crate ffi;
|
extern crate ffi;
|
||||||
pub extern crate gdk;
|
extern crate gdk;
|
||||||
pub extern crate gdk_pixbuf;
|
extern crate gdk_pixbuf;
|
||||||
pub extern crate gdk_pixbuf_sys;
|
extern crate gdk_pixbuf_sys;
|
||||||
pub extern crate gdk_sys;
|
extern crate gdk_sys;
|
||||||
pub extern crate gio;
|
extern crate gio;
|
||||||
pub extern crate glib;
|
extern crate glib;
|
||||||
pub extern crate glib_sys;
|
extern crate glib_sys;
|
||||||
pub extern crate gobject_sys;
|
extern crate gobject_sys;
|
||||||
pub extern crate gtk;
|
pub extern crate gtk;
|
||||||
pub extern crate gtk_sys;
|
extern crate gtk_sys;
|
||||||
pub extern crate libc;
|
extern crate libc;
|
||||||
pub extern crate png;
|
extern crate png;
|
||||||
pub extern crate which;
|
extern crate w_result;
|
||||||
pub extern crate xdg;
|
extern crate which;
|
||||||
|
extern crate x11;
|
||||||
|
extern crate xdg;
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
pub extern crate libnotify;
|
pub extern crate libnotify;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
pub mod glade_helpers;
|
|
||||||
|
|
||||||
pub mod alsa_card;
|
|
||||||
pub mod app_state;
|
pub mod app_state;
|
||||||
pub mod audio;
|
pub mod audio;
|
||||||
|
pub mod hotkey;
|
||||||
|
pub mod hotkeys;
|
||||||
pub mod prefs;
|
pub mod prefs;
|
||||||
pub mod support_alsa;
|
|
||||||
pub mod support_audio;
|
|
||||||
pub mod support_cmd;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod support_ui;
|
pub mod support;
|
||||||
pub mod ui_entry;
|
#[macro_use]
|
||||||
pub mod ui_popup_menu;
|
pub mod ui;
|
||||||
pub mod ui_popup_window;
|
|
||||||
pub mod ui_prefs_dialog;
|
|
||||||
pub mod ui_tray_icon;
|
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
pub mod notif;
|
pub mod notif;
|
||||||
|
96
src/notif.rs
96
src/notif.rs
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
|
|
||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::*;
|
use audio::frontend::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use glib::prelude::*;
|
use glib::prelude::*;
|
||||||
use libnotify;
|
use libnotify;
|
||||||
@ -21,7 +21,7 @@ pub struct Notif {
|
|||||||
enabled: Cell<bool>,
|
enabled: Cell<bool>,
|
||||||
from_popup: Cell<bool>,
|
from_popup: Cell<bool>,
|
||||||
from_tray: Cell<bool>,
|
from_tray: Cell<bool>,
|
||||||
// TODO: from hotkey
|
from_hotkeys: Cell<bool>,
|
||||||
from_external: Cell<bool>,
|
from_external: Cell<bool>,
|
||||||
|
|
||||||
volume_notif: libnotify::Notification,
|
volume_notif: libnotify::Notification,
|
||||||
@ -30,47 +30,58 @@ pub struct Notif {
|
|||||||
|
|
||||||
impl Notif {
|
impl Notif {
|
||||||
/// Create a new notification instance from the current preferences.
|
/// Create a new notification instance from the current preferences.
|
||||||
|
/// This should only be done once at startup. This also initializes
|
||||||
|
/// the libnotify system.
|
||||||
pub fn new(prefs: &Prefs) -> Result<Self> {
|
pub fn new(prefs: &Prefs) -> Result<Self> {
|
||||||
|
libnotify::init("PNMixer-rs")?;
|
||||||
|
|
||||||
let notif = Notif {
|
let notif = Notif {
|
||||||
enabled: Cell::new(false),
|
enabled: Cell::new(false),
|
||||||
from_popup: Cell::new(false),
|
from_popup: Cell::new(false),
|
||||||
from_tray: Cell::new(false),
|
from_tray: Cell::new(false),
|
||||||
|
from_hotkeys: Cell::new(false),
|
||||||
from_external: Cell::new(false),
|
from_external: Cell::new(false),
|
||||||
|
|
||||||
volume_notif: libnotify::Notification::new("", None, None),
|
volume_notif: libnotify::Notification::new("", None, None),
|
||||||
text_notif: libnotify::Notification::new("", None, None),
|
text_notif: libnotify::Notification::new("", None, None),
|
||||||
};
|
};
|
||||||
|
|
||||||
notif.reload(prefs)?;
|
notif.reload(prefs);
|
||||||
|
|
||||||
return Ok(notif);
|
return Ok(notif);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reload the notification instance from the current
|
/// Reload the notification instance from the current
|
||||||
/// preferences.
|
/// preferences.
|
||||||
pub fn reload(&self, prefs: &Prefs) -> Result<()> {
|
pub fn reload(&self, prefs: &Prefs) {
|
||||||
let timeout = prefs.notify_prefs.notifcation_timeout;
|
let timeout = prefs.notify_prefs.notifcation_timeout;
|
||||||
|
|
||||||
self.enabled.set(prefs.notify_prefs.enable_notifications);
|
self.enabled.set(prefs.notify_prefs.enable_notifications);
|
||||||
self.from_popup.set(prefs.notify_prefs.notify_popup);
|
self.from_popup.set(prefs.notify_prefs.notify_popup);
|
||||||
self.from_tray.set(prefs.notify_prefs.notify_mouse_scroll);
|
self.from_tray.set(prefs.notify_prefs.notify_mouse_scroll);
|
||||||
|
self.from_hotkeys.set(prefs.notify_prefs.notify_hotkeys);
|
||||||
self.from_external.set(prefs.notify_prefs.notify_external);
|
self.from_external.set(prefs.notify_prefs.notify_external);
|
||||||
|
|
||||||
self.volume_notif.set_timeout(timeout as i32);
|
self.volume_notif.set_timeout(timeout as i32);
|
||||||
self.volume_notif.set_hint("x-canonical-private-synchronous",
|
self.volume_notif.set_hint(
|
||||||
Some("".to_variant()));
|
"x-canonical-private-synchronous",
|
||||||
|
Some("".to_variant()),
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
self.text_notif.set_timeout(timeout as i32);
|
self.text_notif.set_timeout(timeout as i32);
|
||||||
self.text_notif.set_hint("x-canonical-private-synchronous",
|
self.text_notif.set_hint(
|
||||||
Some("".to_variant()));
|
"x-canonical-private-synchronous",
|
||||||
|
Some("".to_variant()),
|
||||||
return Ok(());
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shows a volume notification, e.g. for volume or mute state change.
|
/// Shows a volume notification, e.g. for volume or mute state change.
|
||||||
pub fn show_volume_notif(&self, audio: &Audio) -> Result<()> {
|
pub fn show_volume_notif<T>(&self, audio: &T) -> Result<()>
|
||||||
let vol = audio.vol()?;
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
let vol = audio.get_vol()?;
|
||||||
let vol_level = audio.vol_level();
|
let vol_level = audio.vol_level();
|
||||||
|
|
||||||
let icon = {
|
let icon = {
|
||||||
@ -87,23 +98,23 @@ impl Notif {
|
|||||||
match vol_level {
|
match vol_level {
|
||||||
VolLevel::Muted => String::from("Volume muted"),
|
VolLevel::Muted => String::from("Volume muted"),
|
||||||
_ => {
|
_ => {
|
||||||
format!("{} ({})\nVolume: {}",
|
format!(
|
||||||
audio.acard
|
"{} ({})\nVolume: {}",
|
||||||
.borrow()
|
audio.card_name()?,
|
||||||
.card_name()?,
|
audio.chan_name()?,
|
||||||
audio.acard
|
vol as i32
|
||||||
.borrow()
|
)
|
||||||
.chan_name()?,
|
|
||||||
vol as i32)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: error handling
|
self.volume_notif
|
||||||
self.volume_notif.update(summary.as_str(), None, Some(icon)).unwrap();
|
.update(summary.as_str(), None, Some(icon))?;
|
||||||
self.volume_notif.set_hint("value", Some((vol as i32).to_variant()));
|
self.volume_notif.set_hint(
|
||||||
// TODO: error handling
|
"value",
|
||||||
self.volume_notif.show().unwrap();
|
Some((vol as i32).to_variant()),
|
||||||
|
);
|
||||||
|
self.volume_notif.show()?;
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -111,47 +122,56 @@ impl Notif {
|
|||||||
|
|
||||||
/// Shows a text notification, e.g. for warnings or errors.
|
/// Shows a text notification, e.g. for warnings or errors.
|
||||||
pub fn show_text_notif(&self, summary: &str, body: &str) -> Result<()> {
|
pub fn show_text_notif(&self, summary: &str, body: &str) -> Result<()> {
|
||||||
// TODO: error handling
|
self.text_notif.update(summary, Some(body), None)?;
|
||||||
self.text_notif.update(summary, Some(body), None).unwrap();
|
self.text_notif.show()?;
|
||||||
// TODO: error handling
|
|
||||||
self.text_notif.show().unwrap();
|
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for Notif {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
libnotify::uninit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Initialize the notification subsystem.
|
/// Initialize the notification subsystem.
|
||||||
pub fn init_notify(appstate: Rc<AppS>) {
|
pub fn init_notify<T>(appstate: Rc<AppS<T>>)
|
||||||
debug!("Blah");
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
{
|
{
|
||||||
/* connect handler */
|
/* connect handler */
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
appstate.audio.connect_handler(Box::new(move |s, u| {
|
appstate.audio.connect_handler(Box::new(move |s, u| {
|
||||||
let notif = &apps.notif;
|
let notif = &apps.notif;
|
||||||
if !notif.enabled.get() {
|
if notif.is_none() || !notif.as_ref().unwrap().enabled.get() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let notif = notif.as_ref().unwrap();
|
||||||
match (s,
|
match (s,
|
||||||
u,
|
u,
|
||||||
(notif.from_popup.get(),
|
(notif.from_popup.get(),
|
||||||
notif.from_tray.get(),
|
notif.from_tray.get(),
|
||||||
notif.from_external.get())) {
|
notif.from_external.get(),
|
||||||
|
notif.from_hotkeys.get())) {
|
||||||
(AudioSignal::NoCard, _, _) => try_w!(notif.show_text_notif("No sound card", "No playable soundcard found")),
|
(AudioSignal::NoCard, _, _) => try_w!(notif.show_text_notif("No sound card", "No playable soundcard found")),
|
||||||
(AudioSignal::CardDisconnected, _, _) => try_w!(notif.show_text_notif("Soundcard disconnected", "Soundcard has been disconnected, reloading sound system...")),
|
(AudioSignal::CardDisconnected, _, _) => try_w!(notif.show_text_notif("Soundcard disconnected", "Soundcard has been disconnected, reloading sound system...")),
|
||||||
(AudioSignal::CardError, _, _) => (),
|
(AudioSignal::CardError, _, _) => (),
|
||||||
(AudioSignal::ValuesChanged,
|
(AudioSignal::ValuesChanged,
|
||||||
AudioUser::TrayIcon,
|
AudioUser::TrayIcon,
|
||||||
(_, true, _)) => try_w!(notif.show_volume_notif(&apps.audio)),
|
(_, true, _, _)) => try_w!(notif.show_volume_notif(apps.audio.as_ref())),
|
||||||
(AudioSignal::ValuesChanged,
|
(AudioSignal::ValuesChanged,
|
||||||
AudioUser::Popup,
|
AudioUser::Popup,
|
||||||
(true, _, _)) => try_w!(notif.show_volume_notif(&apps.audio)),
|
(true, _, _, _)) => try_w!(notif.show_volume_notif(apps.audio.as_ref())),
|
||||||
(AudioSignal::ValuesChanged,
|
(AudioSignal::ValuesChanged,
|
||||||
AudioUser::Unknown,
|
AudioUser::Unknown,
|
||||||
(_, _, true)) => try_w!(notif.show_volume_notif(&apps.audio)),
|
(_, _, true, _)) => try_w!(notif.show_volume_notif(apps.audio.as_ref())),
|
||||||
|
(AudioSignal::ValuesChanged,
|
||||||
// TODO hotkeys
|
AudioUser::Hotkeys,
|
||||||
|
(_, _, _, true)) => try_w!(notif.show_volume_notif(apps.audio.as_ref())),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
105
src/prefs.rs
105
src/prefs.rs
@ -73,16 +73,16 @@ impl From<MiddleClickAction> for i32 {
|
|||||||
/// Device preferences tab.
|
/// Device preferences tab.
|
||||||
pub struct DevicePrefs {
|
pub struct DevicePrefs {
|
||||||
pub card: String,
|
pub card: String,
|
||||||
pub channel: String,
|
pub channel: String,
|
||||||
// TODO: normalize volume?
|
// TODO: normalize volume?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DevicePrefs {
|
impl Default for DevicePrefs {
|
||||||
fn default() -> DevicePrefs {
|
fn default() -> DevicePrefs {
|
||||||
return DevicePrefs {
|
return DevicePrefs {
|
||||||
card: String::from("(default)"),
|
card: String::from("(default)"),
|
||||||
channel: String::from("Master"),
|
channel: String::from("Master"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,18 +94,18 @@ pub struct ViewPrefs {
|
|||||||
pub draw_vol_meter: bool,
|
pub draw_vol_meter: bool,
|
||||||
pub vol_meter_offset: i32,
|
pub vol_meter_offset: i32,
|
||||||
pub system_theme: bool,
|
pub system_theme: bool,
|
||||||
pub vol_meter_color: VolColor,
|
pub vol_meter_color: VolColor,
|
||||||
// TODO: Display text folume/text volume pos?
|
// TODO: Display text folume/text volume pos?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ViewPrefs {
|
impl Default for ViewPrefs {
|
||||||
fn default() -> ViewPrefs {
|
fn default() -> ViewPrefs {
|
||||||
return ViewPrefs {
|
return ViewPrefs {
|
||||||
draw_vol_meter: true,
|
draw_vol_meter: true,
|
||||||
vol_meter_offset: 10,
|
vol_meter_offset: 10,
|
||||||
system_theme: true,
|
system_theme: true,
|
||||||
vol_meter_color: VolColor::default(),
|
vol_meter_color: VolColor::default(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,10 +122,10 @@ pub struct VolColor {
|
|||||||
impl Default for VolColor {
|
impl Default for VolColor {
|
||||||
fn default() -> VolColor {
|
fn default() -> VolColor {
|
||||||
return VolColor {
|
return VolColor {
|
||||||
red: 0.960784313725,
|
red: 0.960784313725,
|
||||||
green: 0.705882352941,
|
green: 0.705882352941,
|
||||||
blue: 0.0,
|
blue: 0.0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,19 +139,19 @@ pub struct BehaviorPrefs {
|
|||||||
pub vol_scroll_step: f64,
|
pub vol_scroll_step: f64,
|
||||||
pub vol_fine_scroll_step: f64,
|
pub vol_fine_scroll_step: f64,
|
||||||
pub middle_click_action: MiddleClickAction,
|
pub middle_click_action: MiddleClickAction,
|
||||||
pub custom_command: Option<String>, // TODO: fine scroll step?
|
pub custom_command: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for BehaviorPrefs {
|
impl Default for BehaviorPrefs {
|
||||||
fn default() -> BehaviorPrefs {
|
fn default() -> BehaviorPrefs {
|
||||||
return BehaviorPrefs {
|
return BehaviorPrefs {
|
||||||
unmute_on_vol_change: true,
|
unmute_on_vol_change: true,
|
||||||
vol_control_cmd: None,
|
vol_control_cmd: None,
|
||||||
vol_scroll_step: 5.0,
|
vol_scroll_step: 5.0,
|
||||||
vol_fine_scroll_step: 1.0,
|
vol_fine_scroll_step: 1.0,
|
||||||
middle_click_action: MiddleClickAction::default(),
|
middle_click_action: MiddleClickAction::default(),
|
||||||
custom_command: None,
|
custom_command: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,24 +165,36 @@ pub struct NotifyPrefs {
|
|||||||
pub notifcation_timeout: i64,
|
pub notifcation_timeout: i64,
|
||||||
pub notify_mouse_scroll: bool,
|
pub notify_mouse_scroll: bool,
|
||||||
pub notify_popup: bool,
|
pub notify_popup: bool,
|
||||||
pub notify_external: bool,
|
pub notify_external: bool,
|
||||||
// TODO: notify_hotkeys?
|
pub notify_hotkeys: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
impl Default for NotifyPrefs {
|
impl Default for NotifyPrefs {
|
||||||
fn default() -> NotifyPrefs {
|
fn default() -> NotifyPrefs {
|
||||||
return NotifyPrefs {
|
return NotifyPrefs {
|
||||||
enable_notifications: true,
|
enable_notifications: true,
|
||||||
notifcation_timeout: 1500,
|
notifcation_timeout: 1500,
|
||||||
notify_mouse_scroll: true,
|
notify_mouse_scroll: true,
|
||||||
notify_popup: true,
|
notify_popup: true,
|
||||||
notify_external: true,
|
notify_external: true,
|
||||||
};
|
notify_hotkeys: true,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Serialize, Default)]
|
||||||
|
/// Hotkey preferences.
|
||||||
|
/// The `String`s represent gtk accelerator strings.
|
||||||
|
pub struct HotkeyPrefs {
|
||||||
|
pub enable_hotkeys: bool,
|
||||||
|
pub mute_unmute_key: Option<String>,
|
||||||
|
pub vol_up_key: Option<String>,
|
||||||
|
pub vol_down_key: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Serialize, Default)]
|
#[derive(Deserialize, Debug, Serialize, Default)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
/// Main preferences struct, holding all sub-preferences.
|
/// Main preferences struct, holding all sub-preferences.
|
||||||
@ -191,8 +203,8 @@ pub struct Prefs {
|
|||||||
pub view_prefs: ViewPrefs,
|
pub view_prefs: ViewPrefs,
|
||||||
pub behavior_prefs: BehaviorPrefs,
|
pub behavior_prefs: BehaviorPrefs,
|
||||||
#[cfg(feature = "notify")]
|
#[cfg(feature = "notify")]
|
||||||
pub notify_prefs: NotifyPrefs,
|
pub notify_prefs: NotifyPrefs,
|
||||||
// TODO: HotKeys?
|
pub hotkey_prefs: HotkeyPrefs,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Prefs {
|
impl Prefs {
|
||||||
@ -223,6 +235,7 @@ impl Prefs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: unused
|
||||||
/// Reload the current preferences from the config file.
|
/// Reload the current preferences from the config file.
|
||||||
pub fn reload_config(&mut self) -> Result<()> {
|
pub fn reload_config(&mut self) -> Result<()> {
|
||||||
debug!("Reloading config...");
|
debug!("Reloading config...");
|
||||||
@ -236,13 +249,26 @@ impl Prefs {
|
|||||||
|
|
||||||
/// Store the current preferences to the config file.
|
/// Store the current preferences to the config file.
|
||||||
pub fn store_config(&self) -> Result<()> {
|
pub fn store_config(&self) -> Result<()> {
|
||||||
let config_path = get_xdg_dirs().place_config_file("pnmixer.toml")
|
let config_path = get_xdg_dirs()
|
||||||
.from_err()?;
|
.place_config_file("pnmixer.toml")
|
||||||
|
.chain_err(|| {
|
||||||
|
format!(
|
||||||
|
"Could not create config directory at {:?}",
|
||||||
|
get_xdg_dirs().get_config_home()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
debug!("Storing config in {:?}", config_path);
|
debug!("Storing config in {:?}", config_path);
|
||||||
|
|
||||||
let mut f = File::create(config_path)?;
|
let mut f = File::create(config_path.clone()).chain_err(|| {
|
||||||
f.write_all(self.to_str().as_bytes())?;
|
format!(
|
||||||
|
"Could not open/create config file {:?} for writing",
|
||||||
|
config_path
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
f.write_all(self.to_str().as_bytes()).chain_err(|| {
|
||||||
|
format!("Could not write to config file {:?}", config_path)
|
||||||
|
})?;
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -275,9 +301,10 @@ impl Prefs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Prefs {
|
impl Display for Prefs {
|
||||||
fn fmt(&self,
|
fn fmt(
|
||||||
f: &mut Formatter)
|
&self,
|
||||||
-> std::result::Result<(), std::fmt::Error> {
|
f: &mut Formatter,
|
||||||
|
) -> std::result::Result<(), std::fmt::Error> {
|
||||||
let s = self.to_str();
|
let s = self.to_str();
|
||||||
return write!(f, "{}", s);
|
return write!(f, "{}", s);
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
//! out the details we don't care about.
|
//! out the details we don't care about.
|
||||||
|
|
||||||
|
|
||||||
use alsa::card::Card;
|
use alsa_lib::card::Card;
|
||||||
use alsa::mixer::{Mixer, Selem, Elem};
|
use alsa_lib::mixer::{Mixer, Selem, Elem};
|
||||||
use alsa;
|
use alsa_lib;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
|
|
||||||
@ -25,8 +25,8 @@ pub fn get_alsa_card_by_id(index: c_int) -> Card {
|
|||||||
|
|
||||||
|
|
||||||
/// Get all available alsa cards.
|
/// Get all available alsa cards.
|
||||||
pub fn get_alsa_cards() -> alsa::card::Iter {
|
pub fn get_alsa_cards() -> alsa_lib::card::Iter {
|
||||||
return alsa::card::Iter::new();
|
return alsa_lib::card::Iter::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ pub fn alsa_card_has_playable_selem(card: &Card) -> bool {
|
|||||||
|
|
||||||
/// Get the `Mixer` for the given alsa card.
|
/// Get the `Mixer` for the given alsa card.
|
||||||
pub fn get_mixer(card: &Card) -> Result<Mixer> {
|
pub fn get_mixer(card: &Card) -> Result<Mixer> {
|
||||||
return Mixer::new(&format!("hw:{}", card.get_index()), false).from_err();
|
return Ok(Mixer::new(&format!("hw:{}", card.get_index()), false)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -144,13 +144,12 @@ pub fn get_playable_selem_names(mixer: &Mixer) -> Vec<String> {
|
|||||||
|
|
||||||
|
|
||||||
/// Get a playable `Selem` by the given name.
|
/// Get a playable `Selem` by the given name.
|
||||||
pub fn get_playable_selem_by_name(mixer: &Mixer,
|
pub fn get_playable_selem_by_name(
|
||||||
name: String)
|
mixer: &Mixer,
|
||||||
-> Result<Selem> {
|
name: String,
|
||||||
|
) -> Result<Selem> {
|
||||||
for selem in get_playable_selems(mixer) {
|
for selem in get_playable_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);
|
@ -6,10 +6,10 @@
|
|||||||
//! but are important helpers.
|
//! but are important helpers.
|
||||||
|
|
||||||
|
|
||||||
use audio::{Audio, AudioUser};
|
use audio::frontend::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use prefs::*;
|
use prefs::*;
|
||||||
use support_alsa::*;
|
use support::alsa::*;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
@ -54,14 +54,14 @@ pub fn lrint(v: f64, dir: VolDir) -> f64 {
|
|||||||
|
|
||||||
|
|
||||||
/// Reload the audio system.
|
/// Reload the audio system.
|
||||||
pub fn audio_reload(audio: &Audio,
|
pub fn audio_reload<T>(audio: &T, prefs: &Prefs, user: AudioUser) -> Result<()>
|
||||||
prefs: &Prefs,
|
where
|
||||||
user: AudioUser)
|
T: AudioFrontend,
|
||||||
-> Result<()> {
|
{
|
||||||
let card = &prefs.device_prefs.card;
|
let card = &prefs.device_prefs.card;
|
||||||
let channel = &prefs.device_prefs.channel;
|
let channel = &prefs.device_prefs.channel;
|
||||||
// TODO: is this clone safe?
|
// TODO: is this clone safe?
|
||||||
return audio.switch_acard(Some(card.clone()), Some(channel.clone()), user);
|
return audio.switch_card(Some(card.clone()), Some(channel.clone()), user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -70,10 +70,12 @@ pub fn audio_reload(audio: &Audio,
|
|||||||
/// of the volume level.
|
/// of the volume level.
|
||||||
pub fn vol_to_percent(vol: i64, range: (i64, i64)) -> Result<f64> {
|
pub fn vol_to_percent(vol: i64, range: (i64, i64)) -> Result<f64> {
|
||||||
let (min, max) = range;
|
let (min, max) = range;
|
||||||
ensure!(min < max,
|
ensure!(
|
||||||
"Invalid playback volume range [{} - {}]",
|
min < max,
|
||||||
min,
|
"Invalid playback volume range [{} - {}]",
|
||||||
max);
|
min,
|
||||||
|
max
|
||||||
|
);
|
||||||
let perc = ((vol - min) as f64) / ((max - min) as f64) * 100.0;
|
let perc = ((vol - min) as f64) / ((max - min) as f64) * 100.0;
|
||||||
return Ok(perc);
|
return Ok(perc);
|
||||||
}
|
}
|
||||||
@ -84,10 +86,12 @@ pub fn vol_to_percent(vol: i64, range: (i64, i64)) -> Result<f64> {
|
|||||||
/// range.
|
/// range.
|
||||||
pub fn percent_to_vol(vol: f64, range: (i64, i64), dir: VolDir) -> Result<i64> {
|
pub fn percent_to_vol(vol: f64, range: (i64, i64), dir: VolDir) -> Result<i64> {
|
||||||
let (min, max) = range;
|
let (min, max) = range;
|
||||||
ensure!(min < max,
|
ensure!(
|
||||||
"Invalid playback volume range [{} - {}]",
|
min < max,
|
||||||
min,
|
"Invalid playback volume range [{} - {}]",
|
||||||
max);
|
min,
|
||||||
|
max
|
||||||
|
);
|
||||||
|
|
||||||
let _v = lrint(vol / 100.0 * ((max - min) as f64), dir) + (min as f64);
|
let _v = lrint(vol / 100.0 * ((max - min) as f64), dir) + (min as f64);
|
||||||
return Ok(_v as i64);
|
return Ok(_v as i64);
|
@ -1,5 +1,6 @@
|
|||||||
//! Helper functions for invoking system commands.
|
//! Helper functions for invoking system commands.
|
||||||
|
|
||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use glib;
|
use glib;
|
||||||
use prefs::Prefs;
|
use prefs::Prefs;
|
||||||
@ -23,10 +24,8 @@ pub fn execute_vol_control_command(prefs: &Prefs) -> Result<()> {
|
|||||||
|
|
||||||
/// Try to execute the given command asynchronously via gtk.
|
/// Try to execute the given command asynchronously via gtk.
|
||||||
pub fn execute_command(cmd: &str) -> Result<()> {
|
pub fn execute_command(cmd: &str) -> Result<()> {
|
||||||
return glib::spawn_command_line_async(cmd)
|
return Ok(glib::spawn_command_line_async(cmd)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
std::io::Error::new(std::io::ErrorKind::Other,
|
std::io::Error::new(std::io::ErrorKind::Other, e.description())
|
||||||
e.description())
|
})?);
|
||||||
})
|
|
||||||
.from_err();
|
|
||||||
}
|
}
|
82
src/support/gdk_x11.rs
Normal file
82
src/support/gdk_x11.rs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
//! Glue code between gdk and x11, allowing some `gdk_x11_*` functions.
|
||||||
|
//!
|
||||||
|
//! This is not a complete binding, but just provides what we need in a
|
||||||
|
//! reasonable way.
|
||||||
|
|
||||||
|
|
||||||
|
use gdk;
|
||||||
|
use gdk_sys::GdkDisplay;
|
||||||
|
use glib::translate::*;
|
||||||
|
use x11::xlib::{Display, Window};
|
||||||
|
|
||||||
|
|
||||||
|
// https://developer.gnome.org/gdk3/stable/gdk3-X-Window-System-Interaction.html
|
||||||
|
mod ffi {
|
||||||
|
use gdk_sys::{GdkDisplay, GdkWindow};
|
||||||
|
use x11::xlib::{Display, Window};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
pub fn gdk_x11_get_default_xdisplay() -> *mut Display;
|
||||||
|
pub fn gdk_x11_get_default_root_xwindow() -> Window;
|
||||||
|
pub fn gdk_x11_window_foreign_new_for_display(
|
||||||
|
display: *mut GdkDisplay,
|
||||||
|
window: Window,
|
||||||
|
) -> *mut GdkWindow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Gets the default GTK+ display.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// the Xlib Display* for the display specified in the `--display`
|
||||||
|
/// command line option or the `DISPLAY` environment variable.
|
||||||
|
pub fn gdk_x11_get_default_xdisplay() -> *mut Display {
|
||||||
|
unsafe {
|
||||||
|
return ffi::gdk_x11_get_default_xdisplay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Gets the root window of the default screen (see `gdk_x11_get_default_screen()`).
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// an Xlib Window.
|
||||||
|
pub fn gdk_x11_get_default_root_xwindow() -> Window {
|
||||||
|
unsafe {
|
||||||
|
return ffi::gdk_x11_get_default_root_xwindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Wraps a native window in a GdkWindow. The function will try to look up the
|
||||||
|
/// window using `gdk_x11_window_lookup_for_display()` first. If it does not find
|
||||||
|
/// it there, it will create a new window.
|
||||||
|
///
|
||||||
|
/// This may fail if the window has been destroyed. If the window was already
|
||||||
|
/// known to GDK, a new reference to the existing GdkWindow is returned.
|
||||||
|
/// ## `display`
|
||||||
|
/// the GdkDisplay where the window handle comes from.
|
||||||
|
/// ## ` window`
|
||||||
|
/// an Xlib Window
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// a GdkWindow wrapper for the native window, or `None` if the window has been
|
||||||
|
/// destroyed. The wrapper will be newly created, if one doesn’t exist already.
|
||||||
|
pub fn gdk_x11_window_foreign_new_for_display(
|
||||||
|
gdk_display: &mut gdk::Display,
|
||||||
|
xwindow: Window,
|
||||||
|
) -> Option<gdk::Window> {
|
||||||
|
unsafe {
|
||||||
|
let display: *mut GdkDisplay =
|
||||||
|
mut_override(gdk_display.to_glib_none().0);
|
||||||
|
|
||||||
|
return from_glib_full(ffi::gdk_x11_window_foreign_new_for_display(
|
||||||
|
display,
|
||||||
|
xwindow,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
13
src/support/mod.rs
Normal file
13
src/support/mod.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
//! Support subsystem, with no specific logical coherence.
|
||||||
|
//!
|
||||||
|
//! This module provides helper/support functions of various types that
|
||||||
|
//! don't logically fit elsewhere.
|
||||||
|
|
||||||
|
pub mod alsa;
|
||||||
|
pub mod audio;
|
||||||
|
pub mod cmd;
|
||||||
|
pub mod gdk_x11;
|
||||||
|
#[macro_use]
|
||||||
|
pub mod glade;
|
||||||
|
#[macro_use]
|
||||||
|
pub mod ui;
|
@ -3,43 +3,29 @@
|
|||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use gdk_pixbuf;
|
use gdk_pixbuf;
|
||||||
use gdk_pixbuf_sys;
|
|
||||||
use glib::translate::FromGlibPtrFull;
|
|
||||||
use glib::translate::ToGlibPtr;
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk;
|
use gtk;
|
||||||
use std::path::*;
|
use std::path::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Copy a `Pixbuf` explicitly, since they don't implement the `Copy` trait.
|
|
||||||
/// Currently does not call `gdk_pixbuf_copy_options()`.
|
|
||||||
pub fn copy_pixbuf(pixbuf: &gdk_pixbuf::Pixbuf) -> gdk_pixbuf::Pixbuf {
|
|
||||||
|
|
||||||
let new_pixbuf = unsafe {
|
|
||||||
let gdk_pixbuf = pixbuf.to_glib_full();
|
|
||||||
let copy = gdk_pixbuf_sys::gdk_pixbuf_copy(gdk_pixbuf);
|
|
||||||
FromGlibPtrFull::from_glib_full(copy)
|
|
||||||
};
|
|
||||||
|
|
||||||
return new_pixbuf;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Get a pixbuf by name from the given theme with the requested size.
|
/// Get a pixbuf by name from the given theme with the requested size.
|
||||||
/// Note that the size is not enforced, but rather a hint.
|
/// Note that the size is not enforced, but rather a hint.
|
||||||
pub fn pixbuf_new_from_theme(icon_name: &str,
|
pub fn pixbuf_new_from_theme(
|
||||||
size: i32,
|
icon_name: &str,
|
||||||
theme: >k::IconTheme)
|
size: i32,
|
||||||
-> Result<gdk_pixbuf::Pixbuf> {
|
theme: >k::IconTheme,
|
||||||
|
) -> Result<gdk_pixbuf::Pixbuf> {
|
||||||
|
|
||||||
let icon_info =
|
let icon_info = theme
|
||||||
theme.lookup_icon(icon_name, size, gtk::IconLookupFlags::empty())
|
.lookup_icon(icon_name, size, gtk::IconLookupFlags::empty())
|
||||||
.ok_or(format!("Couldn't find icon {}", icon_name))?;
|
.ok_or(format!("Couldn't find icon {}", icon_name))?;
|
||||||
|
|
||||||
debug!("Loading stock icon {} from {:?}",
|
debug!(
|
||||||
icon_name,
|
"Loading stock icon {} from {:?}",
|
||||||
icon_info.get_filename().unwrap_or(PathBuf::new()));
|
icon_name,
|
||||||
|
icon_info.get_filename().unwrap_or(PathBuf::new())
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: propagate error
|
// TODO: propagate error
|
||||||
let pixbuf = icon_info.load_icon().unwrap();
|
let pixbuf = icon_info.load_icon().unwrap();
|
137
src/ui/entry.rs
Normal file
137
src/ui/entry.rs
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
//! Global GUI state.
|
||||||
|
|
||||||
|
|
||||||
|
use app_state::*;
|
||||||
|
use audio::frontend::*;
|
||||||
|
use gtk::DialogExt;
|
||||||
|
use gtk::GtkWindowExt;
|
||||||
|
use gtk::MessageDialogExt;
|
||||||
|
use gtk::WidgetExt;
|
||||||
|
use gtk;
|
||||||
|
use gtk_sys::GTK_RESPONSE_YES;
|
||||||
|
use prefs::*;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use support::audio::*;
|
||||||
|
use ui::popup_menu::*;
|
||||||
|
use ui::popup_window::*;
|
||||||
|
use ui::prefs_dialog::*;
|
||||||
|
use ui::tray_icon::*;
|
||||||
|
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
use notif::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// The GUI struct mostly describing the main widgets (mostly wrapped)
|
||||||
|
/// the user interacts with.
|
||||||
|
pub struct Gui {
|
||||||
|
_cant_construct: (),
|
||||||
|
/// The tray icon.
|
||||||
|
pub tray_icon: TrayIcon,
|
||||||
|
/// The popup window.
|
||||||
|
pub popup_window: PopupWindow,
|
||||||
|
/// The popup menu.
|
||||||
|
pub popup_menu: PopupMenu,
|
||||||
|
/* prefs_dialog is dynamically created and destroyed */
|
||||||
|
/// The preferences dialog.
|
||||||
|
pub prefs_dialog: RefCell<Option<PrefsDialog>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Gui {
|
||||||
|
/// Constructor. The prefs dialog is initialized as `None`.
|
||||||
|
pub fn new(
|
||||||
|
builder_popup_window: gtk::Builder,
|
||||||
|
builder_popup_menu: gtk::Builder,
|
||||||
|
prefs: &Prefs,
|
||||||
|
) -> Gui {
|
||||||
|
return Gui {
|
||||||
|
_cant_construct: (),
|
||||||
|
tray_icon: TrayIcon::new(prefs).unwrap(),
|
||||||
|
popup_window: PopupWindow::new(builder_popup_window),
|
||||||
|
popup_menu: PopupMenu::new(builder_popup_menu),
|
||||||
|
prefs_dialog: RefCell::new(None),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Initialize the GUI system.
|
||||||
|
pub fn init<T>(appstate: Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
|
{
|
||||||
|
/* "global" audio signal handler */
|
||||||
|
let apps = appstate.clone();
|
||||||
|
appstate.audio.connect_handler(
|
||||||
|
Box::new(move |s, u| match (s, u) {
|
||||||
|
(AudioSignal::CardDisconnected, _) => {
|
||||||
|
try_w!(audio_reload(
|
||||||
|
apps.audio.as_ref(),
|
||||||
|
&apps.prefs.borrow(),
|
||||||
|
AudioUser::Unknown,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
(AudioSignal::CardError, _) => {
|
||||||
|
if run_audio_error_dialog(
|
||||||
|
&apps.gui.popup_menu.menu_window,
|
||||||
|
) == (GTK_RESPONSE_YES as i32)
|
||||||
|
{
|
||||||
|
try_w!(audio_reload(
|
||||||
|
apps.audio.as_ref(),
|
||||||
|
&apps.prefs.borrow(),
|
||||||
|
AudioUser::Unknown,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
init_tray_icon(appstate.clone());
|
||||||
|
init_popup_window(appstate.clone());
|
||||||
|
init_popup_menu(appstate.clone());
|
||||||
|
init_prefs_callback(appstate.clone());
|
||||||
|
|
||||||
|
#[cfg(feature = "notify")] init_notify(appstate.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Used to run a dialog when an audio error occured, suggesting the user
|
||||||
|
/// may reload the audio system either manually or by confirming the dialog
|
||||||
|
/// via the confirmation button.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `GTK_RESPONSE_YES` if the user wants to reload the audio system,
|
||||||
|
/// `GTK_RESPONSE_NO` otherwise.
|
||||||
|
fn run_audio_error_dialog(parent: >k::Window) -> i32 {
|
||||||
|
error!(
|
||||||
|
"Connection with audio failed, you probably need to restart pnmixer."
|
||||||
|
);
|
||||||
|
|
||||||
|
let dialog = gtk::MessageDialog::new(
|
||||||
|
Some(parent),
|
||||||
|
gtk::DialogFlags::DESTROY_WITH_PARENT,
|
||||||
|
gtk::MessageType::Error,
|
||||||
|
gtk::ButtonsType::YesNo,
|
||||||
|
"Warning: Connection to sound system failed.",
|
||||||
|
);
|
||||||
|
dialog.set_property_secondary_text(Some(
|
||||||
|
"Do you want to re-initialize the audio connection ?
|
||||||
|
|
||||||
|
If you do not, you will either need to restart PNMixer
|
||||||
|
or select the 'Reload Audio' option in the right-click
|
||||||
|
menu in order for PNMixer to function.",
|
||||||
|
));
|
||||||
|
|
||||||
|
dialog.set_title("PNMixer-rs Error");
|
||||||
|
|
||||||
|
let resp = dialog.run();
|
||||||
|
dialog.destroy();
|
||||||
|
|
||||||
|
return resp;
|
||||||
|
}
|
143
src/ui/hotkey_dialog.rs
Normal file
143
src/ui/hotkey_dialog.rs
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
//! The ui hotkey preferences dialog.
|
||||||
|
//!
|
||||||
|
//! Usually run from the preferences window.
|
||||||
|
|
||||||
|
|
||||||
|
use errors::*;
|
||||||
|
use gdk::DeviceExt;
|
||||||
|
use gdk;
|
||||||
|
use gdk_sys;
|
||||||
|
use glib::translate::*;
|
||||||
|
use gtk::prelude::*;
|
||||||
|
use gtk;
|
||||||
|
use libc::c_uint;
|
||||||
|
use std;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// Hotkey dialog struct holding the relevant gtk widgets.
|
||||||
|
pub struct HotkeyDialog {
|
||||||
|
hotkey_dialog: gtk::Dialog,
|
||||||
|
// instruction_label: gtk::Label, // not needed
|
||||||
|
key_pressed_label: gtk::Label,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HotkeyDialog {
|
||||||
|
/// Creates a new hotkey dialog.
|
||||||
|
pub fn new<P>(parent: &P, hotkey: String) -> HotkeyDialog
|
||||||
|
where
|
||||||
|
P: IsA<gtk::Window>,
|
||||||
|
{
|
||||||
|
let builder = gtk::Builder::new_from_string(include_str!(concat!(
|
||||||
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
|
"/data/ui/hotkey-dialog.glade"
|
||||||
|
)));
|
||||||
|
|
||||||
|
let hotkey_dialog: gtk::Dialog =
|
||||||
|
builder.get_object("hotkey_dialog").unwrap();
|
||||||
|
let instruction_label: gtk::Label =
|
||||||
|
builder.get_object("instruction_label").unwrap();
|
||||||
|
let key_pressed_label: gtk::Label =
|
||||||
|
builder.get_object("key_pressed_label").unwrap();
|
||||||
|
|
||||||
|
hotkey_dialog.set_title(format!("Set {} HotKey", hotkey).as_str());
|
||||||
|
instruction_label.set_markup(
|
||||||
|
format!("Press new HotKey for <b>{}</b>", hotkey)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
|
hotkey_dialog.set_transient_for(parent);
|
||||||
|
|
||||||
|
{
|
||||||
|
let key_pressed_label = key_pressed_label.clone();
|
||||||
|
hotkey_dialog.connect_key_press_event(move |_, e| {
|
||||||
|
let mut state = e.get_state();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let mut keyval: c_uint = 0;
|
||||||
|
let mut consumed: gdk_sys::GdkModifierType =
|
||||||
|
gdk_sys::GdkModifierType::empty();
|
||||||
|
gdk_sys::gdk_keymap_translate_keyboard_state(
|
||||||
|
gdk_sys::gdk_keymap_get_default(),
|
||||||
|
e.get_hardware_keycode() as u32,
|
||||||
|
state.to_glib(),
|
||||||
|
e.get_group() as i32,
|
||||||
|
&mut keyval as *mut c_uint,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
&mut consumed as *mut gdk_sys::GdkModifierType,
|
||||||
|
);
|
||||||
|
|
||||||
|
let consumed: gdk::ModifierType = from_glib(!consumed);
|
||||||
|
state = state & consumed;
|
||||||
|
state = state & gtk::accelerator_get_default_mod_mask();
|
||||||
|
|
||||||
|
let key_text = gtk::accelerator_name(keyval, state);
|
||||||
|
key_pressed_label.set_text(
|
||||||
|
key_text
|
||||||
|
.unwrap_or(String::from("(None)"))
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
return Inhibit(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
hotkey_dialog.connect_key_release_event(move |w, _| {
|
||||||
|
w.response(gtk::ResponseType::Ok.into());
|
||||||
|
return Inhibit(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return HotkeyDialog {
|
||||||
|
hotkey_dialog,
|
||||||
|
key_pressed_label,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the hotkey dialog and returns a String representing the hotkey
|
||||||
|
/// that has been pressed.
|
||||||
|
pub fn run(&self) -> Result<String> {
|
||||||
|
self.hotkey_dialog.show_now();
|
||||||
|
let device = gtk::get_current_event_device().ok_or(
|
||||||
|
"Could not get current device",
|
||||||
|
)?;
|
||||||
|
let window = self.hotkey_dialog.get_window().ok_or(
|
||||||
|
"Could not get window",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let m_grab_status = device.grab(
|
||||||
|
&window,
|
||||||
|
gdk::GrabOwnership::Application,
|
||||||
|
true,
|
||||||
|
gdk::EventMask::KEY_PRESS_MASK,
|
||||||
|
None,
|
||||||
|
gdk_sys::GDK_CURRENT_TIME as u32,
|
||||||
|
);
|
||||||
|
|
||||||
|
if m_grab_status != gdk::GrabStatus::Success {
|
||||||
|
bail!("Could not grab the keyboard");
|
||||||
|
}
|
||||||
|
|
||||||
|
let resp = self.hotkey_dialog.run();
|
||||||
|
device.ungrab(gdk_sys::GDK_CURRENT_TIME as u32);
|
||||||
|
|
||||||
|
if resp != gtk::ResponseType::Ok.into() {
|
||||||
|
bail!(ErrorKind::GtkResponseCancel(
|
||||||
|
String::from("not assigning hotkey"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(self.key_pressed_label.get_text().ok_or(
|
||||||
|
"Could not get text",
|
||||||
|
)?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Drop for HotkeyDialog {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.hotkey_dialog.destroy();
|
||||||
|
}
|
||||||
|
}
|
8
src/ui/mod.rs
Normal file
8
src/ui/mod.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
//! The UI subsystem.
|
||||||
|
|
||||||
|
pub mod entry;
|
||||||
|
pub mod hotkey_dialog;
|
||||||
|
pub mod popup_menu;
|
||||||
|
pub mod popup_window;
|
||||||
|
pub mod prefs_dialog;
|
||||||
|
pub mod tray_icon;
|
@ -12,44 +12,46 @@
|
|||||||
//! * Quit
|
//! * Quit
|
||||||
|
|
||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::AudioUser;
|
use audio::frontend::*;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk;
|
use gtk;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use support_audio::*;
|
use support::audio::*;
|
||||||
use support_cmd::*;
|
use support::cmd::*;
|
||||||
use ui_prefs_dialog::*;
|
use ui::prefs_dialog::*;
|
||||||
|
|
||||||
|
|
||||||
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
create_builder_item!(PopupMenu,
|
create_builder_item!(
|
||||||
menu_window: gtk::Window,
|
PopupMenu,
|
||||||
menubar: gtk::MenuBar,
|
menu_window: gtk::Window,
|
||||||
menu: gtk::Menu,
|
menubar: gtk::MenuBar,
|
||||||
about_item: gtk::MenuItem,
|
menu: gtk::Menu,
|
||||||
mixer_item: gtk::MenuItem,
|
about_item: gtk::MenuItem,
|
||||||
mute_item: gtk::MenuItem,
|
mixer_item: gtk::MenuItem,
|
||||||
mute_check: gtk::CheckButton,
|
mute_item: gtk::MenuItem,
|
||||||
prefs_item: gtk::MenuItem,
|
mute_check: gtk::CheckButton,
|
||||||
quit_item: gtk::MenuItem,
|
prefs_item: gtk::MenuItem,
|
||||||
reload_item: gtk::MenuItem);
|
quit_item: gtk::MenuItem,
|
||||||
|
reload_item: gtk::MenuItem
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Initialize the popup menu subsystem, registering all callbacks.
|
/// Initialize the popup menu subsystem, registering all callbacks.
|
||||||
pub fn init_popup_menu(appstate: Rc<AppS>) {
|
pub fn init_popup_menu<T>(appstate: Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
/* audio.connect_handler */
|
/* audio.connect_handler */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
appstate.audio.connect_handler(Box::new(move |s, u| {
|
appstate.audio.connect_handler(Box::new(move |s, u| {
|
||||||
/* skip if window is hidden */
|
/* skip if window is hidden */
|
||||||
if !apps.gui
|
if !apps.gui.popup_menu.menu.get_visible() {
|
||||||
.popup_menu
|
|
||||||
.menu
|
|
||||||
.get_visible() {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match (s, u) {
|
match (s, u) {
|
||||||
@ -62,10 +64,9 @@ pub fn init_popup_menu(appstate: Rc<AppS>) {
|
|||||||
/* popup_menu.menu.connect_show */
|
/* popup_menu.menu.connect_show */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
appstate.gui
|
appstate.gui.popup_menu.menu.connect_show(
|
||||||
.popup_menu
|
move |_| set_mute_check(&apps),
|
||||||
.menu
|
);
|
||||||
.connect_show(move |_| set_mute_check(&apps));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +75,10 @@ pub fn init_popup_menu(appstate: Rc<AppS>) {
|
|||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let mixer_item = &appstate.gui.popup_menu.mixer_item;
|
let mixer_item = &appstate.gui.popup_menu.mixer_item;
|
||||||
mixer_item.connect_activate(move |_| {
|
mixer_item.connect_activate(move |_| {
|
||||||
try_w!(execute_vol_control_command(&apps.prefs.borrow()));
|
let _ = result_warn!(
|
||||||
|
execute_vol_control_command(&apps.prefs.borrow()),
|
||||||
|
Some(&apps.gui.popup_menu.menu_window)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,10 +86,8 @@ pub fn init_popup_menu(appstate: Rc<AppS>) {
|
|||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let mute_item = &appstate.gui.popup_menu.mute_item;
|
let mute_item = &appstate.gui.popup_menu.mute_item;
|
||||||
mute_item.connect_activate(move |_| {
|
mute_item.connect_activate(move |_| if apps.audio.has_mute() {
|
||||||
if apps.audio.has_mute() {
|
try_w!(apps.audio.toggle_mute(AudioUser::Popup));
|
||||||
try_w!(apps.audio.toggle_mute(AudioUser::Popup));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,18 +95,18 @@ pub fn init_popup_menu(appstate: Rc<AppS>) {
|
|||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let about_item = &appstate.gui.popup_menu.about_item;
|
let about_item = &appstate.gui.popup_menu.about_item;
|
||||||
about_item.connect_activate(move |_| {
|
about_item.connect_activate(
|
||||||
on_about_item_activate(&apps);
|
move |_| { on_about_item_activate(&apps); },
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prefs_item.connect_activate_link */
|
/* prefs_item.connect_activate_link */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let prefs_item = &appstate.gui.popup_menu.prefs_item;
|
let prefs_item = &appstate.gui.popup_menu.prefs_item;
|
||||||
prefs_item.connect_activate(move |_| {
|
prefs_item.connect_activate(
|
||||||
on_prefs_item_activate(&apps);
|
move |_| { on_prefs_item_activate(&apps); },
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reload_item.connect_activate_link */
|
/* reload_item.connect_activate_link */
|
||||||
@ -112,10 +114,12 @@ pub fn init_popup_menu(appstate: Rc<AppS>) {
|
|||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let reload_item = &appstate.gui.popup_menu.reload_item;
|
let reload_item = &appstate.gui.popup_menu.reload_item;
|
||||||
reload_item.connect_activate(move |_| {
|
reload_item.connect_activate(move |_| {
|
||||||
try_w!(audio_reload(&apps.audio,
|
try_w!(audio_reload(
|
||||||
&apps.prefs.borrow(),
|
apps.audio.as_ref(),
|
||||||
AudioUser::Popup))
|
&apps.prefs.borrow(),
|
||||||
});
|
AudioUser::Popup,
|
||||||
|
))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -128,7 +132,10 @@ pub fn init_popup_menu(appstate: Rc<AppS>) {
|
|||||||
|
|
||||||
|
|
||||||
/// When the about menu item is activated.
|
/// When the about menu item is activated.
|
||||||
fn on_about_item_activate(appstate: &AppS) {
|
fn on_about_item_activate<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let popup_menu = &appstate.gui.popup_menu.menu_window;
|
let popup_menu = &appstate.gui.popup_menu.menu_window;
|
||||||
let about_dialog = create_about_dialog();
|
let about_dialog = create_about_dialog();
|
||||||
about_dialog.set_skip_taskbar_hint(true);
|
about_dialog.set_skip_taskbar_hint(true);
|
||||||
@ -170,14 +177,20 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.",
|
|||||||
|
|
||||||
|
|
||||||
/// When the Preferences item is activated.
|
/// When the Preferences item is activated.
|
||||||
fn on_prefs_item_activate(appstate: &Rc<AppS>) {
|
fn on_prefs_item_activate<T>(appstate: &Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
/* TODO: only create if needed */
|
/* TODO: only create if needed */
|
||||||
show_prefs_dialog(appstate);
|
show_prefs_dialog(appstate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// When the Mute item is checked.
|
/// When the Mute item is checked.
|
||||||
fn set_mute_check(apps: &Rc<AppS>) {
|
fn set_mute_check<T>(apps: &Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let mute_check = &apps.gui.popup_menu.mute_check;
|
let mute_check = &apps.gui.popup_menu.mute_check;
|
||||||
let m_muted = apps.audio.get_mute();
|
let m_muted = apps.audio.get_mute();
|
||||||
match m_muted {
|
match m_muted {
|
@ -5,12 +5,13 @@
|
|||||||
|
|
||||||
|
|
||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::*;
|
use audio::frontend::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use gdk::DeviceExt;
|
use gdk::DeviceExt;
|
||||||
use gdk::{GrabOwnership, GrabStatus, BUTTON_PRESS_MASK, KEY_PRESS_MASK};
|
use gdk::{GrabOwnership, GrabStatus};
|
||||||
use gdk;
|
use gdk;
|
||||||
use gdk_sys::{GDK_KEY_Escape, GDK_CURRENT_TIME};
|
use gdk_sys::{GDK_KEY_Escape, GDK_CURRENT_TIME};
|
||||||
|
use glib::translate::*;
|
||||||
use glib;
|
use glib;
|
||||||
use gtk::ToggleButtonExt;
|
use gtk::ToggleButtonExt;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
@ -18,8 +19,8 @@ use gtk;
|
|||||||
use prefs::*;
|
use prefs::*;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use support_audio::*;
|
use support::audio::*;
|
||||||
use support_cmd::*;
|
use support::cmd::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -49,33 +50,40 @@ impl PopupWindow {
|
|||||||
/// Constructor.
|
/// Constructor.
|
||||||
pub fn new(builder: gtk::Builder) -> PopupWindow {
|
pub fn new(builder: gtk::Builder) -> PopupWindow {
|
||||||
return PopupWindow {
|
return PopupWindow {
|
||||||
_cant_construct: (),
|
_cant_construct: (),
|
||||||
popup_window: builder.get_object("popup_window").unwrap(),
|
popup_window: builder.get_object("popup_window").unwrap(),
|
||||||
vol_scale_adj: builder.get_object("vol_scale_adj").unwrap(),
|
vol_scale_adj: builder.get_object("vol_scale_adj").unwrap(),
|
||||||
vol_scale: builder.get_object("vol_scale").unwrap(),
|
vol_scale: builder.get_object("vol_scale").unwrap(),
|
||||||
mute_check: builder.get_object("mute_check").unwrap(),
|
mute_check: builder.get_object("mute_check").unwrap(),
|
||||||
mixer_button: builder.get_object("mixer_button").unwrap(),
|
mixer_button: builder.get_object("mixer_button").unwrap(),
|
||||||
toggle_signal: Cell::new(0),
|
toggle_signal: Cell::new(0),
|
||||||
changed_signal: Cell::new(0),
|
changed_signal: Cell::new(0),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the popup window state, including the slider
|
/// Update the popup window state, including the slider
|
||||||
/// and the mute checkbutton.
|
/// and the mute checkbutton.
|
||||||
pub fn update(&self, audio: &Audio) -> Result<()> {
|
pub fn update<T>(&self, audio: &T) -> Result<()>
|
||||||
let cur_vol = audio.vol()?;
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
let cur_vol = audio.get_vol()?;
|
||||||
set_slider(&self.vol_scale_adj, cur_vol);
|
set_slider(&self.vol_scale_adj, cur_vol);
|
||||||
|
|
||||||
self.update_mute_check(&audio);
|
self.update_mute_check(audio);
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the mute checkbutton.
|
/// Update the mute checkbutton.
|
||||||
pub fn update_mute_check(&self, audio: &Audio) {
|
pub fn update_mute_check<T>(&self, audio: &T)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let m_muted = audio.get_mute();
|
let m_muted = audio.get_mute();
|
||||||
|
|
||||||
glib::signal_handler_block(&self.mute_check, self.toggle_signal.get());
|
glib::signal_handler_block(&self.mute_check,
|
||||||
|
&from_glib(self.toggle_signal.get()));
|
||||||
|
|
||||||
match m_muted {
|
match m_muted {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
@ -93,32 +101,36 @@ impl PopupWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glib::signal_handler_unblock(&self.mute_check,
|
glib::signal_handler_unblock(
|
||||||
self.toggle_signal.get());
|
&self.mute_check,
|
||||||
|
&from_glib(self.toggle_signal.get()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the page increment fro the volume scale adjustment based on the
|
/// Set the page increment fro the volume scale adjustment based on the
|
||||||
/// preferences.
|
/// preferences.
|
||||||
fn set_vol_increment(&self, prefs: &Prefs) {
|
fn set_vol_increment(&self, prefs: &Prefs) {
|
||||||
self.vol_scale_adj
|
self.vol_scale_adj.set_page_increment(
|
||||||
.set_page_increment(prefs.behavior_prefs.vol_scroll_step);
|
prefs.behavior_prefs.vol_scroll_step,
|
||||||
self.vol_scale_adj
|
);
|
||||||
.set_step_increment(prefs.behavior_prefs.vol_fine_scroll_step);
|
self.vol_scale_adj.set_step_increment(
|
||||||
|
prefs.behavior_prefs.vol_fine_scroll_step,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Initialize the popup window subsystem.
|
/// Initialize the popup window subsystem.
|
||||||
pub fn init_popup_window(appstate: Rc<AppS>) {
|
pub fn init_popup_window<T>(appstate: Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
/* audio.connect_handler */
|
/* audio.connect_handler */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
appstate.audio.connect_handler(Box::new(move |s, u| {
|
appstate.audio.connect_handler(Box::new(move |s, u| {
|
||||||
/* skip if window is hidden */
|
/* skip if window is hidden */
|
||||||
if !apps.gui
|
if !apps.gui.popup_window.popup_window.get_visible() {
|
||||||
.popup_window
|
|
||||||
.popup_window
|
|
||||||
.get_visible() {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match (s, u) {
|
match (s, u) {
|
||||||
@ -130,11 +142,13 @@ pub fn init_popup_window(appstate: Rc<AppS>) {
|
|||||||
* and not the real value reported by the audio system.
|
* and not the real value reported by the audio system.
|
||||||
*/
|
*/
|
||||||
(_, AudioUser::Popup) => {
|
(_, AudioUser::Popup) => {
|
||||||
apps.gui.popup_window.update_mute_check(&apps.audio);
|
apps.gui.popup_window.update_mute_check(
|
||||||
|
apps.audio.as_ref(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
/* external change, safe to update slider too */
|
/* external change, safe to update slider too */
|
||||||
(_, _) => {
|
(_, _) => {
|
||||||
try_w!(apps.gui.popup_window.update(&apps.audio));
|
try_w!(apps.gui.popup_window.update(apps.audio.as_ref()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@ -143,83 +157,70 @@ pub fn init_popup_window(appstate: Rc<AppS>) {
|
|||||||
/* mute_check.connect_toggled */
|
/* mute_check.connect_toggled */
|
||||||
{
|
{
|
||||||
let _appstate = appstate.clone();
|
let _appstate = appstate.clone();
|
||||||
let mute_check = &appstate.clone()
|
let mute_check = &appstate.clone().gui.popup_window.mute_check;
|
||||||
.gui
|
let toggle_signal = mute_check.connect_toggled(move |_| {
|
||||||
.popup_window
|
on_mute_check_toggled(&_appstate)
|
||||||
.mute_check;
|
});
|
||||||
let toggle_signal =
|
appstate.gui.popup_window.toggle_signal.set(toggle_signal.to_glib());
|
||||||
mute_check.connect_toggled(move |_| {
|
|
||||||
on_mute_check_toggled(&_appstate)
|
|
||||||
});
|
|
||||||
appstate.gui
|
|
||||||
.popup_window
|
|
||||||
.toggle_signal
|
|
||||||
.set(toggle_signal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* popup_window.connect_show */
|
/* popup_window.connect_show */
|
||||||
{
|
{
|
||||||
let _appstate = appstate.clone();
|
let _appstate = appstate.clone();
|
||||||
let popup_window = &appstate.clone()
|
let popup_window = &appstate.clone().gui.popup_window.popup_window;
|
||||||
.gui
|
|
||||||
.popup_window
|
|
||||||
.popup_window;
|
|
||||||
popup_window.connect_show(move |_| on_popup_window_show(&_appstate));
|
popup_window.connect_show(move |_| on_popup_window_show(&_appstate));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vol_scale_adj.connect_value_changed */
|
/* vol_scale_adj.connect_value_changed */
|
||||||
{
|
{
|
||||||
let _appstate = appstate.clone();
|
let _appstate = appstate.clone();
|
||||||
let vol_scale_adj = &appstate.clone()
|
let vol_scale_adj = &appstate.clone().gui.popup_window.vol_scale_adj;
|
||||||
.gui
|
let changed_signal = vol_scale_adj.connect_value_changed(move |_| {
|
||||||
.popup_window
|
on_vol_scale_value_changed(&_appstate)
|
||||||
.vol_scale_adj;
|
});
|
||||||
let changed_signal = vol_scale_adj.connect_value_changed(
|
|
||||||
move |_| on_vol_scale_value_changed(&_appstate),
|
|
||||||
);
|
|
||||||
|
|
||||||
appstate.gui
|
appstate.gui.popup_window.changed_signal.set(changed_signal.to_glib());
|
||||||
.popup_window
|
|
||||||
.changed_signal
|
|
||||||
.set(changed_signal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* popup_window.connect_event */
|
/* popup_window.connect_event */
|
||||||
{
|
{
|
||||||
let popup_window = &appstate.clone()
|
let popup_window = &appstate.clone().gui.popup_window.popup_window;
|
||||||
.gui
|
|
||||||
.popup_window
|
|
||||||
.popup_window;
|
|
||||||
popup_window.connect_event(move |w, e| on_popup_window_event(w, e));
|
popup_window.connect_event(move |w, e| on_popup_window_event(w, e));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mixer_button.connect_clicked */
|
/* mixer_button.connect_clicked */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let mixer_button = &appstate.clone()
|
let mixer_button = &appstate.clone().gui.popup_window.mixer_button;
|
||||||
.gui
|
|
||||||
.popup_window
|
|
||||||
.mixer_button;
|
|
||||||
mixer_button.connect_clicked(move |_| {
|
mixer_button.connect_clicked(move |_| {
|
||||||
apps.gui
|
apps.gui.popup_window.popup_window.hide();
|
||||||
.popup_window
|
let _ = result_warn!(
|
||||||
.popup_window
|
execute_vol_control_command(&apps.prefs.borrow()),
|
||||||
.hide();
|
Some(&apps.gui.popup_menu.menu_window)
|
||||||
try_w!(execute_vol_control_command(&apps.prefs.borrow()));
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// When the popup window is shown.
|
/// When the popup window is shown.
|
||||||
fn on_popup_window_show(appstate: &AppS) {
|
fn on_popup_window_show<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let popup_window = &appstate.gui.popup_window;
|
let popup_window = &appstate.gui.popup_window;
|
||||||
appstate.gui.popup_window.set_vol_increment(&appstate.prefs.borrow());
|
appstate.gui.popup_window.set_vol_increment(
|
||||||
glib::signal_handler_block(&popup_window.vol_scale_adj,
|
&appstate.prefs.borrow(),
|
||||||
popup_window.changed_signal.get());
|
);
|
||||||
try_w!(appstate.gui.popup_window.update(&appstate.audio));
|
glib::signal_handler_block(
|
||||||
glib::signal_handler_unblock(&popup_window.vol_scale_adj,
|
&popup_window.vol_scale_adj,
|
||||||
popup_window.changed_signal.get());
|
&from_glib(popup_window.changed_signal.get()),
|
||||||
|
);
|
||||||
|
try_w!(appstate.gui.popup_window.update(appstate.audio.as_ref()));
|
||||||
|
glib::signal_handler_unblock(
|
||||||
|
&popup_window.vol_scale_adj,
|
||||||
|
&from_glib(popup_window.changed_signal.get()),
|
||||||
|
);
|
||||||
popup_window.vol_scale.grab_focus();
|
popup_window.vol_scale.grab_focus();
|
||||||
try_w!(grab_devices(&appstate.gui.popup_window.popup_window));
|
try_w!(grab_devices(&appstate.gui.popup_window.popup_window));
|
||||||
}
|
}
|
||||||
@ -256,29 +257,31 @@ fn on_popup_window_event(w: >k::Window, e: &gdk::Event) -> gtk::Inhibit {
|
|||||||
|
|
||||||
|
|
||||||
/// When the volume scale slider is moved.
|
/// When the volume scale slider is moved.
|
||||||
fn on_vol_scale_value_changed(appstate: &AppS) {
|
fn on_vol_scale_value_changed<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let audio = &appstate.audio;
|
let audio = &appstate.audio;
|
||||||
let old_vol = try_w!(audio.vol());
|
let old_vol = try_w!(audio.get_vol());
|
||||||
|
|
||||||
let val = appstate.gui
|
let val = appstate.gui.popup_window.vol_scale.get_value();
|
||||||
.popup_window
|
|
||||||
.vol_scale
|
|
||||||
.get_value();
|
|
||||||
|
|
||||||
let dir = vol_change_to_voldir(old_vol, val);
|
let dir = vol_change_to_voldir(old_vol, val);
|
||||||
|
|
||||||
try_w!(audio.set_vol(val,
|
try_w!(audio.set_vol(
|
||||||
AudioUser::Popup,
|
val,
|
||||||
dir,
|
AudioUser::Popup,
|
||||||
appstate.prefs
|
dir,
|
||||||
.borrow()
|
appstate.prefs.borrow().behavior_prefs.unmute_on_vol_change,
|
||||||
.behavior_prefs
|
));
|
||||||
.unmute_on_vol_change));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// When the mute checkbutton is toggled.
|
/// When the mute checkbutton is toggled.
|
||||||
fn on_mute_check_toggled(appstate: &AppS) {
|
fn on_mute_check_toggled<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let audio = &appstate.audio;
|
let audio = &appstate.audio;
|
||||||
try_w!(audio.toggle_mute(AudioUser::Popup))
|
try_w!(audio.toggle_mute(AudioUser::Popup))
|
||||||
}
|
}
|
||||||
@ -297,32 +300,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,
|
gdk::EventMask::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(
|
||||||
GrabOwnership::None,
|
&gdk_window,
|
||||||
true,
|
GrabOwnership::None,
|
||||||
KEY_PRESS_MASK,
|
true,
|
||||||
None,
|
gdk::EventMask::KEY_PRESS_MASK,
|
||||||
GDK_CURRENT_TIME as u32);
|
None,
|
||||||
|
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(());
|
725
src/ui/prefs_dialog.rs
Normal file
725
src/ui/prefs_dialog.rs
Normal file
@ -0,0 +1,725 @@
|
|||||||
|
//! The preferences window subsystem, when the user clicks the "Preferences"
|
||||||
|
//! menu item on the popup menu.
|
||||||
|
|
||||||
|
|
||||||
|
use app_state::*;
|
||||||
|
use audio::frontend::*;
|
||||||
|
use errors::*;
|
||||||
|
use gdk;
|
||||||
|
use gtk::ResponseType;
|
||||||
|
use gtk::prelude::*;
|
||||||
|
use gtk;
|
||||||
|
use prefs::*;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use support::audio::*;
|
||||||
|
use ui::hotkey_dialog::HotkeyDialog;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// The main preferences dialog, holding all the relevant subwidgets we
|
||||||
|
/// need to convert its state to preferences and back.
|
||||||
|
pub struct PrefsDialog {
|
||||||
|
_cant_construct: (),
|
||||||
|
prefs_dialog: gtk::Dialog,
|
||||||
|
notebook: gtk::Notebook,
|
||||||
|
|
||||||
|
/* DevicePrefs */
|
||||||
|
card_combo: gtk::ComboBoxText,
|
||||||
|
chan_combo: gtk::ComboBoxText,
|
||||||
|
|
||||||
|
/* ViewPrefs */
|
||||||
|
vol_meter_draw_check: gtk::CheckButton,
|
||||||
|
vol_meter_pos_spin: gtk::SpinButton,
|
||||||
|
vol_meter_color_button: gtk::ColorButton,
|
||||||
|
system_theme: gtk::CheckButton,
|
||||||
|
|
||||||
|
/* BehaviorPrefs */
|
||||||
|
unmute_on_vol_change: gtk::CheckButton,
|
||||||
|
vol_control_entry: gtk::Entry,
|
||||||
|
scroll_step_spin: gtk::SpinButton,
|
||||||
|
fine_scroll_step_spin: gtk::SpinButton,
|
||||||
|
middle_click_combo: gtk::ComboBoxText,
|
||||||
|
custom_entry: gtk::Entry,
|
||||||
|
|
||||||
|
/* NotifyPrefs */
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_enable_check: gtk::CheckButton,
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_timeout_spin: gtk::SpinButton,
|
||||||
|
// pub noti_hotkey_check: gtk::CheckButton,
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_mouse_check: gtk::CheckButton,
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_popup_check: gtk::CheckButton,
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_ext_check: gtk::CheckButton,
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_hotkey_check: gtk::CheckButton,
|
||||||
|
|
||||||
|
/* HotkeyPrefs */
|
||||||
|
hotkeys_enable_check: gtk::CheckButton,
|
||||||
|
hotkeys_mute_label: gtk::Label,
|
||||||
|
hotkeys_up_label: gtk::Label,
|
||||||
|
hotkeys_down_label: gtk::Label,
|
||||||
|
|
||||||
|
/* Hotkey stuff (not prefs) */
|
||||||
|
hotkeys_mute_eventbox: gtk::EventBox,
|
||||||
|
hotkeys_up_eventbox: gtk::EventBox,
|
||||||
|
hotkeys_down_eventbox: gtk::EventBox,
|
||||||
|
|
||||||
|
hotkey_dialog: RefCell<Option<HotkeyDialog>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PrefsDialog {
|
||||||
|
fn new() -> PrefsDialog {
|
||||||
|
let builder = gtk::Builder::new_from_string(include_str!(concat!(
|
||||||
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
|
"/data/ui/prefs-dialog.glade"
|
||||||
|
)));
|
||||||
|
let prefs_dialog = PrefsDialog {
|
||||||
|
_cant_construct: (),
|
||||||
|
prefs_dialog: builder.get_object("prefs_dialog").unwrap(),
|
||||||
|
notebook: builder.get_object("notebook").unwrap(),
|
||||||
|
|
||||||
|
/* DevicePrefs */
|
||||||
|
card_combo: builder.get_object("card_combo").unwrap(),
|
||||||
|
chan_combo: builder.get_object("chan_combo").unwrap(),
|
||||||
|
|
||||||
|
/* ViewPrefs */
|
||||||
|
vol_meter_draw_check: builder
|
||||||
|
.get_object("vol_meter_draw_check")
|
||||||
|
.unwrap(),
|
||||||
|
vol_meter_pos_spin: builder
|
||||||
|
.get_object("vol_meter_pos_spin")
|
||||||
|
.unwrap(),
|
||||||
|
vol_meter_color_button: builder
|
||||||
|
.get_object("vol_meter_color_button")
|
||||||
|
.unwrap(),
|
||||||
|
system_theme: builder.get_object("system_theme").unwrap(),
|
||||||
|
|
||||||
|
/* BehaviorPrefs */
|
||||||
|
unmute_on_vol_change: builder
|
||||||
|
.get_object("unmute_on_vol_change")
|
||||||
|
.unwrap(),
|
||||||
|
vol_control_entry: builder.get_object("vol_control_entry").unwrap(),
|
||||||
|
scroll_step_spin: builder.get_object("scroll_step_spin").unwrap(),
|
||||||
|
fine_scroll_step_spin: builder
|
||||||
|
.get_object("fine_scroll_step_spin")
|
||||||
|
.unwrap(),
|
||||||
|
middle_click_combo: builder
|
||||||
|
.get_object("middle_click_combo")
|
||||||
|
.unwrap(),
|
||||||
|
custom_entry: builder.get_object("custom_entry").unwrap(),
|
||||||
|
|
||||||
|
/* NotifyPrefs */
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_enable_check: builder.get_object("noti_enable_check").unwrap(),
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_timeout_spin: builder.get_object("noti_timeout_spin").unwrap(),
|
||||||
|
// noti_hotkey_check: builder.get_object("noti_hotkey_check").unwrap(),
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_mouse_check: builder.get_object("noti_mouse_check").unwrap(),
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_popup_check: builder.get_object("noti_popup_check").unwrap(),
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_ext_check: builder.get_object("noti_ext_check").unwrap(),
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
noti_hotkey_check: builder.get_object("noti_hotkey_check").unwrap(),
|
||||||
|
|
||||||
|
/* HotkeyPrefs */
|
||||||
|
hotkeys_enable_check: builder
|
||||||
|
.get_object("hotkeys_enable_check")
|
||||||
|
.unwrap(),
|
||||||
|
hotkeys_mute_label: builder
|
||||||
|
.get_object("hotkeys_mute_label")
|
||||||
|
.unwrap(),
|
||||||
|
hotkeys_up_label: builder.get_object("hotkeys_up_label").unwrap(),
|
||||||
|
hotkeys_down_label: builder
|
||||||
|
.get_object("hotkeys_down_label")
|
||||||
|
.unwrap(),
|
||||||
|
|
||||||
|
/* Hotkey stuff (not prefs) */
|
||||||
|
hotkeys_mute_eventbox: builder
|
||||||
|
.get_object("hotkeys_mute_eventbox")
|
||||||
|
.unwrap(),
|
||||||
|
hotkeys_up_eventbox: builder
|
||||||
|
.get_object("hotkeys_up_eventbox")
|
||||||
|
.unwrap(),
|
||||||
|
hotkeys_down_eventbox: builder
|
||||||
|
.get_object("hotkeys_down_eventbox")
|
||||||
|
.unwrap(),
|
||||||
|
hotkey_dialog: RefCell::new(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
let notify_tab: gtk::Box =
|
||||||
|
builder.get_object("noti_vbox_enabled").unwrap();
|
||||||
|
#[cfg(not(feature = "notify"))]
|
||||||
|
let notify_tab: gtk::Box =
|
||||||
|
builder.get_object("noti_vbox_disabled").unwrap();
|
||||||
|
|
||||||
|
prefs_dialog.notebook.append_page(
|
||||||
|
¬ify_tab,
|
||||||
|
Some(>k::Label::new(Some("Notifications"))),
|
||||||
|
);
|
||||||
|
return prefs_dialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Import the given preferences into the preferences dialog state.
|
||||||
|
fn from_prefs(&self, prefs: &Prefs) {
|
||||||
|
/* DevicePrefs */
|
||||||
|
/* filled on show signal with audio info */
|
||||||
|
self.card_combo.remove_all();
|
||||||
|
self.chan_combo.remove_all();
|
||||||
|
|
||||||
|
/* ViewPrefs */
|
||||||
|
self.vol_meter_draw_check.set_active(
|
||||||
|
prefs.view_prefs.draw_vol_meter,
|
||||||
|
);
|
||||||
|
self.vol_meter_pos_spin.set_value(
|
||||||
|
prefs.view_prefs.vol_meter_offset as
|
||||||
|
f64,
|
||||||
|
);
|
||||||
|
|
||||||
|
let rgba = gdk::RGBA {
|
||||||
|
red: prefs.view_prefs.vol_meter_color.red,
|
||||||
|
green: prefs.view_prefs.vol_meter_color.green,
|
||||||
|
blue: prefs.view_prefs.vol_meter_color.blue,
|
||||||
|
alpha: 1.0,
|
||||||
|
};
|
||||||
|
self.vol_meter_color_button.set_rgba(&rgba);
|
||||||
|
self.system_theme.set_active(prefs.view_prefs.system_theme);
|
||||||
|
|
||||||
|
/* BehaviorPrefs */
|
||||||
|
self.unmute_on_vol_change.set_active(
|
||||||
|
prefs
|
||||||
|
.behavior_prefs
|
||||||
|
.unmute_on_vol_change,
|
||||||
|
);
|
||||||
|
self.vol_control_entry.set_text(
|
||||||
|
prefs
|
||||||
|
.behavior_prefs
|
||||||
|
.vol_control_cmd
|
||||||
|
.as_ref()
|
||||||
|
.unwrap_or(&String::from(""))
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
self.scroll_step_spin.set_value(
|
||||||
|
prefs.behavior_prefs.vol_scroll_step,
|
||||||
|
);
|
||||||
|
self.fine_scroll_step_spin.set_value(
|
||||||
|
prefs
|
||||||
|
.behavior_prefs
|
||||||
|
.vol_fine_scroll_step,
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: make sure these values always match, must be a better way
|
||||||
|
// also check to_prefs()
|
||||||
|
self.middle_click_combo.append_text("Toggle Mute");
|
||||||
|
self.middle_click_combo.append_text("Show Preferences");
|
||||||
|
self.middle_click_combo.append_text("Volume Control");
|
||||||
|
self.middle_click_combo.append_text(
|
||||||
|
"Custom Command (set below)",
|
||||||
|
);
|
||||||
|
self.middle_click_combo.set_active(
|
||||||
|
prefs
|
||||||
|
.behavior_prefs
|
||||||
|
.middle_click_action
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
self.custom_entry.set_text(
|
||||||
|
prefs
|
||||||
|
.behavior_prefs
|
||||||
|
.custom_command
|
||||||
|
.as_ref()
|
||||||
|
.unwrap_or(&String::from(""))
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
|
/* NotifyPrefs */
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
{
|
||||||
|
self.noti_enable_check.set_active(
|
||||||
|
prefs
|
||||||
|
.notify_prefs
|
||||||
|
.enable_notifications,
|
||||||
|
);
|
||||||
|
self.noti_timeout_spin.set_value(
|
||||||
|
prefs.notify_prefs.notifcation_timeout as
|
||||||
|
f64,
|
||||||
|
);
|
||||||
|
self.noti_mouse_check.set_active(
|
||||||
|
prefs.notify_prefs.notify_mouse_scroll,
|
||||||
|
);
|
||||||
|
self.noti_popup_check.set_active(
|
||||||
|
prefs.notify_prefs.notify_popup,
|
||||||
|
);
|
||||||
|
self.noti_ext_check.set_active(
|
||||||
|
prefs.notify_prefs.notify_external,
|
||||||
|
);
|
||||||
|
self.noti_hotkey_check.set_active(
|
||||||
|
prefs.notify_prefs.notify_hotkeys,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* hotkey prefs */
|
||||||
|
self.hotkeys_enable_check.set_active(
|
||||||
|
prefs.hotkey_prefs.enable_hotkeys,
|
||||||
|
);
|
||||||
|
self.hotkeys_mute_label.set_text(
|
||||||
|
prefs
|
||||||
|
.hotkey_prefs
|
||||||
|
.mute_unmute_key
|
||||||
|
.clone()
|
||||||
|
.unwrap_or(String::from("(None)"))
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
self.hotkeys_up_label.set_text(
|
||||||
|
prefs
|
||||||
|
.hotkey_prefs
|
||||||
|
.vol_up_key
|
||||||
|
.clone()
|
||||||
|
.unwrap_or(String::from("(None)"))
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
self.hotkeys_down_label.set_text(
|
||||||
|
prefs
|
||||||
|
.hotkey_prefs
|
||||||
|
.vol_down_key
|
||||||
|
.clone()
|
||||||
|
.unwrap_or(String::from("(None)"))
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Export the dialog state to the `Prefs` struct, which can be used
|
||||||
|
/// to write them to the config file.
|
||||||
|
fn to_prefs(&self) -> Prefs {
|
||||||
|
let card = self.card_combo.get_active_text();
|
||||||
|
let channel = self.chan_combo.get_active_text();
|
||||||
|
|
||||||
|
if card.is_none() || channel.is_none() {
|
||||||
|
return Prefs::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
let device_prefs = DevicePrefs {
|
||||||
|
card: self.card_combo.get_active_text().unwrap(),
|
||||||
|
channel: self.chan_combo.get_active_text().unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let vol_meter_color = VolColor {
|
||||||
|
red: (self.vol_meter_color_button.get_rgba().red),
|
||||||
|
green: (self.vol_meter_color_button.get_rgba().green),
|
||||||
|
blue: (self.vol_meter_color_button.get_rgba().blue),
|
||||||
|
};
|
||||||
|
|
||||||
|
let view_prefs = ViewPrefs {
|
||||||
|
draw_vol_meter: self.vol_meter_draw_check.get_active(),
|
||||||
|
vol_meter_offset: self.vol_meter_pos_spin.get_value_as_int(),
|
||||||
|
system_theme: self.system_theme.get_active(),
|
||||||
|
vol_meter_color,
|
||||||
|
};
|
||||||
|
|
||||||
|
let vol_control_cmd = self.vol_control_entry.get_text().and_then(|x| {
|
||||||
|
if x.is_empty() { None } else { Some(x) }
|
||||||
|
});
|
||||||
|
|
||||||
|
let custom_command =
|
||||||
|
self.custom_entry.get_text().and_then(|x| if x.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(x)
|
||||||
|
});
|
||||||
|
|
||||||
|
let behavior_prefs = BehaviorPrefs {
|
||||||
|
unmute_on_vol_change: self.unmute_on_vol_change.get_active(),
|
||||||
|
vol_control_cmd,
|
||||||
|
vol_scroll_step: self.scroll_step_spin.get_value(),
|
||||||
|
vol_fine_scroll_step: self.fine_scroll_step_spin.get_value(),
|
||||||
|
middle_click_action: self.middle_click_combo.get_active().into(),
|
||||||
|
custom_command,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
let notify_prefs = NotifyPrefs {
|
||||||
|
enable_notifications: self.noti_enable_check.get_active(),
|
||||||
|
notifcation_timeout: self.noti_timeout_spin.get_value_as_int() as
|
||||||
|
i64,
|
||||||
|
notify_mouse_scroll: self.noti_mouse_check.get_active(),
|
||||||
|
notify_popup: self.noti_popup_check.get_active(),
|
||||||
|
notify_external: self.noti_ext_check.get_active(),
|
||||||
|
notify_hotkeys: self.noti_hotkey_check.get_active(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let hotkey_prefs =
|
||||||
|
HotkeyPrefs {
|
||||||
|
enable_hotkeys: self.hotkeys_enable_check.get_active(),
|
||||||
|
mute_unmute_key: self.hotkeys_mute_label.get_text().and_then(
|
||||||
|
|s| {
|
||||||
|
if s == "(None)" { None } else { Some(s) }
|
||||||
|
},
|
||||||
|
),
|
||||||
|
vol_up_key: self.hotkeys_up_label.get_text().and_then(
|
||||||
|
|s| if s ==
|
||||||
|
"(None)"
|
||||||
|
{
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(s)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
vol_down_key: self.hotkeys_down_label.get_text().and_then(
|
||||||
|
|s| if s ==
|
||||||
|
"(None)"
|
||||||
|
{
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(s)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
return Prefs {
|
||||||
|
device_prefs,
|
||||||
|
view_prefs,
|
||||||
|
behavior_prefs,
|
||||||
|
#[cfg(feature = "notify")]
|
||||||
|
notify_prefs,
|
||||||
|
hotkey_prefs,
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Show the preferences dialog. This is created and destroyed dynamically
|
||||||
|
/// and not persistent across the application lifetime.
|
||||||
|
pub fn show_prefs_dialog<T>(appstate: &Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
|
if appstate.gui.prefs_dialog.borrow().is_some() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
*appstate.gui.prefs_dialog.borrow_mut() = Some(PrefsDialog::new());
|
||||||
|
init_prefs_dialog(&appstate);
|
||||||
|
{
|
||||||
|
let m_pd = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let prefs_dialog = &m_pd.as_ref().unwrap();
|
||||||
|
let prefs_dialog_w = &prefs_dialog.prefs_dialog;
|
||||||
|
|
||||||
|
prefs_dialog.from_prefs(&appstate.prefs.borrow());
|
||||||
|
|
||||||
|
prefs_dialog_w.set_transient_for(&appstate.gui.popup_menu.menu_window);
|
||||||
|
prefs_dialog_w.present();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Initialize the internal prefs dialog handler that connects to the audio
|
||||||
|
/// system.
|
||||||
|
pub fn init_prefs_callback<T>(appstate: Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
appstate.audio.connect_handler(Box::new(move |s, u| {
|
||||||
|
/* skip if prefs window is not present */
|
||||||
|
if apps.gui.prefs_dialog.borrow().is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
match (s, u) {
|
||||||
|
(AudioSignal::CardInitialized, _) => (),
|
||||||
|
(AudioSignal::CardCleanedUp, _) => {
|
||||||
|
fill_card_combo(&apps);
|
||||||
|
fill_chan_combo(&apps, None);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Initialize the preferences dialog gtk callbacks.
|
||||||
|
fn init_prefs_dialog<T>(appstate: &Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
|
|
||||||
|
/* prefs_dialog.connect_show */
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
let m_pd = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let pd = m_pd.as_ref().unwrap();
|
||||||
|
pd.prefs_dialog.connect_show(move |_| {
|
||||||
|
fill_card_combo(&apps);
|
||||||
|
fill_chan_combo(&apps, None);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* card_combo.connect_changed */
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
let m_cc = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let card_combo = &m_cc.as_ref().unwrap().card_combo;
|
||||||
|
|
||||||
|
card_combo.connect_changed(move |_| {
|
||||||
|
let m_cc = apps.gui.prefs_dialog.borrow();
|
||||||
|
let card_combo = &m_cc.as_ref().unwrap().card_combo;
|
||||||
|
let card_name = card_combo.get_active_text().unwrap();
|
||||||
|
fill_chan_combo(&apps, Some(card_name));
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prefs_dialog.connect_response */
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
let m_pd = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let pd = m_pd.as_ref().unwrap();
|
||||||
|
pd.prefs_dialog.connect_response(move |_, response_id| {
|
||||||
|
|
||||||
|
if response_id == ResponseType::Ok.into() ||
|
||||||
|
response_id == ResponseType::Apply.into()
|
||||||
|
{
|
||||||
|
let mut prefs = apps.prefs.borrow_mut();
|
||||||
|
let prefs_dialog = apps.gui.prefs_dialog.borrow();
|
||||||
|
*prefs = prefs_dialog.as_ref().unwrap().to_prefs();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if response_id != ResponseType::Apply.into() {
|
||||||
|
let mut prefs_dialog = apps.gui.prefs_dialog.borrow_mut();
|
||||||
|
prefs_dialog.as_ref().unwrap().prefs_dialog.destroy();
|
||||||
|
*prefs_dialog = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if response_id == ResponseType::Ok.into() ||
|
||||||
|
response_id == ResponseType::Apply.into()
|
||||||
|
{
|
||||||
|
try_w!(apps.update_popup_window());
|
||||||
|
try_w!(apps.update_tray_icon());
|
||||||
|
let _ = result_warn!(
|
||||||
|
apps.update_hotkeys(),
|
||||||
|
Some(&apps.gui.popup_menu.menu_window)
|
||||||
|
);
|
||||||
|
apps.update_notify();
|
||||||
|
try_w!(apps.update_audio(AudioUser::PrefsWindow));
|
||||||
|
let _ = result_warn!(
|
||||||
|
apps.update_config(),
|
||||||
|
Some(&apps.gui.popup_menu.menu_window)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prefs_dialog.hotkeys_mute_eventbox */
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
let m_pd = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let pd = m_pd.as_ref().unwrap();
|
||||||
|
|
||||||
|
pd.hotkeys_mute_eventbox.connect_button_press_event(
|
||||||
|
move |w, e| {
|
||||||
|
return Inhibit(
|
||||||
|
on_hotkey_event_box_button_press_event(&apps, &w, e),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prefs_dialog.hotkeys_up_eventbox */
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
let m_pd = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let pd = m_pd.as_ref().unwrap();
|
||||||
|
|
||||||
|
pd.hotkeys_up_eventbox.connect_button_press_event(
|
||||||
|
move |w, e| {
|
||||||
|
return Inhibit(
|
||||||
|
on_hotkey_event_box_button_press_event(&apps, &w, e),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prefs_dialog.hotkeys_down_eventbox */
|
||||||
|
{
|
||||||
|
let apps = appstate.clone();
|
||||||
|
let m_pd = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let pd = m_pd.as_ref().unwrap();
|
||||||
|
|
||||||
|
pd.hotkeys_down_eventbox.connect_button_press_event(
|
||||||
|
move |w, e| {
|
||||||
|
return Inhibit(
|
||||||
|
on_hotkey_event_box_button_press_event(&apps, &w, e),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Fill the card combo box in the Devices tab.
|
||||||
|
fn fill_card_combo<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
let m_cc = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let card_combo = &m_cc.as_ref().unwrap().card_combo;
|
||||||
|
card_combo.remove_all();
|
||||||
|
let audio = &appstate.audio;
|
||||||
|
|
||||||
|
/* set card combo */
|
||||||
|
let cur_card_name =
|
||||||
|
try_w!(audio.card_name(), "Can't get current card name!");
|
||||||
|
let available_card_names = get_playable_card_names();
|
||||||
|
|
||||||
|
/* set_active_id doesn't work, so save the index */
|
||||||
|
let mut c_index: i32 = -1;
|
||||||
|
for i in 0..available_card_names.len() {
|
||||||
|
let name = available_card_names.get(i).unwrap();
|
||||||
|
if *name == cur_card_name {
|
||||||
|
c_index = i as i32;
|
||||||
|
}
|
||||||
|
card_combo.append_text(&name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO, block signal?
|
||||||
|
card_combo.set_active(c_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Fill the channel combo box in the Devices tab.
|
||||||
|
fn fill_chan_combo<T>(appstate: &AppS<T>, cardname: Option<String>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
let m_cc = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let chan_combo = &m_cc.as_ref().unwrap().chan_combo;
|
||||||
|
chan_combo.remove_all();
|
||||||
|
|
||||||
|
let audio = &appstate.audio;
|
||||||
|
let available_chan_names = match cardname {
|
||||||
|
Some(name) => get_playable_chan_names(name),
|
||||||
|
None => audio.playable_chan_names(),
|
||||||
|
};
|
||||||
|
|
||||||
|
/* set chan combo */
|
||||||
|
let cur_chan_name = try_w!(audio.chan_name());
|
||||||
|
|
||||||
|
/* set_active_id doesn't work, so save the index */
|
||||||
|
let mut c_index: i32 = -1;
|
||||||
|
for i in 0..available_chan_names.len() {
|
||||||
|
let name = available_chan_names.get(i).unwrap();
|
||||||
|
if *name == cur_chan_name {
|
||||||
|
c_index = i as i32;
|
||||||
|
}
|
||||||
|
chan_combo.append_text(&name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO, block signal?`*/
|
||||||
|
chan_combo.set_active(c_index);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn on_hotkey_event_box_button_press_event<T>(
|
||||||
|
appstate: &AppS<T>,
|
||||||
|
widget: >k::EventBox,
|
||||||
|
event: &gdk::EventButton,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
let borrow = appstate.gui.prefs_dialog.borrow();
|
||||||
|
let prefs_dialog = &borrow.as_ref().unwrap();
|
||||||
|
/* we want a left-click */
|
||||||
|
if event.get_button() != 1 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* we want it to be double-click */
|
||||||
|
if event.get_event_type() != gdk::EventType::DoubleButtonPress {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (hotkey_label, hotkey) = {
|
||||||
|
if *widget == prefs_dialog.hotkeys_mute_eventbox {
|
||||||
|
(
|
||||||
|
prefs_dialog.hotkeys_mute_label.clone(),
|
||||||
|
String::from("Mute/Unmute"),
|
||||||
|
)
|
||||||
|
} else if *widget == prefs_dialog.hotkeys_up_eventbox {
|
||||||
|
(
|
||||||
|
prefs_dialog.hotkeys_up_label.clone(),
|
||||||
|
String::from("Volume Up"),
|
||||||
|
)
|
||||||
|
} else if *widget == prefs_dialog.hotkeys_down_eventbox {
|
||||||
|
(
|
||||||
|
prefs_dialog.hotkeys_down_label.clone(),
|
||||||
|
String::from("Volume Down"),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
warn!("Unknown hotkey eventbox");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Ensure there's no dialog already running */
|
||||||
|
if prefs_dialog.hotkey_dialog.borrow().is_some() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Unbind hotkeys */
|
||||||
|
appstate.hotkeys.borrow().unbind();
|
||||||
|
|
||||||
|
/* Run the hotkey dialog */
|
||||||
|
let hotkey_dialog = &prefs_dialog.hotkey_dialog;
|
||||||
|
*hotkey_dialog.borrow_mut() =
|
||||||
|
Some(HotkeyDialog::new(&prefs_dialog.prefs_dialog, hotkey));
|
||||||
|
let key_pressed = hotkey_dialog.borrow().as_ref().unwrap().run();
|
||||||
|
*hotkey_dialog.borrow_mut() = None;
|
||||||
|
|
||||||
|
/* Bind hotkeys */
|
||||||
|
appstate.hotkeys.borrow().bind();
|
||||||
|
|
||||||
|
/* Check the response */
|
||||||
|
match key_pressed {
|
||||||
|
Ok(k) => {
|
||||||
|
println!("k: {}", k);
|
||||||
|
if k.eq_ignore_ascii_case("<Primary>c") {
|
||||||
|
hotkey_label.set_text("(None)");
|
||||||
|
} else {
|
||||||
|
hotkey_label.set_text(k.as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(Error(ErrorKind::GtkResponseCancel(msg), _)) => {
|
||||||
|
info!("{}", ErrorKind::GtkResponseCancel(msg));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// Could not grab hotkey, most likely
|
||||||
|
error_dialog!(
|
||||||
|
e.description(),
|
||||||
|
Some(&appstate.gui.popup_menu.menu_window)
|
||||||
|
);
|
||||||
|
warn!("{}", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
|
|
||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::*;
|
use audio::frontend::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use gdk;
|
use gdk;
|
||||||
use gdk_pixbuf;
|
use gdk_pixbuf;
|
||||||
@ -16,9 +16,9 @@ use prefs::{Prefs, MiddleClickAction};
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use support_cmd::*;
|
use support::cmd::*;
|
||||||
use support_ui::*;
|
use support::ui::*;
|
||||||
use ui_prefs_dialog::show_prefs_dialog;
|
use ui::prefs_dialog::show_prefs_dialog;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -42,7 +42,8 @@ pub struct TrayIcon {
|
|||||||
impl TrayIcon {
|
impl TrayIcon {
|
||||||
/// Constructor. `audio_pix` is initialized as empty GdkPixbuf, to save
|
/// Constructor. `audio_pix` is initialized as empty GdkPixbuf, to save
|
||||||
/// one iteration of png decoding (`update_all()` is triggered immediately
|
/// one iteration of png decoding (`update_all()` is triggered immediately
|
||||||
/// on startup through `tray_icon.connect_size_changed`.
|
/// on startup through `tray_icon.connect_size_changed` usually,
|
||||||
|
/// otherwise we have to trigger it manually).
|
||||||
pub fn new(prefs: &Prefs) -> Result<TrayIcon> {
|
pub fn new(prefs: &Prefs) -> Result<TrayIcon> {
|
||||||
let draw_vol_meter = prefs.view_prefs.draw_vol_meter;
|
let draw_vol_meter = prefs.view_prefs.draw_vol_meter;
|
||||||
|
|
||||||
@ -62,21 +63,22 @@ impl TrayIcon {
|
|||||||
let status_icon = gtk::StatusIcon::new();
|
let status_icon = gtk::StatusIcon::new();
|
||||||
|
|
||||||
return Ok(TrayIcon {
|
return Ok(TrayIcon {
|
||||||
_cant_construct: (),
|
_cant_construct: (),
|
||||||
volmeter,
|
volmeter,
|
||||||
audio_pix: RefCell::new(audio_pix),
|
audio_pix: RefCell::new(audio_pix),
|
||||||
status_icon,
|
status_icon,
|
||||||
icon_size: Cell::new(ICON_MIN_SIZE),
|
icon_size: Cell::new(ICON_MIN_SIZE),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Update the volume meter Pixbuf, which is drawn on top of the
|
/// Update the volume meter Pixbuf, which is drawn on top of the
|
||||||
/// actual Pixbuf.
|
/// actual Pixbuf.
|
||||||
fn update_vol_meter(&self,
|
fn update_vol_meter(
|
||||||
cur_vol: f64,
|
&self,
|
||||||
vol_level: VolLevel)
|
cur_vol: f64,
|
||||||
-> Result<()> {
|
vol_level: VolLevel,
|
||||||
|
) -> Result<()> {
|
||||||
let audio_pix = self.audio_pix.borrow();
|
let audio_pix = self.audio_pix.borrow();
|
||||||
let pixbuf = audio_pix.select_pix(vol_level);
|
let pixbuf = audio_pix.select_pix(vol_level);
|
||||||
|
|
||||||
@ -95,16 +97,16 @@ impl TrayIcon {
|
|||||||
|
|
||||||
|
|
||||||
/// Update the tooltip of the tray icon.
|
/// Update the tooltip of the tray icon.
|
||||||
fn update_tooltip(&self, audio: &Audio) {
|
fn update_tooltip<T>(&self, audio: &T)
|
||||||
let cardname = audio.acard
|
where
|
||||||
.borrow()
|
T: AudioFrontend,
|
||||||
.card_name()
|
{
|
||||||
.unwrap_or(String::from("Unknown card"));
|
let cardname =
|
||||||
let channame = audio.acard
|
audio.card_name().unwrap_or(String::from("Unknown card"));
|
||||||
.borrow()
|
let channame =
|
||||||
.chan_name()
|
audio.chan_name().unwrap_or(String::from("unknown channel"));
|
||||||
.unwrap_or(String::from("unknown channel"));
|
let vol = audio
|
||||||
let vol = audio.vol()
|
.get_vol()
|
||||||
.map(|s| format!("{}", s.round()))
|
.map(|s| format!("{}", s.round()))
|
||||||
.unwrap_or(String::from("unknown volume"));
|
.unwrap_or(String::from("unknown volume"));
|
||||||
let mute_info = {
|
let mute_info = {
|
||||||
@ -116,21 +118,28 @@ impl TrayIcon {
|
|||||||
""
|
""
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.status_icon.set_tooltip_text(format!("{} ({})\nVolume: {}{}",
|
self.status_icon.set_tooltip_text(
|
||||||
cardname,
|
format!(
|
||||||
channame,
|
"{} ({})\nVolume: {}{}",
|
||||||
vol,
|
cardname,
|
||||||
mute_info)
|
channame,
|
||||||
.as_str());
|
vol,
|
||||||
|
mute_info
|
||||||
|
).as_str(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Update the whole tray icon state.
|
/// Update the whole tray icon state.
|
||||||
pub fn update_all(&self,
|
pub fn update_all<T>(
|
||||||
prefs: &Prefs,
|
&self,
|
||||||
audio: &Audio,
|
prefs: &Prefs,
|
||||||
m_size: Option<i32>)
|
audio: &T,
|
||||||
-> Result<()> {
|
m_size: Option<i32>,
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
match m_size {
|
match m_size {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
if s < ICON_MIN_SIZE {
|
if s < ICON_MIN_SIZE {
|
||||||
@ -152,8 +161,8 @@ impl TrayIcon {
|
|||||||
*self.volmeter.borrow_mut() = Some(volmeter);
|
*self.volmeter.borrow_mut() = Some(volmeter);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_tooltip(&audio);
|
self.update_tooltip(audio);
|
||||||
return self.update_vol_meter(audio.vol()?, audio.vol_level());
|
return self.update_vol_meter(audio.get_vol()?, audio.vol_level());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,50 +186,61 @@ impl VolMeter {
|
|||||||
/// Constructor. `width` and `row` are initialized with default values.
|
/// Constructor. `width` and `row` are initialized with default values.
|
||||||
fn new(prefs: &Prefs) -> VolMeter {
|
fn new(prefs: &Prefs) -> VolMeter {
|
||||||
return VolMeter {
|
return VolMeter {
|
||||||
red: (prefs.view_prefs.vol_meter_color.red * 255.0) as u8,
|
red: (prefs.view_prefs.vol_meter_color.red * 255.0) as u8,
|
||||||
green: (prefs.view_prefs.vol_meter_color.green * 255.0) as
|
green: (prefs.view_prefs.vol_meter_color.green * 255.0) as u8,
|
||||||
u8,
|
blue: (prefs.view_prefs.vol_meter_color.blue * 255.0) as u8,
|
||||||
blue: (prefs.view_prefs.vol_meter_color.blue * 255.0) as u8,
|
x_offset_pct: prefs.view_prefs.vol_meter_offset as i64,
|
||||||
x_offset_pct: prefs.view_prefs.vol_meter_offset as i64,
|
y_offset_pct: 10,
|
||||||
y_offset_pct: 10,
|
/* dynamic */
|
||||||
/* dynamic */
|
width: Cell::new(0),
|
||||||
width: Cell::new(0),
|
row: RefCell::new(vec![]),
|
||||||
row: RefCell::new(vec![]),
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: cache input pixbuf?
|
// TODO: cache input pixbuf?
|
||||||
/// Draw the volume meter on top of the actual tray icon Pixbuf.
|
/// Draw the volume meter on top of the actual tray icon Pixbuf.
|
||||||
fn meter_draw(&self,
|
fn meter_draw(
|
||||||
volume: i64,
|
&self,
|
||||||
pixbuf: &gdk_pixbuf::Pixbuf)
|
volume: i64,
|
||||||
-> Result<gdk_pixbuf::Pixbuf> {
|
pixbuf: &gdk_pixbuf::Pixbuf,
|
||||||
|
) -> Result<gdk_pixbuf::Pixbuf> {
|
||||||
|
|
||||||
ensure!(pixbuf.get_colorspace() == gdk_pixbuf_sys::GDK_COLORSPACE_RGB,
|
ensure!(
|
||||||
"Invalid colorspace in pixbuf");
|
pixbuf.get_colorspace() == gdk_pixbuf_sys::GDK_COLORSPACE_RGB,
|
||||||
ensure!(pixbuf.get_bits_per_sample() == 8,
|
"Invalid colorspace in pixbuf"
|
||||||
"Invalid bits per sample in pixbuf");
|
);
|
||||||
|
ensure!(
|
||||||
|
pixbuf.get_bits_per_sample() == 8,
|
||||||
|
"Invalid bits per sample in pixbuf"
|
||||||
|
);
|
||||||
ensure!(pixbuf.get_has_alpha(), "No alpha channel in pixbuf");
|
ensure!(pixbuf.get_has_alpha(), "No alpha channel in pixbuf");
|
||||||
ensure!(pixbuf.get_n_channels() == 4,
|
ensure!(
|
||||||
"Invalid number of channels in pixbuf");
|
pixbuf.get_n_channels() == 4,
|
||||||
|
"Invalid number of channels in pixbuf"
|
||||||
|
);
|
||||||
|
|
||||||
let i_width = pixbuf.get_width() as i64;
|
let i_width = pixbuf.get_width() as i64;
|
||||||
let i_height = pixbuf.get_height() as i64;
|
let i_height = pixbuf.get_height() as i64;
|
||||||
|
|
||||||
let new_pixbuf = copy_pixbuf(pixbuf);
|
let new_pixbuf = pixbuf.copy();
|
||||||
|
|
||||||
let vm_width = i_width / 6;
|
let vm_width = i_width / 6;
|
||||||
let x = (self.x_offset_pct as f64 *
|
let x = (self.x_offset_pct as f64 *
|
||||||
((i_width - vm_width) as f64 / 100.0)) as i64;
|
((i_width - vm_width) as f64 / 100.0)) as
|
||||||
ensure!(x >= 0 && (x + vm_width) <= i_width,
|
i64;
|
||||||
"x coordinate invalid: {}",
|
ensure!(
|
||||||
x);
|
x >= 0 && (x + vm_width) <= i_width,
|
||||||
|
"x coordinate invalid: {}",
|
||||||
|
x
|
||||||
|
);
|
||||||
let y = (self.y_offset_pct as f64 * (i_height as f64 / 100.0)) as i64;
|
let y = (self.y_offset_pct as f64 * (i_height as f64 / 100.0)) as i64;
|
||||||
let vm_height =
|
let vm_height =
|
||||||
((i_height - (y * 2)) as f64 * (volume as f64 / 100.0)) as i64;
|
((i_height - (y * 2)) as f64 * (volume as f64 / 100.0)) as i64;
|
||||||
ensure!(y >= 0 && (y + vm_height) <= i_height,
|
ensure!(
|
||||||
"y coordinate invalid: {}",
|
y >= 0 && (y + vm_height) <= i_height,
|
||||||
y);
|
"y coordinate invalid: {}",
|
||||||
|
y
|
||||||
|
);
|
||||||
|
|
||||||
/* Let's check if the icon width changed, in which case we
|
/* Let's check if the icon width changed, in which case we
|
||||||
* must reinit our internal row of pixels.
|
* must reinit our internal row of pixels.
|
||||||
@ -256,8 +276,9 @@ impl VolMeter {
|
|||||||
let p_index = ((row_offset * rowstride) + col_offset) as usize;
|
let p_index = ((row_offset * rowstride) + col_offset) as usize;
|
||||||
|
|
||||||
let row = self.row.borrow();
|
let row = self.row.borrow();
|
||||||
pixels[p_index..p_index + row.len()]
|
pixels[p_index..p_index + row.len()].copy_from_slice(
|
||||||
.copy_from_slice(row.as_ref());
|
row.as_ref(),
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -282,22 +303,22 @@ pub struct AudioPix {
|
|||||||
|
|
||||||
impl Default for AudioPix {
|
impl Default for AudioPix {
|
||||||
fn default() -> AudioPix {
|
fn default() -> AudioPix {
|
||||||
let dummy_pixbuf =
|
let dummy_pixbuf = unsafe {
|
||||||
unsafe {
|
gdk_pixbuf::Pixbuf::new(
|
||||||
gdk_pixbuf::Pixbuf::new(gdk_pixbuf_sys::GDK_COLORSPACE_RGB,
|
gdk_pixbuf_sys::GDK_COLORSPACE_RGB,
|
||||||
false,
|
false,
|
||||||
8,
|
8,
|
||||||
1,
|
1,
|
||||||
1)
|
1,
|
||||||
.unwrap()
|
).unwrap()
|
||||||
};
|
};
|
||||||
return AudioPix {
|
return AudioPix {
|
||||||
muted: dummy_pixbuf.clone(),
|
muted: dummy_pixbuf.clone(),
|
||||||
low: dummy_pixbuf.clone(),
|
low: dummy_pixbuf.clone(),
|
||||||
medium: dummy_pixbuf.clone(),
|
medium: dummy_pixbuf.clone(),
|
||||||
high: dummy_pixbuf.clone(),
|
high: dummy_pixbuf.clone(),
|
||||||
off: dummy_pixbuf.clone(),
|
off: dummy_pixbuf.clone(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,11 +372,21 @@ impl AudioPix {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AudioPix {
|
AudioPix {
|
||||||
muted: pixbuf_new_from_png!("../data/pixmaps/pnmixer-muted.png")?,
|
muted: pixbuf_new_from_png!(
|
||||||
low: pixbuf_new_from_png!("../data/pixmaps/pnmixer-low.png")?,
|
"../../data/pixmaps/pnmixer-muted.png"
|
||||||
medium: pixbuf_new_from_png!("../data/pixmaps/pnmixer-medium.png")?,
|
)?,
|
||||||
high: pixbuf_new_from_png!("../data/pixmaps/pnmixer-high.png")?,
|
low: pixbuf_new_from_png!(
|
||||||
off: pixbuf_new_from_png!("../data/pixmaps/pnmixer-off.png")?,
|
"../../data/pixmaps/pnmixer-low.png"
|
||||||
|
)?,
|
||||||
|
medium: pixbuf_new_from_png!(
|
||||||
|
"../../data/pixmaps/pnmixer-medium.png"
|
||||||
|
)?,
|
||||||
|
high: pixbuf_new_from_png!(
|
||||||
|
"../../data/pixmaps/pnmixer-high.png"
|
||||||
|
)?,
|
||||||
|
off: pixbuf_new_from_png!(
|
||||||
|
"../../data/pixmaps/pnmixer-off.png"
|
||||||
|
)?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -377,7 +408,10 @@ impl AudioPix {
|
|||||||
|
|
||||||
|
|
||||||
/// Initialize the tray icon subsystem.
|
/// Initialize the tray icon subsystem.
|
||||||
pub fn init_tray_icon(appstate: Rc<AppS>) {
|
pub fn init_tray_icon<T>(appstate: Rc<AppS<T>>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
let tray_icon = &appstate.gui.tray_icon;
|
let tray_icon = &appstate.gui.tray_icon;
|
||||||
|
|
||||||
tray_icon.status_icon.set_visible(true);
|
tray_icon.status_icon.set_visible(true);
|
||||||
@ -385,24 +419,31 @@ pub fn init_tray_icon(appstate: Rc<AppS>) {
|
|||||||
/* connect audio handler */
|
/* connect audio handler */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
appstate.audio.connect_handler(Box::new(move |s, u| match (s, u) {
|
appstate.audio.connect_handler(
|
||||||
(_, _) => {
|
Box::new(move |s, u| match (s, u) {
|
||||||
apps.gui.tray_icon.update_tooltip(&apps.audio);
|
(_, _) => {
|
||||||
try_w!(apps.gui.tray_icon.update_vol_meter(try_w!(apps.audio
|
apps.gui.tray_icon.update_tooltip(apps.audio.as_ref());
|
||||||
.vol()),
|
try_w!(apps.gui.tray_icon.update_vol_meter(
|
||||||
apps.audio.vol_level()));
|
try_w!(apps.audio.get_vol()),
|
||||||
}
|
apps.audio.vol_level(),
|
||||||
}));
|
));
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tray_icon.connect_size_changed */
|
/* tray_icon.connect_size_changed */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
tray_icon.status_icon.connect_size_changed(move |_, size| {
|
tray_icon.status_icon.connect_size_changed(move |_, size| {
|
||||||
try_wr!(apps.gui.tray_icon.update_all(&apps.prefs.borrow_mut(),
|
try_wr!(
|
||||||
&apps.audio,
|
apps.gui.tray_icon.update_all(
|
||||||
Some(size)),
|
&apps.prefs.borrow_mut(),
|
||||||
false);
|
apps.audio.as_ref(),
|
||||||
|
Some(size),
|
||||||
|
),
|
||||||
|
false
|
||||||
|
);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -410,9 +451,9 @@ pub fn init_tray_icon(appstate: Rc<AppS>) {
|
|||||||
/* tray_icon.connect_activate */
|
/* tray_icon.connect_activate */
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
tray_icon.status_icon.connect_activate(move |_| {
|
tray_icon.status_icon.connect_activate(
|
||||||
on_tray_icon_activate(&apps)
|
move |_| on_tray_icon_activate(&apps),
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tray_icon.connect_scroll_event */
|
/* tray_icon.connect_scroll_event */
|
||||||
@ -443,19 +484,25 @@ pub fn init_tray_icon(appstate: Rc<AppS>) {
|
|||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
let default_theme = try_w!(gtk::IconTheme::get_default().ok_or(
|
let default_theme = try_w!(gtk::IconTheme::get_default().ok_or(
|
||||||
"Couldn't get default icon theme",
|
"Couldn't get default icon theme",
|
||||||
));
|
));
|
||||||
default_theme.connect_changed(move |_| {
|
default_theme.connect_changed(move |_| {
|
||||||
let tray_icon = &apps.gui.tray_icon;
|
let tray_icon = &apps.gui.tray_icon;
|
||||||
let audio = &apps.audio;
|
try_e!(tray_icon.update_all(
|
||||||
try_e!(tray_icon.update_all(&apps.prefs.borrow_mut(), &audio, None));
|
&apps.prefs.borrow_mut(),
|
||||||
|
apps.audio.as_ref(),
|
||||||
|
None,
|
||||||
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// When the tray icon is activated.
|
/// When the tray icon is activated.
|
||||||
fn on_tray_icon_activate(appstate: &AppS) {
|
fn on_tray_icon_activate<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let popup_window = &appstate.gui.popup_window.popup_window;
|
let popup_window = &appstate.gui.popup_window.popup_window;
|
||||||
|
|
||||||
if popup_window.get_visible() {
|
if popup_window.get_visible() {
|
||||||
@ -467,7 +514,10 @@ fn on_tray_icon_activate(appstate: &AppS) {
|
|||||||
|
|
||||||
|
|
||||||
/// When the popup menu is shown, hide the popup window, if any.
|
/// When the popup menu is shown, hide the popup window, if any.
|
||||||
fn on_tray_icon_popup_menu(appstate: &AppS) {
|
fn on_tray_icon_popup_menu<T>(appstate: &AppS<T>)
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
let popup_window = &appstate.gui.popup_window.popup_window;
|
let popup_window = &appstate.gui.popup_window.popup_window;
|
||||||
let popup_menu = &appstate.gui.popup_menu.menu;
|
let popup_menu = &appstate.gui.popup_menu.menu;
|
||||||
|
|
||||||
@ -478,27 +528,41 @@ fn on_tray_icon_popup_menu(appstate: &AppS) {
|
|||||||
|
|
||||||
/// When the mouse scroll event happens while the mouse pointer is
|
/// When the mouse scroll event happens while the mouse pointer is
|
||||||
/// on the tray icon.
|
/// on the tray icon.
|
||||||
fn on_tray_icon_scroll_event(appstate: &AppS,
|
fn on_tray_icon_scroll_event<T>(
|
||||||
event: &gdk::EventScroll)
|
appstate: &AppS<T>,
|
||||||
-> bool {
|
event: &gdk::EventScroll,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
T: AudioFrontend,
|
||||||
|
{
|
||||||
|
|
||||||
let scroll_dir: gdk::ScrollDirection = event.get_direction();
|
let scroll_dir: gdk::ScrollDirection = event.get_direction();
|
||||||
match scroll_dir {
|
match scroll_dir {
|
||||||
gdk::ScrollDirection::Up => {
|
gdk::ScrollDirection::Up => {
|
||||||
try_wr!(appstate.audio.increase_vol(AudioUser::TrayIcon,
|
try_wr!(
|
||||||
appstate.prefs
|
appstate.audio.increase_vol(
|
||||||
.borrow()
|
AudioUser::TrayIcon,
|
||||||
.behavior_prefs
|
appstate
|
||||||
.unmute_on_vol_change),
|
.prefs
|
||||||
false);
|
.borrow()
|
||||||
|
.behavior_prefs
|
||||||
|
.unmute_on_vol_change,
|
||||||
|
),
|
||||||
|
false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
gdk::ScrollDirection::Down => {
|
gdk::ScrollDirection::Down => {
|
||||||
try_wr!(appstate.audio.decrease_vol(AudioUser::TrayIcon,
|
try_wr!(
|
||||||
appstate.prefs
|
appstate.audio.decrease_vol(
|
||||||
.borrow()
|
AudioUser::TrayIcon,
|
||||||
.behavior_prefs
|
appstate
|
||||||
.unmute_on_vol_change),
|
.prefs
|
||||||
false);
|
.borrow()
|
||||||
|
.behavior_prefs
|
||||||
|
.unmute_on_vol_change,
|
||||||
|
),
|
||||||
|
false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
@ -510,9 +574,13 @@ fn on_tray_icon_scroll_event(appstate: &AppS,
|
|||||||
/// Basically when the tray icon is clicked (although we connect to the `release`
|
/// Basically when the tray icon is clicked (although we connect to the `release`
|
||||||
/// event). This decides whether it was a left, right or middle-click and
|
/// event). This decides whether it was a left, right or middle-click and
|
||||||
/// takes appropriate actions.
|
/// takes appropriate actions.
|
||||||
fn on_tray_button_release_event(appstate: &Rc<AppS>,
|
fn on_tray_button_release_event<T>(
|
||||||
event_button: &gdk::EventButton)
|
appstate: &Rc<AppS<T>>,
|
||||||
-> bool {
|
event_button: &gdk::EventButton,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
T: AudioFrontend + 'static,
|
||||||
|
{
|
||||||
let button = event_button.get_button();
|
let button = event_button.get_button();
|
||||||
|
|
||||||
if button != 2 {
|
if button != 2 {
|
||||||
@ -528,18 +596,25 @@ fn on_tray_button_release_event(appstate: &Rc<AppS>,
|
|||||||
match middle_click_action {
|
match middle_click_action {
|
||||||
&MiddleClickAction::ToggleMute => {
|
&MiddleClickAction::ToggleMute => {
|
||||||
if audio.has_mute() {
|
if audio.has_mute() {
|
||||||
try_wr!(audio.toggle_mute(AudioUser::Popup), false);
|
try_wr!(audio.toggle_mute(AudioUser::TrayIcon), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO
|
// TODO
|
||||||
&MiddleClickAction::ShowPreferences => show_prefs_dialog(&appstate),
|
&MiddleClickAction::ShowPreferences => show_prefs_dialog(&appstate),
|
||||||
&MiddleClickAction::VolumeControl => {
|
&MiddleClickAction::VolumeControl => {
|
||||||
try_wr!(execute_vol_control_command(&appstate.prefs.borrow()),
|
let _ = result_warn!(
|
||||||
false);
|
execute_vol_control_command(&appstate.prefs.borrow()),
|
||||||
|
Some(&appstate.gui.popup_menu.menu_window)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
&MiddleClickAction::CustomCommand => {
|
&MiddleClickAction::CustomCommand => {
|
||||||
match custom_command {
|
match custom_command {
|
||||||
&Some(ref cmd) => try_wr!(execute_command(cmd.as_str()), false),
|
&Some(ref cmd) => {
|
||||||
|
let _ = result_warn!(
|
||||||
|
execute_command(cmd.as_str()),
|
||||||
|
Some(&appstate.gui.popup_menu.menu_window)
|
||||||
|
);
|
||||||
|
}
|
||||||
&None => warn!("No custom command found"),
|
&None => warn!("No custom command found"),
|
||||||
}
|
}
|
||||||
}
|
}
|
121
src/ui_entry.rs
121
src/ui_entry.rs
@ -1,121 +0,0 @@
|
|||||||
//! Global GUI state.
|
|
||||||
|
|
||||||
|
|
||||||
use app_state::*;
|
|
||||||
use audio::{AudioUser, AudioSignal};
|
|
||||||
use gtk::DialogExt;
|
|
||||||
use gtk::MessageDialogExt;
|
|
||||||
use gtk::WidgetExt;
|
|
||||||
use gtk::WindowExt;
|
|
||||||
use gtk;
|
|
||||||
use gtk_sys::GTK_RESPONSE_YES;
|
|
||||||
use prefs::*;
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use support_audio::*;
|
|
||||||
use ui_popup_menu::*;
|
|
||||||
use ui_popup_window::*;
|
|
||||||
use ui_prefs_dialog::*;
|
|
||||||
use ui_tray_icon::*;
|
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
use notif::*;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// The GUI struct mostly describing the main widgets (mostly wrapped)
|
|
||||||
/// the user interacts with.
|
|
||||||
pub struct Gui {
|
|
||||||
_cant_construct: (),
|
|
||||||
/// The tray icon.
|
|
||||||
pub tray_icon: TrayIcon,
|
|
||||||
/// The popup window.
|
|
||||||
pub popup_window: PopupWindow,
|
|
||||||
/// The popup menu.
|
|
||||||
pub popup_menu: PopupMenu,
|
|
||||||
/* prefs_dialog is dynamically created and destroyed */
|
|
||||||
/// The preferences dialog.
|
|
||||||
pub prefs_dialog: RefCell<Option<PrefsDialog>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Gui {
|
|
||||||
/// Constructor. The prefs dialog is initialized as `None`.
|
|
||||||
pub fn new(builder_popup_window: gtk::Builder,
|
|
||||||
builder_popup_menu: gtk::Builder,
|
|
||||||
prefs: &Prefs)
|
|
||||||
-> Gui {
|
|
||||||
return Gui {
|
|
||||||
_cant_construct: (),
|
|
||||||
tray_icon: TrayIcon::new(prefs).unwrap(),
|
|
||||||
popup_window: PopupWindow::new(builder_popup_window),
|
|
||||||
popup_menu: PopupMenu::new(builder_popup_menu),
|
|
||||||
prefs_dialog: RefCell::new(None),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Initialize the GUI system.
|
|
||||||
pub fn init(appstate: Rc<AppS>) {
|
|
||||||
{
|
|
||||||
/* "global" audio signal handler */
|
|
||||||
let apps = appstate.clone();
|
|
||||||
appstate.audio.connect_handler(
|
|
||||||
Box::new(move |s, u| match (s, u) {
|
|
||||||
(AudioSignal::CardDisconnected, _) => {
|
|
||||||
try_w!(audio_reload(&apps.audio,
|
|
||||||
&apps.prefs.borrow(),
|
|
||||||
AudioUser::Unknown));
|
|
||||||
},
|
|
||||||
(AudioSignal::CardError, _) => {
|
|
||||||
if run_audio_error_dialog(&apps.gui.popup_menu.menu_window) == (GTK_RESPONSE_YES as i32) {
|
|
||||||
try_w!(audio_reload(&apps.audio,
|
|
||||||
&apps.prefs.borrow(),
|
|
||||||
AudioUser::Unknown));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
init_tray_icon(appstate.clone());
|
|
||||||
init_popup_window(appstate.clone());
|
|
||||||
init_popup_menu(appstate.clone());
|
|
||||||
init_prefs_callback(appstate.clone());
|
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
init_notify(appstate.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Used to run a dialog when an audio error occured, suggesting the user
|
|
||||||
/// may reload the audio system either manually or by confirming the dialog
|
|
||||||
/// via the confirmation button.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// `GTK_RESPONSE_YES` if the user wants to reload the audio system,
|
|
||||||
/// `GTK_RESPONSE_NO` otherwise.
|
|
||||||
fn run_audio_error_dialog(parent: >k::Window) -> i32 {
|
|
||||||
error!("Connection with audio failed, you probably need to restart pnmixer.");
|
|
||||||
|
|
||||||
let dialog = gtk::MessageDialog::new(Some(parent),
|
|
||||||
gtk::DIALOG_DESTROY_WITH_PARENT,
|
|
||||||
gtk::MessageType::Error,
|
|
||||||
gtk::ButtonsType::YesNo,
|
|
||||||
"Warning: Connection to sound system failed.");
|
|
||||||
dialog.set_property_secondary_text(Some("Do you want to re-initialize the audio connection ?
|
|
||||||
|
|
||||||
If you do not, you will either need to restart PNMixer
|
|
||||||
or select the 'Reload Audio' option in the right-click
|
|
||||||
menu in order for PNMixer to function."));
|
|
||||||
|
|
||||||
dialog.set_title("PNMixer-rs Error");
|
|
||||||
|
|
||||||
let resp = dialog.run();
|
|
||||||
dialog.destroy();
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
}
|
|
@ -1,426 +0,0 @@
|
|||||||
//! The preferences window subsystem, when the user clicks the "Preferences"
|
|
||||||
//! menu item on the popup menu.
|
|
||||||
|
|
||||||
|
|
||||||
use app_state::*;
|
|
||||||
use audio::{AudioUser, AudioSignal};
|
|
||||||
use gdk;
|
|
||||||
use gtk::ResponseType;
|
|
||||||
use gtk::prelude::*;
|
|
||||||
use gtk;
|
|
||||||
use prefs::*;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use support_audio::*;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// The main preferences dialog, holding all the relevant subwidgets we
|
|
||||||
/// need to convert its state to preferences and back.
|
|
||||||
pub struct PrefsDialog {
|
|
||||||
_cant_construct: (),
|
|
||||||
prefs_dialog: gtk::Dialog,
|
|
||||||
notebook: gtk::Notebook,
|
|
||||||
|
|
||||||
/* DevicePrefs */
|
|
||||||
card_combo: gtk::ComboBoxText,
|
|
||||||
chan_combo: gtk::ComboBoxText,
|
|
||||||
|
|
||||||
/* ViewPrefs */
|
|
||||||
vol_meter_draw_check: gtk::CheckButton,
|
|
||||||
vol_meter_pos_spin: gtk::SpinButton,
|
|
||||||
vol_meter_color_button: gtk::ColorButton,
|
|
||||||
system_theme: gtk::CheckButton,
|
|
||||||
|
|
||||||
/* BehaviorPrefs */
|
|
||||||
unmute_on_vol_change: gtk::CheckButton,
|
|
||||||
vol_control_entry: gtk::Entry,
|
|
||||||
scroll_step_spin: gtk::SpinButton,
|
|
||||||
fine_scroll_step_spin: gtk::SpinButton,
|
|
||||||
middle_click_combo: gtk::ComboBoxText,
|
|
||||||
custom_entry: gtk::Entry,
|
|
||||||
|
|
||||||
/* NotifyPrefs */
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_enable_check: gtk::CheckButton,
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_timeout_spin: gtk::SpinButton,
|
|
||||||
// pub noti_hotkey_check: gtk::CheckButton,
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_mouse_check: gtk::CheckButton,
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_popup_check: gtk::CheckButton,
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_ext_check: gtk::CheckButton,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PrefsDialog {
|
|
||||||
fn new() -> PrefsDialog {
|
|
||||||
let builder =
|
|
||||||
gtk::Builder::new_from_string(include_str!(concat!(env!("CARGO_MANIFEST_DIR"),
|
|
||||||
"/data/ui/prefs-dialog.glade")));
|
|
||||||
let prefs_dialog = PrefsDialog {
|
|
||||||
_cant_construct: (),
|
|
||||||
prefs_dialog: builder.get_object("prefs_dialog").unwrap(),
|
|
||||||
notebook: builder.get_object("notebook").unwrap(),
|
|
||||||
|
|
||||||
/* DevicePrefs */
|
|
||||||
card_combo: builder.get_object("card_combo").unwrap(),
|
|
||||||
chan_combo: builder.get_object("chan_combo").unwrap(),
|
|
||||||
|
|
||||||
/* ViewPrefs */
|
|
||||||
vol_meter_draw_check: builder.get_object("vol_meter_draw_check")
|
|
||||||
.unwrap(),
|
|
||||||
vol_meter_pos_spin: builder.get_object("vol_meter_pos_spin")
|
|
||||||
.unwrap(),
|
|
||||||
vol_meter_color_button: builder.get_object("vol_meter_color_button")
|
|
||||||
.unwrap(),
|
|
||||||
system_theme: builder.get_object("system_theme").unwrap(),
|
|
||||||
|
|
||||||
/* BehaviorPrefs */
|
|
||||||
unmute_on_vol_change: builder.get_object("unmute_on_vol_change")
|
|
||||||
.unwrap(),
|
|
||||||
vol_control_entry: builder.get_object("vol_control_entry").unwrap(),
|
|
||||||
scroll_step_spin: builder.get_object("scroll_step_spin").unwrap(),
|
|
||||||
fine_scroll_step_spin: builder.get_object("fine_scroll_step_spin")
|
|
||||||
.unwrap(),
|
|
||||||
middle_click_combo: builder.get_object("middle_click_combo")
|
|
||||||
.unwrap(),
|
|
||||||
custom_entry: builder.get_object("custom_entry").unwrap(),
|
|
||||||
|
|
||||||
/* NotifyPrefs */
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_enable_check: builder.get_object("noti_enable_check").unwrap(),
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_timeout_spin: builder.get_object("noti_timeout_spin").unwrap(),
|
|
||||||
// noti_hotkey_check: builder.get_object("noti_hotkey_check").unwrap(),
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_mouse_check: builder.get_object("noti_mouse_check").unwrap(),
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_popup_check: builder.get_object("noti_popup_check").unwrap(),
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
noti_ext_check: builder.get_object("noti_ext_check").unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
let notify_tab: gtk::Box = builder.get_object("noti_vbox_enabled")
|
|
||||||
.unwrap();
|
|
||||||
#[cfg(not(feature = "notify"))]
|
|
||||||
let notify_tab: gtk::Box = builder.get_object("noti_vbox_disabled")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
prefs_dialog.notebook.append_page(¬ify_tab,
|
|
||||||
Some(>k::Label::new(Some("Notifications"))));
|
|
||||||
return prefs_dialog;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Import the given preferences into the preferences dialog state.
|
|
||||||
fn from_prefs(&self, prefs: &Prefs) {
|
|
||||||
/* DevicePrefs */
|
|
||||||
/* filled on show signal with audio info */
|
|
||||||
self.card_combo.remove_all();
|
|
||||||
self.chan_combo.remove_all();
|
|
||||||
|
|
||||||
/* ViewPrefs */
|
|
||||||
self.vol_meter_draw_check.set_active(prefs.view_prefs.draw_vol_meter);
|
|
||||||
self.vol_meter_pos_spin.set_value(prefs.view_prefs.vol_meter_offset as
|
|
||||||
f64);
|
|
||||||
|
|
||||||
let rgba = gdk::RGBA {
|
|
||||||
red: prefs.view_prefs.vol_meter_color.red,
|
|
||||||
green: prefs.view_prefs.vol_meter_color.green,
|
|
||||||
blue: prefs.view_prefs.vol_meter_color.blue,
|
|
||||||
alpha: 1.0,
|
|
||||||
};
|
|
||||||
self.vol_meter_color_button.set_rgba(&rgba);
|
|
||||||
self.system_theme.set_active(prefs.view_prefs.system_theme);
|
|
||||||
|
|
||||||
/* BehaviorPrefs */
|
|
||||||
self.unmute_on_vol_change
|
|
||||||
.set_active(prefs.behavior_prefs.unmute_on_vol_change);
|
|
||||||
self.vol_control_entry.set_text(prefs.behavior_prefs
|
|
||||||
.vol_control_cmd
|
|
||||||
.as_ref()
|
|
||||||
.unwrap_or(&String::from(""))
|
|
||||||
.as_str());
|
|
||||||
self.scroll_step_spin.set_value(prefs.behavior_prefs.vol_scroll_step);
|
|
||||||
self.fine_scroll_step_spin
|
|
||||||
.set_value(prefs.behavior_prefs.vol_fine_scroll_step);
|
|
||||||
|
|
||||||
// TODO: make sure these values always match, must be a better way
|
|
||||||
// also check to_prefs()
|
|
||||||
self.middle_click_combo.append_text("Toggle Mute");
|
|
||||||
self.middle_click_combo.append_text("Show Preferences");
|
|
||||||
self.middle_click_combo.append_text("Volume Control");
|
|
||||||
self.middle_click_combo.append_text("Custom Command (set below)");
|
|
||||||
self.middle_click_combo.set_active(prefs.behavior_prefs
|
|
||||||
.middle_click_action
|
|
||||||
.into());
|
|
||||||
self.custom_entry.set_text(prefs.behavior_prefs
|
|
||||||
.custom_command
|
|
||||||
.as_ref()
|
|
||||||
.unwrap_or(&String::from(""))
|
|
||||||
.as_str());
|
|
||||||
|
|
||||||
/* NotifyPrefs */
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
{
|
|
||||||
self.noti_enable_check
|
|
||||||
.set_active(prefs.notify_prefs.enable_notifications);
|
|
||||||
self.noti_timeout_spin
|
|
||||||
.set_value(prefs.notify_prefs.notifcation_timeout as f64);
|
|
||||||
self.noti_mouse_check
|
|
||||||
.set_active(prefs.notify_prefs.notify_mouse_scroll);
|
|
||||||
self.noti_popup_check.set_active(prefs.notify_prefs.notify_popup);
|
|
||||||
self.noti_ext_check.set_active(prefs.notify_prefs.notify_external);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Export the dialog state to the `Prefs` struct, which can be used
|
|
||||||
/// to write them to the config file.
|
|
||||||
fn to_prefs(&self) -> Prefs {
|
|
||||||
let card = self.card_combo.get_active_text();
|
|
||||||
let channel = self.chan_combo.get_active_text();
|
|
||||||
|
|
||||||
if card.is_none() || channel.is_none() {
|
|
||||||
return Prefs::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
let device_prefs = DevicePrefs {
|
|
||||||
card: self.card_combo.get_active_text().unwrap(),
|
|
||||||
channel: self.chan_combo.get_active_text().unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let vol_meter_color = VolColor {
|
|
||||||
red: (self.vol_meter_color_button.get_rgba().red),
|
|
||||||
green: (self.vol_meter_color_button.get_rgba().green),
|
|
||||||
blue: (self.vol_meter_color_button.get_rgba().blue),
|
|
||||||
};
|
|
||||||
|
|
||||||
let view_prefs = ViewPrefs {
|
|
||||||
draw_vol_meter: self.vol_meter_draw_check.get_active(),
|
|
||||||
vol_meter_offset: self.vol_meter_pos_spin.get_value_as_int(),
|
|
||||||
system_theme: self.system_theme.get_active(),
|
|
||||||
vol_meter_color,
|
|
||||||
};
|
|
||||||
|
|
||||||
let vol_control_cmd =
|
|
||||||
self.vol_control_entry.get_text().and_then(|x| if x.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(x)
|
|
||||||
});
|
|
||||||
|
|
||||||
let custom_command =
|
|
||||||
self.custom_entry.get_text().and_then(|x| if x.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(x)
|
|
||||||
});
|
|
||||||
|
|
||||||
let behavior_prefs = BehaviorPrefs {
|
|
||||||
unmute_on_vol_change: self.unmute_on_vol_change.get_active(),
|
|
||||||
vol_control_cmd,
|
|
||||||
vol_scroll_step: self.scroll_step_spin.get_value(),
|
|
||||||
vol_fine_scroll_step: self.fine_scroll_step_spin.get_value(),
|
|
||||||
middle_click_action: self.middle_click_combo.get_active().into(),
|
|
||||||
custom_command,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
let notify_prefs = NotifyPrefs {
|
|
||||||
enable_notifications: self.noti_enable_check.get_active(),
|
|
||||||
notifcation_timeout: self.noti_timeout_spin.get_value_as_int() as
|
|
||||||
i64,
|
|
||||||
notify_mouse_scroll: self.noti_mouse_check.get_active(),
|
|
||||||
notify_popup: self.noti_popup_check.get_active(),
|
|
||||||
notify_external: self.noti_ext_check.get_active(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Prefs {
|
|
||||||
device_prefs,
|
|
||||||
view_prefs,
|
|
||||||
behavior_prefs,
|
|
||||||
#[cfg(feature = "notify")]
|
|
||||||
notify_prefs,
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Show the preferences dialog. This is created and destroyed dynamically
|
|
||||||
/// and not persistent across the application lifetime.
|
|
||||||
pub fn show_prefs_dialog(appstate: &Rc<AppS>) {
|
|
||||||
if appstate.gui
|
|
||||||
.prefs_dialog
|
|
||||||
.borrow()
|
|
||||||
.is_some() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*appstate.gui.prefs_dialog.borrow_mut() = Some(PrefsDialog::new());
|
|
||||||
init_prefs_dialog(&appstate);
|
|
||||||
{
|
|
||||||
let m_pd = appstate.gui.prefs_dialog.borrow();
|
|
||||||
let prefs_dialog = &m_pd.as_ref().unwrap();
|
|
||||||
let prefs_dialog_w = &prefs_dialog.prefs_dialog;
|
|
||||||
|
|
||||||
prefs_dialog.from_prefs(&appstate.prefs.borrow());
|
|
||||||
|
|
||||||
prefs_dialog_w.set_transient_for(&appstate.gui.popup_menu.menu_window);
|
|
||||||
prefs_dialog_w.present();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Initialize the internal prefs dialog handler that connects to the audio
|
|
||||||
/// system.
|
|
||||||
pub fn init_prefs_callback(appstate: Rc<AppS>) {
|
|
||||||
let apps = appstate.clone();
|
|
||||||
appstate.audio.connect_handler(Box::new(move |s, u| {
|
|
||||||
/* skip if prefs window is not present */
|
|
||||||
if apps.gui
|
|
||||||
.prefs_dialog
|
|
||||||
.borrow()
|
|
||||||
.is_none() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
match (s, u) {
|
|
||||||
(AudioSignal::CardInitialized, _) => (),
|
|
||||||
(AudioSignal::CardCleanedUp, _) => {
|
|
||||||
fill_card_combo(&apps);
|
|
||||||
fill_chan_combo(&apps, None);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Initialize the preferences dialog gtk callbacks.
|
|
||||||
fn init_prefs_dialog(appstate: &Rc<AppS>) {
|
|
||||||
|
|
||||||
/* prefs_dialog.connect_show */
|
|
||||||
{
|
|
||||||
let apps = appstate.clone();
|
|
||||||
let m_pd = appstate.gui.prefs_dialog.borrow();
|
|
||||||
let pd = m_pd.as_ref().unwrap();
|
|
||||||
pd.prefs_dialog.connect_show(move |_| {
|
|
||||||
fill_card_combo(&apps);
|
|
||||||
fill_chan_combo(&apps, None);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/* card_combo.connect_changed */
|
|
||||||
{
|
|
||||||
let apps = appstate.clone();
|
|
||||||
let m_cc = appstate.gui.prefs_dialog.borrow();
|
|
||||||
let card_combo = &m_cc.as_ref().unwrap().card_combo;
|
|
||||||
|
|
||||||
card_combo.connect_changed(move |_| {
|
|
||||||
let m_cc = apps.gui.prefs_dialog.borrow();
|
|
||||||
let card_combo = &m_cc.as_ref().unwrap().card_combo;
|
|
||||||
let card_name = card_combo.get_active_text().unwrap();
|
|
||||||
fill_chan_combo(&apps, Some(card_name));
|
|
||||||
return;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/* prefs_dialog.connect_response */
|
|
||||||
{
|
|
||||||
let apps = appstate.clone();
|
|
||||||
let m_pd = appstate.gui.prefs_dialog.borrow();
|
|
||||||
let pd = m_pd.as_ref().unwrap();
|
|
||||||
pd.prefs_dialog.connect_response(move |_, response_id| {
|
|
||||||
|
|
||||||
if response_id == ResponseType::Ok.into() ||
|
|
||||||
response_id == ResponseType::Apply.into() {
|
|
||||||
let mut prefs = apps.prefs.borrow_mut();
|
|
||||||
let prefs_dialog = apps.gui.prefs_dialog.borrow();
|
|
||||||
*prefs = prefs_dialog.as_ref().unwrap().to_prefs();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if response_id != ResponseType::Apply.into() {
|
|
||||||
let mut prefs_dialog = apps.gui.prefs_dialog.borrow_mut();
|
|
||||||
prefs_dialog.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.prefs_dialog
|
|
||||||
.destroy();
|
|
||||||
*prefs_dialog = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if response_id == ResponseType::Ok.into() ||
|
|
||||||
response_id == ResponseType::Apply.into() {
|
|
||||||
// TODO: update hotkeys
|
|
||||||
try_w!(apps.update_notify());
|
|
||||||
try_w!(apps.update_tray_icon());
|
|
||||||
try_w!(apps.update_popup_window());
|
|
||||||
try_w!(apps.update_audio(AudioUser::PrefsWindow));
|
|
||||||
try_w!(apps.update_config());
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Fill the card combo box in the Devices tab.
|
|
||||||
fn fill_card_combo(appstate: &AppS) {
|
|
||||||
let m_cc = appstate.gui.prefs_dialog.borrow();
|
|
||||||
let card_combo = &m_cc.as_ref().unwrap().card_combo;
|
|
||||||
card_combo.remove_all();
|
|
||||||
let audio = &appstate.audio;
|
|
||||||
|
|
||||||
/* set card combo */
|
|
||||||
let cur_card_name = try_w!(audio.card_name(),
|
|
||||||
"Can't get current card name!");
|
|
||||||
let available_card_names = get_playable_card_names();
|
|
||||||
|
|
||||||
/* set_active_id doesn't work, so save the index */
|
|
||||||
let mut c_index: i32 = -1;
|
|
||||||
for i in 0..available_card_names.len() {
|
|
||||||
let name = available_card_names.get(i).unwrap();
|
|
||||||
if *name == cur_card_name {
|
|
||||||
c_index = i as i32;
|
|
||||||
}
|
|
||||||
card_combo.append_text(&name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO, block signal?
|
|
||||||
card_combo.set_active(c_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Fill the channel combo box in the Devices tab.
|
|
||||||
fn fill_chan_combo(appstate: &AppS, cardname: Option<String>) {
|
|
||||||
let m_cc = appstate.gui.prefs_dialog.borrow();
|
|
||||||
let chan_combo = &m_cc.as_ref().unwrap().chan_combo;
|
|
||||||
chan_combo.remove_all();
|
|
||||||
|
|
||||||
let audio = &appstate.audio;
|
|
||||||
let available_chan_names = match cardname {
|
|
||||||
Some(name) => get_playable_chan_names(name),
|
|
||||||
None => audio.playable_chan_names(),
|
|
||||||
};
|
|
||||||
|
|
||||||
/* set chan combo */
|
|
||||||
let cur_chan_name = try_w!(audio.chan_name());
|
|
||||||
|
|
||||||
/* set_active_id doesn't work, so save the index */
|
|
||||||
let mut c_index: i32 = -1;
|
|
||||||
for i in 0..available_chan_names.len() {
|
|
||||||
let name = available_chan_names.get(i).unwrap();
|
|
||||||
if *name == cur_chan_name {
|
|
||||||
c_index = i as i32;
|
|
||||||
}
|
|
||||||
chan_combo.append_text(&name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO, block signal?`*/
|
|
||||||
chan_combo.set_active(c_index);
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user