Debian Codegraph @

gdraw/hotkeys.c

raw symbols SCIP
1 /* Copyright (C) 2012 by Ben Martin */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gdraw.h"
31 #include "gfile.h"
32 #include "hotkeys.h"
33 #include "intl.h"
34 #include "ustring.h"
35
36 #include <errno.h>
37 #include <locale.h>
38 #include <unistd.h>
39
40 #ifdef __MINGW32__
41 #define fsync _commit
42 #endif
43
44 static int hotkeySystemCanUseMacCommand = 0;
45
46 struct dlistnode* hotkeys = 0;
47
48 static char* hotkeyGetWindowTypeString( Hotkey* hk )
49 {
50 char* pt = strchr(hk->action,'.');
51 if( !pt )
52 return 0;
53 int len = pt - hk->action;
54 static char buffer[HOTKEY_ACTION_MAX_SIZE+1];
55 strncpy( buffer, hk->action, len );
56 buffer[len] = '\0';
57 return buffer;
58 }
59
60 /**
61 * Does the hotkey 'hk' have the right window_type to trigger its
62 * action on the window 'w'.
63 */
64 static int hotkeyHasMatchingWindowTypeString( char* windowType, Hotkey* hk ) {
65 if( !windowType )
66 return 0;
67 char* pt = strchr(hk->action,'.');
68 if( !pt )
69 return 0;
70
71 int len = pt - hk->action;
72 if( strlen(windowType) < len )
73 return 0;
74 int rc = strncmp( windowType, hk->action, len );
75 if( !rc )
76 return 1;
77 return 0;
78 }
79
80 /**
81 * Does the hotkey 'hk' have the right window_type to trigger its
82 * action on the window 'w'.
83 */
84 /*
85 * Unused
86 static int hotkeyHasMatchingWindowType( GWindow w, Hotkey* hk ) {
87 char* windowType = GDrawGetWindowTypeName( w );
88 return hotkeyHasMatchingWindowTypeString( windowType, hk );
89 }
90 */
91
92 static struct dlistnodeExternal*
93 hotkeyFindAllByStateAndKeysym( char* windowType, uint16_t state, uint16_t keysym ) {
94
95 struct dlistnodeExternal* ret = 0;
96 struct dlistnode* node = hotkeys;
97 for( ; node; node=node->next ) {
98 Hotkey* hk = (Hotkey*)node;
99 if( hk->keysym ) {
100 if( keysym == hk->keysym ) {
101 if( state == hk->state ) {
102 if( hotkeyHasMatchingWindowTypeString( windowType, hk ) ) {
103 dlist_pushfront_external( (struct dlistnode **)&ret, hk );
104 }
105 }
106 }
107 }
108 }
109 return ret;
110 }
111
112
113 static Hotkey* hotkeyFindByStateAndKeysym( char* windowType, uint16_t state, uint16_t keysym ) {
114
115 struct dlistnode* node = hotkeys;
116 for( ; node; node=node->next ) {
117 Hotkey* hk = (Hotkey*)node;
118 if( hk->keysym ) {
119 if( keysym == hk->keysym ) {
120 if( state == hk->state ) {
121 if( hotkeyHasMatchingWindowTypeString( windowType, hk ) ) {
122 return hk;
123 }
124 }
125 }
126 }
127 }
128 return 0;
129 }
130
131
132 struct dlistnodeExternal* hotkeyFindAllByEvent( GWindow w, GEvent *event ) {
133 char* windowType = GDrawGetWindowTypeName( w );
134 return hotkeyFindAllByStateAndKeysym( windowType,
135 event->u.chr.state,
136 event->u.chr.keysym );
137 }
138
139
140 Hotkey* hotkeyFindByEvent( GWindow w, GEvent *event ) {
141 char* windowType = GDrawGetWindowTypeName( w );
142 return hotkeyFindByStateAndKeysym( windowType, event->u.chr.state, event->u.chr.keysym );
143 }
144
145 /**
146 * Return the file name of the user defined hotkeys.
147 * The return value must be freed by the caller.
148 *
149 * If extension is not null it will be postpended to the returned path.
150 * This way you get to avoid dealing with appending to a string in c.
151 */
152 static char* getHotkeyFilename(char* extension) {
153 char *ret = NULL;
154 char buffer[1025];
155 char *ffdir = getFontForgeUserDir(Config);
156
157 if ( ffdir==NULL ) {
158 fprintf(stderr,_("Cannot find your hotkey definition file!\n"));
159 return NULL;
160 }
161 if( !extension )
162 extension = "";
163
164 sprintf(buffer,"%s/hotkeys%s", ffdir, extension);
165 ret = copy(buffer);
166 free(ffdir);
167 return ret;
168 }
169
170 /**
171 * Remove leading and trailing " " characters. N memory allocations
172 * are performed, null is injected at the end of string and if there are leading
173 * spaces the return value will be past them.
174 */
175 static char* trimspaces( char* line ) {
176 while ( line[strlen(line)-1]==' ' )
177 line[strlen(line)-1] = '\0';
178 while( *line == ' ' )
179 ++line;
180 return line;
181 }
182
183 // User hotkeys have priority over "native" hotkeys, but since they get set after
184 // that takes care of itself. Python hotkeys (from registerMenuItem) have the
185 // lowest priority, so this only sets one if it is free.
186 Hotkey* hotkeySetFull( const char* action, const char* keydefinition, int append, enum hk_source source )
187 {
188 Hotkey* hk = calloc(1,sizeof(Hotkey));
189 if ( hk==NULL ) return 0;
190 strncpy( hk->action, action, HOTKEY_ACTION_MAX_SIZE );
191 HotkeyParse( hk, keydefinition );
192
193 // If we didn't get a hotkey (No Shortcut)
194 // then we move along
195 if( !hk->state && !hk->keysym ) {
196 free(hk);
197 return NULL;
198 }
199
200 // If we already have a binding for that hotkey combination
201 // for this window, forget the old one. One combo = One action.
202 Hotkey* oldkey = hotkeyFindByStateAndKeysym( hotkeyGetWindowTypeString(hk),
203 hk->state, hk->keysym );
204
205 if( oldkey && oldkey->source!=hk_python && source==hk_python ) {
206 free(hk);
207 return NULL;
208 }
209
210 if( !append && oldkey ) {
211 dlist_erase( &hotkeys, (struct dlistnode *)oldkey );
212 free(oldkey);
213 }
214
215 hk->source = source;
216 dlist_pushfront( &hotkeys, (struct dlistnode *)hk );
217 return hk;
218 }
219
220 /**
221 * Load all the hotkeys from the file at filename
222 */
223 static void loadHotkeysFromFile( const char* filename, enum hk_source source, int warnIfNotFound )
224 {
225 char line[1100];
226 FILE* f = fopen(filename,"r");
227 if( !f ) {
228 if( warnIfNotFound ) {
229 fprintf(stderr,_("Failed to open hotkey definition file: %s\n"), filename );
230 }
231 return;
232 }
233
234 while ( fgets(line,sizeof(line),f)!=NULL ) {
235 int append = 0;
236
237 if ( *line=='#' )
238 continue;
239 char* pt = strchr(line,':');
240 if ( pt==NULL )
241 continue;
242 *pt = '\0';
243 char* keydefinition = pt+1;
244 chomp( keydefinition );
245 keydefinition = trimspaces( keydefinition );
246 char* action = line;
247 if( line[0] == '+' ) {
248 append = 1;
249 action++;
250 }
251
252 hotkeySetFull( action, keydefinition, append, source );
253 }
254 fclose(f);
255 }
256
257 /**
258 * Load all the default hotkeys for this locale and then the users
259 * ~/.Fontforge/hotkeys.
260 */
261 void hotkeysLoad()
262 {
263 char localefn[PATH_MAX+1];
264 char* p = 0;
265 const char* sharedir = getShareDir();
266
267 snprintf(localefn,PATH_MAX,"%s/hotkeys/default", sharedir );
268 loadHotkeysFromFile( localefn, hk_ff, true );
269
270 // FUTURE: perhaps find out how to convert en_AU.UTF-8 that setlocale()
271 // gives to its fallback of en_GB. There are likely to be a bunch of other
272 // languages which are similar but have specific locales
273 char* currentlocale = copy(setlocale(LC_MESSAGES, 0));
274 snprintf(localefn,PATH_MAX,"%s/hotkeys/%s", sharedir, currentlocale);
275 loadHotkeysFromFile( localefn, hk_ff, false );
276 while((p = strrchr( currentlocale, '.' ))) {
277 *p = '\0';
278 snprintf(localefn,PATH_MAX,"%s/hotkeys/%s", sharedir, currentlocale);
279 loadHotkeysFromFile( localefn, hk_ff, false );
280 }
281 while((p = strrchr( currentlocale, '_' ))) {
282 *p = '\0';
283 snprintf(localefn,PATH_MAX,"%s/hotkeys/%s", sharedir, currentlocale);
284 loadHotkeysFromFile( localefn, hk_ff, false );
285 }
286 free(currentlocale);
287
288 char* fn = getHotkeyFilename(0);
289 if( !fn ) {
290 return;
291 }
292 loadHotkeysFromFile( fn, hk_user, false );
293 free(fn);
294 }
295
296 static void hotkeysSaveCallback(Hotkey* hk,FILE* f) {
297 if( hk->source==hk_user ) {
298 fprintf( f, "%s:%s\n", hk->action, hk->text );
299 }
300 }
301
302 /**
303 * Save all the user defined hotkeys back to the users
304 * ~/.Fontforge/hotkeys file.
305 */
306 void hotkeysSave() {
307 char* fn = getHotkeyFilename(".new");
308 if( !fn ) {
309 return;
310 }
311 FILE* f = fopen(fn,"w");
312 if( !f ) {
313 free(fn);
314 fprintf(stderr,_("Failed to open your hotkey definition file for updates.\n"));
315 return;
316 }
317
318 dlist_foreach_reverse_udata( &hotkeys,
319 (dlist_foreach_udata_func_type)hotkeysSaveCallback, f );
320 fsync(fileno(f));
321 fclose(f);
322
323 //
324 // Atomic rename of new over the old.
325 //
326 char* newpath = getHotkeyFilename(0);
327 #ifdef __MINGW32__
328 //Atomic rename doesn't exist on Windows.
329 unlink(newpath);
330 #endif
331 int rc = rename( fn, newpath );
332 int e = errno;
333 free(fn);
334 free(newpath);
335 if( rc == -1 ) {
336 fprintf(stderr,_("Failed to rename the new hotkeys file over your old one!\n"));
337 fprintf(stderr,_("Reason:%s\n"), strerror(e));
338 }
339 }
340
341
342 char* hotkeysGetKeyDescriptionFromAction( char* action ) {
343 struct dlistnode* node = hotkeys;
344 for( ; node; node=node->next ) {
345 Hotkey* hk = (Hotkey*)node;
346 if(!strcmp(hk->action,action)) {
347 return hk->text;
348 }
349 }
350 return 0;
351 }
352
353
354 /**
355 * Find a hotkey by the action. This is useful for menus to find out
356 * what hotkey is currently bound to them. So if the user changes
357 * file/open to be alt+j then the menu can adjust the hotkey is is
358 * displaying to show the user what key they have assigned.
359 */
360 static Hotkey* hotkeyFindByAction( char* action ) {
361 struct dlistnode* node = hotkeys;
362 for( ; node; node=node->next ) {
363 Hotkey* hk = (Hotkey*)node;
364 if(!strcmp(hk->action,action)) {
365 return hk;
366 }
367 }
368 return 0;
369 }
370
371 Hotkey* isImmediateKey( GWindow w, char* path, GEvent *event )
372 {
373 char* wt = GDrawGetWindowTypeName(w);
374 if(!wt)
375 return 0;
376
377 char* subMenuName = "_ImmediateKeys";
378 char line[PATH_MAX+1];
379 snprintf(line,PATH_MAX,"%s.%s.%s",wt, subMenuName, path );
380 // printf("line:%s\n",line);
381 Hotkey* hk = hotkeyFindByAction( line );
382 if( !hk )
383 return 0;
384
385 if( event->u.chr.keysym == hk->keysym )
386 return hk;
387
388 return 0;
389 }
390
391 Hotkey* hotkeyFindByMenuPathInSubMenu( GWindow w, char* subMenuName, char* path ) {
392
393 char* wt = GDrawGetWindowTypeName(w);
394 if(!wt)
395 return 0;
396
397 char line[PATH_MAX+1];
398 snprintf(line,PATH_MAX,"%s.%s%s%s",wt, subMenuName, ".Menu.", path );
399 // printf("line:%s\n",line);
400 return(hotkeyFindByAction(line));
401 }
402
403 Hotkey* hotkeyFindByMenuPath( GWindow w, char* path ) {
404
405 char* wt = GDrawGetWindowTypeName(w);
406 if(!wt)
407 return 0;
408
409 char line[PATH_MAX+1];
410 snprintf(line,PATH_MAX,"%s%s%s",wt, ".Menu.", path );
411 return(hotkeyFindByAction(line));
412 }
413
414
415 char* hotkeyTextToMacModifiers( char* keydesc )
416 {
417 keydesc = copy( keydesc );
418 keydesc = str_replace_all( keydesc, "Ctrl", "⌘", 1 );
419 keydesc = str_replace_all( keydesc, "Ctl", "⌘", 1 );
420 keydesc = str_replace_all( keydesc, "Command", "⌘", 1 );
421 keydesc = str_replace_all( keydesc, "Cmd", "⌘", 1 );
422 keydesc = str_replace_all( keydesc, "Shift", "⇧", 1 );
423 keydesc = str_replace_all( keydesc, "Shft", "⇧", 1 );
424 keydesc = str_replace_all( keydesc, "Alt", "⎇", 1 );
425 keydesc = str_replace_all( keydesc, "+", "", 1 );
426 return keydesc;
427 }
428
429
430 char* hotkeyTextWithoutModifiers( char* hktext ) {
431 if( !strcmp( hktext, "no shortcut" )
432 || !strcmp( hktext, "No shortcut" )
433 || !strcmp( hktext, "No Shortcut" ))
434 return "";
435
436 char* p = strrchr( hktext, '+' );
437 if( !p )
438 return hktext;
439
440 //
441 // Handle Control++ by moving back over the last plus
442 //
443 if( p > hktext )
444 {
445 char* pp = p - 1;
446 if( *pp == '+' )
447 --p;
448 }
449 return p+1;
450 }
451
452
453 void hotkeySystemSetCanUseMacCommand( int v )
454 {
455 hotkeySystemCanUseMacCommand = v;
456 }
457
458 int hotkeySystemGetCanUseMacCommand()
459 {
460 return hotkeySystemCanUseMacCommand;
461 }