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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
pub const DEFAULT_REPEAT: libc::c_long = 1_000i64;
pub const DEFAULT_INTERVAL: libc::c_long = 1_000i64;
mod buf;

use std::fmt;
use std::mem;
use std::io::Write;
use std::ops::BitOr;
use std::ops::{Add, Sub, BitAnd, Not, DerefMut};

use ::libc;
use ::time;

pub use super::device::In;

use super::display::Display;
use super::device::control::Control;

pub use super::display::winsz::Winszed;
#[cfg(feature = "task")]
pub use super::device::BufProc;
pub use super::device::{Out, DeviceState};
pub use super::device::control::operate::key::Key;
pub use super::device::control::operate::mouse::Mouse;

use self::buf::Buf;

fn catch_numbers<'a>(mut acc: Vec<libc::size_t>, buf: &'a [u8]) -> (Vec<libc::size_t>, &'a [u8])
{ match parse_number!(buf)
  { Some((number, &[b';', ref next..])) =>
      { acc.push(number);
        catch_numbers(acc, next) },
    Some((number, &[ref next..])) =>
      { acc.push(number);
        (acc, next) },
    _ =>
      { (acc, buf) }, }}

#[derive(Default, Copy, Clone)]
pub struct ShellState {
    /// The time limit required for a repetition.
    repeat: libc::c_long,
    size: Option<Winszed>,
    /// The time limit required for a repetition.
    interval: libc::c_long,
    /// Update.
    idle: Option<()>,
    /// The pressed character.
    in_down: Option<Control>,
    /// The released character.
    in_up: Option<Control>,
    /// The number of the repetition.
    in_repeat: Option<libc::c_ulong>,
    /// The segment intervals.
    in_interval: Option<time::Tm>,
    /// The output of last text //printed.
    out_last: Option<(Out, libc::size_t)>,
    #[cfg(feature = "task")] task: Option<BufProc>,
    /// The tmp buffer
    buffer: Buf,
}

impl ShellState {

    /// The constructor method `new` returns a empty ShellState.
    pub fn new (
        repeat: Option<libc::c_long>,
        interval: Option<libc::c_long>,
    ) -> Self {
        match () {
            #[cfg(feature = "task")]
            () => ShellState {
                repeat: repeat.unwrap_or(DEFAULT_REPEAT),
                interval: interval.unwrap_or(DEFAULT_INTERVAL),
                idle: None,
                in_down: None,
                in_up: None,
                in_repeat: None,
                in_interval: None,
                out_last: None,
                buffer: Buf([0; 100], 0),
                task: None,
                size: None,
            },
            #[cfg(not(feature = "task"))]
            () => ShellState {
                repeat: repeat.unwrap_or(DEFAULT_REPEAT),
                interval: interval.unwrap_or(DEFAULT_INTERVAL),
                idle: None,
                in_down: None,
                in_up: None,
                in_repeat: None,
                in_interval: None,
                out_last: None,
                buffer: Buf([0; 100], 0),
                size: None,
            },
        }
    }

    pub fn set_input_keyown(&mut self, key: char) {
        let mut buf: In = In::default();
        unsafe {
            let data: [libc::c_uchar; 4] = mem::transmute::<char, [libc::c_uchar; 4]>(key);

            buf[0] = data[0];
            buf[1] = data[1];
            buf[2] = data[2];
            buf[3] = data[3];
            self.in_down = Some(
                Control::new(buf, key.len_utf8())
            );
        }
    }

    /// The mutator method `set_repeat` change the time limit of repetition.
    pub fn set_repeat(&mut self, repeat: libc::c_long) {
        self.repeat = repeat;
    }

    /// The mutator method `set_interval` change the interval.
    pub fn set_interval(&mut self, interval: libc::c_long) {
        self.interval = interval;
    }

    /// The mutator method `set_idle` update the idle event status.
    pub fn set_idle(&mut self, entry: Option<()>) {
        self.idle = entry;
    }

    /// The mutator method `set_input` update the `in_text`
    /// and save the old `in_text` to `in_text_past`.
    pub fn set_input(&mut self, out_screen: &mut Display, mut down: Option<Control>) {
        match down 
        { Some(is_mouse) =>
            { match is_mouse.as_slice()
              { &[b'\x1B', b'[', b'<', ref next..] =>
                  { let (bonjour, coucou) =
                    { catch_numbers(Vec::new(), next) };
                    match coucou
                    { &[b'M', ..] =>
                      { if out_screen.get_mouse().3 == false && bonjour[0] > 2
                        { down = None; }
                        else if out_screen.get_mouse().3 == false && out_screen.get_mouse().0 == false && out_screen.get_mouse().1 == false
                        { down = None; }
                        else if out_screen.get_mouse().3 == false && (out_screen.get_mouse().1 == true || out_screen.get_mouse().0 == true) && bonjour[0] == 0 {
                            let mut buf: In = In::default();

                            buf[0] = b'\x1B';
                            buf[1] = b'[';
                            buf[2] = b'M';
                            buf[3] = b' ';
                            buf[4] = bonjour[1] as u8 + 32;
                            buf[5] = bonjour[2] as u8 + 32;
                            down = Some(Control::new(buf, 6));
                        }
                        else if out_screen.get_mouse().3 == false && out_screen.get_mouse().0 == true
                        { down = None; }},
                      &[b'm', ..] =>
                      { if out_screen.get_mouse().0 == false && out_screen.get_mouse().1 == false
                        { down = None; }
                        else if out_screen.get_mouse().1 == true && out_screen.get_mouse().3 == false && bonjour[0] <= 2 {
                            let mut buf: In = In::default();

                            buf[0] = b'\x1B';
                            buf[1] = b'[';
                            buf[2] = b'M';
                            buf[3] = b'#';
                            buf[4] = bonjour[1] as u8 + 32;
                            buf[5] = bonjour[2] as u8 + 32;
                            down = Some(Control::new(buf, 6));
                        }
                        else if out_screen.get_mouse().0 == true
                        { down = None; }},
                      _ => {}, }},
               _ => {}, }},
          _ => {}, };
          if out_screen.get_ss()
          { let ss: libc::c_uchar = match down
            { Some(after) =>
              { match after.as_slice()
                { &[b'\x1B', b'[', b'A', ..] => b'A',
                  &[b'\x1B', b'[', b'B', ..] => b'B',
                  &[b'\x1B', b'[', b'C', ..] => b'C',
                  &[b'\x1B', b'[', b'D', ..] => b'D',
                  _ => 0, }},
              _ => 0, };
            if ss > 0 {
                let mut buf: In = In::default();

                buf[0] = b'\x1B';
                buf[1] = b'O';
                buf[2] = ss;
                down = Some(Control::new(buf, 3));
            }}

        self.in_down = down;
        if let Some(after) = down {
            if let Some(before) = self.in_up {
                if before.eq(&after).bitand(
                    before.as_time().add(
                        time::Duration::milliseconds(self.repeat)
                    ) >= after.as_time()
                ) {
                    self.in_repeat = Some(self.in_repeat.unwrap_or_default().add(&1));
                } else {
                    self.in_repeat = Some(0);
                }
            } else {
                self.in_interval = Some(after.as_time());
            }
            self.in_up = Some(after);
        } else if let Some(before) = self.in_up {
            if before.as_time().add(
               time::Duration::milliseconds(self.repeat)
            ) <= time::now() {
                self.in_repeat = None;
                self.in_interval = None;
            }
        }
    }

    /// The mutator method `set_output` update the both `out_text`
    /// and `out_screen` variable.
    pub fn set_output(&mut self, out_screen: &mut Display, entry: Option<(Out, libc::size_t)>) {
        if let Some((buf, len)) = entry {
            self.out_last = Some((buf, len));

            let mut tmp = [0u8; 596];
            let seeker = self.buffer.1;
            { let hey: &mut [u8] = self.buffer.deref_mut();
              if seeker > 0
              { { let mut buffer: &mut [u8] = &mut tmp[..];
                  buffer.write(&[b'\x1B']).unwrap(); }
                { let mut buffer: &mut [u8] = &mut tmp[1..];
                  buffer.write(&hey[..]).unwrap(); }
                { let mut buffer: &mut [u8] = &mut tmp[seeker..];
                  buffer.write(&buf[..len]).unwrap(); }}
              else
              { let mut buffer: &mut [u8] = &mut tmp[..];
                buffer.write(&buf[..len]).unwrap(); }
            }

            let _ = out_screen.write(&tmp[..len + self.buffer.1]);

            { let hey: &mut [u8] = self.buffer.deref_mut();
              { let mut buffer: &mut [u8] = &mut hey[..];
                let ss = [0u8; 100];
                buffer.write(&ss[..]).unwrap(); }}
            self.buffer.1 = 0;

            if (&buf[..len]).iter().position(|&i| i.eq(&b'\x1B')).is_some()
            { match (&buf[..len]).split(|&at| at == b'\x1B').take(len).last()
              { Some(get) =>
                  { if get.is_empty() || get.iter().position(|&i| i.eq(&b';').not().bitand(i.eq(&b'(').not()).bitand(i.eq(&b'[').not()).bitand(i.lt(&b'0').bitor(i.gt(&b'9')))).is_none()
                    { self.buffer.1 = get.len() + 1;
                      let mut coucou: &mut [u8] = self.buffer.deref_mut();
                      coucou.write(&get[..]).unwrap(); }},
                None => {}, }}

            else if len.ge(&3) && (&buf[..len]).iter().position(|&i| (i & 0b11110000 == 0b11110000) || (i & 0b11100000 == 0b11100000) || (i & 0b11000000 == 0b11000000)).is_some()
            { let hs = &buf[len-3..len];
              self.buffer.1 = 3;

              let mut coucou: &mut [u8] = self.buffer.deref_mut();
              if hs[2] & 0b11000000 == 0b11000000 || hs[2] & 0b11100000 == 0b11100000 || hs[2] & 0b11110000 == 0b11110000
              { coucou.write(&hs[2..]).unwrap(); }
              else if hs[1] & 0b11100000 == 0b11100000 || hs[1] & 0b11110000 == 0b11110000
              { coucou.write(&hs[1..]).unwrap(); }
              else if hs[0] & 0b11110000 == 0b11110000
              { coucou.write(&hs[0..]).unwrap(); }}

        } else {
            self.out_last = None;
        }
    }

    /// The mutator method `set_task` updates the task event.
    #[cfg(feature = "task")]
    pub fn set_task(&mut self, task: Option<BufProc>) {
        self.task = task;
    }

    /// The accessor method `is_idle` returns the Idle event.
    pub fn is_idle(&self) -> Option<()> {
        self.idle
    }

    /// The accessor method `is_input_keydown` returns the pressed Key event.
    pub fn is_input_keydown(&self) -> Option<Key> {
        if let Some(ref control) = self.in_down {
            control.is_key()
        } else {
            None
        }
    }

    /// The accessor method `is_input_keyrepeat` returns the number's repetition
    /// of the Key.
    pub fn is_input_keyrepeat(&self) -> Option<libc::c_ulong> {
        if let Some(_) = self.in_up {
            self.in_repeat
        } else {
            None
        }
    }

    /// The accessor method `is_input_keyinterval` returns the number's of repetition
    /// between a range of the interval.
    pub fn is_input_keyinterval(&self) -> Option<libc::c_longlong> {
        if let (Some(first), Some(last)) = (self.in_interval, self.in_down) {
            Some(
                first.sub(last.as_time()).num_milliseconds().abs()/self.interval
            )
        } else {
            None
        }
    }

    /// The accessor method `is_input_mouse` returns the pressed Mouse event.
    pub fn is_input_mouse(&self) -> Option<Mouse> {
        if let Some(ref control) = self.in_down {
            control.is_mouse()
        } else {
            None
        }
    }

    /// The accessor method `is_in_slice` returns the bytes for a Input event.
    pub fn is_input_slice(&self) -> Option<&[libc::c_uchar]> {
        if let Some(ref control) = self.in_down {
            Some(control.as_slice())
        } else {
            None
        }
    }

    /// The accessor method `is_output_last` returns the Output text event.
    pub fn is_output_last(&self) -> Option<&[libc::c_uchar]> {
        if let Some((ref out, len)) = self.out_last {
            Some(&out[..len])
        } else {
            None
        }
    }

    /// The accessor method `is_output_screen` returns the Output screen event.
    pub fn is_output_screen(&self) -> Option<()> {
        if self.is_output_last().is_some() {
            Some(())
        } else {
            None
        }
    }

    /// The mutator method `set_task` updates the task event.
    #[cfg(feature = "task")]
    pub fn is_task(&self) -> Option<&BufProc> {
        if let Some(ref task) = self.task {
            Some(task)
        } else {
            None
        }
    }

    #[cfg(feature = "auto-resize")]
    pub fn is_resized(&self) -> Option<Winszed> {
        self.size
    }

    #[cfg(feature = "auto-resize")]
    fn set_resized(&mut self, out_screen: &mut Display) {
        match Winszed::new(libc::STDIN_FILENO) {
            Ok(size) if size.ne(&out_screen.get_window_size()) => {
                self.size = Some(size);
            },
            _ => {
                self.size = None;
            },
        }
    }

    /// The method `with_device` updates the state from
    /// the event DeviceState interface.
    #[cfg(all(feature = "task", not(feature = "auto-resize")))]
    pub fn update_from(&mut self, out_screen: &mut Display, event: DeviceState) {
        self.set_task(event.is_task());
        self.set_idle(event.is_idle());
        self.set_output(out_screen, event.is_out_text());
        self.set_input(out_screen, event.is_input());
    }

    /// The method `with_device` updates the state from
    /// the event DeviceState interface.
    #[cfg(all(not(feature = "task"), not(feature = "auto-resize")))]
    pub fn update_from(&mut self, out_screen: &mut Display, event: DeviceState) {
        self.set_idle(event.is_idle());
        self.set_output(out_screen, event.is_out_text());
        self.set_input(out_screen, event.is_input());
    }

    /// The method `with_device` updates the state from
    /// the event DeviceState interface.
    #[cfg(all(feature = "task", feature = "auto-resize"))]
    pub fn update_from(&mut self, out_screen: &mut Display, event: DeviceState) {
        self.set_task(event.is_task());
        self.set_idle(event.is_idle());
        self.set_output(out_screen, event.is_out_text());
        self.set_input(out_screen, event.is_input());
        self.set_resized(out_screen);
    }

    /// The method `with_device` updates the state from
    /// the event DeviceState interface.
    #[cfg(all(not(feature = "task"), feature = "auto-resize"))]
    pub fn update_from(&mut self, out_screen: &mut Display, event: DeviceState) {
        self.set_idle(event.is_idle());
        self.set_output(out_screen, event.is_out_text());
        self.set_input(out_screen, event.is_input());
        self.set_resized(out_screen);
    }
}

impl fmt::Debug for ShellState {

    #[cfg(feature = "task")]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
            "ShellState {{ task: {:?}, idle: {:?}, input: {:?}, output: {:?} }}",
               self.task, self.idle, self.in_down, self.out_last.is_some()
        )
    }

    #[cfg(not(feature = "task"))]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
          "ShellState {{ idle: {:?}, input: {:?}, output: {:?}}}",
               self.idle, self.in_down, self.out_last.is_some()
        )
    }
}