Files
dwm/vene.cpp
T

162 lines
3.3 KiB
C++

#include <time.h>
#include <cstring>
#include <cstdlib>
#include <string>
#include <chrono>
#include <cppipe/commands.hpp>
// #include "vene.h"
#include "vene_env.h"
using namespace std;
using namespace std::chrono;
using namespace std::chrono_literals;
char* HOME;
char SCREEN_CAPS_DIR[64];
namespace
{
time_point<system_clock> next_update;
void schedule_update()
{
next_update = system_clock::now() + 100ms;
}
}
extern "C"
{
// from dwm
typedef union {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
void updatestatus();
// end
void vene_setup()
{
// Init vars
HOME = getenv("HOME");
strcpy(SCREEN_CAPS_DIR, HOME);
strcat(SCREEN_CAPS_DIR, "/screen_caps/");
// Signal handlers
// signal(SIGRTMIN, goto_tag);
// strcpy(BOOKMARK_DIR, HOME);
// strcat(BOOKMARK_DIR, "/notes/bookmarks/web");
// sprintf(OPEN_BOOKMARK_CMD, "xdg-open $(dmenumap ~/notes/bookmarks/web/$(ls %s | dmenu "DMENU_OPTS") "DMENU_OPTS")", BOOKMARK_DIR);
// strcpy(OPEN_BOOKMARK_CMD, "xdg-open $(dmenumap "DMENU_OPTS" ~/notes/bookmarks/web/$(ls ");
// strcat(OPEN_BOOKMARK_CMD, BOOKMARK_DIR);
// strcat(OPEN_BOOKMARK_CMD, " | dmenu "DMENU_OPTS);
}
void vene_updatestatus(char status[])
{
using std::string;
static string new_stat;
new_stat.clear();
// Volume
string amix_out = $(Cmd("amixer", "get", AUDIO_DEV));
size_t bracket = amix_out.find('[');
if(bracket != string::npos)
{
bool is_muted = amix_out.find("[on]") == string::npos;
new_stat += is_muted ? "🔇" : "🔉";
new_stat += amix_out[bracket+1];
if(char second = amix_out[bracket+2]; second != '%')
new_stat += second;
if(char third = amix_out[bracket+3]; third != '%')
new_stat += third;
new_stat += '%';
}
new_stat += ' ';
// Time
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
new_stat += (timeinfo->tm_hour/10) + '0';
new_stat += (timeinfo->tm_hour%10) + '0';
new_stat += ':';
new_stat += (timeinfo->tm_min/10) + '0';
new_stat += (timeinfo->tm_min%10) + '0';
strcpy(status, new_stat.c_str());
}
void vene_run() // update status bar every 5 sec
{
time_point<system_clock> now = system_clock::now();
if(now > next_update)
{
next_update = now + 5s;
updatestatus();
}
}
void vene_webbookmark()
{
using std::string;
Cmd dmenu( "dmenu", "-f", "-b", "-l", "30");
string bookmark (HOME);
bookmark += "/sync/bookmarks/web/";
Cmd list_dirs ("ls");
list_dirs += bookmark.c_str();
// Choose bookmark directory
bookmark += $(list_dirs | dmenu);
bookmark.pop_back(); // remove trailing newline
// Choose bookmark
string link =$(dmenu < bookmark.c_str());
size_t link_begin = link.find('-') + 1;
if(link_begin != string::npos)
{
Cmd open_bookmark(BROWSER);
link = link.substr(link_begin);
open_bookmark += link.c_str();
open_bookmark.detach();
}
}
void vene_inc_vol(const Arg*)
{
Cmd inc_vol("amixer", "set", AUDIO_DEV, "3%+");
inc_vol.detach();
schedule_update();
}
void vene_dec_vol(const Arg*)
{
Cmd dec_vol("amixer", "set", AUDIO_DEV, "3%-");
dec_vol.detach();
schedule_update();
}
void vene_toggle_vol(const Arg*)
{
Cmd toggle_vol("amixer", "set", AUDIO_DEV, "toggle");
toggle_vol.detach();
schedule_update();
}
}