forked from remacs/remacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.rs
292 lines (240 loc) · 8.48 KB
/
term.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::ptr;
use super::{
display_info::{DisplayInfo, DisplayInfoRef},
glyph::GlyphStringRef,
output::OutputRef,
};
use crate::{
frame::LispFrameRef,
lisp::{ExternalPtr, LispObject},
remacs_sys::{
allocate_kboard, create_terminal, current_kboard, draw_fringe_bitmap_params,
draw_window_fringes, face_id, frame_parm_handler, glyph_row, glyph_string, initial_kboard,
output_method, redisplay_interface, terminal, xlispstrdup, Fcons, Lisp_Frame, Lisp_Window,
Qnil, Qwr, KBOARD,
},
remacs_sys::{
block_input, unblock_input, x_clear_end_of_line, x_clear_window_mouse_face,
x_draw_right_divider, x_draw_vertical_border, x_fix_overlapping_area,
x_get_glyph_overhangs, x_produce_glyphs, x_set_bottom_divider_width, x_set_font,
x_set_font_backend, x_set_left_fringe, x_set_right_divider_width, x_set_right_fringe,
x_write_glyphs,
},
windows::LispWindowRef,
};
pub type TerminalRef = ExternalPtr<terminal>;
impl Default for TerminalRef {
fn default() -> Self {
Self::new(ptr::null_mut())
}
}
pub type KboardRef = ExternalPtr<KBOARD>;
impl KboardRef {
pub fn add_ref(&mut self) {
(*self).reference_count = (*self).reference_count + 1;
}
}
type RedisplayInterfaceRef = ExternalPtr<redisplay_interface>;
unsafe impl Sync for RedisplayInterfaceRef {}
fn get_frame_parm_handlers() -> [frame_parm_handler; 45] {
// Keep this list in the same order as frame_parms in frame.c.
// Use None for unsupported frame parameters.
let handlers: [frame_parm_handler; 45] = [
None,
None,
None,
None,
None,
None,
None,
Some(x_set_font),
None,
None,
None,
None,
Some(x_set_right_divider_width),
Some(x_set_bottom_divider_width),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Some(x_set_left_fringe),
Some(x_set_right_fringe),
None,
None,
Some(x_set_font_backend),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
];
handlers
}
lazy_static! {
static ref REDISPLAY_INTERFACE: RedisplayInterfaceRef = {
let frame_parm_handlers = Box::new(get_frame_parm_handlers());
let interface = Box::new(redisplay_interface {
frame_parm_handlers: (Box::into_raw(frame_parm_handlers)) as *mut Option<_>,
produce_glyphs: Some(x_produce_glyphs),
write_glyphs: Some(x_write_glyphs),
insert_glyphs: None,
clear_end_of_line: Some(x_clear_end_of_line),
scroll_run_hook: None,
after_update_window_line_hook: Some(after_update_window_line),
update_window_begin_hook: Some(update_window_begin),
update_window_end_hook: Some(update_window_end),
flush_display: Some(flush_display),
clear_window_mouse_face: Some(x_clear_window_mouse_face),
get_glyph_overhangs: Some(x_get_glyph_overhangs),
fix_overlapping_area: Some(x_fix_overlapping_area),
draw_fringe_bitmap: Some(draw_fringe_bitmap),
define_fringe_bitmap: None,
destroy_fringe_bitmap: None,
compute_glyph_string_overhangs: None,
draw_glyph_string: Some(draw_glyph_string),
define_frame_cursor: None,
clear_frame_area: Some(clear_frame_area),
draw_window_cursor: None,
draw_vertical_window_border: Some(draw_vertical_window_border),
draw_window_divider: Some(draw_window_divider),
shift_glyphs_for_insert: None,
show_hourglass: None,
hide_hourglass: None,
});
RedisplayInterfaceRef::new(Box::into_raw(interface))
};
}
#[allow(unused_variables)]
extern "C" fn update_window_begin(w: *mut Lisp_Window) {}
extern "C" fn update_window_end(
window: *mut Lisp_Window,
_cursor_no_p: bool,
_mouse_face_overwritten_p: bool,
) {
let mut window: LispWindowRef = window.into();
if window.pseudo_window_p() {
return;
}
unsafe { block_input() };
if window.right_divider_width() > 0 {
unsafe { x_draw_right_divider(window.as_mut()) }
} else {
unsafe { x_draw_vertical_border(window.as_mut()) }
}
unsafe { unblock_input() };
}
extern "C" fn flush_display(f: *mut Lisp_Frame) {
let frame: LispFrameRef = f.into();
let mut output: OutputRef = unsafe { frame.output_data.wr.into() };
output.flush();
}
#[allow(unused_variables)]
extern "C" fn after_update_window_line(w: *mut Lisp_Window, desired_row: *mut glyph_row) {}
#[allow(unused_variables)]
extern "C" fn draw_glyph_string(s: *mut glyph_string) {
let s: GlyphStringRef = s.into();
let output: OutputRef = {
let frame: LispFrameRef = s.f.into();
unsafe { frame.output_data.wr.into() }
};
output.canvas().draw_glyph_string(s);
}
extern "C" fn draw_fringe_bitmap(
window: *mut Lisp_Window,
row: *mut glyph_row,
p: *mut draw_fringe_bitmap_params,
) {
let window: LispWindowRef = window.into();
let frame: LispFrameRef = window.get_frame();
let output: OutputRef = unsafe { frame.output_data.wr.into() };
output.canvas().draw_fringe_bitmap(row, p);
}
extern "C" fn draw_window_divider(window: *mut Lisp_Window, x0: i32, x1: i32, y0: i32, y1: i32) {
let window: LispWindowRef = window.into();
let frame: LispFrameRef = window.get_frame();
let output: OutputRef = unsafe { frame.output_data.wr.into() };
let face = frame.face_from_id(face_id::WINDOW_DIVIDER_FACE_ID);
let face_first = frame.face_from_id(face_id::WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
let face_last = frame.face_from_id(face_id::WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
let color = match face {
Some(f) => unsafe { (*f).foreground },
None => frame.foreground_pixel,
};
let color_first = match face_first {
Some(f) => unsafe { (*f).foreground },
None => frame.foreground_pixel,
};
let color_last = match face_last {
Some(f) => unsafe { (*f).foreground },
None => frame.foreground_pixel,
};
output
.canvas()
.draw_window_divider(color, color_first, color_last, x0, x1, y0, y1);
}
extern "C" fn draw_vertical_window_border(window: *mut Lisp_Window, x: i32, y0: i32, y1: i32) {
let window: LispWindowRef = window.into();
let frame: LispFrameRef = window.get_frame();
let output: OutputRef = unsafe { frame.output_data.wr.into() };
let face = frame.face_from_id(face_id::VERTICAL_BORDER_FACE_ID);
output.canvas().draw_vertical_window_border(face, x, y0, y1);
}
#[allow(unused_variables)]
extern "C" fn clear_frame_area(s: *mut Lisp_Frame, x: i32, y: i32, width: i32, height: i32) {}
fn wr_create_terminal(mut dpyinfo: DisplayInfoRef) -> TerminalRef {
let terminal_ptr = unsafe {
create_terminal(
output_method::output_wr,
REDISPLAY_INTERFACE.clone().as_mut(),
)
};
let mut terminal = TerminalRef::new(terminal_ptr);
// Link terminal and dpyinfo together
terminal.display_info.wr = dpyinfo.as_mut();
dpyinfo.get_inner().terminal = terminal;
// Other hooks are NULL by default.
terminal
}
pub fn wr_term_init(display_name: LispObject) -> DisplayInfoRef {
let dpyinfo = Box::new(DisplayInfo::new());
let mut dpyinfo_ref = DisplayInfoRef::new(Box::into_raw(dpyinfo));
let mut terminal = wr_create_terminal(dpyinfo_ref);
let mut kboard = KboardRef::new(unsafe { allocate_kboard(Qwr) });
terminal.kboard = kboard.as_mut();
// Don't let the initial kboard remain current longer than necessary.
// That would cause problems if a file loaded on startup tries to
// prompt in the mini-buffer.
unsafe {
if current_kboard == initial_kboard {
current_kboard = terminal.kboard;
}
}
kboard.add_ref();
dpyinfo_ref.name_list_element = unsafe { Fcons(display_name, Qnil) };
// https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg00194.html
dpyinfo_ref.smallest_font_height = 1;
dpyinfo_ref.smallest_char_width = 1;
dpyinfo_ref.resx = 1.0;
dpyinfo_ref.resy = 1.0;
// Set the name of the terminal.
terminal.name = unsafe { xlispstrdup(display_name) };
dpyinfo_ref
}