2010-01-07 03:45:19 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* MOC Conky integration
|
2008-09-24 20:53:58 +00:00
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008, Henri Häkkinen
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-09-24 20:56:09 +00:00
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2008-09-24 20:53:58 +00:00
|
|
|
|
|
|
|
#include "conky.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "logging.h"
|
2009-11-08 18:44:03 +00:00
|
|
|
#include "text_object.h"
|
2010-01-13 18:52:35 +00:00
|
|
|
#include "timed-thread.h"
|
2008-09-24 20:53:58 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-01-04 04:50:02 +00:00
|
|
|
#include <mutex>
|
2008-09-24 20:53:58 +00:00
|
|
|
|
2009-11-08 18:44:03 +00:00
|
|
|
static struct {
|
|
|
|
char *state;
|
|
|
|
char *file;
|
|
|
|
char *title;
|
|
|
|
char *artist;
|
|
|
|
char *song;
|
|
|
|
char *album;
|
|
|
|
char *totaltime;
|
|
|
|
char *timeleft;
|
|
|
|
char *curtime;
|
|
|
|
char *bitrate;
|
|
|
|
char *rate;
|
|
|
|
} moc;
|
|
|
|
|
2010-01-04 04:50:02 +00:00
|
|
|
static timed_thread_ptr moc_thread;
|
2008-12-22 18:31:48 +00:00
|
|
|
|
2009-11-08 18:44:03 +00:00
|
|
|
void free_moc(struct text_object *obj)
|
2008-09-24 20:53:58 +00:00
|
|
|
{
|
2009-11-08 18:44:03 +00:00
|
|
|
(void)obj;
|
2010-02-24 01:14:20 +00:00
|
|
|
free_and_zero(moc.state);
|
|
|
|
free_and_zero(moc.file);
|
|
|
|
free_and_zero(moc.title);
|
|
|
|
free_and_zero(moc.artist);
|
|
|
|
free_and_zero(moc.song);
|
|
|
|
free_and_zero(moc.album);
|
|
|
|
free_and_zero(moc.totaltime);
|
|
|
|
free_and_zero(moc.timeleft);
|
|
|
|
free_and_zero(moc.curtime);
|
|
|
|
free_and_zero(moc.bitrate);
|
|
|
|
free_and_zero(moc.rate);
|
2008-09-24 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:31:48 +00:00
|
|
|
static void update_infos(void)
|
2008-09-24 20:53:58 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
2009-11-08 18:44:03 +00:00
|
|
|
free_moc(NULL);
|
2008-09-24 20:53:58 +00:00
|
|
|
fp = popen("mocp -i", "r");
|
|
|
|
if (!fp) {
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.state = strndup("Can't run 'mocp -i'", text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
char line[100];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* Read a line from the pipe and strip the possible '\n'. */
|
|
|
|
if (!fgets(line, 100, fp))
|
|
|
|
break;
|
|
|
|
if ((p = strrchr(line, '\n')))
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* Parse infos. */
|
|
|
|
if (strncmp(line, "State:", 6) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.state = strndup(line + 7, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "File:", 5) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.file = strndup(line + 6, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "Title:", 6) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.title = strndup(line + 7, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "Artist:", 7) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.artist = strndup(line + 8, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "SongTitle:", 10) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.song = strndup(line + 11, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "Album:", 6) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.album = strndup(line + 7, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "TotalTime:", 10) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.totaltime = strndup(line + 11, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "TimeLeft:", 9) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.timeleft = strndup(line + 10, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "CurrentTime:", 12) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.curtime = strndup(line + 13, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "Bitrate:", 8) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.bitrate = strndup(line + 9, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
else if (strncmp(line, "Rate:", 5) == 0)
|
2010-08-29 21:50:32 +00:00
|
|
|
moc.rate = strndup(line + 6, text_buffer_size.get(*state));
|
2008-09-24 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pclose(fp);
|
|
|
|
}
|
|
|
|
|
2010-01-04 04:50:02 +00:00
|
|
|
static void update_moc_loop(thread_handle &handle)
|
2008-09-24 20:53:58 +00:00
|
|
|
{
|
|
|
|
while (1) {
|
2010-01-04 04:50:02 +00:00
|
|
|
{
|
2010-02-24 19:10:26 +00:00
|
|
|
std::lock_guard<std::mutex> lock(handle.mutex());
|
2010-01-04 04:50:02 +00:00
|
|
|
update_infos();
|
|
|
|
}
|
|
|
|
if (handle.test(0)) {
|
|
|
|
return;
|
2008-09-24 20:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* never reached */
|
|
|
|
}
|
|
|
|
|
2010-06-10 17:13:15 +00:00
|
|
|
static int run_moc_thread(std::chrono::microseconds interval)
|
2008-09-24 20:53:58 +00:00
|
|
|
{
|
2008-12-22 18:31:48 +00:00
|
|
|
if (moc_thread)
|
|
|
|
return 0;
|
|
|
|
|
2010-02-24 19:10:26 +00:00
|
|
|
moc_thread = timed_thread::create(std::bind(update_moc_loop, std::placeholders::_1), interval);
|
2008-12-22 18:31:48 +00:00
|
|
|
if (!moc_thread) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("Failed to create MOC timed thread");
|
2008-12-22 18:31:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-09-24 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 16:46:04 +00:00
|
|
|
int update_moc(void)
|
2009-09-06 22:14:54 +00:00
|
|
|
{
|
2010-09-11 12:45:19 +00:00
|
|
|
run_moc_thread(std::chrono::microseconds(long(music_player_interval.get(*state) * 1000000)));
|
2010-05-05 16:46:04 +00:00
|
|
|
return 0;
|
2009-09-06 22:14:54 +00:00
|
|
|
}
|
2009-11-08 18:44:03 +00:00
|
|
|
|
|
|
|
#define MOC_PRINT_GENERATOR(type, alt) \
|
|
|
|
void print_moc_##type(struct text_object *obj, char *p, int p_max_size) \
|
|
|
|
{ \
|
|
|
|
(void)obj; \
|
|
|
|
snprintf(p, p_max_size, "%s", (moc.type ? moc.type : alt)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
MOC_PRINT_GENERATOR(state, "??")
|
|
|
|
MOC_PRINT_GENERATOR(file, "no file")
|
|
|
|
MOC_PRINT_GENERATOR(title, "no title")
|
|
|
|
MOC_PRINT_GENERATOR(artist, "no artist")
|
|
|
|
MOC_PRINT_GENERATOR(song, "no song")
|
|
|
|
MOC_PRINT_GENERATOR(album, "no album")
|
|
|
|
MOC_PRINT_GENERATOR(totaltime, "0:00")
|
|
|
|
MOC_PRINT_GENERATOR(timeleft, "0:00")
|
|
|
|
MOC_PRINT_GENERATOR(curtime, "0:00")
|
|
|
|
MOC_PRINT_GENERATOR(bitrate, "0Kbps")
|
|
|
|
MOC_PRINT_GENERATOR(rate, "0KHz")
|
|
|
|
|
|
|
|
#undef MOC_PRINT_GENERATOR
|