1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
* Copyright (C) 2006-2008 Jim Huang <jserv.tw@gmail.com>
* 2006-2008 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
* 2008 Frank ENDRES <frank_endres@yahoo.fr>
* 2009 Marty Jack <martyj19@comcast.net>
* 2014 Andriy Grytsenko <andrej@rep.kiev.ua>
*
* This file is a part of LXPanel project.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <gtk/gtk.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <glib/gi18n.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "plugin.h"
#include "dbg.h"
#include "volume-impl.h"
#include "volume_xpm.h"
int mixer_fd;
GtkSpinButton *vol_spin;
static gdouble vol_before_mute;
static gdouble curr_volume;
static GtkWidget *curr_image;
static gboolean skip_botton1_event;
typedef struct {
LXPanel *panel;
GtkWidget *dlg;
} volume_t;
static void
volume_destructor(gpointer user_data)
{
volume_t *vol = (volume_t *) user_data;
ENTER;
if (vol->dlg)
gtk_widget_destroy(vol->dlg);
if (mixer_fd)
close(mixer_fd);
g_free(vol);
RET();
}
static void update_icon (GtkWidget *p, volume_t *vol)
{
GdkPixbuf *icon;
GtkWidget *image;
GtkIconTheme* theme;
GtkIconInfo* info;
int icon_size;
theme = panel_get_icon_theme(vol->panel);
icon_size = panel_get_icon_size(vol->panel);
if (curr_volume <= 0) {
info = gtk_icon_theme_lookup_icon( theme, "stock_volume-mute", icon_size, 0 );
}
else if (curr_volume > 0 && curr_volume <= 50) {
info = gtk_icon_theme_lookup_icon( theme, "stock_volume-min", icon_size, 0 );
}
else if (curr_volume > 50 && curr_volume <= 75) {
info = gtk_icon_theme_lookup_icon( theme, "stock_volume-med", icon_size, 0 );
}
else /* curr_volume > 75 */ {
info = gtk_icon_theme_lookup_icon( theme, "stock_volume-max", icon_size, 0 );
}
if (info ) {
icon = gdk_pixbuf_new_from_file_at_size(
gtk_icon_info_get_filename( info ),
icon_size, icon_size, NULL );
gtk_icon_info_free( info );
}
else {
icon = gdk_pixbuf_new_from_xpm_data((const char **) volume_xpm);
}
if (icon) {
if (curr_image) {
gtk_widget_destroy(curr_image);
curr_image = NULL;
}
image = gtk_image_new_from_pixbuf(icon);
gtk_container_add(GTK_CONTAINER(p), image);
curr_image = image;
}
gtk_widget_show_all(p);
return;
}
static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, GtkWidget *p)
{
volume_t *vol = lxpanel_plugin_get_data(p);
if (! vol_spin) return;
GtkAdjustment *vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
if (! vol_adjustment) return;
curr_volume = gtk_adjustment_get_value (vol_adjustment);
update_icon(p, vol);
/* FIXME: use smarter method */
gtk_widget_destroy( dlg );
vol->dlg = NULL;
}
static void on_mouse_scroll (GtkWidget* widget, GdkEventScroll* evt, volume_t *vol)
{
if ( ! vol->dlg ) {
vol->dlg = create_volume_window();
g_signal_connect( vol->dlg, "delete-event",
G_CALLBACK(on_volume_focus), widget );
}
else {
if (! vol_spin) return;
GtkAdjustment *vol_adjustment =
gtk_spin_button_get_adjustment (vol_spin);
if (! vol_adjustment) return;
curr_volume = gtk_adjustment_get_value (vol_adjustment);
if (evt->direction == GDK_SCROLL_UP) {
curr_volume += 2;
}
else /*if (evt->direction == GDK_SCROLL_DOWN)*/ {
curr_volume -= 2;
}
update_icon(widget, vol);
gtk_adjustment_set_value (vol_adjustment, curr_volume);
gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
skip_botton1_event = TRUE;
}
}
static gboolean on_button_press (GtkWidget* widget, GdkEventButton* evt, LXPanel* p)
{
volume_t *vol = lxpanel_plugin_get_data(widget);
/* for scroll correction */
if (skip_botton1_event) {
gtk_widget_destroy(vol->dlg);
vol->dlg = NULL;
skip_botton1_event = FALSE;
}
switch ( evt->button ) {
case 1: { /* Left click */
if ( ! vol->dlg ) {
vol->dlg = create_volume_window();
/* setting background to default */
gtk_widget_set_style(vol->dlg, panel_get_defstyle(p));
g_signal_connect( vol->dlg, "focus-out-event",
G_CALLBACK(on_volume_focus), widget );
gtk_window_present( GTK_WINDOW(vol->dlg) );
}
else {
/* update icon */
if (! vol_spin) return FALSE;
GtkAdjustment *vol_adjustment =
gtk_spin_button_get_adjustment (vol_spin);
if (! vol_adjustment) return FALSE;
curr_volume = gtk_adjustment_get_value (vol_adjustment);
update_icon(widget, vol);
gtk_widget_destroy(vol->dlg);
vol->dlg = NULL;
}
break;
}
case 2: { /* middle mouse button */
if ( ! vol->dlg ) {
vol->dlg = create_volume_window();
}
if (! vol_spin) return FALSE;
GtkAdjustment *vol_adjustment =
gtk_spin_button_get_adjustment (vol_spin);
if (! vol_adjustment) return FALSE;
curr_volume = gtk_adjustment_get_value (vol_adjustment);
if (curr_volume > 0) {
/* turning to mute */
vol_before_mute = curr_volume;
curr_volume = 0;
}
else {
curr_volume = vol_before_mute;
}
gtk_adjustment_set_value (vol_adjustment, curr_volume);
gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
update_icon(widget, vol);
gtk_widget_destroy( vol->dlg );
vol->dlg = NULL;
break;
}
default: /* never here */
break;
}
return FALSE;
}
static GtkWidget *volume_constructor(LXPanel *panel, config_setting_t *settings)
{
volume_t *vol;
GtkWidget *p;
GtkAdjustment *vol_adjustment;
vol_before_mute = 1;
curr_volume = 0;
curr_image = NULL;
skip_botton1_event = FALSE;
ENTER;
/* check if OSS mixer device could be open */
mixer_fd = open ("/dev/mixer", O_RDWR, 0);
if (mixer_fd < 0) {
RET(NULL);
}
/* try to obtain current volume */
p = create_volume_window(); /* use pointer */
if (! vol_spin)
goto _error;
vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
if (! vol_adjustment)
{
_error:
gtk_widget_destroy(p);
RET(NULL);
}
curr_volume = gtk_adjustment_get_value (vol_adjustment);
vol = g_new0(volume_t, 1);
vol->dlg = p; /* it was reused */
p = gtk_event_box_new();
lxpanel_plugin_set_data(p, vol, volume_destructor);
vol->panel = panel;
g_signal_connect(p, "scroll-event", G_CALLBACK(on_mouse_scroll), vol);
gtk_widget_set_size_request(p, panel_get_icon_size(panel), panel_get_icon_size(panel));
update_icon(p, vol);
gtk_widget_destroy( vol->dlg );
vol->dlg = NULL;
/* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
gtk_widget_set_tooltip_text(p, _("Volume control"));
RET(p);
}
FM_DEFINE_MODULE(lxpanel_gtk, volume)
LXPanelPluginInit fm_module_init_lxpanel_gtk = {
.name = N_("Volume Control"),
.description = "Display and control volume",
.new_instance = volume_constructor,
.button_press_event = on_button_press
};
|