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