Set volume directly from python (requires gst-python).

Cleanups.
This commit is contained in:
Marco Pesenti Gritti 2007-06-13 16:22:49 +02:00
parent 396fcb392c
commit 42385b069f
8 changed files with 56 additions and 277 deletions

View File

@ -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) 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, gtk+-2.0)
PKG_CHECK_MODULES(LIB_BINDINGS, pygtk-2.0) PKG_CHECK_MODULES(LIB_BINDINGS, pygtk-2.0)

View File

@ -22,8 +22,6 @@ _extensions_la_SOURCES = \
$(BUILT_SOURCES) \ $(BUILT_SOURCES) \
eggaccelerators.h \ eggaccelerators.h \
eggaccelerators.c \ eggaccelerators.c \
sugar-audio-manager.c \
sugar-audio-manager.h \
sugar-key-grabber.h \ sugar-key-grabber.h \
sugar-key-grabber.c \ sugar-key-grabber.c \
_extensionsmodule.c _extensionsmodule.c

View File

@ -8,13 +8,6 @@
(gtype-id "SUGAR_TYPE_KEY_GRABBER") (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 ... ;; Enumerations and flags ...
;; From sugar-key-grabber.h ;; From sugar-key-grabber.h
@ -42,19 +35,3 @@
'("guint" "state") '("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")
)
)

View File

@ -5,7 +5,6 @@ headers
#include "pygobject.h" #include "pygobject.h"
#include "sugar-key-grabber.h" #include "sugar-key-grabber.h"
#include "sugar-audio-manager.h"
%% %%
modulename extensions modulename extensions

View File

@ -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 <gst/gst.h>
#include <gst/audio/mixerutils.h>
#include <gst/interfaces/mixer.h>
#include <gst/interfaces/propertyprobe.h>
#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);
}

View File

@ -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 <glib-object.h>
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__ */

View File

@ -17,9 +17,10 @@
import logging import logging
import dbus import dbus
import gst
import gst.interfaces
from hardware.nmclient import NMClient from hardware.nmclient import NMClient
from extensions import AudioManager
_HARDWARE_MANAGER_INTERFACE = 'org.laptop.HardwareManager' _HARDWARE_MANAGER_INTERFACE = 'org.laptop.HardwareManager'
_HARDWARE_MANAGER_SERVICE = 'org.laptop.HardwareManager' _HARDWARE_MANAGER_SERVICE = 'org.laptop.HardwareManager'
@ -30,18 +31,52 @@ B_AND_W_MODE = 1
class HardwareManager(object): class HardwareManager(object):
def __init__(self): def __init__(self):
bus = dbus.SystemBus() try:
proxy = bus.get_object(_HARDWARE_MANAGER_SERVICE, bus = dbus.SystemBus()
_HARDWARE_MANAGER_OBJECT_PATH) proxy = bus.get_object(_HARDWARE_MANAGER_SERVICE,
self._service = dbus.Interface(proxy, _HARDWARE_MANAGER_INTERFACE) _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): def set_display_mode(self, mode):
if not self._service:
return
self._service.set_display_mode(mode) self._service.set_display_mode(mode)
def set_display_brightness(self, level): def set_display_brightness(self, level):
if not self._service:
return
self._service.set_display_brightness(level) self._service.set_display_brightness(level)
def toggle_keyboard_brightness(self): def toggle_keyboard_brightness(self):
if not self._service:
return
if self._service.get_keyboard_brightness(): if self._service.get_keyboard_brightness():
self._service.set_keyboard_brightness(False) self._service.set_keyboard_brightness(False)
else: else:
@ -50,19 +85,10 @@ class HardwareManager(object):
def get_hardware_manager(): def get_hardware_manager():
return _hardware_manager return _hardware_manager
def get_audio_manager():
return _audio_manager
def get_network_manager(): def get_network_manager():
return _network_manager return _network_manager
_audio_manager = AudioManager() _hardware_manager = HardwareManager()
try:
_hardware_manager = HardwareManager()
except dbus.DBusException, e:
_hardware_manager = None
logging.info('Hardware manager service not found.')
try: try:
_network_manager = NMClient() _network_manager = NMClient()

View File

@ -47,7 +47,6 @@ _actions_table = {
class KeyHandler(object): class KeyHandler(object):
def __init__(self, shell): def __init__(self, shell):
self._shell = shell self._shell = shell
self._audio_manager = hardwaremanager.get_audio_manager()
self._screen_rotation = 0 self._screen_rotation = 0
self._key_pressed = None self._key_pressed = None
self._keycode_pressed = 0 self._keycode_pressed = 0
@ -64,17 +63,15 @@ class KeyHandler(object):
def _set_display_brightness(self, level): def _set_display_brightness(self, level):
hw_manager = hardwaremanager.get_hardware_manager() hw_manager = hardwaremanager.get_hardware_manager()
if hw_manager: hw_manager.set_display_brightness(level)
hw_manager.set_display_brightness(level) if level == 0:
if level == 0: self._set_display_mode(hardwaremanager.B_AND_W_MODE)
self._set_display_mode(hardwaremanager.B_AND_W_MODE) else:
else: self._set_display_mode(hardwaremanager.COLOR_MODE)
self._set_display_mode(hardwaremanager.COLOR_MODE)
def _set_display_mode(self, mode): def _set_display_mode(self, mode):
hw_manager = hardwaremanager.get_hardware_manager() 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): def handle_zoom_mesh(self):
self._shell.set_zoom_level(sugar.ZOOM_MESH) self._shell.set_zoom_level(sugar.ZOOM_MESH)
@ -101,16 +98,20 @@ class KeyHandler(object):
self._set_display_brightness(15) self._set_display_brightness(15)
def handle_volume_1(self): 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): 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): 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): 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): def handle_color_mode(self):
self._set_display_mode(hardwaremanager.COLOR_MODE) self._set_display_mode(hardwaremanager.COLOR_MODE)