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