aboutsummaryrefslogtreecommitdiffstats
path: root/src/screen.rs
blob: 0a41a2dba793198c6c922c2b38b844165bfd3c81 (plain) (blame)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use libc;
use std;

use cell;
use color;
use ffi;
use types;

pub struct Screen(*mut types::ScreenImpl);

#[repr(C)]
struct ScreenGridPrefix {
    cur: types::Loc,
    max: types::Loc,
}

#[repr(C)]
struct ScreenPrefix {
    grid: *mut ScreenGridPrefix,
    alternate: *mut ScreenGridPrefix,

    title: *mut libc::c_char,
    title_len: libc::size_t,
    icon_name: *mut libc::c_char,
    icon_name_len: libc::size_t,

    attrs: types::CellAttrs,
}

impl Screen {
    pub fn new(rows: i32, cols: i32) -> Screen {
        let screen_impl = unsafe {
            ffi::vt100_screen_new(rows as libc::c_int, cols as libc::c_int)
        };
        Screen(screen_impl)
    }

    pub fn rows(&self) -> i32 {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe { (*(*prefix).grid).max.row }
    }

    pub fn cols(&self) -> i32 {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe { (*(*prefix).grid).max.col }
    }

    pub fn set_window_size(&self, rows: i32, cols: i32) {
        let Screen(screen_impl) = *self;
        unsafe { ffi::vt100_screen_set_window_size(screen_impl, rows, cols) };
    }

    pub fn set_scrollback_length(&self, rows: i32) {
        let Screen(screen_impl) = *self;
        unsafe { ffi::vt100_screen_set_scrollback_length(screen_impl, rows) };
    }

    pub fn process(&mut self, s: &[u8]) -> u64 {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_screen_process_string(
                screen_impl,
                s.as_ptr() as *const libc::c_char,
                s.len()
            ) as u64
        }
    }

    pub fn window_contents(&self,
        row_start: i32,
        col_start: i32,
        row_end: i32,
        col_end: i32
    ) -> String {
        let Screen(screen_impl) = *self;
        let row_start = std::cmp::min(
            std::cmp::max(row_start, 0),
            self.rows() - 1
        );
        let col_start = std::cmp::min(
            std::cmp::max(col_start, 0),
            self.cols() - 1
        );
        let row_end = std::cmp::min(
            std::cmp::max(row_end, 0),
            self.rows() - 1
        );
        let col_end = std::cmp::min(
            std::cmp::max(col_end, 0),
            self.cols() - 1
        );

        let start_loc = types::Loc { row: row_start, col: col_start };
        let end_loc = types::Loc { row: row_end, col: col_end };

        let mut plaintext: *mut libc::c_char = unsafe { std::mem::uninitialized() };
        let mut len: libc::size_t = unsafe { std::mem::uninitialized() };
        unsafe {
            ffi::vt100_screen_get_string_plaintext(
                screen_impl,
                &start_loc as *const types::Loc,
                &end_loc as *const types::Loc,
                &mut plaintext as *mut *mut libc::c_char,
                &mut len as *mut libc::size_t,
            )
        };
        let rust_plaintext = unsafe {
            std::slice::from_raw_parts(
                plaintext as *mut libc::c_uchar,
                len
            )
        }.to_vec();
        std::string::String::from_utf8(rust_plaintext).unwrap()
    }

    pub fn cell(&self, row: i32, col: i32) -> Option<cell::Cell> {
        let Screen(screen_impl) = *self;
        if row < 0 || row >= self.rows() || col < 0 || col >= self.cols() {
            return None
        }
        let cell_impl = unsafe {
            ffi::vt100_screen_cell_at(screen_impl, row, col)
        };
        Some(cell::Cell::new(cell_impl))
    }

    pub fn cursor_position(&self) -> (i32, i32) {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe {
            ((*(*prefix).grid).cur.col, (*(*prefix).grid).cur.col)
        }
    }

    pub fn title(&self) -> Option<&str> {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        if unsafe { (*prefix).title }.is_null() {
            None
        }
        else {
            let slice: &mut [u8] = unsafe {
                std::slice::from_raw_parts_mut(
                    (*prefix).title as *mut u8,
                    (*prefix).title_len
                )
            };
            Some(std::str::from_utf8(slice).unwrap())
        }
    }

    pub fn icon_name(&self) -> Option<&str> {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        if unsafe { (*prefix).icon_name }.is_null() {
            None
        }
        else {
            let slice: &mut [u8] = unsafe {
                std::slice::from_raw_parts_mut(
                    (*prefix).icon_name as *mut u8,
                    (*prefix).icon_name_len
                )
            };
            Some(std::str::from_utf8(slice).unwrap())
        }
    }

    pub fn fgcolor(&self) -> color::Color {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        let attrs = unsafe { &(*prefix).attrs };
        color::Color::new(&attrs.fgcolor)
    }

    pub fn bgcolor(&self) -> color::Color {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        let attrs = unsafe { &(*prefix).attrs };
        color::Color::new(&attrs.bgcolor)
    }

    pub fn bold(&self) -> bool {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_bold(&mut (*prefix).attrs) != 0
        }
    }

    pub fn italic(&self) -> bool {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_italic(&mut (*prefix).attrs) != 0
        }
    }

    pub fn underline(&self) -> bool {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_underline(&mut (*prefix).attrs) != 0
        }
    }

    pub fn inverse(&self) -> bool {
        let Screen(screen_impl) = *self;
        let prefix: *mut ScreenPrefix = unsafe {
            std::mem::transmute(screen_impl)
        };
        unsafe {
            ffi::vt100_wrapper_cell_attrs_inverse(&mut (*prefix).attrs) != 0
        }
    }

    pub fn hide_cursor(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_hide_cursor(screen_impl) != 0
        }
    }

    pub fn application_keypad(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_application_keypad(screen_impl) != 0
        }
    }

    pub fn application_cursor(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_application_cursor(screen_impl) != 0
        }
    }

    pub fn mouse_reporting_press(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_mouse_reporting_press(screen_impl) != 0
        }
    }

    pub fn mouse_reporting_press_release(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_mouse_reporting_press_release(screen_impl) != 0
        }
    }

    pub fn mouse_reporting_button_motion(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_mouse_reporting_button_motion(screen_impl) != 0
        }
    }

    pub fn mouse_reporting_sgr_mode(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_mouse_reporting_sgr_mode(screen_impl) != 0
        }
    }

    pub fn bracketed_paste(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            ffi::vt100_wrapper_screen_bracketed_paste(screen_impl) != 0
        }
    }

    pub fn check_visual_bell(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            let state = ffi::vt100_wrapper_screen_visual_bell(screen_impl) != 0;
            ffi::vt100_wrapper_screen_clear_visual_bell(screen_impl);
            state
        }
    }

    pub fn check_audible_bell(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            let state = ffi::vt100_wrapper_screen_audible_bell(screen_impl) != 0;
            ffi::vt100_wrapper_screen_clear_audible_bell(screen_impl);
            state
        }
    }

    pub fn check_update_title(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            let state = ffi::vt100_wrapper_screen_update_title(screen_impl) != 0;
            ffi::vt100_wrapper_screen_clear_update_title(screen_impl);
            state
        }
    }

    pub fn check_update_icon_name(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            let state = ffi::vt100_wrapper_screen_update_icon_name(screen_impl) != 0;
            ffi::vt100_wrapper_screen_clear_update_icon_name(screen_impl);
            state
        }
    }

    pub fn check_dirty(&self) -> bool {
        let Screen(screen_impl) = *self;
        unsafe {
            let state = ffi::vt100_wrapper_screen_dirty(screen_impl) != 0;
            ffi::vt100_wrapper_screen_clear_dirty(screen_impl);
            state
        }
    }
}

impl Drop for Screen {
    fn drop(&mut self) {
        let Screen(screen_impl) = *self;
        unsafe { ffi::vt100_screen_delete(screen_impl) };
    }
}