src/plugins/volume/*: New plugin to display and adjust volume.
[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
6#include <gdk-pixbuf/gdk-pixbuf.h>
7
8#include "panel.h"
9#include "misc.h"
10#include "plugin.h"
11
12#include "dbg.h"
13
14#include "volume-impl.h"
15
16#include "volume_xpm.h"
17
18int mixer_fd;
19
20typedef struct {
21 GtkWidget *mainw;
22 GtkWidget *dlg;
23} volume_t;
24
25static void
26volume_destructor(plugin *p)
27{
28 volume_t *ns = (volume_t *) p->priv;
29
30 ENTER;
31 gtk_widget_destroy(ns->mainw);
32 if (mixer_fd)
33 close(mixer_fd);
34 g_free(ns);
35 RET();
36}
37
38static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, volume_t *ns)
39{
40 /* FIXME: use smarter method */
41 gtk_widget_destroy( dlg );
42 ns->dlg = NULL;
43}
44
45static void on_button_press (GtkWidget* widget, GdkEventButton* evt, plugin* p)
46{
47 volume_t *ns = (volume_t*) p->priv;
48
49 if( evt->button == 1 ) { /* Left click*/
50 if( ! ns->dlg ) {
51 ns->dlg = create_volume_window();
52 g_signal_connect( ns->dlg, "focus-out-event", on_volume_focus, ns );
53 }
54 gtk_window_present( GTK_WINDOW(ns->dlg) );
55 }
56}
57
58static int
59volume_constructor(plugin *p)
60{
61 volume_t *ns;
62 line s;
63 GdkPixbuf *icon;
64 GtkWidget *image;
65
66 ENTER;
67 s.len = 256;
68 ns = g_new0(volume_t, 1);
69 g_return_val_if_fail(ns != NULL, 0);
70 p->priv = ns;
71
72 /* check if OSS mixer device could be open */
73 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
74 if (mixer_fd < 0) {
75 RET(0);
76 }
77
78 ns->mainw = gtk_event_box_new();
79
80 icon = gdk_pixbuf_new_from_xpm_data(volume_xpm);
81 if(icon) {
82 image = gtk_image_new_from_pixbuf(icon);
83 gtk_container_add (GTK_CONTAINER (ns->mainw), image);
84 }
85
86 gtk_widget_add_events( ns->mainw, GDK_BUTTON_PRESS_MASK );
87 g_signal_connect( ns->mainw, "button-press-event",
88 G_CALLBACK(on_button_press), p );
89 gtk_widget_set_size_request( ns->mainw, 24, 24 );
90
91 gtk_widget_show_all(ns->mainw);
92
93 gtk_container_add(GTK_CONTAINER(p->pwid), ns->mainw);
94
95 RET(1);
96
97error:
98 volume_destructor(p);
99 RET(0);
100}
101
102
103plugin_class volume_plugin_class = {
104 fname: NULL,
105 count: 0,
106
107 type : "volume",
108 name : "volume",
109 version: "1.0",
110 description : "Display and control volume",
111
112 constructor : volume_constructor,
113 destructor : volume_destructor,
114};