[cpu plugin] Added border to visual effects.
[lxde/lxpanel.git] / src / plugins / volume / volume.c
CommitLineData
4542c20d 1/**
a99ee9e1
JH
2 * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
31b80004
JH
19#include <gtk/gtk.h>
20#include <stdlib.h>
21#include <fcntl.h>
22#include <unistd.h>
23
f277dbb7 24#include <glib/gi18n.h>
31b80004
JH
25#include <gdk-pixbuf/gdk-pixbuf.h>
26
27#include "panel.h"
28#include "misc.h"
29#include "plugin.h"
30
31#include "dbg.h"
32
33#include "volume-impl.h"
34
35#include "volume_xpm.h"
460e2e78 36#undef const
31b80004
JH
37
38int mixer_fd;
39
40typedef struct {
4542c20d
HJYP
41 GtkWidget *mainw;
42 GtkWidget *dlg;
43 GtkTooltips* tooltips;
31b80004
JH
44} volume_t;
45
46static void
22242ed4 47volume_destructor(Plugin *p)
31b80004 48{
4542c20d
HJYP
49 volume_t *vol = (volume_t *) p->priv;
50
51 ENTER;
52 if (vol->dlg)
53 gtk_widget_destroy(vol->dlg);
54 g_object_unref( vol->tooltips );
55 gtk_widget_destroy(vol->mainw);
56 if (mixer_fd)
57 close(mixer_fd);
58 g_free(vol);
59 RET();
31b80004
JH
60}
61
f277dbb7 62static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, volume_t *vol)
31b80004 63{
4542c20d
HJYP
64 /* FIXME: use smarter method */
65 gtk_widget_destroy( dlg );
66 vol->dlg = NULL;
31b80004
JH
67}
68
5d54407c 69static int on_button_press (GtkWidget* widget, GdkEventButton* evt, Plugin* p)
31b80004 70{
4542c20d 71 volume_t *vol = (volume_t*) p->priv;
31b80004 72
4542c20d
HJYP
73 if( evt->button == 1 ) { /* Left click*/
74 if( ! vol->dlg ) {
75 vol->dlg = create_volume_window();
cecb0eaa 76
4542c20d
HJYP
77 /* setting background to default */
78 gtk_widget_set_style(vol->dlg, p->panel->defstyle);
cecb0eaa 79
4542c20d
HJYP
80 g_signal_connect( vol->dlg, "focus-out-event",
81 G_CALLBACK(on_volume_focus), vol );
cecb0eaa 82
4542c20d
HJYP
83 gtk_window_present( GTK_WINDOW(vol->dlg) );
84 } else {
85 gtk_widget_destroy(vol->dlg);
86 vol->dlg = NULL;
87 }
88 }
7414a73f
HJYP
89 else if( evt->button == 3 ) /* right button */
90 {
91 GtkMenu* popup = lxpanel_get_panel_menu( p->panel, p, FALSE );
92 gtk_menu_popup( popup, NULL, NULL, NULL, NULL, evt->button, evt->time );
93 return TRUE;
94 }
95 return FALSE;
31b80004
JH
96}
97
98static int
22242ed4 99volume_constructor(Plugin *p, char **fp)
31b80004 100{
4542c20d
HJYP
101 volume_t *vol;
102 line s;
103 GdkPixbuf *icon;
104 GtkWidget *image;
105 GtkIconTheme* theme;
106 GtkIconInfo* info;
107
108 ENTER;
109 s.len = 256;
110 vol = g_new0(volume_t, 1);
111 g_return_val_if_fail(vol != NULL, 0);
112 p->priv = vol;
113
114 /* check if OSS mixer device could be open */
115 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
116 if (mixer_fd < 0) {
117 RET(0);
118 }
119
120 vol->mainw = gtk_event_box_new();
121
122 theme = gtk_icon_theme_get_default();
123 if ( (info = gtk_icon_theme_lookup_icon( theme, "stock_volume", 24, 0 ) ) ) {
124 icon = gdk_pixbuf_new_from_file_at_size(
125 gtk_icon_info_get_filename( info ),
126 24, 24, NULL );
127 gtk_icon_info_free( info );
128 }
129 else
130 icon = gdk_pixbuf_new_from_xpm_data((const char **) volume_xpm);
131 if (icon) {
132 image = gtk_image_new_from_pixbuf(icon);
133 gtk_container_add (GTK_CONTAINER (vol->mainw), image);
134 }
135
136 gtk_widget_add_events( vol->mainw, GDK_BUTTON_PRESS_MASK );
137 g_signal_connect( vol->mainw, "button-press-event",
138 G_CALLBACK(on_button_press), p );
139 gtk_widget_set_size_request( vol->mainw, 24, 24 );
140
141 vol->dlg = NULL;
142 gtk_widget_show_all(vol->mainw);
143
144 vol->tooltips = gtk_tooltips_new ();
3806c5aa
HJYP
145#if GLIB_CHECK_VERSION( 2, 10, 0 )
146 g_object_ref_sink( vol->tooltips );
147#else
148 g_object_ref( vol->tooltips );
149 gtk_object_sink( vol->tooltips );
150#endif
151
4542c20d
HJYP
152 /* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
153 gtk_tooltips_set_tip (vol->tooltips, vol->mainw, _("Volume control"), NULL);
31b80004 154
4542c20d
HJYP
155 p->pwid = vol->mainw;
156 RET(1);
31b80004
JH
157}
158
159
22242ed4 160PluginClass volume_plugin_class = {
4542c20d
HJYP
161 fname: NULL,
162 count: 0,
31b80004 163
4542c20d 164 type : "volume",
ce522551 165 name : N_("Volume Control"),
4542c20d
HJYP
166 version: "1.0",
167 description : "Display and control volume",
31b80004 168
4542c20d
HJYP
169 constructor : volume_constructor,
170 destructor : volume_destructor,
9c97f69e
HJYP
171 config : NULL,
172 save : NULL
31b80004 173};