[go: up one dir, main page]

File: keybindings.c

package info (click to toggle)
tilem 2.0-5.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,136 kB
  • sloc: ansic: 27,147; makefile: 740; sh: 344; xml: 315
file content (255 lines) | stat: -rw-r--r-- 6,292 bytes parent folder | download | duplicates (5)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * TilEm II
 *
 * Copyright (c) 2010-2011 Thibault Duponchelle 
 * Copyright (c) 2010-2011 Benjamin Moody
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <ticalcs.h>
#include <tilem.h>

#include "gui.h"
#include "msgbox.h"
#include "files.h"

/* Get the associated calculator key name */
static int calc_key_from_name(const TilemCalc *calc, const char *name)
{
	int i;

	for (i = 0; i < 64; i++)
		if (calc->hw.keynames[i]
		    && !strcmp(calc->hw.keynames[i], name))
			return i + 1;

	/* kludge: accept aliases for a few keys */
	for (i = 0; i < 64; i++) {
		if (!calc->hw.keynames[i])
			continue;

		if (!strcmp(name, "Matrix")
		    && !strcmp(calc->hw.keynames[i], "Apps"))
			return i + 1;
		if (!strcmp(name, "Apps")
		    && !strcmp(calc->hw.keynames[i], "AppsMenu"))
			return i + 1;
		if (!strcmp(name, "List")
		    && !strcmp(calc->hw.keynames[i], "StatEd"))
			return i + 1;
		if (!strcmp(name, "Power")
		    && !strcmp(calc->hw.keynames[i], "Expon"))
			return i + 1;
		if (!strcmp(name, "Stat")
		    && !strcmp(calc->hw.keynames[i], "Table"))
			return i + 1;
	}

	return 0;
}

/* Parse a line of the group (model) in the keybindings file */
static gboolean parse_binding(TilemKeyBinding *kb,
                              const char *pckeys, const char *tikeys,
                              const TilemCalc *calc)
{
	const char *p;
	char *s;
	int n, k;

	kb->modifiers = 0;
	kb->keysym = 0;
	kb->nscancodes = 0;
	kb->scancodes = NULL;

	/* Parse modifiers */

	while ((p = strchr(pckeys, '+'))) {
		s = g_strndup(pckeys, p - pckeys);
		g_strstrip(s);
		if (!g_ascii_strcasecmp(s, "ctrl")
		    || !g_ascii_strcasecmp(s, "control"))
			kb->modifiers |= GDK_CONTROL_MASK;
		else if (!g_ascii_strcasecmp(s, "shift"))
			kb->modifiers |= GDK_SHIFT_MASK;
		else if (!g_ascii_strcasecmp(s, "alt")
		         || !g_ascii_strcasecmp(s, "mod1"))
			kb->modifiers |= GDK_MOD1_MASK;
		else if (!g_ascii_strcasecmp(s, "mod2"))
			kb->modifiers |= GDK_MOD2_MASK;
		else if (!g_ascii_strcasecmp(s, "mod3"))
			kb->modifiers |= GDK_MOD3_MASK;
		else if (!g_ascii_strcasecmp(s, "mod4"))
			kb->modifiers |= GDK_MOD4_MASK;
		else if (!g_ascii_strcasecmp(s, "mod5"))
			kb->modifiers |= GDK_MOD5_MASK;
		else if (!g_ascii_strcasecmp(s, "lock")
		         || !g_ascii_strcasecmp(s, "capslock"))
			kb->modifiers |= GDK_LOCK_MASK;
		else {
			g_free(s);
			return FALSE;
		}
		g_free(s);
		pckeys = p + 1;
	}

	/* Parse keysym */

	s = g_strstrip(g_strdup(pckeys));
	kb->keysym = gdk_keyval_from_name(s);
	g_free(s);
	if (!kb->keysym)
		return FALSE;

	/* Parse calculator keys */

	/* FIXME: allow combinations of simultaneous keys (separated
	   by '+'); current TilemKeyBinding struct doesn't provide for
	   this */

	n = 0;
	do {
		if ((p = strchr(tikeys, ',')))
			s = g_strndup(tikeys, p - tikeys);
		else
			s = g_strdup(tikeys);
		g_strstrip(s);

		k = calc_key_from_name(calc, s);
		g_free(s);

		if (!k) {
			g_free(kb->scancodes);
			kb->scancodes = NULL;
			return FALSE;
		}

		kb->nscancodes++;
		if (kb->nscancodes >= n) {
			n = kb->nscancodes * 2;
			kb->scancodes = g_renew(byte, kb->scancodes, n);
		}
		kb->scancodes[kb->nscancodes - 1] = k;

		tikeys = (p ? p + 1 : NULL);
	} while (tikeys);

	return TRUE;
}

/* Parse a group (model) in the keybindings file */
static void parse_binding_group(TilemCalcEmulator *emu, GKeyFile *gkf,
                                const char *group, int maxdepth)
{
	gchar **keys, **groups;
	char *k, *v;
	int i, n;

	keys = g_key_file_get_keys(gkf, group, NULL, NULL);
	if (!keys) {
		printf("no bindings for %s\n", group);
		return;
	}

	for (i = 0; keys[i]; i++)
		;

	n = emu->nkeybindings;
	emu->keybindings = g_renew(TilemKeyBinding, emu->keybindings, n + i);

	for(i = 0; keys[i]; i++) {
		k = keys[i];
		if (!strcmp(k, "INHERIT"))
			continue;

		v = g_key_file_get_value(gkf, group, k, NULL);
		if (!v)
			continue;

		if (parse_binding(&emu->keybindings[n], k, v, emu->calc))
			n++;
		else
			g_printerr("syntax error in key bindings: '%s=%s'\n",
			           k, v);
		g_free(v);
	}

	emu->nkeybindings = n;

	g_strfreev(keys);

	/* Include all bindings from groups marked as INHERIT */

	if (maxdepth == 0)
		return;

	groups = g_key_file_get_string_list(gkf, group, "INHERIT",
	                                    NULL, NULL);
	for (i = 0; groups && groups[i]; i++)
		parse_binding_group(emu, gkf, groups[i], maxdepth - 1);
	g_strfreev(groups);

}

/* Init the keybindings struct and open the keybindings file */
void tilem_keybindings_init(TilemCalcEmulator *emu, const char *model)
{
	char *kfname = get_shared_file_path("keybindings.ini", NULL);
	char *dname;
	GKeyFile *gkf;
	GError *err = NULL;

	g_return_if_fail(emu != NULL);
	g_return_if_fail(emu != NULL);
	g_return_if_fail(emu->calc != NULL);

	if (kfname == NULL) {
		messagebox00(NULL, GTK_MESSAGE_ERROR,
		             "Unable to load key bindings",
		             "The file keybindings.ini could not be found."
		             "  TilEm may not have been installed correctly.");
		return;
	}

	gkf = g_key_file_new();
	if (!g_key_file_load_from_file(gkf, kfname, 0, &err)) {
		dname = g_filename_display_name(kfname);
		messagebox02(NULL, GTK_MESSAGE_ERROR,
		             "Unable to load key bindings",
		             "An error occurred while reading %s: %s",
		             dname, err->message);
		g_error_free(err);
		g_free(dname);
		g_free(kfname);
		return;
	}

	g_free(emu->keybindings);
	emu->keybindings = NULL;
	emu->nkeybindings = 0;

	parse_binding_group(emu, gkf, model, 5);

	g_key_file_free(gkf);
	g_free(kfname);
}