From 42385b069f4251662f86ad4b305da49b74166654 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Wed, 13 Jun 2007 16:22:49 +0200 Subject: [PATCH] Set volume directly from python (requires gst-python). Cleanups. --- configure.ac | 2 +- shell/extensions/Makefile.am | 2 - shell/extensions/_extensions.defs | 23 ---- shell/extensions/_extensions.override | 1 - shell/extensions/sugar-audio-manager.c | 168 ------------------------- shell/extensions/sugar-audio-manager.h | 54 -------- shell/hardware/hardwaremanager.py | 56 ++++++--- shell/view/keyhandler.py | 27 ++-- 8 files changed, 56 insertions(+), 277 deletions(-) delete mode 100644 shell/extensions/sugar-audio-manager.c delete mode 100644 shell/extensions/sugar-audio-manager.h diff --git a/configure.ac b/configure.ac index 364e8fc2..53be98b1 100644 --- a/configure.ac +++ b/configure.ac @@ -17,7 +17,7 @@ AM_CHECK_PYTHON_HEADERS(,[AC_MSG_ERROR(could not find Python headers)]) AC_PATH_PROG(PYGTK_CODEGEN, pygtk-codegen-2.0, no) -PKG_CHECK_MODULES(SHELL, pygtk-2.0 gtk+-2.0 gstreamer-0.10 gstreamer-plugins-base-0.10) +PKG_CHECK_MODULES(SHELL, pygtk-2.0 gtk+-2.0 gst-python-0.10) PKG_CHECK_MODULES(LIB, gtk+-2.0) PKG_CHECK_MODULES(LIB_BINDINGS, pygtk-2.0) diff --git a/shell/extensions/Makefile.am b/shell/extensions/Makefile.am index a151f87d..0d12ab4f 100644 --- a/shell/extensions/Makefile.am +++ b/shell/extensions/Makefile.am @@ -22,8 +22,6 @@ _extensions_la_SOURCES = \ $(BUILT_SOURCES) \ eggaccelerators.h \ eggaccelerators.c \ - sugar-audio-manager.c \ - sugar-audio-manager.h \ sugar-key-grabber.h \ sugar-key-grabber.c \ _extensionsmodule.c diff --git a/shell/extensions/_extensions.defs b/shell/extensions/_extensions.defs index 4fec6ccb..850ebb7e 100644 --- a/shell/extensions/_extensions.defs +++ b/shell/extensions/_extensions.defs @@ -8,13 +8,6 @@ (gtype-id "SUGAR_TYPE_KEY_GRABBER") ) -(define-object AudioManager - (in-module "Sugar") - (parent "GObject") - (c-name "SugarAudioManager") - (gtype-id "SUGAR_TYPE_AUDIO_MANAGER") -) - ;; Enumerations and flags ... ;; From sugar-key-grabber.h @@ -42,19 +35,3 @@ '("guint" "state") ) ) - -;; From sugar-audio-manager.h - -(define-function audio_manager_get_type - (c-name "sugar_audio_manager_get_type") - (return-type "GType") -) - -(define-method set_volume - (of-object "SugarAudioManager") - (c-name "sugar_audio_manager_set_volume") - (return-type "none") - (parameters - '("int" "level") - ) -) diff --git a/shell/extensions/_extensions.override b/shell/extensions/_extensions.override index abc7644d..735867fe 100644 --- a/shell/extensions/_extensions.override +++ b/shell/extensions/_extensions.override @@ -5,7 +5,6 @@ headers #include "pygobject.h" #include "sugar-key-grabber.h" -#include "sugar-audio-manager.h" %% modulename extensions diff --git a/shell/extensions/sugar-audio-manager.c b/shell/extensions/sugar-audio-manager.c deleted file mode 100644 index 6f73cbc4..00000000 --- a/shell/extensions/sugar-audio-manager.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2006 Red Hat, Inc - * - * Sugar 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. - * - * Sugar 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. - */ - -#include -#include -#include -#include - -#include "sugar-audio-manager.h" - -struct _SugarAudioManagerPrivate -{ - GstMixer *mixer; - GstMixerTrack *track; - guint timer_id; -}; - -G_DEFINE_TYPE(SugarAudioManager, sugar_audio_manager, G_TYPE_OBJECT) - -#define SUGAR_AUDIO_MANAGER_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), SUGAR_TYPE_AUDIO_MANAGER, SugarAudioManagerPrivate)) - -/* This is a modified version of code from gnome-control-center */ - -static gboolean -mixer_close_real(SugarAudioManager *manager) -{ - if (manager->priv->mixer != NULL) - { - gst_element_set_state(GST_ELEMENT(manager->priv->mixer), GST_STATE_NULL); - gst_object_unref(GST_OBJECT(manager->priv->mixer)); - g_object_unref(G_OBJECT(manager->priv->track)); - manager->priv->mixer = NULL; - manager->priv->track = NULL; - } - - manager->priv->timer_id = 0; - - return FALSE; -} - -static gboolean -set_mixer_helper(GstMixer *mixer, gpointer user_data) -{ - const GList *tracks; - - tracks = gst_mixer_list_tracks(mixer); - - while (tracks != NULL) { - GstMixerTrack *track = GST_MIXER_TRACK(tracks->data); - - if (GST_MIXER_TRACK_HAS_FLAG(track, GST_MIXER_TRACK_MASTER)) { - SugarAudioManager *manager; - - manager = SUGAR_AUDIO_MANAGER(user_data); - - manager->priv->mixer = mixer; - manager->priv->track = track; - - /* no need to ref the mixer element */ - g_object_ref(manager->priv->track); - return TRUE; - } - - tracks = tracks->next; - } - - return FALSE; -} - -static gboolean -mixer_open(SugarAudioManager *manager) -{ - GList *mixer_list; - - if (manager->priv->timer_id != 0) { - g_source_remove (manager->priv->timer_id); - manager->priv->timer_id = 0; - return TRUE; - } - - mixer_list = gst_audio_default_registry_mixer_filter - (set_mixer_helper, TRUE, manager); - - if (mixer_list == NULL) - return FALSE; - - /* do not unref the mixer as we keep the ref for manager->priv->mixer */ - g_list_free (mixer_list); - - return TRUE; -} - -static void -mixer_close(SugarAudioManager *manager) -{ - manager->priv->timer_id = g_timeout_add (4000, (GSourceFunc)mixer_close_real, manager); -} - -void -sugar_audio_manager_set_volume (SugarAudioManager *manager, - int level) -{ - gint i, *volumes, volume; - GstMixerTrack *track; - - if (mixer_open(manager) == FALSE) - return; - - track = manager->priv->track; - volume = CLAMP(level, 0, 100); - - /* Rescale the volume from [0, 100] to [track min, track max]. */ - volume = (volume / 100.0) * (track->max_volume - track->min_volume) + - track->min_volume; - - volumes = g_new(gint, track->num_channels); - for (i = 0; i < track->num_channels; ++i) - volumes[i] = (gint)volume; - gst_mixer_set_volume(manager->priv->mixer, track, volumes); - g_free (volumes); - - mixer_close(manager); -} - -static void -sugar_audio_manager_finalize (GObject *object) -{ - SugarAudioManager *manager = SUGAR_AUDIO_MANAGER(object); - - mixer_close_real(manager); - - G_OBJECT_CLASS(sugar_audio_manager_parent_class)->finalize(object); -} - -static void -sugar_audio_manager_class_init(SugarAudioManagerClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS(klass); - - gst_init (NULL, NULL); - - object_class->finalize = sugar_audio_manager_finalize; - - g_type_class_add_private(klass, sizeof(SugarAudioManagerPrivate)); -} - -static void -sugar_audio_manager_init(SugarAudioManager *manager) -{ - manager->priv = SUGAR_AUDIO_MANAGER_GET_PRIVATE(manager); -} - - diff --git a/shell/extensions/sugar-audio-manager.h b/shell/extensions/sugar-audio-manager.h deleted file mode 100644 index ba72fa02..00000000 --- a/shell/extensions/sugar-audio-manager.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2006 Red Hat, Inc - * - * Sugar 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. - * - * Sugar 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. - */ - -#ifndef __SUGAR_AUDIO_MANAGER_H__ -#define __SUGAR_AUDIO_MANAGER_H__ - -#include - -G_BEGIN_DECLS - -typedef struct _SugarAudioManager SugarAudioManager; -typedef struct _SugarAudioManagerClass SugarAudioManagerClass; -typedef struct _SugarAudioManagerPrivate SugarAudioManagerPrivate; - -#define SUGAR_TYPE_AUDIO_MANAGER (sugar_audio_manager_get_type()) -#define SUGAR_AUDIO_MANAGER(object) (G_TYPE_CHECK_INSTANCE_CAST((object), SUGAR_TYPE_AUDIO_MANAGER, SugarAudioManager)) -#define SUGAR_AUDIO_MANAGER_CLASS(klass) (G_TYPE_CHACK_CLASS_CAST((klass), SUGAR_TYPE_AUDIO_MANAGER, SugarAudioManagerClass)) -#define SUGAR_IS_AUDIO_MANAGER(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), SUGAR_TYPE_AUDIO_MANAGER)) -#define SUGAR_IS_AUDIO_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), SUGAR_TYPE_AUDIO_MANAGER)) -#define SUGAR_AUDIO_MANAGER_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), SUGAR_TYPE_AUDIO_MANAGER, SugarAudioManagerClass)) - -struct _SugarAudioManager { - GObject base_instance; - - /*< private >*/ - SugarAudioManagerPrivate *priv; -}; - -struct _SugarAudioManagerClass { - GObjectClass base_class; -}; - -GType sugar_audio_manager_get_type (void); -void sugar_audio_manager_set_volume (SugarAudioManager *manager, - int level); - -G_END_DECLS - -#endif /* __SUGAR_AUDIO_MANAGER_H__ */ diff --git a/shell/hardware/hardwaremanager.py b/shell/hardware/hardwaremanager.py index 47129709..3e7066e0 100644 --- a/shell/hardware/hardwaremanager.py +++ b/shell/hardware/hardwaremanager.py @@ -17,9 +17,10 @@ import logging import dbus +import gst +import gst.interfaces from hardware.nmclient import NMClient -from extensions import AudioManager _HARDWARE_MANAGER_INTERFACE = 'org.laptop.HardwareManager' _HARDWARE_MANAGER_SERVICE = 'org.laptop.HardwareManager' @@ -30,18 +31,52 @@ B_AND_W_MODE = 1 class HardwareManager(object): def __init__(self): - bus = dbus.SystemBus() - proxy = bus.get_object(_HARDWARE_MANAGER_SERVICE, - _HARDWARE_MANAGER_OBJECT_PATH) - self._service = dbus.Interface(proxy, _HARDWARE_MANAGER_INTERFACE) + try: + bus = dbus.SystemBus() + proxy = bus.get_object(_HARDWARE_MANAGER_SERVICE, + _HARDWARE_MANAGER_OBJECT_PATH) + self._service = dbus.Interface(proxy, _HARDWARE_MANAGER_INTERFACE) + except dbus.DBusException, e: + self._service = None + logging.info('Hardware manager service not found.') + + self._mixer = gst.element_factory_make('alsamixer') + self._mixer.set_state(gst.STATE_PAUSED) + + + for track in self._mixer.list_tracks(): + if track.flags & gst.interfaces.MIXER_TRACK_MASTER: + self._mixer_master = track + + def set_volume(self, volume): + if volume < 0 or volume > 100: + logging.error('Trying to set an invalid volume value.') + return + + max_volume = self._mixer_master.max_volume + min_volume = self._mixer_master.min_volume + + volume = (volume / 100.0) * (max_volume - min_volume) + min_volume + volume_list = [ volume ] * self._mixer_master.num_channels + + self._mixer.set_volume(self._mixer_master, tuple(volume_list)) def set_display_mode(self, mode): + if not self._service: + return + self._service.set_display_mode(mode) def set_display_brightness(self, level): + if not self._service: + return + self._service.set_display_brightness(level) def toggle_keyboard_brightness(self): + if not self._service: + return + if self._service.get_keyboard_brightness(): self._service.set_keyboard_brightness(False) else: @@ -50,19 +85,10 @@ class HardwareManager(object): def get_hardware_manager(): return _hardware_manager -def get_audio_manager(): - return _audio_manager - def get_network_manager(): return _network_manager -_audio_manager = AudioManager() - -try: - _hardware_manager = HardwareManager() -except dbus.DBusException, e: - _hardware_manager = None - logging.info('Hardware manager service not found.') +_hardware_manager = HardwareManager() try: _network_manager = NMClient() diff --git a/shell/view/keyhandler.py b/shell/view/keyhandler.py index a47cc715..1e877941 100644 --- a/shell/view/keyhandler.py +++ b/shell/view/keyhandler.py @@ -47,7 +47,6 @@ _actions_table = { class KeyHandler(object): def __init__(self, shell): self._shell = shell - self._audio_manager = hardwaremanager.get_audio_manager() self._screen_rotation = 0 self._key_pressed = None self._keycode_pressed = 0 @@ -64,17 +63,15 @@ class KeyHandler(object): def _set_display_brightness(self, level): hw_manager = hardwaremanager.get_hardware_manager() - if hw_manager: - hw_manager.set_display_brightness(level) - if level == 0: - self._set_display_mode(hardwaremanager.B_AND_W_MODE) - else: - self._set_display_mode(hardwaremanager.COLOR_MODE) + hw_manager.set_display_brightness(level) + if level == 0: + self._set_display_mode(hardwaremanager.B_AND_W_MODE) + else: + self._set_display_mode(hardwaremanager.COLOR_MODE) def _set_display_mode(self, mode): hw_manager = hardwaremanager.get_hardware_manager() - if hw_manager: - hw_manager.set_display_mode(mode) + hw_manager.set_display_mode(mode) def handle_zoom_mesh(self): self._shell.set_zoom_level(sugar.ZOOM_MESH) @@ -101,16 +98,20 @@ class KeyHandler(object): self._set_display_brightness(15) def handle_volume_1(self): - self._audio_manager.set_volume(0) + hw_manager = hardwaremanager.get_hardware_manager() + hw_manager.set_volume(0) def handle_volume_2(self): - self._audio_manager.set_volume(50) + hw_manager = hardwaremanager.get_hardware_manager() + hw_manager.set_volume(50) def handle_volume_3(self): - self._audio_manager.set_volume(80) + hw_manager = hardwaremanager.get_hardware_manager() + hw_manager.set_volume(80) def handle_volume_4(self): - self._audio_manager.set_volume(100) + hw_manager = hardwaremanager.get_hardware_manager() + hw_manager.set_volume(100) def handle_color_mode(self): self._set_display_mode(hardwaremanager.COLOR_MODE)