1 /*
2  * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /**
20  * \file
21  * GTK accelerator support
22  *
23  */
24 
25 #include <stdint.h>
26 #include <gtk/gtk.h>
27 
28 #include "utils/log.h"
29 #include "utils/errors.h"
30 #include "utils/hashtable.h"
31 
32 #include "gtk/resources.h"
33 #include "gtk/accelerator.h"
34 
35 /** acclelerators are stored in a fixed-size hash table. */
36 #define HASH_SIZE 53
37 
38 /** The hash table used to store the accelerators */
39 static struct hash_table *accelerators_hash = NULL;
40 
nsgtk_accelerator_init(char ** respaths)41 nserror nsgtk_accelerator_init(char **respaths)
42 {
43 	nserror res;
44 	const uint8_t *data;
45 	size_t data_size;
46 
47 	if (accelerators_hash == NULL) {
48 		accelerators_hash = hash_create(HASH_SIZE);
49 	}
50 	if (accelerators_hash == NULL) {
51 		NSLOG(netsurf, INFO, "Unable to create hash table");
52 		return NSERROR_NOMEM;
53 	}
54 
55 	res = nsgtk_data_from_resname("accelerators", &data, &data_size);
56 	if (res == NSERROR_OK) {
57 		res = hash_add_inline(accelerators_hash, data, data_size);
58 	} else {
59 		const char *accelerators_path;
60 		/* Obtain path to accelerators */
61 		res = nsgtk_path_from_resname("accelerators",
62 					      &accelerators_path);
63 		if (res == NSERROR_OK) {
64 			res = hash_add_file(accelerators_hash,
65 					    accelerators_path);
66 		}
67 	}
68 
69 	return res;
70 }
71 
nsgtk_accelerator_get_desc(const char * key)72 const char *nsgtk_accelerator_get_desc(const char *key)
73 {
74 	if ((key == NULL) ||
75 	    (accelerators_hash == NULL)) {
76 		return NULL;
77 	}
78 	return hash_get(accelerators_hash, key);
79 }
80