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
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
30 struct lirc_config
*lirc_cfg
= NULL
;
31 GIOChannel
*channel
= NULL
;
38 .name
= "LIRC Plugin",
40 .plugin_type
= GMPC_PLUGIN_NO_GUI
,
42 .destroy
= plugin_destroy
,
43 .get_enabled
= plugin_get_enabled
,
44 .set_enabled
= plugin_set_enabled
48 * Plugin enabled get/set
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
64 static int init_lirc()
68 lirc_fd
= lirc_init("gmpc", 1);
70 debug_printf(DEBUG_INFO
, "lirc_init() failed");
74 err
= lirc_readconfig(NULL
, &lirc_cfg
, NULL
);
77 debug_printf(DEBUG_INFO
, "lirc_readconfig() failed");
83 static void destroy_lirc()
86 lirc_freeconfig(lirc_cfg
);
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");
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");
112 input_tag
= g_io_add_watch(channel
, G_IO_IN
,
115 debug_printf(DEBUG_INFO
, "g_io_add_watch() failed");
121 static void destroy_channel()
124 g_source_remove(input_tag
);
129 g_io_channel_shutdown(channel
, TRUE
, NULL
);
140 debug_printf(DEBUG_INFO
, "init_lirc() failed");
144 err
= init_channel();
146 debug_printf(DEBUG_INFO
, "init_channel() failed");
152 void plugin_destroy(void)
158 gboolean
lirc_handler(GIOChannel
*source
,
159 GIOCondition condition
, gpointer data
)
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
);
174 case MPD_PLAYER_PLAY
:
175 case MPD_PLAYER_PAUSE
:
176 mpd_player_pause(connection
);
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) {
207 else if (strcasecmp("HIDE",c
) == 0) {
210 else if (strcasecmp("TOGGLE_HIDDEN",c
) == 0) {
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();