// Dash Interface // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Display.hpp" bool debug(false); class DashInterface { enum Mode { Uname, Music, English, Metric, Nautical, Menu } mode; enum { Running, Audio }; enum { Stopped, Paused, Playing } audio; Timing::Time now; Display display; Pthreads::ThreadMutex displayLock; Audacious::Audacious audacious; //GPS::GPS gps; void Mode_(bool change = false) { switch (mode) { case Uname: return Uname_(change); case Music: if (audacious.IsRunning()) { int position(audacious.GetPlaylistPosition()); std::string artist(audacious.GetTupleFieldData(_B("artist"), position)); display.Set(0, 1, artist.size() > 20 ? artist.erase(20) : artist.append(20 - artist.size(), ' ')); std::string title(audacious.GetTupleFieldData(_B("title"), position)); display.Set(0, 2, title.size() > 20 ? title.erase(20) : title.append(20 - title.size(), ' ')); int played(audacious.GetOutputTime() / 1000); int left(audacious.GetPlaylistTime(position) / 1000 - played); char *times; ::asprintf(×, "%2i:%02i -%2i:%02i", played / 60, played % 60, left / 60, left % 60); display.Set(0, 3, times + std::string(20 - std::strlen(times), ' ')); std::free(times); } else { display.Set(0, 1, std::string(20, ' ')); display.Set(0, 2, std::string(20, ' ')); display.Set(0, 3, _B(" -:-- - -:-- ")); } return; case English: case Metric: case Nautical: case Menu: return; } } void Uname_(bool change = false) { if (!now || change) { utsname os; ::uname(&os); static Pcre::RegEx version(_B("^(\\d+\\.\\d+)-([A-Z])[A-Z]*(-p\\d+)?.*$")); Pcre::RegEx::Match match(version(os.release)); std::string name(os.sysname + _B(" ") + match[1] + match[2] + match[3]); display.Set(0, 1, name.append(20 - name.size(), ' ')); } int mib[] = { CTL_KERN, KERN_BOOTTIME }; timeval boottime; size_t size(sizeof (boottime)); if (now && ::sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) { time_t uptime(now - boottime.tv_sec); if (uptime > 60) uptime += 30; char *when; int days(uptime / 86400); uptime %= 86400; int hours(uptime / 3600); uptime %= 3600; int minutes(uptime / 60), seconds(uptime % 60); ::asprintf(&when, "%i+%02i:%02i:%02i", days, hours, minutes, seconds); std::string then(when); then.append(17 - std::strlen(when), ' '); std::free(when); if (change) display.Set(0, 2, _B("up ") + then); else display.Set(3, 2, then); } else display.Set(0, 2, _B("up -+--:--:-- ")); double averages[3]; if (::getloadavg(averages, 3) == -1 || !now) display.Set(0, 3, _B("-.--, -.--, -.-- ")); else { char *load; ::asprintf(&load, "%.2f, %.2f, %.2f", averages[0], averages[1] , averages[2]); display.Set(0, 3, load + std::string(20 - std::strlen(load), ' ')); std::free(load); } } void Buttons(Display::KeyActivity activity) { _synchronized (displayLock) if (mode != Menu) { bool change(false); switch (activity) { case Display::UpPress: --mode; goto change; case Display::DownPress: ++mode; goto change; case Display::LeftPress: audacious.PlaylistPrevious(); goto update; case Display::RightPress: audacious.PlaylistNext(); goto update; case Display::EnterPress: std::cerr << _B("Menu") << std::endl; mode = Menu; goto change; case Display::ExitPress: audacious.PlayPause(); goto update; default: return; } change: change = true; update: Mode_(change); } else return; } public: DashInterface(const std::string &device) : mode(Uname), audio(Stopped), display(device, reinterpret_cast(::Buttons), this) { _synchronized (displayLock) { display.Clear(); display.SetBacklight(100); display.Set(2, 0, _B("--:--:-- -- ---")); display.Set(Running, Display::Green, 0); display.Set(Running, Display::Red, 100); display.Set(Audio, Display::Green, 0); display.Set(Audio, Display::Red, 100); uint8_t mask(Display::Up | Display::Enter | Display::Cancel | Display::Left | Display::Right | Display::Down); display.KeyReporting(mask, mask); Uname_(); display.Store(); display.Set(Running, Display::Green, 100); display.Set(Running, Display::Red, 0); } } void Run() { _forever { _synchronized (displayLock) { now = Timing::GetTimeOfDay(); if (mode != Menu) { char when[16]; std::strftime(when, sizeof (when), "%l:%M:%S %p %Z", std::localtime(now)); display.Set(2, 0, when); } if (audacious.IsRunning() && audacious.IsPlaying()) if (audacious.IsPaused()) { if (audio != Paused) { display.Set(Audio, Display::Green, 100); display.Set(Audio, Display::Red, 100); audio = Paused; } } else { if (audio != Playing) { display.Set(Audio, Display::Green, 100); display.Set(Audio, Display::Red, 0); audio = Playing; } } else if (audio != Stopped) { display.Set(Audio, Display::Green, 0); display.Set(Audio, Display::Red, 100); audio = Stopped; } Mode_(); } Timing::NanoSleep(Timing::Time(1) -= Timing::GetTimeOfDay().GetNanoseconds()); } } friend void Buttons(Display::KeyActivity activity, DashInterface *interface) { interface->Buttons(activity); } inline friend Mode &operator ++(Mode &mode) { return mode = Mode((mode + 1) % Menu); } inline friend Mode &operator --(Mode &mode) { return mode = Mode((Menu + mode - 1) % Menu); } }; int main(int argc, char *argv[]) { std::string device; { Pcre::RegEx devicePath(_B("^-device=(/.+)$")); Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$")); Pcre::RegEx deviceName(_B("^-device=(.+)$")); for (char **arg = argv + 1; *arg; ++arg) { Pcre::RegEx::Match match; if (*arg == _B("-debug")) debug = true; else if (match = devicePath(*arg)) device = match[1]; else if (match = deviceNumber(*arg)) device = _B("/dev/cuaU") + match[1]; else if (match = deviceName(*arg)) device = _B("/dev/") + match[1]; else goto usage; } } if (device.empty()) { usage: std::cout << _B("Usage: ") << argv[0] << _B(" [-debug] [-device=device]") << std::endl; return 2; } try { DashInterface interface(device); interface.Run(); } catch (const std::exception &exception) { std::cerr << argv[0] << _B(": ") << exception.what() << std::endl; return 1; } return 0; }