udapted vi.po
[rhythmbox.git] / plugins / lirc / rb-lirc-plugin.c
blob9e8fa00404b8c78c1d34c78a75b70ef567e80cf0
1 /*
2 * rb-lirc-plugin.c
4 * Copyright (C) 2006 Jonathan Matthew
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <string.h>
26 #include <unistd.h>
28 #include <glib.h>
29 #include <glib-object.h>
30 #include <glib/gi18n-lib.h>
31 #include <gmodule.h>
32 #include <lirc/lirc_client.h>
34 #include "rb-plugin.h"
35 #include "rb-shell.h"
36 #include "rb-debug.h"
37 #include "rb-shell-player.h"
39 #define RB_TYPE_LIRC_PLUGIN (rb_lirc_plugin_get_type ())
40 #define RB_LIRC_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_LIRC_PLUGIN, RBLircPlugin))
41 #define RB_LIRC_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_LIRC_PLUGIN, RBLircPluginClass))
42 #define RB_IS_LIRC_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_LIRC_PLUGIN))
43 #define RB_IS_LIRC_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_LIRC_PLUGIN))
44 #define RB_LIRC_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_LIRC_PLUGIN, RBLircPluginClass))
46 #define RB_IR_COMMAND_PLAY "play"
47 #define RB_IR_COMMAND_PAUSE "pause"
48 #define RB_IR_COMMAND_PLAYPAUSE "playpause"
49 #define RB_IR_COMMAND_STOP "stop"
50 #define RB_IR_COMMAND_SHUFFLE "shuffle"
51 #define RB_IR_COMMAND_REPEAT "repeat"
52 #define RB_IR_COMMAND_NEXT "next"
53 #define RB_IR_COMMAND_PREVIOUS "previous"
54 #define RB_IR_COMMAND_SEEK_FORWARD "seek_forward"
55 #define RB_IR_COMMAND_SEEK_BACKWARD "seek_backward"
56 #define RB_IR_COMMAND_VOLUME_UP "volume_up"
57 #define RB_IR_COMMAND_VOLUME_DOWN "volume_down"
58 #define RB_IR_COMMAND_MUTE "mute"
60 typedef struct
62 RBPlugin parent;
63 RBShellPlayer *shell_player;
64 struct lirc_config *lirc_config;
65 GIOChannel *lirc_channel;
66 } RBLircPlugin;
68 typedef struct
70 RBPluginClass parent_class;
71 } RBLircPluginClass;
73 G_MODULE_EXPORT GType register_rb_plugin (GTypeModule *module);
74 GType rb_lirc_plugin_get_type (void) G_GNUC_CONST;
76 static void rb_lirc_plugin_init (RBLircPlugin *plugin);
77 static void impl_activate (RBPlugin *plugin, RBShell *shell);
78 static void impl_deactivate (RBPlugin *plugin, RBShell *shell);
80 RB_PLUGIN_REGISTER(RBLircPlugin, rb_lirc_plugin)
82 static void
83 rb_lirc_plugin_class_init (RBLircPluginClass *klass)
85 RBPluginClass *plugin_class = RB_PLUGIN_CLASS (klass);
86 plugin_class->activate = impl_activate;
87 plugin_class->deactivate = impl_deactivate;
90 static void
91 rb_lirc_plugin_init (RBLircPlugin *plugin)
93 rb_debug ("RBLircPlugin initialising");
96 static gboolean
97 rb_lirc_plugin_read_code (GIOChannel *source,
98 GIOCondition condition,
99 RBLircPlugin *plugin)
101 char *code;
102 char *str = NULL; /* owned by lirc config, must not be freed */
103 int ok;
104 gboolean processed = FALSE;
106 if (condition & (G_IO_ERR | G_IO_HUP)) {
107 /* TODO: retry after a minute? */
108 rb_debug ("LIRC connection broken. sorry.");
109 return FALSE;
112 lirc_nextcode (&code);
113 if (code == NULL) {
114 rb_debug ("Got incomplete lirc code");
115 return TRUE;
118 do {
119 ok = lirc_code2char (plugin->lirc_config, code, &str);
121 if (ok != 0) {
122 rb_debug ("couldn't convert lirc code \"%s\" to string", code);
123 } else if (str == NULL) {
124 if (processed == FALSE)
125 rb_debug ("unknown LIRC code \"%s\"", code);
126 break;
127 } else if (strcmp (str, RB_IR_COMMAND_PLAY) == 0) {
128 gboolean playing;
129 rb_shell_player_get_playing (plugin->shell_player, &playing, NULL);
130 if (playing == FALSE)
131 rb_shell_player_playpause (plugin->shell_player, FALSE, NULL);
132 } else if (strcmp (str, RB_IR_COMMAND_PAUSE) == 0) {
133 rb_shell_player_pause (plugin->shell_player, NULL);
134 } else if (strcmp (str, RB_IR_COMMAND_PLAYPAUSE) == 0) {
135 rb_shell_player_playpause (plugin->shell_player, FALSE, NULL);
136 } else if (strcmp (str, RB_IR_COMMAND_STOP) == 0) {
137 rb_shell_player_stop (plugin->shell_player);
138 } else if (strcmp (str, RB_IR_COMMAND_SHUFFLE) == 0) {
139 gboolean shuffle;
140 gboolean repeat;
141 if (rb_shell_player_get_playback_state (plugin->shell_player, &shuffle, &repeat)) {
142 rb_shell_player_set_playback_state (plugin->shell_player, !shuffle, repeat);
144 } else if (strcmp (str, RB_IR_COMMAND_REPEAT) == 0) {
145 gboolean shuffle;
146 gboolean repeat;
147 if (rb_shell_player_get_playback_state (plugin->shell_player, &shuffle, &repeat)) {
148 rb_shell_player_set_playback_state (plugin->shell_player, shuffle, !repeat);
150 } else if (strcmp (str, RB_IR_COMMAND_NEXT) == 0) {
151 rb_shell_player_do_next (plugin->shell_player, NULL);
152 } else if (strcmp (str, RB_IR_COMMAND_PREVIOUS) == 0) {
153 rb_shell_player_do_previous (plugin->shell_player, NULL);
154 } else if (strcmp (str, RB_IR_COMMAND_SEEK_FORWARD) == 0) {
155 rb_shell_player_seek (plugin->shell_player, 10);
156 } else if (strcmp (str, RB_IR_COMMAND_SEEK_BACKWARD) == 0) {
157 rb_shell_player_seek (plugin->shell_player, -10);
158 } else if (strcmp (str, RB_IR_COMMAND_VOLUME_UP) == 0) {
159 rb_shell_player_set_volume_relative (plugin->shell_player, 0.1, NULL);
160 } else if (strcmp (str, RB_IR_COMMAND_VOLUME_DOWN) == 0) {
161 rb_shell_player_set_volume_relative (plugin->shell_player, -0.1, NULL);
162 } else if (strcmp (str, RB_IR_COMMAND_MUTE) == 0) {
163 gboolean mute;
164 if (rb_shell_player_get_mute (plugin->shell_player, &mute, NULL)) {
165 rb_shell_player_set_mute (plugin->shell_player, !mute, NULL);
168 processed = TRUE;
169 } while (ok == 0);
170 g_free (code);
172 return TRUE;
175 static void
176 impl_activate (RBPlugin *rbplugin,
177 RBShell *shell)
179 int fd;
180 RBLircPlugin *plugin = RB_LIRC_PLUGIN (rbplugin);
182 g_object_get (G_OBJECT (shell), "shell-player", &plugin->shell_player, NULL);
184 rb_debug ("Activating lirc plugin");
186 fd = lirc_init ("Rhythmbox", 1);
187 if (fd < 0) {
188 rb_debug ("Couldn't initialize lirc");
189 return;
192 if (lirc_readconfig (NULL, &plugin->lirc_config, NULL) == -1) {
193 close (fd);
194 rb_debug ("Couldn't read lirc configuration");
195 return;
198 plugin->lirc_channel = g_io_channel_unix_new (fd);
199 g_io_channel_set_encoding (plugin->lirc_channel, NULL, NULL);
200 g_io_channel_set_buffered (plugin->lirc_channel, FALSE);
201 g_io_add_watch (plugin->lirc_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
202 (GIOFunc) rb_lirc_plugin_read_code, plugin);
205 static void
206 impl_deactivate (RBPlugin *rbplugin,
207 RBShell *shell)
209 RBLircPlugin *plugin = RB_LIRC_PLUGIN (rbplugin);
210 GError *error = NULL;
212 rb_debug ("Deactivating lirc plugin");
214 if (plugin->lirc_channel) {
215 g_io_channel_shutdown (plugin->lirc_channel, FALSE, &error);
216 if (error != NULL) {
217 g_warning ("Couldn't destroy lirc connection: %s",
218 error->message);
219 g_error_free (error);
221 plugin->lirc_channel = NULL;
224 if (plugin->lirc_config) {
225 lirc_freeconfig (plugin->lirc_config);
226 plugin->lirc_config = NULL;
228 lirc_deinit ();
231 if (plugin->shell_player) {
232 g_object_unref (G_OBJECT (plugin->shell_player));
233 plugin->shell_player = NULL;