Files
dwm/vene.cpp
T
2026-07-30 11:54:49 +03:00

195 lines
4.0 KiB
C++

#include <time.h>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <string>
#include <chrono>
#include <filesystem>
#include <cppipe/commands.hpp>
// #include "vene.h"
#include "vene_env.h"
using namespace std;
using namespace std::chrono;
using namespace std::chrono_literals;
namespace fs = std::filesystem;
char* HOME;
char SCREEN_CAPS_DIR[64];
namespace
{
time_point<system_clock> next_update;
bool has_child;
Proc child;
Proc browser = {.pid = 0};
}
extern "C"
{
// from dwm
typedef union {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
void updatestatus();
void view(const Arg* arg);
// 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();
// Net
string eth_state;
ifstream("/sys/class/net/eth0/operstate") >> eth_state;
if(eth_state == "up")
new_stat += "🌐 ";
// Volume
string amix_out = $(Cmd("amixer", "get", AUDIO_DEV));
size_t vol_bracket = amix_out.find('[');
if(vol_bracket != string::npos)
{
bool is_muted = amix_out.find("[on]") == string::npos;
new_stat += is_muted ? "🔇" : "🔉";
size_t first_digit = vol_bracket + 1;
size_t close_bracket = amix_out.find(']', first_digit);
new_stat.append(amix_out, first_digit, close_bracket - first_digit);
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(has_child && check_exited(child))
{
has_child = false;
updatestatus();
next_update = now + 10s;
}
else if(now > next_update)
{
updatestatus();
next_update = now + 10s;
}
}
void vene_browser(const Arg* arg)
{
if( browser.pid == 0 || check_exited(browser) ) // not running
{
browser = Cmd(BROWSER).detach();
// Clean up all zombies
while (waitpid(-1, nullptr, WNOHANG) > 0);
}
else // running, view it
{
view(arg);
}
}
void vene_webbookmark() // todo: cppipe exceptions
{
Cmd dmenu( "dmenu", "-b", "-f", "-i", "-l", "30");
string bm_root (HOME);
bm_root += "/sync/bookmarks/web/";
Cmd list_dirs ("ls");
list_dirs += bm_root.c_str();
while(true)
{
// Choose bookmark directory
string dir = $(list_dirs | dmenu);
if( dir.empty() )
return;
string bm_dir = bm_root;
bm_dir += dir;
bm_dir.pop_back(); // remove trailing newline
// Choose bookmark
string link = $(dmenu < bm_dir.c_str());
size_t link_begin = link.find('-');
if(link_begin != string::npos)
{
Cmd open_bookmark(BROWSER);
link = link.substr(link_begin+1);
open_bookmark += link.c_str();
open_bookmark.detach();
break;
}
}
}
void vene_inc_vol(const Arg*)
{
Cmd inc_vol("amixer", "-q", "set", AUDIO_DEV, "3%+");
child = inc_vol.detach();
has_child = true;
}
void vene_dec_vol(const Arg*)
{
Cmd dec_vol("amixer", "-q", "set", AUDIO_DEV, "3%-");
child = dec_vol.detach();
has_child = true;
}
void vene_toggle_vol(const Arg*)
{
Cmd toggle_vol("amixer", "-q", "set", AUDIO_DEV, "toggle");
child = toggle_vol.detach();
has_child = true;
}
}