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