Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Keyboard controls autobuild

  • Loading branch information...
commit d8c29545ef2943f3a73ad2d1ea92e7f2b55ceedc 1 parent 1230e7f
Sébastien Bourdeauducq authored April 24, 2011

Showing 1 changed file with 83 additions and 2 deletions. Show diff stats Hide diff stats

  1. 85  src/keyboard.c
85  src/keyboard.c
@@ -16,8 +16,12 @@
16 16
  */
17 17
 
18 18
 #include <bsp.h>
  19
+#include <sys/types.h>
  20
+#include <sys/stat.h>
19 21
 #include <stdio.h>
20 22
 #include <string.h>
  23
+#include <dirent.h>
  24
+#include <unistd.h>
21 25
 #include <mtklib.h>
22 26
 
23 27
 #include "util.h"
@@ -163,6 +167,80 @@ static void addupdate_callback(mtk_event *e, void *arg)
163 167
 	mtk_cmd(appid, "e_filename.set(-text \"\")");
164 168
 }
165 169
 
  170
+static int cmpstringp(const void *p1, const void *p2)
  171
+{
  172
+	return strcmp(*(char * const *)p1, *(char * const *)p2);
  173
+}
  174
+
  175
+static void autobuild(int sk, char *folder)
  176
+{
  177
+	DIR *d;
  178
+	struct dirent *entry;
  179
+	struct stat s;
  180
+	char fullname[384];
  181
+	char *c;
  182
+	char *files[26];
  183
+	int n_files;
  184
+	int max_files = 26 - sk;
  185
+	int i;
  186
+	
  187
+	d = opendir(folder);
  188
+	if(!d) {
  189
+		messagebox("Auto build failed", "Unable to open directory");
  190
+		return;
  191
+	}
  192
+	n_files = 0;
  193
+	while((entry = readdir(d))) {
  194
+		if(entry->d_name[0] == '.') continue;
  195
+		strncpy(fullname, folder, sizeof(fullname));
  196
+		strncat(fullname, entry->d_name, sizeof(fullname));
  197
+		lstat(fullname, &s);
  198
+		if(!S_ISDIR(s.st_mode)) {
  199
+			c = strrchr(entry->d_name, '.');
  200
+			if((c != NULL) && (strcmp(c, ".fnp") == 0)) {
  201
+				if(n_files < max_files) {
  202
+					files[n_files] = strdup(entry->d_name);
  203
+					n_files++;
  204
+				}
  205
+			}
  206
+		}
  207
+	}
  208
+	closedir(d);
  209
+	qsort(files, n_files, sizeof(char *), cmpstringp);
  210
+	
  211
+	for(i=0;i<n_files;i++) {
  212
+		sprintf(key_bindings[i], "%s/%s", folder, files[i]);
  213
+		free(files[i]);
  214
+	}
  215
+}
  216
+
  217
+static void autobuild_callback(mtk_event *e, void *arg)
  218
+{
  219
+	char key[4];
  220
+	char filename[384];
  221
+	int sk;
  222
+	int i;
  223
+
  224
+	mtk_req(appid, key, sizeof(key), "e_key.text");
  225
+	mtk_req(appid, filename, sizeof(filename), "e_filename.text");
  226
+	if(key[0] == 0x00) {
  227
+		key[0] = 'a';
  228
+		key[1] = 0x00;
  229
+	}
  230
+	if(filename[0] == 0x00)
  231
+		strcpy(filename, "/flash");
  232
+	if((key[1] != 0x00) || (key[0] < 'a') || (key[0] > 'z')) {
  233
+		messagebox("Error", "Invalid starting key. Use only one lower case letter from 'a' to 'z'.");
  234
+		return;
  235
+	}
  236
+	i = strlen(filename);
  237
+	if(filename[i-1] == '/')
  238
+		filename[i-1] = 0x00;
  239
+	sk = key[0] - 'a';
  240
+	autobuild(sk, filename);
  241
+	update_list();
  242
+}
  243
+
166 244
 void init_keyboard()
167 245
 {
168 246
 	appid = mtk_init_app("Keyboard");
@@ -204,6 +282,7 @@ void init_keyboard()
204 282
 		"g_addedit1.columnconfig(3, -size 0)",
205 283
 		"g_addedit1.columnconfig(4, -size 0)",
206 284
 		"b_addupdate = new Button(-text \"Add/update\")",
  285
+		"b_autobuild = new Button(-text \"Auto build\")",
207 286
 
208 287
 		"g_btn = new Grid()",
209 288
 		"b_ok = new Button(-text \"OK\")",
@@ -218,8 +297,9 @@ void init_keyboard()
218 297
 		"g.place(g_addedit0, -column 1 -row 3)",
219 298
 		"g.place(g_addedit1, -column 1 -row 4)",
220 299
 		"g.place(b_addupdate, -column 1 -row 5)",
221  
-		"g.rowconfig(6, -size 10)",
222  
-		"g.place(g_btn, -column 1 -row 7)",
  300
+		"g.place(b_autobuild, -column 1 -row 6)",
  301
+		"g.rowconfig(7, -size 10)",
  302
+		"g.place(g_btn, -column 1 -row 8)",
223 303
 
224 304
 		"w = new Window(-content g -title \"Keyboard settings\")",
225 305
 		0);
@@ -229,6 +309,7 @@ void init_keyboard()
229 309
 	mtk_bind(appid, "b_filename", "commit", browse_callback, NULL);
230 310
 	mtk_bind(appid, "b_filenameclear", "commit", clear_callback, NULL);
231 311
 	mtk_bind(appid, "b_addupdate", "commit", addupdate_callback, NULL);
  312
+	mtk_bind(appid, "b_autobuild", "commit", autobuild_callback, NULL);
232 313
 
233 314
 	mtk_bind(appid, "b_ok", "commit", ok_callback, NULL);
234 315
 	mtk_bind(appid, "b_cancel", "commit", cancel_callback, NULL);

0 notes on commit d8c2954

Please sign in to comment.
Something went wrong with that request. Please try again.