Add fix for gmpc-live
[gmpc-lirc.git] / src / plugin.c
blob352365ac171072de234eb5ea3a0f17265333694d
1 /*
2 * vim:ts=4:sw=4
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include <glib.h>
7 #include <gmpc/plugin.h>
8 #include <lirc/lirc_client.h>
9 #include <libmpd/debug_printf.h>
11 #define SEEK_STEP_SIZE 5
12 int plugin_api_version = PLUGIN_API_VERSION;
15 * GMPC Plugin functions
16 * */
17 static int plugin_get_enabled(void);
18 static void plugin_set_enabled(int enabled);
20 static void plugin_init(void);
21 static void plugin_destroy(void);
23 gboolean lirc_handler(GIOChannel *source,
24 GIOCondition condition, gpointer data);
27 * LIRC vars and functions
28 * */
29 int lirc_fd = -1;
30 struct lirc_config *lirc_cfg = NULL;
31 GIOChannel *channel = NULL;
32 guint input_tag = 0;
35 * LIRC Plugin
36 * */
37 gmpcPlugin plugin = {
38 .name = "LIRC Plugin",
39 .version = {0,14,0},
40 .plugin_type = GMPC_PLUGIN_NO_GUI,
41 .init = plugin_init,
42 .destroy = plugin_destroy,
43 .get_enabled = plugin_get_enabled,
44 .set_enabled = plugin_set_enabled
48 * Plugin enabled get/set
49 * */
50 static int plugin_get_enabled(void)
52 return cfg_get_single_value_as_int_with_default(config, "lirc-plugin", "enable", TRUE);
55 static void plugin_set_enabled(int enabled)
57 cfg_set_single_value_as_int(config, "lirc-plugin", "enable", enabled);
61 * LIRC init and destroy
62 * */
64 static int init_lirc()
66 int err = 0;
68 lirc_fd = lirc_init("gmpc", 1);
69 if (lirc_fd == -1) {
70 debug_printf(DEBUG_INFO, "lirc_init() failed");
71 return 1;
74 err = lirc_readconfig(NULL, &lirc_cfg, NULL);
75 if (err == -1) {
76 lirc_deinit();
77 debug_printf(DEBUG_INFO, "lirc_readconfig() failed");
78 return 1;
81 return 0;
83 static void destroy_lirc()
85 if (lirc_cfg) {
86 lirc_freeconfig(lirc_cfg);
87 lirc_cfg = NULL;
89 if (lirc_fd != -1) {
90 lirc_deinit();
91 lirc_fd = -1;
95 static int init_channel()
97 GIOStatus status = G_IO_STATUS_NORMAL;
99 channel = g_io_channel_unix_new(lirc_fd);
100 if (channel == NULL) {
101 debug_printf(DEBUG_INFO, "g_io_channel_unix_new() failed");
102 return 1;
105 status = g_io_channel_set_flags(channel,
106 G_IO_FLAG_IS_READABLE | G_IO_FLAG_NONBLOCK, NULL);
107 if (status != G_IO_STATUS_NORMAL) {
108 debug_printf(DEBUG_INFO, "g_io_channel_set_flags() failed");
109 return 1;
112 input_tag = g_io_add_watch(channel, G_IO_IN,
113 lirc_handler, NULL);
114 if (input_tag < 0) {
115 debug_printf(DEBUG_INFO, "g_io_add_watch() failed");
116 return 1;
119 return 0;
121 static void destroy_channel()
123 if (input_tag) {
124 g_source_remove(input_tag);
125 input_tag = 0;
128 if (channel) {
129 g_io_channel_shutdown(channel, TRUE, NULL);
130 channel = NULL;
134 void plugin_init()
136 int err = 0;
138 err = init_lirc();
139 if (err) {
140 debug_printf(DEBUG_INFO, "init_lirc() failed");
141 return;
144 err = init_channel();
145 if (err) {
146 debug_printf(DEBUG_INFO, "init_channel() failed");
147 destroy_lirc();
148 return;
152 void plugin_destroy(void)
154 destroy_channel();
155 destroy_lirc();
158 gboolean lirc_handler(GIOChannel *source,
159 GIOCondition condition, gpointer data)
161 int err;
162 char *code, *c;
163 while((err = lirc_nextcode(&code))==0 && code!=NULL)
165 while((err = lirc_code2char(lirc_cfg, code, &c))==0 && c!=NULL)
167 if (strcasecmp("PLAYPAUSE",c) == 0) {
168 debug_printf(DEBUG_INFO, "PLAYPAUSE");
169 switch(mpd_player_get_state(connection))
171 case MPD_PLAYER_STOP:
172 mpd_player_play(connection);
173 break;
174 case MPD_PLAYER_PLAY:
175 case MPD_PLAYER_PAUSE:
176 mpd_player_pause(connection);
177 default:
178 /* do nothing */
179 break;
183 else if (strcasecmp("NEXT",c) == 0) {
184 mpd_player_next(connection);
186 else if (strcasecmp("PREV",c) == 0) {
187 mpd_player_prev(connection);
189 else if (strcasecmp("STOP",c) == 0) {
190 mpd_player_stop(connection);
192 else if (strcasecmp("FASTFORWARD",c) == 0) {
193 mpd_player_seek(connection, mpd_status_get_elapsed_song_time(connection)+SEEK_STEP_SIZE);
195 else if (strcasecmp("FASTBACKWARD",c) == 0) {
196 mpd_player_seek(connection, mpd_status_get_elapsed_song_time(connection)-SEEK_STEP_SIZE);
198 else if (strcasecmp("REPEAT",c) == 0) {
199 mpd_player_set_repeat(connection, !mpd_player_get_repeat(connection));
201 else if (strcasecmp("RANDOM",c) == 0) {
202 mpd_player_set_random(connection, !mpd_player_get_random(connection));
204 else if (strcasecmp("RAISE",c) == 0) {
205 pl3_show_window();
207 else if (strcasecmp("HIDE",c) == 0) {
208 pl3_hide();
210 else if (strcasecmp("TOGGLE_HIDDEN",c) == 0) {
211 pl3_toggle_hidden();
213 else if (strcasecmp("VOLUME_UP",c) == 0) {
214 mpd_status_set_volume(connection, mpd_status_get_volume(connection)+5);
216 else if (strcasecmp("VOLUME_DOWN",c) == 0) {
217 mpd_status_set_volume(connection, mpd_status_get_volume(connection)-5);
219 else if (strcasecmp("SHOW_NOTIFICATION",c) == 0) {
220 tray_icon2_create_tooltip();
225 return TRUE;