cruxports/deadbeef-alarm/alarm.cc

196 lines
5.9 KiB
C++
Raw Normal View History

/*
* adapted from the work of Adam Feakin <adamf@snika.uklinux.net>
* and Daniel Stodden <stodden@in.tum.de>
* for the audacious media player
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <glib.h>
#include <deadbeef/deadbeef.h>
#define DDB_WARN_DEPRECATED 1
#define trace(fmt,...)
static const char alarm_conf_dlg[] =
"property \"Hour\" hscale[0,23,1] alarm_conf_hour 6;\n"
"property \"Minute\" hscale[0,59,1] alarm_conf_min 30;\n"
"property \"Monday\" checkbox alarm_conf_day1 1;\n"
"property \"Tuesday\" checkbox alarm_conf_day2 1;\n"
"property \"Wednesday\" checkbox alarm_conf_day3 1;\n"
"property \"Thursday\" checkbox alarm_conf_day4 1;\n"
"property \"Friday\" checkbox alarm_conf_day5 1;\n"
"property \"Saturday\" checkbox alarm_conf_day6 0;\n"
"property \"Sunday\" checkbox alarm_conf_day0 0;\n"
"property \"volume\" hscale[0,100,1] alarm_conf_volume 60;\n"
"property \"playlist\n(leave empty to use current list)\" file alarm_conf_pls \"\";\n"
;
const char* pl_fallback = NULL;
static const char* conf_keys[] = {
"alarm_conf_day0",
"alarm_conf_day1",
"alarm_conf_day2",
"alarm_conf_day3",
"alarm_conf_day4",
"alarm_conf_day5",
"alarm_conf_day6" };
static DB_functions_t* deadbeef = NULL;
static int timeout_source;
static time_t play_start;
/* the main alarm thread */
static gboolean alarm_timeout (void * unused) {
struct tm *currtime;
time_t timenow;
unsigned today;
int alarm_h, alarm_m;
float newVolume;
int alarm_on;
timenow = time(nullptr);
currtime = localtime(&timenow);
today = currtime->tm_wday;
/* already went off? */
if (timenow < play_start + 60)
return true;
/* not activated today? */
alarm_on = deadbeef->conf_get_int(conf_keys[today],1);
if (alarm_on != 1)
return true;
else
{
alarm_h = deadbeef->conf_get_int("alarm_conf_hour",6);
alarm_m = deadbeef->conf_get_int("alarm_conf_min",30);
}
/* compare the current time with the alarm setting */
if ((currtime->tm_hour != alarm_h) || (currtime->tm_min != alarm_m))
return true;
/* none of the early exits were taken;
* it must be time to start playing something.
* First set the volume */
int confVolume = deadbeef->conf_get_int("alarm_conf_volume",60);
newVolume = ((float) confVolume/2)-50;
deadbeef->volume_set_db(newVolume);
/* Next try to load the requested playlist */
deadbeef->pl_select_all();
char pname[200];
deadbeef->conf_get_str("alarm_conf_pls",pl_fallback,pname,sizeof(pname));
trace("adding playlist %s\n", pname);
ddb_playlist_t *pl = deadbeef->plt_get_curr();
DB_playItem_t *after = deadbeef->pl_get_last (PL_MAIN);
if (pname) {
int ab=0;
DB_playItem_t *it;
it = deadbeef->plt_load2 (0, pl, after, pname, &ab, NULL, NULL);
if (!it) {
trace("failed to load playlist %s\n", pname);
}
else {
/* successfully loaded playlist, so delete previous contents */
deadbeef->pl_delete_selected();
}
}
if (after) {
deadbeef->pl_item_unref(after);
}
/* Start playing */
play_start=time(nullptr);
DB_playItem_t *first = deadbeef->pl_get_first (PL_MAIN);
int trackid = deadbeef->plt_get_item_idx (pl, first, PL_MAIN);
/* Avoid hard-coding trackid=1, in case random playorder is in effect */
deadbeef->sendmessage(DB_EV_PLAY_NUM,0,trackid,0);
if (first) {
deadbeef->pl_item_unref(first);
}
deadbeef->plt_unref(pl);
return true;
}
int onStart() {
timeout_source = g_timeout_add_seconds(12, alarm_timeout, nullptr);
return 0;
}
int onStop() {
if (timeout_source)
{
g_source_remove (timeout_source);
timeout_source = 0;
}
return 0;
}
// define plugin interface
static DB_misc_t plugin = {
.plugin = {
.type = DB_PLUGIN_MISC,
.api_vmajor = 1,
.api_vminor = 9,
.version_major = 0,
.version_minor = 1,
.id = "alarm",
2022-03-21 21:36:40 -04:00
.name = "Alarm Clock",
.descr = "load a playlist and start the music at a predetermined time",
.copyright = "Copyright (C) 2022 John McQuah <jmcquah@disroot.org>\n"
"\n"
"This program is free software; you can redistribute it and/or\n"
"modify it under the terms of the GNU General Public License\n"
"as published by the Free Software Foundation; either version 2\n"
"of the License, or (at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n",
2022-03-21 11:31:20 -04:00
.website = "https://git.sdf.org/jmq/cruxports/deadbeef-alarm",
.start = onStart,
.stop = onStop,
.connect = NULL,
.disconnect = NULL,
.configdialog = alarm_conf_dlg,
}
};
extern "C" DB_plugin_t *alarm_load (DB_functions_t *ddb) {
deadbeef = ddb;
return &plugin.plugin;
}