1 /* MOC Conky integration
3 * Please see COPYING for details
5 * Copyright (c) 2008, Henri Häkkinen
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * 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, see <http://www.gnu.org/licenses/>.
19 * vim: ts=4 sw=4 noet ai cindent syntax=c
31 #define xfree(x) if (x) free(x); x = 0
34 static timed_thread *moc_thread = NULL;
51 static void update_infos(void)
56 fp = popen("mocp -i", "r");
58 moc.state = strndup("Can't run 'mocp -i'", text_buffer_size);
66 /* Read a line from the pipe and strip the possible '\n'. */
67 if (!fgets(line, 100, fp))
69 if ((p = strrchr(line, '\n')))
73 if (strncmp(line, "State:", 6) == 0)
74 moc.state = strndup(line + 7, text_buffer_size);
75 else if (strncmp(line, "File:", 5) == 0)
76 moc.file = strndup(line + 6, text_buffer_size);
77 else if (strncmp(line, "Title:", 6) == 0)
78 moc.title = strndup(line + 7, text_buffer_size);
79 else if (strncmp(line, "Artist:", 7) == 0)
80 moc.artist = strndup(line + 8, text_buffer_size);
81 else if (strncmp(line, "SongTitle:", 10) == 0)
82 moc.song = strndup(line + 11, text_buffer_size);
83 else if (strncmp(line, "Album:", 6) == 0)
84 moc.album = strndup(line + 7, text_buffer_size);
85 else if (strncmp(line, "TotalTime:", 10) == 0)
86 moc.totaltime = strndup(line + 11, text_buffer_size);
87 else if (strncmp(line, "TimeLeft:", 9) == 0)
88 moc.timeleft = strndup(line + 10, text_buffer_size);
89 else if (strncmp(line, "CurrentTime:", 12) == 0)
90 moc.curtime = strndup(line + 13, text_buffer_size);
91 else if (strncmp(line, "Bitrate:", 8) == 0)
92 moc.bitrate = strndup(line + 9, text_buffer_size);
93 else if (strncmp(line, "Rate:", 5) == 0)
94 moc.rate = strndup(line + 6, text_buffer_size);
100 void *update_moc(void *) __attribute__((noreturn));
102 void *update_moc(void *arg)
107 timed_thread_lock(moc_thread);
109 timed_thread_unlock(moc_thread);
110 if (timed_thread_test(moc_thread, 0)) {
111 timed_thread_exit(moc_thread);
117 int run_moc_thread(double interval)
122 moc_thread = timed_thread_create(&update_moc, NULL, interval);
124 ERR("Failed to create MOC timed thread");
127 timed_thread_register(moc_thread, &moc_thread);
128 if (timed_thread_run(moc_thread)) {
129 ERR("Failed to run MOC timed thread");