1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * MOC Conky integration
5 * Please see COPYING for details
7 * Copyright (c) 2008, Henri Häkkinen
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * vim: ts=4 sw=4 noet ai cindent syntax=c
33 #define xfree(x) if (x) free(x); x = 0
36 static timed_thread *moc_thread = NULL;
53 static void update_infos(void)
58 fp = popen("mocp -i", "r");
60 moc.state = strndup("Can't run 'mocp -i'", text_buffer_size);
68 /* Read a line from the pipe and strip the possible '\n'. */
69 if (!fgets(line, 100, fp))
71 if ((p = strrchr(line, '\n')))
75 if (strncmp(line, "State:", 6) == 0)
76 moc.state = strndup(line + 7, text_buffer_size);
77 else if (strncmp(line, "File:", 5) == 0)
78 moc.file = strndup(line + 6, text_buffer_size);
79 else if (strncmp(line, "Title:", 6) == 0)
80 moc.title = strndup(line + 7, text_buffer_size);
81 else if (strncmp(line, "Artist:", 7) == 0)
82 moc.artist = strndup(line + 8, text_buffer_size);
83 else if (strncmp(line, "SongTitle:", 10) == 0)
84 moc.song = strndup(line + 11, text_buffer_size);
85 else if (strncmp(line, "Album:", 6) == 0)
86 moc.album = strndup(line + 7, text_buffer_size);
87 else if (strncmp(line, "TotalTime:", 10) == 0)
88 moc.totaltime = strndup(line + 11, text_buffer_size);
89 else if (strncmp(line, "TimeLeft:", 9) == 0)
90 moc.timeleft = strndup(line + 10, text_buffer_size);
91 else if (strncmp(line, "CurrentTime:", 12) == 0)
92 moc.curtime = strndup(line + 13, text_buffer_size);
93 else if (strncmp(line, "Bitrate:", 8) == 0)
94 moc.bitrate = strndup(line + 9, text_buffer_size);
95 else if (strncmp(line, "Rate:", 5) == 0)
96 moc.rate = strndup(line + 6, text_buffer_size);
102 static void *update_moc_loop(void *) __attribute__((noreturn));
104 static void *update_moc_loop(void *arg)
109 timed_thread_lock(moc_thread);
111 timed_thread_unlock(moc_thread);
112 if (timed_thread_test(moc_thread, 0)) {
113 timed_thread_exit(moc_thread);
119 static int run_moc_thread(double interval)
124 moc_thread = timed_thread_create(&update_moc_loop, NULL, interval);
126 NORM_ERR("Failed to create MOC timed thread");
129 timed_thread_register(moc_thread, &moc_thread);
130 if (timed_thread_run(moc_thread)) {
131 NORM_ERR("Failed to run MOC timed thread");
137 void update_moc(void)
139 run_moc_thread(info.music_player_interval * 100000);