[sugar] [PATCH] Add speaker device and icon by default
Martin Dengler
martin
Mon May 12 18:43:42 EDT 2008
Add speaker device and icon by default, as in
http://wiki.laptop.org/go/Designs/Frame#07
---
src/hardware/hardwaremanager.py | 31 +++++++++-
src/model/devices/Makefile.am | 4 +-
src/model/devices/devicesmodel.py | 8 +++
src/model/devices/speaker.py | 59 ++++++++++++++++++
src/view/devices/Makefile.am | 4 +-
src/view/devices/speaker.py | 119 +++++++++++++++++++++++++++++++++++++
6 files changed, 220 insertions(+), 5 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 5b9e330..284a283 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,9 +64,19 @@ class HardwareManager(object):
max_volume = self._master.max_volume
min_volume = self._master.min_volume
- volume = self._mixer.get_volume(self._master)[0]
- return volume * 100.0 / (max_volume - min_volume) + min_volume
+ volumes = self._mixer.get_volume(self._master)
+
+ #sometimes we get a spurious zero from one/more channel(s)
+ #TODO: consider removing this when trac #6933 is resolved
+ nonzero_volumes = [v for v in volumes if v > 0]
+
+ if len(nonzero_volumes) > 0:
+ #we could just pick the first nonzero volume, but this converges
+ volume = sum(nonzero_volumes) / len(nonzero_volumes)
+ return volume * 100.0 / (max_volume - min_volume) + min_volume
+ else:
+ return 0
def set_volume(self, volume):
if not self._mixer or not self._master:
@@ -76,7 +93,15 @@ class HardwareManager(object):
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 sets one/more channels' volume to zero instead
+ # of what we asked for, so try a few times
+ #TODO: consider removing this loop when trac #6934 is resolved
+ last_volumes_read = [0]
+ read_count = 0
+ while (0 in last_volumes_read) and (read_count < 3):
+ 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..e72a9d4 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,13 @@ class DevicesModel(gobject.GObject):
for udi in hal_manager.FindDeviceByCapability('battery'):
self.add_device(battery.Device(udi))
+ try:
+ self.add_device(speaker.Device())
+ except Exception, speaker_fail_msg:
+ logging.error("could not initialize speaker device: %s" %
+ speaker_fail_msg)
+ logging.debug(traceback.format_exc())
+
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..ce9ab0e
--- /dev/null
+++ b/src/view/devices/speaker.py
@@ -0,0 +1,119 @@
+# 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, Icon
+from sugar.graphics.menuitem import MenuItem
+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.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()
+
+ 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
+
+ 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, label=primary_text)
+
+ self._model = model
+
+ self.set_size_request(style.zoom(style.GRID_CELL_SIZE * 4), -1)
+
+ vbox = gtk.VBox()
+ self.set_content(vbox)
+ vbox.show()
+
+ 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)
+ vbox.add(self._hscale)
+ self._hscale.show()
+
+ self._mute_item = MenuItem('')
+ self._mute_icon = Icon(icon_size=gtk.ICON_SIZE_MENU)
+ self._mute_item.set_image(self._mute_icon)
+ self.menu.append(self._mute_item)
+ self._mute_item.show()
+
+ self._adjustment.connect('value_changed', self.__adjustment_changed_cb)
+ self._mute_item.connect('activate', self.__mute_activate_cb)
+ self.connect('popup', self.__popup_cb)
+
+ def _update_info(self):
+ if self._model.props.muted:
+ mute_item_text = _('Unmute')
+ mute_item_icon_name = 'dialog-ok'
+ else:
+ mute_item_text = _('Mute')
+ mute_item_icon_name = 'dialog-cancel'
+ self._mute_item.get_child().set_text(mute_item_text)
+ self._mute_icon.props.icon_name = mute_item_icon_name
+
+ def __adjustment_changed_cb(self, adj_):
+ self._model.props.level = self._adjustment.value
+
+ def __popup_cb(self, palette_):
+ if self._adjustment.value != self._model.props.level:
+ self._adjustment.value = self._model.props.level
+ self._update_info()
+
+ def __mute_activate_cb(self, menuitem_):
+ self._model.props.muted = not self._model.props.muted
--
1.5.3.3
More information about the Sugar-devel
mailing list