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