here comes the big header include rewrite
[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  */
20
21 #include "conky.h"
22 #include "logging.h"
23 #include "moc.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 void init_moc(struct moc_s *moc)
30 {
31         moc->state = NULL;
32         moc->file = NULL;
33         moc->title = NULL;
34         moc->artist = NULL;
35         moc->song = NULL;
36         moc->album = NULL;
37         moc->totaltime = NULL;
38         moc->timeleft = NULL;
39         moc->curtime = NULL;
40         moc->bitrate = NULL;
41         moc->rate = NULL;
42 }
43
44 static void update_infos(struct moc_s *moc)
45 {
46         FILE *fp;
47
48         free_moc(moc);
49         fp = popen("mocp -i", "r");
50         if (!fp) {
51                 moc->state = strndup("Can't run 'mocp -i'", text_buffer_size);
52                 return;
53         }
54
55         while (1) {
56                 char line[100];
57                 char *p;
58
59                 /* Read a line from the pipe and strip the possible '\n'. */
60                 if (!fgets(line, 100, fp))
61                         break;
62                 if ((p = strrchr(line, '\n')))
63                         *p = '\0';
64
65                 /* Parse infos. */
66                 if (strncmp(line, "State:", 6) == 0)
67                         moc->state = strndup(line + 7, text_buffer_size);
68                 else if (strncmp(line, "File:", 5) == 0)
69                         moc->file = strndup(line + 6, text_buffer_size);
70                 else if (strncmp(line, "Title:", 6) == 0)
71                         moc->title = strndup(line + 7, text_buffer_size);
72                 else if (strncmp(line, "Artist:", 7) == 0)
73                         moc->artist = strndup(line + 8, text_buffer_size);
74                 else if (strncmp(line, "SongTitle:", 10) == 0)
75                         moc->song = strndup(line + 11, text_buffer_size);
76                 else if (strncmp(line, "Album:", 6) == 0)
77                         moc->album = strndup(line + 7, text_buffer_size);
78                 else if (strncmp(line, "TotalTime:", 10) == 0)
79                         moc->totaltime = strndup(line + 11, text_buffer_size);
80                 else if (strncmp(line, "TimeLeft:", 9) == 0)
81                         moc->timeleft = strndup(line + 10, text_buffer_size);
82                 else if (strncmp(line, "CurrentTime:", 12) == 0)
83                         moc->curtime = strndup(line + 13, text_buffer_size);
84                 else if (strncmp(line, "Bitrate:", 8) == 0)
85                         moc->bitrate = strndup(line + 9, text_buffer_size);
86                 else if (strncmp(line, "Rate:", 5) == 0)
87                         moc->rate = strndup(line + 6, text_buffer_size);
88         }
89
90         pclose(fp);
91 }
92
93 void *update_moc(void *arg)
94 {
95         struct moc_s *moc;
96
97         if (arg == NULL) {
98                 CRIT_ERR("update_moc called with a null argument!");
99         }
100
101         moc = (struct moc_s *) arg;
102
103         while (1) {
104                 timed_thread_lock(moc->timed_thread);
105                 update_infos(moc);
106                 timed_thread_unlock(moc->timed_thread);
107                 if (timed_thread_test(moc->timed_thread, 0)) {
108                         timed_thread_exit(moc->timed_thread);
109                 }
110         }
111         /* never reached */
112 }
113
114 void free_moc(struct moc_s *moc)
115 {
116         free(moc->state);
117         free(moc->file);
118         free(moc->title);
119         free(moc->artist);
120         free(moc->song);
121         free(moc->album);
122         free(moc->totaltime);
123         free(moc->timeleft);
124         free(moc->curtime);
125         free(moc->bitrate);
126         free(moc->rate);
127
128         init_moc(moc);
129 }
130