get_netwm_icon clean up.
[lxde/lxpanel.git] / src / plugins / volume / volume.c
CommitLineData
31b80004
JH
1#include <gtk/gtk.h>
2#include <stdlib.h>
3#include <fcntl.h>
4#include <unistd.h>
5
f277dbb7 6#include <glib/gi18n.h>
31b80004
JH
7#include <gdk-pixbuf/gdk-pixbuf.h>
8
9#include "panel.h"
10#include "misc.h"
11#include "plugin.h"
12
13#include "dbg.h"
14
15#include "volume-impl.h"
16
17#include "volume_xpm.h"
18
19int mixer_fd;
20
21typedef struct {
22 GtkWidget *mainw;
23 GtkWidget *dlg;
c6780e74 24 GtkTooltips* tooltips;
31b80004
JH
25} volume_t;
26
27static void
28volume_destructor(plugin *p)
29{
f277dbb7 30 volume_t *vol = (volume_t *) p->priv;
31b80004
JH
31
32 ENTER;
f277dbb7
HJYP
33 if (vol->dlg)
34 gtk_widget_destroy(vol->dlg);
c6780e74 35 g_object_unref( vol->tooltips );
f277dbb7 36 gtk_widget_destroy(vol->mainw);
31b80004
JH
37 if (mixer_fd)
38 close(mixer_fd);
f277dbb7 39 g_free(vol);
31b80004
JH
40 RET();
41}
42
f277dbb7 43static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, volume_t *vol)
31b80004
JH
44{
45 /* FIXME: use smarter method */
46 gtk_widget_destroy( dlg );
f277dbb7 47 vol->dlg = NULL;
31b80004
JH
48}
49
50static void on_button_press (GtkWidget* widget, GdkEventButton* evt, plugin* p)
51{
f277dbb7 52 volume_t *vol = (volume_t*) p->priv;
31b80004
JH
53
54 if( evt->button == 1 ) { /* Left click*/
f277dbb7
HJYP
55 if( ! vol->dlg ) {
56 vol->dlg = create_volume_window();
c6780e74
JH
57 g_signal_connect( vol->dlg, "focus-out-event",
58 G_CALLBACK(on_volume_focus), vol );
31b80004 59 }
f277dbb7 60 gtk_window_present( GTK_WINDOW(vol->dlg) );
31b80004
JH
61 }
62}
63
64static int
db449f6e 65volume_constructor(plugin *p, char **fp)
31b80004 66{
f277dbb7 67 volume_t *vol;
31b80004
JH
68 line s;
69 GdkPixbuf *icon;
70 GtkWidget *image;
c6780e74
JH
71 GtkIconTheme* theme;
72 GtkIconInfo* info;
31b80004
JH
73
74 ENTER;
75 s.len = 256;
f277dbb7
HJYP
76 vol = g_new0(volume_t, 1);
77 g_return_val_if_fail(vol != NULL, 0);
78 p->priv = vol;
31b80004
JH
79
80 /* check if OSS mixer device could be open */
81 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
82 if (mixer_fd < 0) {
83 RET(0);
84 }
85
f277dbb7
HJYP
86 vol->mainw = gtk_event_box_new();
87
c6780e74
JH
88 theme = gtk_icon_theme_get_default();
89 if ( (info = gtk_icon_theme_lookup_icon( theme, "stock_volume", 24, 0 ) ) ) {
90 icon = gdk_pixbuf_new_from_file_at_size(
91 gtk_icon_info_get_filename( info ),
92 24, 24, NULL );
93 gtk_icon_info_free( info );
94 }
95 else
96 icon = gdk_pixbuf_new_from_xpm_data((const char **) volume_xpm);
97 if (icon) {
31b80004 98 image = gtk_image_new_from_pixbuf(icon);
f277dbb7 99 gtk_container_add (GTK_CONTAINER (vol->mainw), image);
31b80004
JH
100 }
101
f277dbb7
HJYP
102 gtk_widget_add_events( vol->mainw, GDK_BUTTON_PRESS_MASK );
103 g_signal_connect( vol->mainw, "button-press-event",
31b80004 104 G_CALLBACK(on_button_press), p );
f277dbb7
HJYP
105 gtk_widget_set_size_request( vol->mainw, 24, 24 );
106
107 vol->dlg = NULL;
108 gtk_widget_show_all(vol->mainw);
31b80004 109
f277dbb7 110 gtk_container_add(GTK_CONTAINER(p->pwid), vol->mainw);
31b80004 111
c6780e74 112 vol->tooltips = gtk_tooltips_new ();
3806c5aa
HJYP
113#if GLIB_CHECK_VERSION( 2, 10, 0 )
114 g_object_ref_sink( vol->tooltips );
115#else
116 g_object_ref( vol->tooltips );
117 gtk_object_sink( vol->tooltips );
118#endif
119
c6780e74
JH
120 /* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
121 gtk_tooltips_set_tip (vol->tooltips, vol->mainw, _("Volume control"), NULL);
31b80004
JH
122
123 RET(1);
31b80004
JH
124}
125
126
127plugin_class volume_plugin_class = {
128 fname: NULL,
129 count: 0,
130
131 type : "volume",
ce522551 132 name : N_("Volume Control"),
31b80004
JH
133 version: "1.0",
134 description : "Display and control volume",
135
136 constructor : volume_constructor,
137 destructor : volume_destructor,
9c97f69e
HJYP
138 config : NULL,
139 save : NULL
31b80004 140};