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