[sugar] [PATCH] Add speaker device and icon by default
Martin Dengler
martin at martindengler.com
Thu Apr 24 07:33:38 EDT 2008
Add speaker device and icon by default, as in
http://wiki.laptop.org/go/Designs/Frame#07
---
src/hardware/hardwaremanager.py | 28 +++++++++-
src/model/devices/Makefile.am | 4 +-
src/model/devices/devicesmodel.py | 3 +
src/model/devices/speaker.py | 59 +++++++++++++++++++
src/view/devices/Makefile.am | 4 +-
src/view/devices/speaker.py | 113 +++++++++++++++++++++++++++++++++++++
6 files changed, 207 insertions(+), 4 deletions(-)
create mode 100644 src/model/devices/speaker.py
create mode 100644 src/view/devices/speaker.py
diff --git a/src/hardware/hardwaremanager.py b/src/hardware/hardwaremanager.py
index 29be450..f5bb0e3 100644
--- a/src/hardware/hardwaremanager.py
+++ b/src/hardware/hardwaremanager.py
@@ -50,6 +50,13 @@ class HardwareManager(object):
if track.flags & gst.interfaces.MIXER_TRACK_MASTER:
self._master = track
+ def get_mute(self):
+ if not self._mixer or not self._master:
+ logging.error('Cannot get the mute status')
+ return False
+ return self._master.flags & gst.interfaces.MIXER_TRACK_MUTE \
+ == gst.interfaces.MIXER_TRACK_MUTE
+
def get_volume(self):
if not self._mixer or not self._master:
logging.error('Cannot get the volume')
@@ -57,7 +64,16 @@ class HardwareManager(object):
max_volume = self._master.max_volume
min_volume = self._master.min_volume
- volume = self._mixer.get_volume(self._master)[0]
+
+ volumes = self._mixer.get_volume(self._master)
+
+ #sometimes we get a spurious zero from one/more channel(s)
+ nonzero_vols = [v for v in volumes if v > 0.0]
+ if len(nonzero_vols) != 0 and len(nonzero_vols) != len(volumes):
+ volumes = nonzero_vols
+
+ #we could just pick the first channel's volume, but this converges
+ volume = sum(volumes) / len(volumes)
return volume * 100.0 / (max_volume - min_volume) + min_volume
@@ -73,10 +89,18 @@ class HardwareManager(object):
max_volume = self._master.max_volume
min_volume = self._master.min_volume
+ logging.debug("setting volume: %s (%s/%s/%s)" % (volume, min_volume, max_volume, self._master.num_channels))
volume = volume * (max_volume - min_volume) / 100.0 + min_volume
volume_list = [ volume ] * self._master.num_channels
- self._mixer.set_volume(self._master, tuple(volume_list))
+ #sometimes alsa fails to set all channels' volume, so try a few times
+ last_volumes_read = [0]
+ read_count = 0
+ while (0 in last_volumes_read) and (read_count < 3):
+ logging.debug("setting volume_list %d: %s" % (read_count, volume_list))
+ self._mixer.set_volume(self._master, tuple(volume_list))
+ last_volumes_read = self._mixer.get_volume(self._master)
+ read_count += 1
def set_mute(self, mute):
if not self._mixer or not self._master:
diff --git a/src/model/devices/Makefile.am b/src/model/devices/Makefile.am
index 5440eeb..3124144 100644
--- a/src/model/devices/Makefile.am
+++ b/src/model/devices/Makefile.am
@@ -5,4 +5,6 @@ sugar_PYTHON = \
__init__.py \
device.py \
devicesmodel.py \
- battery.py
+ battery.py \
+ speaker.py
+
diff --git a/src/model/devices/devicesmodel.py b/src/model/devices/devicesmodel.py
index 691e19e..32c77da 100644
--- a/src/model/devices/devicesmodel.py
+++ b/src/model/devices/devicesmodel.py
@@ -23,6 +23,7 @@ from model.devices import device
from model.devices.network import wireless
from model.devices.network import mesh
from model.devices import battery
+from model.devices import speaker
from hardware import hardwaremanager
from hardware import nmclient
@@ -54,6 +55,8 @@ class DevicesModel(gobject.GObject):
for udi in hal_manager.FindDeviceByCapability('battery'):
self.add_device(battery.Device(udi))
+ self.add_device(speaker.Device())
+
def _observe_network_manager(self):
network_manager = hardwaremanager.get_network_manager()
if not network_manager:
diff --git a/src/model/devices/speaker.py b/src/model/devices/speaker.py
new file mode 100644
index 0000000..d3b9967
--- /dev/null
+++ b/src/model/devices/speaker.py
@@ -0,0 +1,59 @@
+# Copyright (C) 2008 Martin Dengler
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import gobject
+
+from hardware import hardwaremanager
+from model.devices import device
+
+class Device(device.Device):
+ __gproperties__ = {
+ 'level' : (int, None, None, 0, 100, 0, gobject.PARAM_READWRITE),
+ 'muted' : (bool, None, None, False, gobject.PARAM_READWRITE),
+ }
+
+ def __init__(self):
+ device.Device.__init__(self)
+ self._manager = hardwaremanager.get_manager()
+
+ def _get_level(self):
+ return self._manager.get_volume()
+
+ def _set_level(self, new_volume):
+ self._manager.set_volume(new_volume)
+ self.notify('level')
+
+ def _get_muted(self):
+ return self._manager.get_mute()
+
+ def _set_muted(self, mute):
+ self._manager.set_mute(mute)
+ self.notify('muted')
+
+ def get_type(self):
+ return 'speaker'
+
+ def do_get_property(self, pspec):
+ if pspec.name == "level":
+ return self._get_level()
+ elif pspec.name == "muted":
+ return self._get_muted()
+
+ def do_set_property(self, pspec, value):
+ if pspec.name == "level":
+ self._set_level(value)
+ elif pspec.name == "muted":
+ self._set_muted(value)
diff --git a/src/view/devices/Makefile.am b/src/view/devices/Makefile.am
index c040beb..2b19443 100644
--- a/src/view/devices/Makefile.am
+++ b/src/view/devices/Makefile.am
@@ -4,4 +4,6 @@ sugardir = $(pkgdatadir)/shell/view/devices
sugar_PYTHON = \
__init__.py \
battery.py \
- deviceview.py
+ deviceview.py \
+ speaker.py
+
diff --git a/src/view/devices/speaker.py b/src/view/devices/speaker.py
new file mode 100644
index 0000000..e3866da
--- /dev/null
+++ b/src/view/devices/speaker.py
@@ -0,0 +1,113 @@
+# Copyright (C) 2008 Martin Dengler
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+from gettext import gettext as _
+
+import gtk
+
+from sugar import profile
+from sugar.graphics import style
+from sugar.graphics.icon import get_icon_state
+from sugar.graphics.tray import TrayIcon
+from sugar.graphics.palette import Palette
+from sugar.graphics.xocolor import XoColor
+
+from view.frame.frameinvoker import FrameWidgetInvoker
+
+_ICON_NAME = 'speaker'
+
+class DeviceView(TrayIcon):
+ def __init__(self, model):
+ TrayIcon.__init__(self, icon_name=_ICON_NAME,
+ xo_color=profile.get_color())
+
+ self._model = model
+ self.palette = SpeakerPalette(_('My Speakers'), model=model)
+ self.set_palette(self.palette)
+ self.palette.props.invoker = FrameWidgetInvoker(self)
+ self.palette.set_group_id('frame')
+
+ model.connect('notify::level', self._speaker_status_changed_cb)
+ model.connect('notify::muted', self._speaker_status_changed_cb)
+ self.connect('expose-event', self._expose_event_cb)
+
+ self._update_info()
+
+ def _update_info(self):
+ name = _ICON_NAME
+ current_level = self._model.props.level
+ xo_color = profile.get_color()
+ badge_name = None
+
+ if self._model.props.muted:
+ name += "-muted"
+ xo_color = XoColor('%s,%s' % (style.COLOR_WHITE.get_svg(),
+ style.COLOR_WHITE.get_svg()))
+
+ self.icon.props.icon_name = get_icon_state(name, current_level)
+ self.icon.props.xo_color = xo_color
+ self.icon.props.badge_name = badge_name
+
+ def _speaker_status_changed_cb(self, pspec, param):
+ self._update_info()
+
+ def _expose_event_cb(self, *args):
+ self._update_info()
+
+class SpeakerPalette(Palette):
+
+ def __init__(self, primary_text, model):
+ Palette.__init__(self, primary_text)
+
+ self._model = model
+
+ self._adjustment = gtk.Adjustment(
+ self._model.props.level, 0.0, 101.0, 1, 1, 1)
+ self._hscale = gtk.HScale(self._adjustment)
+ self._hscale.set_digits(0)
+ self._hscale.set_draw_value(False)
+ self._hscale.show()
+
+ vbox = gtk.VBox()
+ vbox.pack_start(self._hscale)
+ vbox.show()
+
+ self._mute_item = gtk.MenuItem(self._mute_item_text())
+ self._mute_item.connect('activate', self._cb_mute_activate)
+ self.menu.append(self._mute_item)
+ self._mute_item.show()
+
+ self.set_content(vbox)
+
+ self._adjustment.connect("value_changed", self._cb_adjustment_changed)
+ self.connect('popup', self._cb_popup)
+
+ def _mute_item_text(self):
+ mute_item_text = self._model.props.muted and 'Unmute' or 'Mute'
+ mute_item_text += '...'
+ return _(mute_item_text)
+
+ def _cb_adjustment_changed(self, adj_):
+ self._model.props.level = self._adjustment.value
+
+ def _cb_popup(self, palette_):
+ if self._adjustment.value != self._model.props.level:
+ self._adjustment.value = self._model.props.level
+ if self._mute_item.get_child().get_text() != self._mute_item_text():
+ self._mute_item.get_child().set_text(self._mute_item_text())
+
+ def _cb_mute_activate(self, menuitem_):
+ self._model.props.muted = not self._model.props.muted
--
1.5.4.1
More information about the Sugar
mailing list