summaryrefslogtreecommitdiff
path: root/event.cc
blob: bef0cfc7bb74a20c722ca9f6bf0e31c508285926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#include <config.h>

#include "event.h"
#include "my_wctob.h"
#include "dbg.h"

static const char *ext_keyname(int keycode)
{
    switch (keycode) {
    case KEY_DOWN:  return "Down";
    case KEY_UP:    return "Up";
    case KEY_LEFT:  return "Left";
    case KEY_RIGHT: return "Right";
    case KEY_HOME:  return "Home";
    case KEY_END:   return "End";
    case KEY_BACKSPACE: return "BkSp";
    case KEY_DC:    return "Del";
    case KEY_IC:    return "Insert";
    case KEY_NPAGE: return "PgDn";
    case KEY_PPAGE: return "PgUp";
    default: return keyname(keycode);
    }
}

u8string Event::to_string() const
{
    u8string s;
    if ((modifiers & VIRTUAL) || (keycode == 0 && !ch)) {
	// No actual event
	s = "";
    } else if (is_literal()) {
	s = u8string(unistring(&ch, &ch + 1));
    } else {
	//DBG(2, ("comp: "));
	if (modifiers) {
	    if (modifiers & CTRL)
		s += "C-";
	    if (modifiers & ALT)
		s += "M-";
	    if (modifiers & SHIFT)
		s += "Shift-";
	}
	if (ch) {
	    if ((modifiers & ALT) && ch == '\t') {
		// Hmmm... this is a special case. Ctrl-I is actually TAB.
		return u8string("C-M-i");
	    }
	    if ((modifiers & CTRL) && ch == '^') {
		// Hmmm... this is a more friendly representation.
		return u8string("C-Spc");
	    }
	    s += u8string(unistring(&ch, &ch + 1)).c_str();
	} else {
	    if (keycode >= KEY_F(0) && keycode <= KEY_F(63)) {
		u8string num;
		num.cformat("F%d", keycode - KEY_F(0));
		s += num;
	    } else {
		s += ext_keyname(keycode);
	    }
	}
    }
    return s;
}

void Event::print()
{
    DBG(2, ("EVENT: "));
    if (is_literal())
	DBG(2, ("literal: '%lc'\n", ch));
    else {
	DBG(2, ("comp: "));
	if (modifiers) {
	    if (modifiers & CTRL)
		DBG(2, ("CTRL-"));
	    if (modifiers & ALT)
		DBG(2, ("ALT-"));
	    if (modifiers & SHIFT)
		DBG(2, ("SHIFT-"));
	}
	if (ch)
	    DBG(2, ("'%lc'\n", ch));
	else
	    DBG(2, ("%s\n", keyname(keycode)));
    }
}

#ifdef HAVE_WIDE_CURSES
static void low_level_get_wch(Event &evt, WINDOW *wnd)
{
    wint_t c;
    int ret;
    do {
	ret = wget_wch(wnd, &c);
	if (ret == KEY_CODE_YES) {
	    evt.ch = 0;
	    evt.keycode = (int)c;
	} else if (ret == OK) {
	    evt.keycode = 0;
	    evt.ch = (unichar)c;
	}
    } while (ret == ERR);
}
#else
static void low_level_get_wch(Event &evt, WINDOW *wnd)
{
    int ret;
    do {
	ret = wgetch(wnd);
	if (ret >= 256) {
	    evt.ch = 0;
	    evt.keycode = ret;
	} else {
	    evt.keycode = 0;
	    evt.ch = terminal::force_iso88598 ? iso88598_to_unicode(ret) : BTOWC(ret);
	}
    } while (ret == ERR);
}
#endif

static void get_base_event(Event &evt, WINDOW *wnd)
{
    evt.type = evtKbd;
    evt.modifiers = 0;
    low_level_get_wch(evt, wnd);
    if (evt.keycode == 0 && evt.ch < 32) {
	switch (evt.ch) {
	case 9:
	case 13:
	case 27:
	    // leave alone TAB, ENTER, ESC
	    break;
	case 0:
	case 28:
	case 29:
	case 30:
	case 31:
	    evt.ch += 'A' - 1;
	    evt.modifiers = CTRL;
	    break;
	default:
	    evt.ch += 'a' - 1;
	    evt.modifiers = CTRL;
	}
    }
}

bool is_event_pending = false;
Event pending_event;

void get_next_event(Event &evt, WINDOW *wnd)
{
    if (is_event_pending) {
	evt = pending_event;
	is_event_pending = false;
	return;
    }

    get_base_event(evt, wnd);
    if (evt.ch == 27) {
	// emulate ALT
	get_base_event(evt, wnd);
	if (evt.ch != 27) // ...but make sure we can still generate ESCape
	    evt.modifiers |= ALT;
    }
}

void set_next_event(const Event &evt)
{
    is_event_pending = true;
    pending_event = evt;
}