// Dash Interface // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Display.hpp" #include "MenuList.hpp" bool debug(false); class DashInterface { enum Mode { Uname, Music, English, Metric, Nautical, Menu } mode, previous; enum { Running, Audio }; enum { Stopped, Paused, Playing } audio; Timing::Time now; IConv::IConv ascii; Display display; Pthreads::ThreadMutex displayLock; Audacious::Audacious audacious; //GPS::GPS gps; MenuList *list; bool append; inline std::string Filter(const std::string input) { std::string output(ascii.Convert(input)); std::replace(output.begin(), output.end(), '~', '\xce'); return output; } void Mode_(bool change = false) { if (mode != Menu) { char when[16]; std::strftime(when, sizeof (when), "%l:%M:%S %p %Z", std::localtime(now)); display.Set(2, 0, when); } switch (mode) { case Uname: return Uname_(change); case Music: if (audacious.IsRunning()) { int position(audacious.GetPlaylistPosition()); std::string artist(Filter(audacious.GetTupleFieldData(_B("artist"), position))); artist.resize(20, ' '); display.Set(0, 1, artist); std::string title(Filter(audacious.GetTupleFieldData(_B("title"), position))); title.resize(20, ' '); display.Set(0, 2, title); 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); std::string times_(times); std::free(times); times_.resize(20, ' '); display.Set(0, 3, 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: display.Set(0, 1, _B("STUB: GPS ") + (mode == English ? _B("English ") : mode == Metric ? _B("Metric ") : _B("Nautical "))); display.Set(0, 2, std::string(20, ' ')); display.Set(0, 3, std::string(20, ' ')); return; case Menu: if (change) { display.SetCursorPosition(19, 0); display.SetCursorStyle(Display::InvertingBlinkingBlock); append = true; list = new TopList(display, audacious, append); list->Render(); } } } 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]); name.resize(20, ' '); display.Set(0, 1, name); } 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); std::free(when); then.resize(17, ' '); 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]); std::string load_(load); std::free(load); load_.resize(20, ' '); display.Set(0, 3, 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: if (audacious.IsRunning()) audacious.PlaylistPrevious(); goto update; case Display::RightPress: if (audacious.IsRunning()) audacious.PlaylistNext(); goto update; case Display::EnterPress: previous = mode; mode = Menu; goto change; case Display::ExitPress: if (audacious.IsRunning()) audacious.PlayPause(); goto update; default: return; } change: change = true; update: Mode_(change); } else switch (activity) { case Display::UpPress: return (--*list).Render(); case Display::DownPress: return (++*list).Render(); case Display::LeftPress: list = list->Left(); goto render; case Display::RightPress: list = list->Right(); goto render; case Display::EnterPress: list = list->Enter(); render: if (list != NULL) return list->Render(); goto exit; case Display::ExitPress: delete list; exit: mode = previous; display.SetCursorStyle(Display::NoCursor); display.Set(0, 0, _B(" ")); display.Set(18, 0, _B(" ")); Mode_(true); default: return; } } public: DashInterface(const std::string &device) : mode(Uname), audio(Stopped), ascii(_B("ASCII//TRANSLIT//IGNORE"), _B("UTF-8")), display(device, reinterpret_cast(::Buttons), this) { _synchronized (displayLock) { display.Clear(); display.SetCursorStyle(Display::NoCursor); 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 (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; }