PageRenderTime 32ms CodeModel.GetById 19ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/osx/scaffold/cui.d

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