1 #ifndef EL__TERMINAL_EVENT_H
2 #define EL__TERMINAL_EVENT_H
3 
4 #include "terminal/kbd.h"
5 #include "terminal/mouse.h"
6 
7 struct terminal;
8 
9 /* Some constants for the strings inside of {struct terminal}. */
10 
11 #define MAX_TERM_LEN	32	/* this must be multiple of 8! (alignment problems) */
12 #define MAX_CWD_LEN	256	/* this must be multiple of 8! (alignment problems) */
13 
14 
15 enum term_event_type {
16 	EVENT_INIT,
17 	EVENT_KBD,
18 	EVENT_MOUSE,
19 	EVENT_REDRAW,
20 	EVENT_RESIZE,
21 	EVENT_ABORT,
22 };
23 
24 struct term_event {
25 	enum term_event_type ev;
26 
27 	union {
28 		/* EVENT_MOUSE */
29 		struct term_event_mouse mouse;
30 
31 		/* EVENT_KBD */
32 		struct term_event_keyboard keyboard;
33 
34 		/* EVENT_INIT, EVENT_RESIZE, EVENT_REDRAW */
35 		struct term_event_size {
36 			int width, height;
37 		} size;
38 	} info;
39 };
40 
41 static inline void
set_mouse_term_event(struct term_event * ev,int x,int y,unsigned int button)42 set_mouse_term_event(struct term_event *ev, int x, int y, unsigned int button)
43 {
44 	memset(ev, 0, sizeof(*ev));
45 	ev->ev = EVENT_MOUSE;
46 	set_mouse(&ev->info.mouse, x, y, button);
47 }
48 
49 static inline void
set_kbd_term_event(struct term_event * ev,int key,int modifier)50 set_kbd_term_event(struct term_event *ev, int key, int modifier)
51 {
52 	memset(ev, 0, sizeof(*ev));
53 	ev->ev = EVENT_KBD;
54 	kbd_set(&ev->info.keyboard, key, modifier);
55 }
56 
57 static inline void
set_abort_term_event(struct term_event * ev)58 set_abort_term_event(struct term_event *ev)
59 {
60 	memset(ev, 0, sizeof(*ev));
61 	ev->ev = EVENT_ABORT;
62 }
63 
64 static inline void
set_wh_term_event(struct term_event * ev,enum term_event_type type,int width,int height)65 set_wh_term_event(struct term_event *ev, enum term_event_type type, int width, int height)
66 {
67 	memset(ev, 0, sizeof(*ev));
68 	ev->ev = type;
69 	ev->info.size.width = width;
70 	ev->info.size.height = height;
71 }
72 
73 #define set_init_term_event(ev, w, h) set_wh_term_event(ev, EVENT_INIT, w, h)
74 #define set_resize_term_event(ev, w, h) set_wh_term_event(ev, EVENT_RESIZE, w, h)
75 #define set_redraw_term_event(ev, w, h) set_wh_term_event(ev, EVENT_REDRAW, w, h)
76 
77 
78 /* This holds the information used when handling the initial connection between
79  * a dumb and master terminal. */
80 /* XXX: We might be connecting to an older ELinks or an older ELinks is
81  * connecting to a newer ELinks master so for the sake of compatibility it
82  * would be unwise to just change the layout of the struct. If you do have to
83  * add new members add them at the bottom and use magic variables to
84  * distinguish them when decoding the terminal info. */
85 struct terminal_info {
86 	struct term_event event;		/* The EVENT_INIT event */
87 	unsigned char name[MAX_TERM_LEN];	/* $TERM environment name */
88 	unsigned char cwd[MAX_CWD_LEN];		/* Current working directory */
89 	int system_env;				/* System info (X, screen) */
90 	int length;				/* Length of @data member */
91 	int session_info;			/* Value depends on @magic */
92 	int magic;				/* Identity of the connector */
93 
94 	/* In the master that is connected to all bytes after @data will be
95 	 * interpreted as URI string information. */
96 	unsigned char data[1];
97 };
98 
99 /* The @data member has to have size of one for portability but it can be
100  * empty/zero so when reading and writing it we need to ignore the byte. */
101 #define TERMINAL_INFO_SIZE offsetof(struct terminal_info, data)
102 
103 /* We use magic numbers to signal the identity of the dump client terminal.
104  * Magic numbers are composed by the INTERLINK_MAGIC() macro. It is a negative
105  * magic to be able to distinguish the oldest format from the newer ones. */
106 #define INTERLINK_MAGIC(major, minor) -(((major) << 8) + (minor))
107 
108 #define INTERLINK_NORMAL_MAGIC INTERLINK_MAGIC(1, 0)
109 #define INTERLINK_REMOTE_MAGIC INTERLINK_MAGIC(1, 1)
110 
111 void term_send_event(struct terminal *, struct term_event *);
112 void in_term(struct terminal *);
113 
114 /* For keyboard events handling */
115 #define get_kbd_key(event)		(kbd_get_key(&(event)->info.keyboard))
116 #define check_kbd_key(event, key)	(kbd_key_is(&(event)->info.keyboard, (key)))
117 
118 #define get_kbd_modifier(event)		(kbd_get_modifier(&(event)->info.keyboard))
119 #define check_kbd_modifier(event, mod)	(kbd_modifier_is(&(event)->info.keyboard, (mod)))
120 
121 #define check_kbd_textinput_key(event)	(get_kbd_key(event) >= ' ' && get_kbd_key(event) < 256 && check_kbd_modifier(event, KBD_MOD_NONE))
122 #define check_kbd_label_key(event)	(get_kbd_key(event) > ' ' && get_kbd_key(event) < 256)
123 
124 
125 /* For mouse events handling */
126 #define get_mouse_action(event)		 (mouse_get_action(&(event)->info.mouse))
127 #define check_mouse_action(event, value) (mouse_action_is(&(event)->info.mouse, (value)))
128 
129 #define get_mouse_button(event)		 (mouse_get_button(&(event)->info.mouse))
130 #define check_mouse_button(event, value) (mouse_button_is(&(event)->info.mouse, (value)))
131 #define check_mouse_wheel(event)	 (mouse_wheeling(&(event)->info.mouse))
132 
133 #define check_mouse_position(event, box) \
134 	mouse_is_in_box(&(event)->info.mouse, box)
135 
136 
137 #endif /* EL__TERMINAL_EVENT_H */
138