4c440dbc0709f6e1a71451314e8dbef56869a07f
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / shuttle.C
1 #ifdef HAVE_SHUTTLE
2 // Copyright 2013 Eric Messick (FixedImagePhoto.com/Contact)
3 // reworked 2019 for cinelerra-gg by William Morrow
4
5 #include "arraylist.h"
6 #include "cstrdup.h"
7 #include "file.h"
8 #include "guicast.h"
9 #include "linklist.h"
10 #include "loadfile.h"
11 #include "mainmenu.h"
12 #include "shuttle.h"
13 #include "thread.h"
14
15 #include "mwindow.h"
16 #include "mwindowgui.h"
17 #include "awindow.h"
18 #include "awindowgui.h"
19 #include "cwindow.h"
20 #include "cwindowgui.h"
21 #include "vwindow.h"
22 #include "vwindowgui.h"
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <sys/stat.h>
32
33 #include <X11/Xlib.h>
34 #include <X11/keysym.h>
35
36 static Time milliTimeClock()
37 {
38         struct timeval tv;
39         gettimeofday(&tv, 0);
40         return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
41 }
42
43 KeySymMapping KeySymMapping::key_sym_mapping[] = {
44         { "XK_Button_1", XK_Button_1 },
45         { "XK_Button_2", XK_Button_2 },
46         { "XK_Button_3", XK_Button_3 },
47         { "XK_Scroll_Up", XK_Scroll_Up },
48         { "XK_Scroll_Down", XK_Scroll_Down },
49 #include "shuttle_keys.h"
50         { NULL, 0 }
51 };
52
53 KeySym KeySymMapping::to_keysym(const char *str)
54 {
55         for( KeySymMapping *ksp = &key_sym_mapping[0]; ksp->str; ++ksp )
56                 if( !strcmp(str, ksp->str) ) return ksp->sym;
57         return 0;
58 }
59
60 const char *KeySymMapping::to_string(KeySym ks)
61 {
62         for( KeySymMapping *ksp = &key_sym_mapping[0]; ksp->sym; ++ksp )
63                 if( ksp->sym == ks ) return ksp->str;
64         return 0;
65 }
66
67 TransName::TransName(int cin, const char *nm, const char *re)
68 {
69         this->cin = cin;
70         this->name = cstrdup(nm);
71         this->err = regcomp(&this->regex, re, REG_NOSUB);
72         if( err ) {
73                 fprintf(stderr, "error compiling regex for [%s]: %s\n", name, re);
74                 char emsg[BCTEXTLEN];
75                 regerror(err, &regex, emsg, sizeof(emsg));
76                 fprintf(stderr, "regerror: %s\n", emsg);
77         }
78 }
79 TransName::~TransName()
80 {
81         delete [] name;
82         regfree(&regex);
83 }
84
85 void Translation::init(int def)
86 {
87         is_default = def;
88         is_key = 0;
89         first_release_stroke = 0;
90         pressed = 0;
91         released = 0;
92         keysym_down = 0;
93 }
94
95 Translation::Translation(Shuttle *shuttle)
96  : modifiers(this)
97 { // initial default translation
98         init(1);
99         this->shuttle = shuttle;
100         this->name = cstrdup("Default");
101         names.append(new TransName(FOCUS_DEFAULT, name, ""));
102         key_down[K6].add_stroke(XK_Button_1, 1);
103         key_up[K6].add_stroke(XK_Button_1, 0);
104         key_down[K7].add_stroke(XK_Button_2, 1);
105         key_up[K7].add_stroke(XK_Button_2, 0);
106         key_down[K8].add_stroke(XK_Button_3, 1);
107         key_up[K8].add_stroke(XK_Button_3, 0);
108         jog[JL].add_stroke(XK_Scroll_Up, 1);
109         jog[JL].add_stroke(XK_Scroll_Up, 0);
110         jog[JR].add_stroke(XK_Scroll_Down, 0);
111         jog[JR].add_stroke(XK_Scroll_Down, 1);
112 }
113
114 Translation::Translation(Shuttle *shuttle, const char *name)
115  : modifiers(this)
116 {
117         init(0);
118         this->shuttle = shuttle;
119         this->name = cstrdup(name);
120 }
121
122 Translation::~Translation()
123 {
124         delete [] name;
125 }
126
127 void Translation::clear()
128 {
129         names.remove_all_objects();
130         init(0);
131         for( int i=0; i<NUM_KEYS; ++i ) key_down[i].clear();
132         for( int i=0; i<NUM_KEYS; ++i ) key_up[i].clear();
133         for( int i=0; i<NUM_SHUTTLES; ++i ) shuttles[i].clear();
134         for( int i=0; i<NUM_JOGS; ++i ) jog[i].clear();
135 }
136
137 void Translation::append_stroke(KeySym sym, int press)
138 {
139         Stroke *s = pressed_strokes->append();
140         s->keysym = sym;
141         s->press = press;
142 }
143
144 void Translation::add_keysym(KeySym sym, int press_release)
145 {
146 //printf("add_keysym(0x%x, %d)\n", (int)sym, press_release);
147         switch( press_release ) {
148         case PRESS:
149                 append_stroke(sym, 1);
150                 modifiers.mark_as_down(sym, 0);
151                 break;
152         case RELEASE:
153                 append_stroke(sym, 0);
154                 modifiers.mark_as_up(sym);
155                 break;
156         case HOLD:
157                 append_stroke(sym, 1);
158                 modifiers.mark_as_down(sym, 1);
159                 break;
160         case PRESS_RELEASE:
161         default:
162                 if( first_release_stroke ) {
163                         modifiers.re_press();
164                         first_release_stroke = 0;
165                 }
166                 if( keysym_down ) {
167                         append_stroke(keysym_down, 0);
168                 }
169                 append_stroke(sym, 1);
170                 keysym_down = sym;
171                 break;
172         }
173 }
174
175 void Translation::add_release(int all_keys)
176 {
177 //printf("add_release(%d)\n", all_keys);
178         modifiers.release(all_keys);
179         if( !all_keys ) {
180                 pressed_strokes = released_strokes;
181         }
182         if( keysym_down ) {
183                 append_stroke(keysym_down, 0);
184                 keysym_down = 0;
185         }
186         first_release_stroke = 1;
187 }
188
189 void Translation::add_keystroke(const char *keySymName, int press_release)
190 {
191         KeySym sym;
192
193         if( is_key && !strncmp(keySymName, "RELEASE", 8) ) {
194                 add_release(0);
195                 return;
196         }
197         sym = KeySymMapping::to_keysym(keySymName);
198         if( sym != 0 ) {
199                 add_keysym(sym, press_release);
200         }
201         else
202                 fprintf(stderr, "unrecognized KeySym: %s\n", keySymName);
203 }
204
205 void Translation::add_string(const char *str)
206 {
207         while( str && *str ) {
208                 if( *str >= ' ' && *str <= '~' )
209                         add_keysym((KeySym)(*str), PRESS_RELEASE);
210                 ++str;
211         }
212 }
213
214 int Translation::start_line(const char *key)
215 {
216         pressed_strokes = 0;
217         released_strokes = 0;
218         pressed = released = 0;
219         is_key = 0;
220         if( !strcasecmp("JL", key) ) {
221                 pressed = &jog[0];
222         }
223         else if( !strcasecmp("JR", key) ) {
224                 pressed = &jog[1];
225         }
226         else {
227                 char c = 0;  int k = -1, n = 0;
228                 if( sscanf(key, "%c%d%n", &c, &k, &n) != 2 ) return 1;
229                 switch( c ) {
230                 case 'K': case 'k':
231                         --k;
232                         if( k >= K1 && k <= K15 ) {
233                                 pressed = &key_down[k];
234                                 released = &key_up[k];
235                                 is_key = 1;
236                         }
237                         break;
238                 case 'S': case 's':
239                         if( k >= S_7 && k <= S7 ) {
240                                 pressed = &shuttles[k-S_7];
241                         }
242                         break;
243                 }
244                 if( !pressed ) {
245                         fprintf(stderr, "bad key name: [%s]%s\n", name, key);
246                         return 1;
247                 }
248                 if( pressed->first ) {
249                         fprintf(stderr, "dupl key name: [%s]%s\n", name, key);
250                         return 1;
251                 }
252         }
253         pressed_strokes = pressed;
254         released_strokes = released;
255         return 0;
256 }
257
258 void Translation::print_stroke(Stroke *s)
259 {
260         if( !s ) return;
261         const char *cp = KeySymMapping::to_string(s->keysym);
262         if( !cp ) { printf("0x%x", (int)s->keysym); cp = "???"; }
263         printf("%s/%c ", cp, s->press ? 'D' : 'U');
264 }
265
266 void Translation::print_strokes(const char *name, const char *up_dn, Strokes *strokes)
267 {
268         printf("%s[%s]: ", name, up_dn);
269         for( Stroke *s=strokes->first; s; s=s->next )
270                 print_stroke(s);
271         printf("\n");
272 }
273
274 void Translation::finish_line()
275 {
276 //printf("finish_line()\n");
277         if( is_key ) {
278                 add_release(0);
279         }
280         add_release(1);
281 }
282
283 void Translation::print_line(const char *key)
284 {
285         if( is_key ) {
286                 print_strokes(key, "D", pressed_strokes);
287                 print_strokes(key, "U", released_strokes);
288         }
289         else {
290                 print_strokes(key, "", pressed_strokes);
291         }
292         printf("\n");
293 }
294
295 // press values in Modifiers:
296 // PRESS -> down
297 // HOLD -> held
298 // PRESS_RELEASE -> released, but to be re-pressed if necessary
299 // RELEASE -> up
300
301 void Modifiers::mark_as_down(KeySym sym, int hold)
302 {
303         Modifiers &modifiers = *this;
304         for( int i=0; i<size(); ++i ) {
305                 Stroke &s = modifiers[i];
306                 if( s.keysym == sym ) {
307                         s.press = hold ? HOLD : PRESS;
308                         return;
309                 }
310         }
311         Stroke &s = append();
312         s.keysym = sym;
313         s.press = hold ? HOLD : PRESS;
314 }
315
316 void Modifiers::mark_as_up(KeySym sym)
317 {
318         Modifiers &modifiers = *this;
319         for( int i=0; i<size(); ++i ) {
320                 Stroke &s = modifiers[i];
321                 if( s.keysym == sym ) {
322                         s.press = RELEASE;
323                         return;
324                 }
325         }
326 }
327
328 void Modifiers::release(int allkeys)
329 {
330         Modifiers &modifiers = *this;
331         for( int i=0; i<size(); ++i ) {
332                 Stroke &s = modifiers[i];
333                 if( s.press == PRESS ) {
334                         trans->append_stroke(s.keysym, 0);
335                         s.press = PRESS_RELEASE;
336                 }
337                 else if( allkeys && s.press == HOLD ) {
338                         trans->append_stroke(s.keysym, 0);
339                         s.press = RELEASE;
340                 }
341         }
342 }
343
344 void Modifiers::re_press()
345 {
346         Modifiers &modifiers = *this;
347         for( int i=0; i<size(); ++i ) {
348                 Stroke &s = modifiers[i];
349                 if( s.press == PRESS_RELEASE ) {
350                         trans->append_stroke(s.keysym, 1);
351                         s.press = PRESS;
352                 }
353         }
354 }
355
356
357 Shuttle::Shuttle(MWindow *mwindow)
358  : Thread(0, 0, 0)
359 {
360         this->mwindow = mwindow;
361
362         fd = -1;
363         wdw = 0;
364         win = 0;
365         msk = 0;
366         rx = ry = 0;
367         wx = wy = 0;
368         jogvalue = 0xffff;
369         shuttlevalue = 0xffff;
370         last_shuttle.tv_sec = 0;
371         last_shuttle.tv_usec = 0;
372         need_synthetic_shuttle = 0;
373         dev_name = 0;
374
375         done = -1;
376         failed = 0;
377         first_time = 1;
378         tr = 0;
379         last_translation = 0;
380         last_focused = 0;
381
382         default_translation = new Translation(this);
383         config_path = 0;
384         config_mtime = 0;
385         ev.type = ~0;
386 }
387
388 Shuttle::~Shuttle()
389 {
390         stop();
391         delete default_translation;
392         delete [] config_path;
393 }
394
395 int Shuttle::send_button(unsigned int button, int press)
396 {
397         if( debug )
398                 printf("btn: %u %d\n", button, press);
399         XButtonEvent *b = new XButtonEvent();
400         memset(b, 0, sizeof(*b));
401         b->type = press ? ButtonPress : ButtonRelease;
402         b->time = milliTimeClock();
403         b->display = wdw->top_level->display;
404         b->root = wdw->top_level->rootwin;
405         b->window = win;
406         b->x_root = rx;
407         b->y_root = ry;
408         b->x = wx;
409         b->y = wy;
410         b->state = msk;
411         b->button = button;
412         b->same_screen = 1;
413         wdw->top_level->put_event((XEvent *) b);
414         return 0;
415 }
416 int Shuttle::send_key(KeySym keysym, int press)
417 {
418         KeyCode keycode = XKeysymToKeycode(wdw->top_level->display, keysym);
419         if( debug )
420                 printf("key: %04x %d\n", (unsigned)keycode, press);
421         XKeyEvent *k = new XKeyEvent();
422         memset(k, 0, sizeof(*k));
423         k->type = press ? KeyPress : KeyRelease;
424         k->time = milliTimeClock();
425         k->display = wdw->top_level->display;
426         k->root = wdw->top_level->rootwin;
427         k->window = win;
428         k->x_root = rx;
429         k->y_root = ry;
430         k->x = wx;
431         k->y = wy;
432         k->state = msk;
433         k->keycode = keycode;
434         k->same_screen = 1;
435         wdw->top_level->put_event((XEvent *) k);
436         return 0;
437 }
438
439 int Shuttle::send_keysym(KeySym keysym, int press)
440 {
441         return keysym >= XK_Button_1 && keysym <= XK_Scroll_Down ?
442                 send_button((unsigned int)keysym - XK_Button_0, press) :
443                 send_key(keysym, press ? True : False);
444 }
445
446
447 static Stroke *fetch_stroke(Translation *translation, int kjs, int index)
448 {
449         Stroke *ret = 0;
450         if( translation ) {
451                 switch( kjs ) {
452                 default:
453                 case KJS_KEY_DOWN:  ret = translation->key_down[index].first;   break;
454                 case KJS_KEY_UP:    ret = translation->key_up[index].first;     break;
455                 case KJS_JOG:       ret = translation->jog[index].first;        break;
456                 case KJS_SHUTTLE:   ret = translation->shuttles[index].first;   break;
457                 }
458         }
459         return ret;
460 }
461
462 void Shuttle::send_stroke_sequence(int kjs, int index)
463 {
464         if( !wdw ) return;
465         Stroke *s = fetch_stroke(tr, kjs, index);
466         if( !s ) s = fetch_stroke(default_translation, kjs, index);
467         while( s ) {
468                 send_keysym(s->keysym, s->press);
469                 s = s->next;
470         }
471 }
472
473 void Shuttle::key(unsigned short code, unsigned int value)
474 {
475         code -= EVENT_CODE_KEY1;
476         if( code >= NUM_KEYS ) {
477                 fprintf(stderr, "key(%d, %d) out of range\n", code + EVENT_CODE_KEY1, value);
478                 return;
479         }
480         send_stroke_sequence(value ? KJS_KEY_DOWN : KJS_KEY_UP, code);
481 }
482
483
484 void Shuttle:: shuttle(int value)
485 {
486         if( value < S_7 || value > S7 ) {
487                 fprintf(stderr, "shuttle(%d) out of range\n", value);
488                 return;
489         }
490         gettimeofday(&last_shuttle, 0);
491         need_synthetic_shuttle = value != 0;
492         if( value != shuttlevalue ) {
493                 shuttlevalue = value;
494                 send_stroke_sequence(KJS_SHUTTLE, value+7);
495         }
496 }
497
498 // Due to a bug (?) in the way Linux HID handles the ShuttlePro, the
499 // center position is not reported for the shuttle wheel.       Instead,
500 // a jog event is generated immediately when it returns.        We check to
501 // see if the time since the last shuttle was more than a few ms ago
502 // and generate a shuttle of 0 if so.
503 //
504 // Note, this fails if jogvalue happens to be 0, as we don't see that
505 // event either!
506 void Shuttle::jog(unsigned int value)
507 {
508         int direction;
509         struct timeval now;
510         struct timeval delta;
511
512         // We should generate a synthetic event for the shuttle going
513         // to the home position if we have not seen one recently
514         if( need_synthetic_shuttle ) {
515                 gettimeofday( &now, 0 );
516                 timersub( &now, &last_shuttle, &delta );
517
518                 if( delta.tv_sec >= 1 || delta.tv_usec >= 5000 ) {
519                         shuttle(0);
520                         need_synthetic_shuttle = 0;
521                 }
522         }
523
524         if( jogvalue != 0xffff ) {
525                 value = value & 0xff;
526                 direction = ((value - jogvalue) & 0x80) ? -1 : 1;
527                 while( jogvalue != value ) {
528                         // driver fails to send an event when jogvalue == 0
529                         if( jogvalue != 0 ) {
530         send_stroke_sequence(KJS_JOG, direction > 0 ? 1 : 0);
531                         }
532                         jogvalue = (jogvalue + direction) & 0xff;
533                 }
534         }
535         jogvalue = value;
536 }
537
538 void Shuttle::jogshuttle(unsigned short code, unsigned int value)
539 {
540         switch( code ) {
541         case EVENT_CODE_JOG:
542                 jog(value);
543                 break;
544         case EVENT_CODE_SHUTTLE:
545                 shuttle(value);
546                 break;
547         default:
548                 fprintf(stderr, "jogshuttle(%d, %d) invalid code\n", code, value);
549                 break;
550         }
551 }
552
553 const char *Shuttle::probe()
554 {
555         struct stat st;
556         static const char *shuttle_devs[] = {
557                 "/dev/input/by-id/usb-Contour_Design_ShuttleXpress-event-if00",
558                 "/dev/input/by-id/usb-Contour_Design_ShuttlePRO_v2-event-if00",
559         };
560         int ret = sizeof(shuttle_devs) / sizeof(shuttle_devs[0]);
561         while( --ret >= 0 && stat(shuttle_devs[ret] , &st) );
562         return ret >= 0 ? shuttle_devs[ret] : 0;
563 }
564
565 void Shuttle::start(const char *dev_name)
566 {
567         this->dev_name = dev_name;
568         first_time = 1;
569         done = 0;
570         Thread::start();
571 }
572
573 void Shuttle::stop()
574 {
575         if( running() && !done ) {
576                 done = 1;
577                 cancel();
578                 join();
579         }
580 }
581
582
583 int Shuttle::get_focused_window_translation()
584 {
585         MWindowGUI *gui = mwindow->gui;
586         Display *dpy = gui->display;
587         Window focus = 0;
588         int ret = 0, revert = 0;
589         char win_title[BCTEXTLEN];
590         gui->lock_window("Shuttle::get_focused_window_translation");
591         XGetInputFocus(dpy, &focus, &revert);
592         if( last_focused != focus ) {
593                 last_focused = focus;
594                 Atom prop = XInternAtom(dpy, "WM_NAME", False);
595                 Atom type;  int form;
596                 unsigned long remain, len;
597                 unsigned char *list;
598                 if( XGetWindowProperty(dpy, focus, prop, 0, sizeof(win_title)-1, False,
599                         AnyPropertyType, &type, &form, &len, &remain, &list) == Success ) {
600                         len = len*(form/8) - remain;
601                         memcpy(win_title, list, len);
602                         win_title[len] = 0;
603                         XFree(list);
604                         if( debug )
605                                 printf("new focus: %08x\n", (unsigned)focus);
606                 }
607                 else {
608                         last_focused = 0;
609                         fprintf(stderr, "XGetWindowProperty failed for window 0x%x\n",
610                                         (int)focus);
611                         ret = 1;
612                 }
613         }
614         gui->unlock_window();
615         if( ret ) return -1;
616
617         this->wdw = 0;  this->win = 0;
618         this->wx = 0;   this->wy = 0;
619         this->rx = 0;   this->ry = 0;
620         this->msk = 0;
621         BC_WindowBase *wdw = 0;
622         int cin = -1;
623         if( (wdw=mwindow->gui) && wdw->win == focus )
624                 cin = FOCUS_MWINDOW;
625         else if( (wdw=mwindow->awindow->gui) && wdw->win == focus )
626                 cin = FOCUS_AWINDOW;
627         else if( (wdw=mwindow->cwindow->gui) && wdw->win == focus )
628                 cin = FOCUS_CWINDOW;
629         else if( (wdw=mwindow->gui->mainmenu->load_file->thread->window) &&
630                  wdw->win == focus )
631                 cin = FOCUS_LOAD;
632         else {
633                 int i = mwindow->vwindows.size();
634                 while( --i >= 0 ) {
635                         VWindow *vwdw =  mwindow->vwindows[i];
636                         if( !vwdw->is_running() ) continue;
637                         if( (wdw=vwdw->gui) && wdw->win == focus ) {
638                                 cin = FOCUS_VIEWER;  break;
639                         }
640                 }
641         }
642         if( cin < 0 ) return -1;
643         Window root = 0, child = 0;
644         int root_x = 0, root_y = 0, win_x = 0, win_y = 0, x = 0, y = 0;
645         unsigned int mask = 0, width = 0, height = 0, border_width = 0, depth = 0;
646         wdw->lock_window("Shuttle::get_focused_window_translation 1");
647         if( XQueryPointer(wdw->display, focus, &root, &child,
648                         &root_x, &root_y, &win_x, &win_y, &mask) ) {
649                 if( !child ) {
650                         if( wdw->active_menubar )
651                                 child = wdw->active_menubar->win;
652                         else if( wdw->active_popup_menu )
653                                 child = wdw->active_popup_menu->win;
654                         else if( wdw->active_subwindow )
655                                 child = wdw->active_subwindow->win;
656                 }
657                 if( child )
658                         XGetGeometry(wdw->display, child, &root, &x, &y,
659                                 &width, &height, &border_width, &depth);
660         }
661         wdw->unlock_window();
662         if( !child || !wdw->match_window(child) ) return -1;
663 // success
664         this->wdw = wdw;
665         this->win = child;
666         this->msk = mask;
667         this->rx = root_x;
668         this->ry = root_y;
669         this->wx = win_x - x;
670         this->wy = win_y - y;
671         for( tr=translations.first; tr; tr=tr->next ) {
672                 if( tr->is_default ) return 1;
673                 for( int i=0; i<tr->names.size(); ++i ) {
674                         TransName *name = tr->names[i];
675                         if( name->cin != cin ) continue;
676                         if( regexec(&name->regex, win_title, 0, NULL, 0) )
677                                 return 1;
678                 }
679         }
680         tr = default_translation;
681         return 0;
682 }
683
684 void Shuttle::handle_event()
685 {
686         if( read_config_file() > 0 ) {
687                 done = 1;
688                 return;
689         }
690         if( get_focused_window_translation() < 0 )
691                 return;
692         if( last_translation != tr ) {
693                 last_translation = tr;
694                 if( debug )
695                         printf("new translation: %s\n", tr->name);
696         }
697 //fprintf(stderr, "event: (%d, %d, 0x%x)\n", ev.type, ev.code, ev.value);
698         switch( ev.type ) {
699         case EVENT_TYPE_DONE:
700         case EVENT_TYPE_ACTIVE_KEY:
701                 break;
702         case EVENT_TYPE_KEY:
703                 key(ev.code, ev.value);
704                 break;
705         case EVENT_TYPE_JOGSHUTTLE:
706                 jogshuttle(ev.code, ev.value);
707                 break;
708         default:
709                 fprintf(stderr, "handle_event() invalid type code\n");
710                 break;
711         }
712 }
713
714 void Shuttle::run()
715 {
716         for( enable_cancel(); !done; sleep(1) ) {
717                 fd = open(dev_name, O_RDONLY);
718                 if( fd < 0 ) {
719                         perror(dev_name);
720                         if( first_time ) break;
721                         continue;
722                 }
723                 if( !ioctl(fd, EVIOCGRAB, 1) ) { // exclusive access
724                         first_time = 0;
725                         while( !done ) {
726                                 int ret = read(fd, &ev, sizeof(ev));
727                                 if( done ) break;
728                                 if( ret != sizeof(ev) ) {
729                                         if( ret < 0 ) { perror("read event"); break; }
730                                         fprintf(stderr, "bad read: %d\n", ret);
731                                         break;
732                                 }
733                                 handle_event();
734                         }
735                 }
736                 else
737                         perror( "evgrab ioctl" );
738                 close(fd);
739         }
740         done = 2;
741 }
742
743 int Shuttle::read_config_file()
744 {
745         if( !config_path ) {
746                 const char *env;
747                 config_path = (env=getenv("SHUTTLE_CONFIG_FILE")) != 0 ? cstrdup(env) :
748                         (env=getenv("HOME")) != 0 ? cstrcat(2, env, "/.shuttlerc") : 0;
749                 if( !config_path ) { fprintf(stderr, "no config file\n");  return 1; }
750         }
751         struct stat st;
752         if( stat(config_path, &st) ) {
753                 perror(config_path);
754                 char shuttlerc[BCTEXTLEN];
755                 snprintf(shuttlerc, sizeof(shuttlerc), "%s/shuttlerc",
756                                 File::get_cindat_path());
757                 if( stat(shuttlerc, &st) ) {
758                         perror(shuttlerc);
759                         return 1;
760                 }
761                 delete [] config_path;
762                 config_path = cstrdup(shuttlerc);
763         }
764         if( config_mtime > 0 &&
765             config_mtime == st.st_mtime ) return 0;
766         FILE *fp = fopen(config_path, "r");
767         if( !fp ) {
768                 perror(config_path);
769                 return 1;
770         }
771
772         config_mtime = st.st_mtime;
773         translations.clear();
774         debug = 0;
775 #define ws(ch) (ch==' ' || ch=='\t')
776         char line[BCTEXTLEN], *cp;
777         Translation *trans = 0;
778         int no = 0;
779         int ret = fgets(cp=line,sizeof(line),fp) ? 0 : -1;
780         if( !ret ) ++no;
781 // lines
782         while( !ret ) {
783 // translation names
784                 while( !ret && *cp == '[' ) {
785                         char *name = ++cp;
786                         while( *cp && *cp != ']' ) ++cp;
787                         *cp++ = 0;
788                         if( !name || !*name ) { ret = 1;  break; }
789                         int cin = -1;
790                         if( !strcasecmp("default", name) )
791                                 cin = FOCUS_DEFAULT;
792                         else if( !strcasecmp("cinelerra", name) )
793                                 cin = FOCUS_MWINDOW;
794                         else if( !strcasecmp("resources", name) )
795                                 cin = FOCUS_AWINDOW;
796                         else if( !strcasecmp("composer", name) )
797                                 cin = FOCUS_CWINDOW;
798                         else if( !strcasecmp("viewer", name) )
799                                 cin = FOCUS_VIEWER;
800                         else if( !strcasecmp("load", name) )
801                                 cin = FOCUS_LOAD;
802                         else {
803                                 fprintf(stderr, "unknown focus target window: %s\n",
804                                          name);
805                                 ret = 1;  break;
806                         }
807                         if( !trans ) {
808                                 if( cin == FOCUS_DEFAULT ) {
809                                         trans = default_translation;
810                                         trans->clear();
811                                 }
812                                 else {
813                                         trans = new Translation(this, name);
814                                 }
815                         }
816                         while( ws(*cp) ) ++cp;
817 // regex in TransName constructor
818                         trans->names.append(new TransName(cin, name, cp));
819                         if( trans->names.last()->err ) { ret = 1;  break; }
820                         ret = fgets(cp=line,sizeof(line),fp) ? 0 : 1;
821                         if( ret ) {
822                                 fprintf(stderr, "hit eof, no translation def for: %s\n",
823                                         trans->names.last()->name);
824                                 ret = 1;  break;
825                         }
826                         ++no;
827                 }
828                 if( ret ) break;
829                 if( debug && trans ) {
830                         printf("------------------------\n");
831                         TransNames &names = trans->names;
832                         for( int i=0; i<names.size(); ++i ) {
833                                 TransName *tp = names[i];
834                                 printf("[%s] # %d\n\n", tp->name, tp->cin);
835                         }
836                 }
837 // rules lines: "tok <stroke list>\n"
838                 while( !ret && *cp != '[' ) {
839                         const char *key = 0, *tok = 0;
840                         while( ws(*cp) ) ++cp;
841                         if( !*cp || *cp == '\n' || *cp == '#' ) goto skip;
842                         tok = cp;
843                         while( *cp && !ws(*cp) && *cp != '\n' ) ++cp;
844                         *cp++ = 0;
845                         if( !strcmp(tok, "DEBUG") ) {
846                                 debug = 1;  goto skip;
847                         }
848                         key = tok;
849                         if( !trans ) {
850                                 fprintf(stderr, "no translation section defining key: %s\n", key);
851                                 ret = 1;  break;
852                         }
853         
854                         ret = trans->start_line(key);
855                         while( !ret && *cp && *cp != '\n' ) {
856                                 while( ws(*cp) ) ++cp;
857                                 if( !*cp || *cp == '#' || *cp == '\n' ) break;
858                                 if( *cp == '"' ) {
859                                         tok = ++cp;
860                                         while( *cp && *cp != '"' && *cp != '\n' ) {
861                                                 if( *cp != '\\' ) { ++cp;  continue; }
862                                                 for( char *bp=cp; *bp; ++bp ) bp[0] = bp[1];
863                                         }
864                                         *cp++ = 0;
865                                         trans->add_string(tok);
866                                         continue;
867                                 }
868                                 tok = cp;
869                                 while( *cp && !ws(*cp) && *cp!='/' && *cp != '\n' ) ++cp;
870                                 int dhu = PRESS_RELEASE;
871                                 if( *cp == '/' ) {
872                                         *cp++ = 0;
873                                         switch( *cp ) {
874                                         case 'D':  dhu = PRESS;    break;
875                                         case 'H':  dhu = HOLD;     break;
876                                         case 'U':  dhu = RELEASE;  break;
877                                         default:
878                                                 fprintf(stderr, "invalid up/down modifier [%s]%s: '%c'\n",
879                                                         trans->name, tok, *cp);
880                                                 ret = 1;  break;
881                                         }
882                                         ++cp;
883                                 }
884                                 else
885                                         *cp++ = 0;
886                                 trans->add_keystroke(tok, dhu);
887                         }
888                         if( ret ) break;
889                         trans->finish_line();
890                         if( debug )
891                                 trans->print_line(key);
892                 skip:   ret = fgets(cp=line,sizeof(line),fp) ? 0 : -1;
893                         if( !ret ) ++no;
894                 }
895                 if( trans ) {
896                         if( trans != default_translation )
897                                 translations.append(trans);
898                         trans = 0;
899                 }
900         }
901         if( ret > 0 )
902                 fprintf(stderr, "shuttle config err file: %s, line:%d\n",
903                         config_path, no);
904
905         fclose(fp);
906         return ret;
907 }
908 #endif