rework transportque for shuttle speed codes, add rusage, cleanup
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / shuttle.h
1 #ifndef __SHUTTLE_H__
2 #define __SHUTTLE_H__
3
4 #include "arraylist.h"
5 #include "bcwindowbase.inc"
6 #include "linklist.h"
7 #include "shuttle.inc"
8 #include "thread.h"
9
10 #include <linux/input.h>
11 #include <sys/types.h>
12 #include <regex.h>
13
14 // Copyright 2013 Eric Messick (FixedImagePhoto.com/Contact)
15 // reworked 2019 for cinelerra-gg by William Morrow (aka goodguy)
16
17 // delay in ms before processing each XTest event
18 // CurrentTime means no delay
19 #define DELAY CurrentTime
20 // playback max speed -64x .. 64x
21 #define SHUTTLE_MAX_SPEED 64.
22
23 // protocol for events from the shuttlepro HUD device
24 //
25 // ev.type values:
26 #define EVENT_TYPE_DONE 0
27 #define EVENT_TYPE_KEY 1
28 #define EVENT_TYPE_JOGSHUTTLE 2
29 #define EVENT_TYPE_ACTIVE_KEY 4
30
31 // ev.code when ev.type == KEY
32 #define EVENT_CODE_KEY1 256
33 // KEY2 257, etc...
34
35 enum { K1=0,K2,K3,K4,K5,K6,K7,K8,K9,K10,K11,K12,K13,K14,K15, };
36 enum { S_7=-7,S_6,S_5,S_4,S_3,S_2,S_1,S0,S1,S2,S3,S4,S5,S6,S7, };
37 enum { JL=0,JR };
38
39 // ev.value when ev.type == KEY
40 // 1 -> PRESS; 0 -> RELEASE
41
42 // ev.code when ev.type == JOGSHUTTLE
43 #define EVENT_CODE_JOG 7
44 #define EVENT_CODE_SHUTTLE 8
45
46 // ev.value when ev.code == JOG
47 // 8 bit value changing by one for each jog step
48
49 // ev.value when ev.code == SHUTTLE
50 // -7 .. 7 encoding shuttle position
51
52 // we define these as extra KeySyms to represent mouse events
53 #define XK_Button_0 0x2000000 // just an offset, not a real button
54 #define XK_Button_1 0x2000001
55 #define XK_Button_2 0x2000002
56 #define XK_Button_3 0x2000003
57 #define XK_Scroll_Up 0x2000004
58 #define XK_Scroll_Down 0x2000005
59
60 #define PRESS 1
61 #define RELEASE 2
62 #define PRESS_RELEASE 3
63 #define HOLD 4
64
65 #define NUM_KEYS 15
66 #define NUM_SHUTTLES 15
67 #define NUM_JOGS 2
68
69 #define KJS_KEY_DOWN 1
70 #define KJS_KEY_UP 2
71 #define KJS_SHUTTLE 3
72 #define KJS_JOG 4
73
74 // cinelerra window input targets
75 #define FOCUS_DEFAULT 0
76 #define FOCUS_MWINDOW 1
77 #define FOCUS_AWINDOW 2
78 #define FOCUS_CWINDOW 3
79 #define FOCUS_VIEWER  4
80 #define FOCUS_LOAD    5
81
82 class KeySymMapping {
83 public:
84         static KeySym to_keysym(const char *str);
85         static const char *to_string(KeySym ks);
86         static KeySymMapping key_sym_mapping[];
87         const char *str;
88         KeySym sym;
89 };
90
91 class Stroke : public ListItem<Stroke>
92 {
93 public:
94         KeySym keysym;
95         int press; // 1:press, 0:release
96 };
97
98 class Strokes : public List<Stroke>
99 {
100 public:
101         Strokes() {}
102         ~Strokes() {}
103         void clear() { while( last ) delete last; }
104         void add_stroke(KeySym keysym, int press=1) {
105                 Stroke *s = append();
106                 s->keysym = keysym; s->press = press;
107         }
108 };
109
110 class Modifiers : public ArrayList<Stroke>
111 {
112         Translation *trans;
113 public:
114         Modifiers(Translation *trans) { this->trans = trans; }
115         ~Modifiers() {}
116
117         void mark_as_down(KeySym sym, int hold);
118         void mark_as_up(KeySym sym);
119         void release(int allkeys);
120         void re_press();
121 };
122
123 class TransName
124 {
125 public:
126         int cin, err;
127         const char *name;
128         regex_t regex;
129
130         TransName(int cin, const char *nm, const char *re);
131         ~TransName();
132 };
133
134 class TransNames : public ArrayList<TransName *>
135 {
136 public:
137         TransNames() {}
138         ~TransNames() { remove_all_objects(); }
139 };
140
141 class Translation : public ListItem<Translation>
142 {
143 public:
144         Translation(Shuttle *shuttle);
145         Translation(Shuttle *shuttle, const char *name);
146         ~Translation();
147         void init(int def);
148         void clear();
149         void append_stroke(KeySym sym, int press);
150         void add_release(int all_keys);
151         void add_keystroke(const char *keySymName, int press_release);
152         void add_keysym(KeySym sym, int press_release);
153         void add_string(const char *str);
154         int start_line(const char *key);
155         void print_strokes(const char *name, const char *up_dn, Strokes *strokes);
156         void print_stroke(Stroke *s);
157         void finish_line();
158         void print_line(const char *key);
159
160         Shuttle *shuttle;
161         TransNames names;
162         const char *name;
163         int is_default, is_key;
164         int first_release_stroke;
165         Strokes *pressed, *released;
166         Strokes *pressed_strokes, *released_strokes;
167         KeySym keysym_down;
168
169         Strokes key_down[NUM_KEYS];
170         Strokes key_up[NUM_KEYS];
171         Strokes shuttles[NUM_SHUTTLES];
172         Strokes jog[NUM_JOGS];
173         Modifiers modifiers;
174 };
175
176 class Translations : public List<Translation>
177 {
178 public:
179         Translations() {}
180         ~Translations() {}
181         void clear() { while( last ) delete last; }
182 };
183
184 class Shuttle : public Thread
185 {
186         int fd;
187         unsigned short jogvalue;
188         int shuttlevalue;
189         struct timeval last_shuttle;
190         int need_synthetic_shuttle;
191         const char *dev_name;
192         Translation *default_translation;
193         Translations translations;
194 public:
195         Shuttle(MWindow *mwindow);
196         ~Shuttle();
197
198         int send_button(unsigned int button, int press);
199         int send_keycode(unsigned int keycode, int press, int send);
200         int send_keysym(KeySym keysym, int press);
201         void send_stroke_sequence(int kjs, int index);
202         void key(unsigned short code, unsigned int value);
203         void shuttle(int value);
204         void jog(unsigned int value);
205         void jogshuttle(unsigned short code, unsigned int value);
206         void start(const char *dev_name);
207         void stop();
208         void handle_event();
209         int get_focused_window_translation();
210         static const char *probe();
211         void run();
212         int read_config_file();
213         static BC_WindowBase *owns(BC_WindowBase *wdw, Window win);
214
215         int done;
216         int failed;
217         int first_time;
218         int debug;
219
220         MWindow *mwindow;
221         Translation *tr, *last_translation;
222         BC_WindowBase *wdw;
223         Window win;
224         unsigned msk;
225         int rx, ry, wx, wy;
226         Window last_focused;
227
228         const char *config_path;
229         time_t config_mtime;
230         input_event ev;
231 };
232
233 #endif