JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
read stdin instead of polling a text file
[now-playing-d.git] / now-playing-d.c
1 /* now-playing-d.c
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #include <stdio.h>
19 #include <gtk/gtk.h>
20 #include "eggtrayicon.h"
21
22
23 gchar* playing[2];
24 int playing_i = 0;
25
26
27 // this gets called regularly by gtk
28 gboolean
29 stdin_callback(GIOChannel *channel, GIOCondition cond, gpointer data) {
30         GtkWidget *widget;
31         GIOStatus ret;
32         gsize length;
33         gsize terminator_pos;
34         GError *error = NULL;
35
36         widget = GTK_WIDGET(data);
37
38         if (widget->window == NULL) {
39                 return FALSE;
40         }
41
42         ret = g_io_channel_read_line(
43                 channel,
44                 &(playing[playing_i]),
45                 &length,
46                 &terminator_pos,
47                 &error);
48
49         if (ret == G_IO_STATUS_ERROR) {
50                 g_error ("Error reading: %s\n", error->message);
51                 gtk_exit(2);
52         }
53         if (length == 0) {
54                 gtk_exit(0);
55         }
56
57         // remove terminator
58         if(terminator_pos) {
59                 playing[playing_i][terminator_pos] = 0;
60         }
61
62
63         if(strcmp(playing[0], playing[1])) {
64                 gtk_label_set_text(GTK_LABEL(widget), playing[playing_i]);
65         }
66
67         playing_i = 1 - playing_i;
68         g_free(playing[playing_i]);
69
70         // gtk_widget_queue_draw(widget);
71         return TRUE;
72 }
73
74
75 int
76 main(int argc, char *argv[]) {
77         GtkWidget *label;
78         EggTrayIcon *egg;
79         GIOChannel *channel;
80
81         // initialize GTK
82         gtk_set_locale();
83         gtk_init(&argc, &argv);
84
85         // initialize globals
86         playing_i = 0;
87         playing[1] = g_strdup("");
88
89         // make the widget
90         label = gtk_label_new("");
91         egg = egg_tray_icon_new("what's this string do?");
92         // nest and display the widget
93         gtk_container_add(GTK_CONTAINER(egg), label);
94         gtk_widget_show_all(GTK_WIDGET(egg));
95
96         // watch stdin
97         channel = g_io_channel_unix_new(fileno(stdin));
98         g_io_channel_set_encoding(channel, NULL, NULL); // kiss
99         g_io_add_watch(channel, G_IO_IN, stdin_callback, (gpointer)label);
100
101         gtk_main();
102 }