update machinery: fix accidential naming conflict
[monky] / src / moc.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * MOC Conky integration
4  *
5  * Please see COPYING for details
6  *
7  * Copyright (c) 2008, Henri Häkkinen
8  *
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.
13  *
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/>.
20  *
21  * vim: ts=4 sw=4 noet ai cindent syntax=c
22  *
23  */
24
25 #include "conky.h"
26 #include "logging.h"
27 #include "moc.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define xfree(x) if (x) free(x); x = 0
34
35 struct moc_s moc;
36 static timed_thread *moc_thread = NULL;
37
38 void free_moc(void)
39 {
40         xfree(moc.state);
41         xfree(moc.file);
42         xfree(moc.title);
43         xfree(moc.artist);
44         xfree(moc.song);
45         xfree(moc.album);
46         xfree(moc.totaltime);
47         xfree(moc.timeleft);
48         xfree(moc.curtime);
49         xfree(moc.bitrate);
50         xfree(moc.rate);
51 }
52
53 static void update_infos(void)
54 {
55         FILE *fp;
56
57         free_moc();
58         fp = popen("mocp -i", "r");
59         if (!fp) {
60                 moc.state = strndup("Can't run 'mocp -i'", text_buffer_size);
61                 return;
62         }
63
64         while (1) {
65                 char line[100];
66                 char *p;
67
68                 /* Read a line from the pipe and strip the possible '\n'. */
69                 if (!fgets(line, 100, fp))
70                         break;
71                 if ((p = strrchr(line, '\n')))
72                         *p = '\0';
73
74                 /* Parse infos. */
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);
97         }
98
99         pclose(fp);
100 }
101
102 static void *update_moc_loop(void *) __attribute__((noreturn));
103
104 static void *update_moc_loop(void *arg)
105 {
106         (void)arg;
107
108         while (1) {
109                 timed_thread_lock(moc_thread);
110                 update_infos();
111                 timed_thread_unlock(moc_thread);
112                 if (timed_thread_test(moc_thread, 0)) {
113                         timed_thread_exit(moc_thread);
114                 }
115         }
116         /* never reached */
117 }
118
119 static int run_moc_thread(double interval)
120 {
121         if (moc_thread)
122                 return 0;
123
124         moc_thread = timed_thread_create(&update_moc_loop, NULL, interval);
125         if (!moc_thread) {
126                 NORM_ERR("Failed to create MOC timed thread");
127                 return 1;
128         }
129         timed_thread_register(moc_thread, &moc_thread);
130         if (timed_thread_run(moc_thread)) {
131                 NORM_ERR("Failed to run MOC timed thread");
132                 return 2;
133         }
134         return 0;
135 }
136
137 void update_moc(void)
138 {
139         run_moc_thread(info.music_player_interval * 100000);
140 }