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/>.
29 #define xfree(x) if (x) free(x); x = 0
32 static timed_thread *moc_thread = NULL;
49 static void update_infos(void)
54 fp = popen("mocp -i", "r");
56 moc.state = strndup("Can't run 'mocp -i'", text_buffer_size);
64 /* Read a line from the pipe and strip the possible '\n'. */
65 if (!fgets(line, 100, fp))
67 if ((p = strrchr(line, '\n')))
71 if (strncmp(line, "State:", 6) == 0)
72 moc.state = strndup(line + 7, text_buffer_size);
73 else if (strncmp(line, "File:", 5) == 0)
74 moc.file = strndup(line + 6, text_buffer_size);
75 else if (strncmp(line, "Title:", 6) == 0)
76 moc.title = strndup(line + 7, text_buffer_size);
77 else if (strncmp(line, "Artist:", 7) == 0)
78 moc.artist = strndup(line + 8, text_buffer_size);
79 else if (strncmp(line, "SongTitle:", 10) == 0)
80 moc.song = strndup(line + 11, text_buffer_size);
81 else if (strncmp(line, "Album:", 6) == 0)
82 moc.album = strndup(line + 7, text_buffer_size);
83 else if (strncmp(line, "TotalTime:", 10) == 0)
84 moc.totaltime = strndup(line + 11, text_buffer_size);
85 else if (strncmp(line, "TimeLeft:", 9) == 0)
86 moc.timeleft = strndup(line + 10, text_buffer_size);
87 else if (strncmp(line, "CurrentTime:", 12) == 0)
88 moc.curtime = strndup(line + 13, text_buffer_size);
89 else if (strncmp(line, "Bitrate:", 8) == 0)
90 moc.bitrate = strndup(line + 9, text_buffer_size);
91 else if (strncmp(line, "Rate:", 5) == 0)
92 moc.rate = strndup(line + 6, text_buffer_size);
98 void *update_moc(void *) __attribute__((noreturn));
100 void *update_moc(void *arg)
105 timed_thread_lock(moc_thread);
107 timed_thread_unlock(moc_thread);
108 if (timed_thread_test(moc_thread, 0)) {
109 timed_thread_exit(moc_thread);
115 int run_moc_thread(double interval)
120 moc_thread = timed_thread_create(&update_moc, NULL, interval);
122 ERR("Failed to create MOC timed thread");
125 timed_thread_register(moc_thread, &moc_thread);
126 if (timed_thread_run(moc_thread)) {
127 ERR("Failed to run MOC timed thread");