PageRenderTime 58ms CodeModel.GetById 31ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/unix/scaffold/cui.d

http://github.com/wilkie/djehuty
D | 180 lines | 137 code | 30 blank | 13 comment | 36 complexity | 83c6270b46b66e877f8c7b7e11faf156 MD5 | raw file
  1/*
  2 * cui.d
  3 *
  4 * This module implements the Cui event loop.
  5 *
  6 * Author: Dave Wilkinson
  7 * Originated: August 17th 2009
  8 *
  9 */
 10
 11module scaffold.cui;
 12
 13import platform.vars.cui;
 14
 15import platform.unix.common;
 16
 17import scaffold.console;
 18
 19import core.definitions;
 20
 21import platform.application;
 22
 23void CuiStart(CuiPlatformVars* vars) {
 24	Curses.savetty();
 25
 26	ApplicationController app = ApplicationController.instance();
 27	app.usingCurses = true;
 28	setvbuf (stdout, null, _IONBF, 0);
 29
 30	Curses.initscr();
 31	setlocale(LC_ALL, "");
 32	setlocale(LC_CTYPE, "");
 33	Curses.start_color();
 34	Curses.keypad(Curses.stdscr, 1);
 35
 36	Curses.move(0,0);
 37
 38	Curses.nonl();
 39	Curses.cbreak();
 40	Curses.noecho();
 41
 42	Curses.raw();
 43
 44	Curses.mmask_t oldmask;
 45	Curses.mousemask(Curses.ALL_MOUSE_EVENTS | Curses.REPORT_MOUSE_POSITION, &oldmask);
 46	Curses.mouseinterval(0);
 47
 48	// Get current position
 49	m_x = 0;
 50	m_y = 0;
 51
 52	Curses.getmaxyx(Curses.stdscr, m_height, m_width);
 53	setvbuf (stdout, null, _IONBF, 0);
 54}
 55
 56void CuiEnd(CuiPlatformVars* vars) {
 57	if (ApplicationController.instance.usingCurses) {
 58		ApplicationController.instance.usingCurses = false;
 59	}
 60	Curses.endwin();
 61	Curses.reset_shell_mode();
 62	Curses.resetterm();
 63}
 64
 65void CuiNextEvent(CuiEvent* evt, CuiPlatformVars* vars) {
 66
 67start:
 68
 69	static int mouse_x = -1;
 70	static int mouse_y = -1;
 71
 72	//become IO bound
 73
 74	ulong ky; uint tky;
 75
 76	Key key = consoleGetKey();
 77
 78	if (key.code == Curses.KEY_RESIZE) {
 79		// Resize
 80		evt.type = CuiEvent.Type.Size;
 81		return;
 82	}
 83	else if (key.code == Curses.KEY_MOUSE) {
 84		// Mouse
 85		Curses.MEVENT event;
 86		if (Curses.getmouse(&event) == Curses.ERR) {
 87			goto start;
 88		}
 89
 90		evt.info.mouse.x = event.x;
 91		evt.info.mouse.y = event.y;
 92
 93		const auto CLICKED = Curses.BUTTON1_CLICKED | Curses.BUTTON2_CLICKED |
 94				Curses.BUTTON3_CLICKED | Curses.BUTTON4_CLICKED |
 95				Curses.BUTTON5_CLICKED;
 96		const auto DOUBLE_CLICKED = Curses.BUTTON1_DOUBLE_CLICKED | Curses.BUTTON2_DOUBLE_CLICKED |
 97				Curses.BUTTON3_DOUBLE_CLICKED | Curses.BUTTON4_DOUBLE_CLICKED |
 98				Curses.BUTTON5_DOUBLE_CLICKED;
 99		const auto TRIPLE_CLICKED = Curses.BUTTON1_DOUBLE_CLICKED | Curses.BUTTON2_DOUBLE_CLICKED |
100				Curses.BUTTON3_TRIPLE_CLICKED | Curses.BUTTON4_DOUBLE_CLICKED |
101				Curses.BUTTON5_TRIPLE_CLICKED;
102
103		if (event.bstate & CLICKED) {
104			evt.info.mouse.clicks = 1;
105		}
106		else if (event.bstate & DOUBLE_CLICKED) {
107			evt.info.mouse.clicks = 2;
108		}
109		else if (event.bstate & TRIPLE_CLICKED) {
110			evt.info.mouse.clicks = 3;
111		}
112
113		if (event.bstate & Curses.BUTTON1_PRESSED) {
114			evt.type = CuiEvent.Type.MouseDown;
115			evt.info.mouse.leftDown = true;
116			evt.aux = 0;
117			return;
118		}
119		else if (event.bstate & Curses.BUTTON2_PRESSED) {
120			evt.type = CuiEvent.Type.MouseDown;
121			evt.info.mouse.rightDown = true;
122			evt.aux = 1;
123			return;
124		}
125		else if (event.bstate & Curses.BUTTON3_PRESSED) {
126			evt.type = CuiEvent.Type.MouseDown;
127			evt.info.mouse.middleDown = true;
128			evt.aux = 2;
129			return;
130		}
131		else if (event.bstate & Curses.BUTTON4_PRESSED) {
132			evt.type = CuiEvent.Type.MouseDown;
133			evt.aux = 3;
134			return;
135		}
136		else if (event.bstate & Curses.BUTTON5_PRESSED) {
137			evt.type = CuiEvent.Type.MouseDown;
138			evt.aux = 4;
139			return;
140		}
141		else if (event.bstate & Curses.BUTTON1_RELEASED) {
142			evt.type = CuiEvent.Type.MouseUp;
143			evt.aux = 0;
144			return;
145		}
146		else if (event.bstate & Curses.BUTTON2_RELEASED) {
147			evt.type = CuiEvent.Type.MouseUp;
148			evt.aux = 1;
149			return;
150		}
151		else if (event.bstate & Curses.BUTTON3_RELEASED) {
152			evt.type = CuiEvent.Type.MouseUp;
153			evt.aux = 2;
154			return;
155		}
156		else if (event.bstate & Curses.BUTTON4_RELEASED) {
157			evt.type = CuiEvent.Type.MouseUp;
158			evt.aux = 3;
159			return;
160		}
161		else if (event.bstate & Curses.BUTTON5_RELEASED) {
162			evt.type = CuiEvent.Type.MouseUp;
163			evt.aux = 4;
164			return;
165		}
166
167		if ((event.x != mouse_x) || (event.y != mouse_y)) {
168			mouse_x = event.x;
169			mouse_y = event.y;
170
171			evt.type = CuiEvent.Type.MouseMove;
172			return;
173		}
174		goto start;
175	}
176
177	evt.type = CuiEvent.Type.KeyDown;
178	evt.info.key = key;
179	return;
180}