diff options
author | Sebastian Reichel <sre@ring0.de> | 2013-03-26 15:52:57 +0100 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2013-03-26 15:52:57 +0100 |
commit | 7bfb48ef84384ff0460f273ea5841fba628d2a46 (patch) | |
tree | 898d019f33a554f03cac91495adcb7165344382e /src/curses-ui | |
parent | 03a4e9f901cd36792de2172b4ebb8f6e852fe1cd (diff) | |
download | serial-barcode-scanner-7bfb48ef84384ff0460f273ea5841fba628d2a46.tar.bz2 |
code restructure
Diffstat (limited to 'src/curses-ui')
-rw-r--r-- | src/curses-ui/Makefile | 9 | ||||
-rw-r--r-- | src/curses-ui/clock.vala | 76 | ||||
-rw-r--r-- | src/curses-ui/curses-ui.vala | 83 | ||||
-rw-r--r-- | src/curses-ui/dialog.vala | 51 | ||||
-rw-r--r-- | src/curses-ui/logo.vala | 45 | ||||
-rw-r--r-- | src/curses-ui/main.vala | 49 | ||||
-rw-r--r-- | src/curses-ui/message_box.vala | 60 | ||||
-rw-r--r-- | src/curses-ui/numbers.vala | 113 | ||||
-rw-r--r-- | src/curses-ui/status.vala | 39 |
9 files changed, 525 insertions, 0 deletions
diff --git a/src/curses-ui/Makefile b/src/curses-ui/Makefile new file mode 100644 index 0000000..5d168ba --- /dev/null +++ b/src/curses-ui/Makefile @@ -0,0 +1,9 @@ +all: curses-ui + +curses-ui: clock.vala curses-ui.vala dialog.vala logo.vala main.vala message_box.vala numbers.vala status.vala ../audio/audio-interface.vala + valac -o $@ --pkg curses -X -lncursesw --pkg posix --pkg gio-2.0 $^ + +clean: + rm -rf curses-ui + +.PHONY: all clean diff --git a/src/curses-ui/clock.vala b/src/curses-ui/clock.vala new file mode 100644 index 0000000..dd3ddcd --- /dev/null +++ b/src/curses-ui/clock.vala @@ -0,0 +1,76 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +using Curses; + +public class ClockWindow { + AsciiNumbers ascii; + Window win; + + public ClockWindow() { + ascii = new AsciiNumbers(); + win = new Window(6, 18, 1, COLS-2-18); + win.bkgdset(COLOR_PAIR(0) | Attribute.BOLD); + + win.clrtobot(); + win.box(0, 0); + + win.refresh(); + } + + public void update() { + string[] x; + var now = new DateTime.now_local(); + + x = ascii.get('0' + (char) (now.get_hour() / 10)); + win.mvaddstr(1,1, x[0]); + win.mvaddstr(2,1, x[1]); + win.mvaddstr(3,1, x[2]); + + x = ascii.get('0' + (char) (now.get_hour() % 10)); + win.mvaddstr(1,4, x[0]); + win.mvaddstr(2,4, x[1]); + win.mvaddstr(3,4, x[2]); + + x = ascii.get(':'); + win.mvaddstr(1,7, x[0]); + win.mvaddstr(2,7, x[1]); + win.mvaddstr(3,7, x[2]); + + x = ascii.get('0' + (char) (now.get_minute() / 10)); + win.mvaddstr(1,10, x[0]); + win.mvaddstr(2,10, x[1]); + win.mvaddstr(3,10, x[2]); + + x = ascii.get('0' + (char) (now.get_minute() % 10)); + win.mvaddstr(1,13, x[0]); + win.mvaddstr(2,13, x[1]); + win.mvaddstr(3,13, x[2]); + + + win.clrtobot(); + win.box(0, 0); + + win.mvaddstr(5,4, now.format("%Y-%m-%d")); + + win.refresh(); + } + + public void redraw() { + win.touchwin(); + win.refresh(); + } + +} diff --git a/src/curses-ui/curses-ui.vala b/src/curses-ui/curses-ui.vala new file mode 100644 index 0000000..ec007f2 --- /dev/null +++ b/src/curses-ui/curses-ui.vala @@ -0,0 +1,83 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +public class CursesUI { + MessageBox messages; + Dialog dialog; + Logo banner; + ClockWindow clkwin; + StatusPanel statuswin; + + public CursesUI() { + /* unicode support */ + Intl.setlocale(LocaleCategory.CTYPE, ""); + + /* initialize curses */ + Curses.initscr(); + + /* disable cursor */ + Curses.curs_set(0); + + /* initialize color mode and define color pairs */ + Curses.start_color(); + Curses.init_pair(0, Curses.Color.WHITE, Curses.Color.BLACK); + Curses.init_pair(1, Curses.Color.GREEN, Curses.Color.BLACK); + Curses.init_pair(2, Curses.Color.WHITE, Curses.Color.RED); + + /* initialize widgets */ + banner = new Logo(); + statuswin = new StatusPanel(); + messages = new MessageBox(); + clkwin = new ClockWindow(); + + clkwin.update(); + + Timeout.add_seconds(10, update_time); + } + + ~CursesUI() { + exit(); + } + + public void exit() { + /* Reset the terminal mode */ + Curses.endwin(); + } + + bool update_time() { + clkwin.update(); + return true; + } + + public void status(string message) { + statuswin.set(message); + } + + public void log(string message) { + messages.add(message); + } + + public void dialog_open(string title, string message) { + dialog = new Dialog(message, title); + } + + public void dialog_close() { + dialog = null; + messages.redraw(); + banner.redraw(); + clkwin.redraw(); + statuswin.redraw(); + } +} diff --git a/src/curses-ui/dialog.vala b/src/curses-ui/dialog.vala new file mode 100644 index 0000000..a8585d4 --- /dev/null +++ b/src/curses-ui/dialog.vala @@ -0,0 +1,51 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +using Curses; + +public class Dialog { + Window win; + Window subwin; + + public Dialog(string message, string title = "KtT Shopsystem Error", int h=16, int w=60) + requires (title.length <= w-4) + { + int y = LINES/2-h/2; + int x = COLS/2-w/2; + + int title_x = (w-title.length)/2; + + win = new Window(h, w, y, x); + + /* make the dialog white on red */ + win.bkgdset(COLOR_PAIR(2) | Attribute.BOLD); + win.clrtobot(); + + /* message subwindow */ + subwin = win.derwin(h-4, w-4, 2, 2); + subwin.clrtobot(); + subwin.printw(message); + subwin.refresh(); + + /* dialog title */ + win.box(0,0); + win.mvaddstr(0, title_x, title); + win.mvaddch(0, title_x-2, Acs.RTEE); + win.mvaddch(0, title_x-1, ' '); + win.mvaddch(0, title_x+title.length, ' '); + win.mvaddch(0, title_x+title.length+1, Acs.LTEE); + win.refresh(); + } +} diff --git a/src/curses-ui/logo.vala b/src/curses-ui/logo.vala new file mode 100644 index 0000000..dbc716d --- /dev/null +++ b/src/curses-ui/logo.vala @@ -0,0 +1,45 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +using Curses; + +public class Logo { + Window win; + + public Logo() { + win = new Window(8, COLS - 2, 0, 1); + win.bkgdset(COLOR_PAIR(1) | Attribute.BOLD); + + win.addstr("\n"); + win.addstr(" _ ___ _____ ____ _ \n"); + win.addstr(" | |/ / ||_ _| / ___|| |__ ___ _ __ \n"); + win.addstr(" | ' /| __|| | \\___ \\| '_ \\ / _ \\| '_ \\ \n"); + win.addstr(" | . \\| |_ | | ___) | | | | (_) | |_) )\n"); + win.addstr(" |_|\\_\\\\__||_| |____/|_| |_|\\___/| .__/ \n"); + win.addstr(" |_| \n"); + + win.clrtobot(); + + win.box(0, 0); + + win.refresh(); + } + + public void redraw() { + win.touchwin(); + win.refresh(); + } + +} diff --git a/src/curses-ui/main.vala b/src/curses-ui/main.vala new file mode 100644 index 0000000..1d79c43 --- /dev/null +++ b/src/curses-ui/main.vala @@ -0,0 +1,49 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +public MainLoop loop; + +public static int main(string[] args) { + /* handle unix signals */ + Unix.signal_add(Posix.SIGTERM, handle_signals); + Unix.signal_add(Posix.SIGINT, handle_signals); + + AudioPlayer audio = Bus.get_proxy_sync(BusType.SESSION, "io.mainframe.shopsystem.AudioPlayer", "/io/mainframe/shopsystem/audio"); + + var ui = new CursesUI(); + + ui.log("KtT Shop System has been started"); + audio.play_system("startup.ogg"); + + /* run mainloop */ + loop.run(); + + ui.log("Stopping Shop System"); + audio.play_system("shutdown.ogg"); + + /* we need to run the mainloop to play audio */ + audio.end_of_stream.connect(() => { loop.quit(); }); + loop.run(); + + /* leave curses mode */ + ui.exit(); + + return 0; +} + +bool handle_signals() { + loop.quit(); + return false; +} diff --git a/src/curses-ui/message_box.vala b/src/curses-ui/message_box.vala new file mode 100644 index 0000000..cc258b4 --- /dev/null +++ b/src/curses-ui/message_box.vala @@ -0,0 +1,60 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +using Curses; + +public class MessageBox { + Window win; + Window subwin; + DateTime last; + + public MessageBox() { + win = new Window(LINES-9, COLS - 2, 8, 1); + win.bkgdset(COLOR_PAIR(0)); + + win.clrtobot(); + win.box(0, 0); + win.refresh(); + + subwin = win.derwin(LINES-11, COLS-4, 1, 1); + subwin.scrollok(true); + subwin.clrtobot(); + subwin.refresh(); + + last = new DateTime.from_unix_utc(0); + } + + public void add(string msg) { + var now = new DateTime.now_local(); + + if(now.get_day_of_year() != last.get_day_of_year() || now.get_year() != last.get_year()) { + string curtime = now.format("%Y-%m-%d"); + subwin.addstr("\nDate Changed: " + curtime); + } + + last = now; + + string curtime = now.format("%H:%M:%S"); + subwin.addstr("\n[" + curtime + "] " + msg); + subwin.refresh(); + } + + public void redraw() { + win.touchwin(); + win.refresh(); + subwin.touchwin(); + subwin.refresh(); + } +} diff --git a/src/curses-ui/numbers.vala b/src/curses-ui/numbers.vala new file mode 100644 index 0000000..200cf63 --- /dev/null +++ b/src/curses-ui/numbers.vala @@ -0,0 +1,113 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +public class AsciiNumbers { + + public string[] zero = { + " _ ", + "/ \\", + "\\_/" + }; + + public string[] one = { + " ", + " /|", + " |" + }; + + public string[] two = { + "__ ", + " _)", + "(__" + }; + + public string[] three = { + "__ ", + " _)", + "__)" + }; + + public string[] four = { + " ", + "|_|", + " |" + }; + + public string[] five = { + " __", + "|_ ", + "__)" + }; + + public string[] six = { + " _ ", + "/_ ", + "\\_)" + }; + + public string[] seven = { + "___", + " /", + " / " + }; + + public string[] eight = { + " _ ", + "(_)", + "(_)" + }; + + public string[] nine = { + " _ ", + "(_\\", + " _/" + }; + + public string[] colon = { + " ", + " o ", + " o " + }; + + public string[] get(char c) { + switch(c) { + case '0': + return zero; + case '1': + return one; + case '2': + return two; + case '3': + return three; + case '4': + return four; + case '5': + return five; + case '6': + return six; + case '7': + return seven; + case '8': + return eight; + case '9': + return nine; + case ':': + return colon; + default: + return {}; + } + } + +} diff --git a/src/curses-ui/status.vala b/src/curses-ui/status.vala new file mode 100644 index 0000000..5be6a00 --- /dev/null +++ b/src/curses-ui/status.vala @@ -0,0 +1,39 @@ +/* Copyright 2013, Sebastian Reichel <sre@ring0.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +using Curses; + +public class StatusPanel { + Window win; + + public StatusPanel() { + win = new Window(1, COLS - 2, LINES-1, 1); + win.bkgdset(COLOR_PAIR(2) | Attribute.BOLD); + + win.clrtobot(); + win.refresh(); + } + + public void set(string msg) { + win.mvaddstr(0,1, msg); + win.clrtobot(); + win.refresh(); + } + + public void redraw() { + win.touchwin(); + win.refresh(); + } +} |