Fix removing the config and sending a SIGUSR1 results in segfault
[monky] / src / moc.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * MOC Conky integration
5  *
6  * Please see COPYING for details
7  *
8  * Copyright (c) 2008, Henri Häkkinen
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "conky.h"
25 #include "logging.h"
26 #include "moc.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define xfree(x) if (x) free(x); x = 0
33
34 struct moc_s moc;
35 static timed_thread *moc_thread = NULL;
36
37 void free_moc(void)
38 {
39         xfree(moc.state);
40         xfree(moc.file);
41         xfree(moc.title);
42         xfree(moc.artist);
43         xfree(moc.song);
44         xfree(moc.album);
45         xfree(moc.totaltime);
46         xfree(moc.timeleft);
47         xfree(moc.curtime);
48         xfree(moc.bitrate);
49         xfree(moc.rate);
50 }
51
52 static void update_infos(void)
53 {
54         FILE *fp;
55
56         free_moc();
57         fp = popen("mocp -i", "r");
58         if (!fp) {
59                 moc.state = strndup("Can't run 'mocp -i'", text_buffer_size);
60                 return;
61         }
62
63         while (1) {
64                 char line[100];
65                 char *p;
66
67                 /* Read a line from the pipe and strip the possible '\n'. */
68                 if (!fgets(line, 100, fp))
69                         break;
70                 if ((p = strrchr(line, '\n')))
71                         *p = '\0';
72
73                 /* Parse infos. */
74                 if (strncmp(line, "State:", 6) == 0)
75                         moc.state = strndup(line + 7, text_buffer_size);
76                 else if (strncmp(line, "File:", 5) == 0)
77                         moc.file = strndup(line + 6, text_buffer_size);
78                 else if (strncmp(line, "Title:", 6) == 0)
79                         moc.title = strndup(line + 7, text_buffer_size);
80                 else if (strncmp(line, "Artist:", 7) == 0)
81                         moc.artist = strndup(line + 8, text_buffer_size);
82                 else if (strncmp(line, "SongTitle:", 10) == 0)
83                         moc.song = strndup(line + 11, text_buffer_size);
84                 else if (strncmp(line, "Album:", 6) == 0)
85                         moc.album = strndup(line + 7, text_buffer_size);
86                 else if (strncmp(line, "TotalTime:", 10) == 0)
87                         moc.totaltime = strndup(line + 11, text_buffer_size);
88                 else if (strncmp(line, "TimeLeft:", 9) == 0)
89                         moc.timeleft = strndup(line + 10, text_buffer_size);
90                 else if (strncmp(line, "CurrentTime:", 12) == 0)
91                         moc.curtime = strndup(line + 13, text_buffer_size);
92                 else if (strncmp(line, "Bitrate:", 8) == 0)
93                         moc.bitrate = strndup(line + 9, text_buffer_size);
94                 else if (strncmp(line, "Rate:", 5) == 0)
95                         moc.rate = strndup(line + 6, text_buffer_size);
96         }
97
98         pclose(fp);
99 }
100
101 static void *update_moc_loop(void *) __attribute__((noreturn));
102
103 static void *update_moc_loop(void *arg)
104 {
105         (void)arg;
106
107         while (1) {
108                 timed_thread_lock(moc_thread);
109                 update_infos();
110                 timed_thread_unlock(moc_thread);
111                 if (timed_thread_test(moc_thread, 0)) {
112                         timed_thread_exit(moc_thread);
113                 }
114         }
115         /* never reached */
116 }
117
118 static int run_moc_thread(double interval)
119 {
120         if (moc_thread)
121                 return 0;
122
123         moc_thread = timed_thread_create(&update_moc_loop, NULL, interval);
124         if (!moc_thread) {
125                 NORM_ERR("Failed to create MOC timed thread");
126                 return 1;
127         }
128         timed_thread_register(moc_thread, &moc_thread);
129         if (timed_thread_run(moc_thread)) {
130                 NORM_ERR("Failed to run MOC timed thread");
131                 return 2;
132         }
133         return 0;
134 }
135
136 void update_moc(void)
137 {
138         run_moc_thread(info.music_player_interval * 100000);
139 }