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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#![deny(missing_docs)]
//! A SDL2 window back-end for the Piston game engine.

extern crate sdl2;
extern crate window;
extern crate input;
extern crate shader_version;
extern crate gl;

// External crates.
use window::{BuildFromWindowSettings, OpenGLWindow, ProcAddress, Window, AdvancedWindow,
             WindowSettings, Size, Position, Api, UnsupportedGraphicsApiError};
use input::{keyboard, Button, ButtonArgs, ButtonState, MouseButton, Input, Motion, CloseArgs,
            ControllerAxisArgs, ControllerButton, Touch, TouchArgs, ControllerHat, TimeStamp,
            ResizeArgs, Event};
use input::HatState as PistonHat;
use sdl2::joystick::HatState;

use std::vec::Vec;
use std::time::Duration;
use std::error::Error;

pub use shader_version::OpenGL;

struct JoystickState {
    joysticks: Vec<sdl2::joystick::Joystick>,
    subsystem: sdl2::JoystickSubsystem,
}

impl JoystickState {
    fn new(subsystem: sdl2::JoystickSubsystem) -> Self {
        JoystickState {
            joysticks: Vec::new(),
            subsystem: subsystem,
        }
    }
}

/// A window implemented by SDL2 back-end.
pub struct Sdl2Window {
    /// SDL window handle.
    pub window: sdl2::video::Window,
    /// Allow dead code because this keeps track of the OpenGL context.
    /// Will be released on drop.
    #[allow(dead_code)]
    pub context: sdl2::video::GLContext,
    /// SDL context.
    pub sdl_context: sdl2::Sdl,
    /// Video subsystem.
    pub video_subsystem: sdl2::VideoSubsystem,
    joystick_state: Option<JoystickState>,
    should_close: bool,
    automatic_close: bool,
    // Stores relative coordinates to emit on next poll.
    mouse_relative: Option<(f64, f64, TimeStamp)>,
    // Whether the cursor is captured.
    is_capturing_cursor: bool,
    // Used to ignore relative events when warping mouse
    // to center of window.
    ignore_relative_event: Option<(i32, i32)>,
    exit_on_esc: bool,
    title: String,
}

impl Sdl2Window {
    /// Creates a new game window for SDL2. This will initialize SDL and the video subsystem.
    /// You can retrieve both via the public fields on the `Sdl2Window` struct.
    pub fn new(settings: &WindowSettings) -> Result<Self, Box<Error>> {
        let sdl = sdl2::init()?;
        let video_subsystem = sdl.video()?;
        let mut window = Self::with_subsystem(video_subsystem, settings)?;
        if settings.get_controllers() {
            window.init_joysticks()?;
        }
        Ok(window)
    }

    /// Creates a window with the supplied SDL Video subsystem.
    pub fn with_subsystem(video_subsystem: sdl2::VideoSubsystem,
                          settings: &WindowSettings)
                          -> Result<Self, Box<Error>> {
        use sdl2::video::GLProfile;

        let sdl_context = video_subsystem.sdl();
        let api = settings.get_maybe_graphics_api().unwrap_or(Api::opengl(3, 2));
        if api.api != "OpenGL" {
            return Err(UnsupportedGraphicsApiError {
                found: api.api,
                expected: vec!["OpenGL".into()],
            }.into());
        }

        {
            let gl_attr = video_subsystem.gl_attr();

            // Not all drivers default to 32bit color, so explicitly set it to 32bit color.
            gl_attr.set_red_size(8);
            gl_attr.set_green_size(8);
            gl_attr.set_blue_size(8);
            gl_attr.set_alpha_size(8);
            gl_attr.set_stencil_size(8);
            gl_attr.set_context_version(api.major as u8, api.minor as u8);
            gl_attr.set_framebuffer_srgb_compatible(settings.get_srgb());
        }

        if api >= Api::opengl(3, 2) {
            video_subsystem.gl_attr().set_context_profile(GLProfile::Core);
        }
        if settings.get_samples() != 0 {
            let gl_attr = video_subsystem.gl_attr();
            gl_attr.set_multisample_buffers(1);
            gl_attr.set_multisample_samples(settings.get_samples());
        }

        let mut window_builder = video_subsystem.window(&settings.get_title(),
                                                        settings.get_size().width as u32,
                                                        settings.get_size().height as u32);

        let window_builder = window_builder.position_centered()
            .opengl();

        let window_builder = if settings.get_resizable() {
            window_builder.resizable()
        } else {
            window_builder
        };

        let window_builder = if settings.get_decorated() {
            window_builder
        } else {
            window_builder.borderless()
        };

        let window_builder = if settings.get_fullscreen() {
            window_builder.fullscreen()
        } else {
            window_builder
        };

        let window = window_builder.build();

        let window = match window {
            Ok(w) => w,
            Err(_) => {
                if settings.get_samples() != 0 {
                    // Retry without requiring anti-aliasing.
                    let gl_attr = video_subsystem.gl_attr();
                    gl_attr.set_multisample_buffers(0);
                    gl_attr.set_multisample_samples(0);
                    try!(window_builder.build().map_err(|e| format!("{}", e)))
                } else {
                    try!(window.map_err(|e| format!("{}", e)))
                }
            }
        };

        // Send text input events.
        video_subsystem.text_input().start();

        let context = try!(window.gl_create_context()
            .map_err(|e| format!("{}", e)));

        // Load the OpenGL function pointers.
        gl::load_with(|name| video_subsystem.gl_get_proc_address(name) as *const _);

        if settings.get_vsync() {
            video_subsystem.gl_set_swap_interval(1)?;
        } else {
            video_subsystem.gl_set_swap_interval(0)?;
        }

        let window = Sdl2Window {
            exit_on_esc: settings.get_exit_on_esc(),
            should_close: false,
            automatic_close: settings.get_automatic_close(),
            is_capturing_cursor: false,
            ignore_relative_event: None,
            window: window,
            context: context,
            sdl_context: sdl_context,
            video_subsystem: video_subsystem,
            joystick_state: None,
            mouse_relative: None,
            title: settings.get_title(),
        };
        Ok(window)
    }

    /// Initialize the joystick subsystem. Required before joystick input
    /// events will be returned. Returns the number available or error.
    pub fn init_joysticks(&mut self) -> Result<u32, String> {
        let subsystem = try!(self.sdl_context.joystick().map_err(|e| format!("{}", e)));
        let mut state = JoystickState::new(subsystem);
        let available = try!(state.subsystem.num_joysticks().map_err(|e| format!("{}", e)));

        // Open all the joysticks
        for id in 0..available {
            match state.subsystem.open(id) {
                Ok(c) => state.joysticks.push(c),
                Err(e) => return Err(format!("{}", e)),
            }
        }

        self.joystick_state = Some(state);

        Ok(available)
    }

    fn wait_event(&mut self) -> Event {
        loop {
            if let Some(event) = self.check_pending_event() {
                return event;
            };
            let sdl_event = self.sdl_context.event_pump().unwrap().wait_event();
            let mut unknown = false;
            if let Some(event) = self.handle_event(Some(sdl_event), &mut unknown) {
                return event;
            }
        }
    }

    fn wait_event_timeout(&mut self, timeout: Duration) -> Option<Event> {
        let event = self.check_pending_event();
        if event.is_some() {
            return event;
        };

        let timeout_ms = timeout.as_secs() as u32 * 1000 + (timeout.subsec_nanos() / 1_000_000);
        let sdl_event = self.sdl_context.event_pump().unwrap().wait_event_timeout(timeout_ms);

        let mut unknown = false;
        let event = self.handle_event(sdl_event, &mut unknown);
        if unknown { self.poll_event() } else { event }
    }

    fn poll_event(&mut self) -> Option<Event> {
        // Loop for ignoring unknown events.
        loop {
            let event = self.check_pending_event();
            if event.is_some() {
                return event;
            };

            // Even though we create a new EventPump each time we poll an event
            // this should not be a problem since it only contains phantom data
            // and therefore should actually not have any overhead.
            let sdl_event = self.sdl_context.event_pump().unwrap().poll_event();
            let mut unknown = false;
            let event = self.handle_event(sdl_event, &mut unknown);
            if unknown {
                continue;
            };
            return event;
        }
    }

    fn check_pending_event(&mut self) -> Option<Event> {
        // First check for a pending relative mouse move event.
        if let Some((x, y, timestamp)) = self.mouse_relative {
            self.mouse_relative = None;
            return Some(input::Event::Input(
                Input::Move(Motion::MouseRelative([x, y])), Some(timestamp)));
        }
        None
    }

    fn handle_event(&mut self,
                    sdl_event: Option<sdl2::event::Event>,
                    unknown: &mut bool)
                    -> Option<Event> {
        use sdl2::event::{Event, WindowEvent};
        let event = match sdl_event {
            Some(ev) => {
                if let Event::MouseMotion { xrel, yrel, .. } = ev {
                    // Ignore a specific mouse motion event caused by
                    // change of coordinates when warping the cursor
                    // to the center.
                    if Some((xrel, yrel)) == self.ignore_relative_event {
                        self.ignore_relative_event = None;
                        return None;
                    }
                }
                ev
            }
            None => {
                // Wait until event queue is empty to reduce
                // risk of error in order.
                if self.is_capturing_cursor {
                    self.fake_capture();
                }
                return None;
            }
        };
        match event {
            Event::Quit { timestamp, .. } => {
                if self.automatic_close {
                    self.should_close = true;
                }
                return Some(input::Event::Input(Input::Close(CloseArgs), Some(timestamp)));
            }
            Event::TextInput { text, timestamp, .. } => {
                return Some(input::Event::Input(Input::Text(text), Some(timestamp)));
            }
            Event::KeyDown { keycode: Some(key), repeat, scancode, timestamp, .. } => {
                // SDL2 repeats the key down event.
                // If the event is the same as last one, ignore it.
                if repeat {
                    return self.poll_event();
                }

                if self.exit_on_esc && key == sdl2::keyboard::Keycode::Escape {
                    self.should_close = true;
                } else {
                    return Some(input::Event::Input(Input::Button(ButtonArgs {
                        state: ButtonState::Press,
                        button: Button::Keyboard(sdl2_map_key(key)),
                        scancode: scancode.map(|scode| scode as i32),
                    }), Some(timestamp)));
                }
            }
            Event::KeyUp { keycode: Some(key), repeat, scancode, timestamp, .. } => {
                if repeat {
                    return self.poll_event();
                }
                return Some(input::Event::Input(Input::Button(ButtonArgs {
                    state: ButtonState::Release,
                    button: Button::Keyboard(sdl2_map_key(key)),
                    scancode: scancode.map(|scode| scode as i32),
                }), Some(timestamp)));
            }
            Event::MouseButtonDown { mouse_btn: button, timestamp, .. } => {
                return Some(input::Event::Input(Input::Button(ButtonArgs {
                    state: ButtonState::Press,
                    button: Button::Mouse(sdl2_map_mouse(button)),
                    scancode: None,
                }), Some(timestamp)));
            }
            Event::MouseButtonUp { mouse_btn: button, timestamp, .. } => {
                return Some(input::Event::Input(Input::Button(ButtonArgs {
                    state: ButtonState::Release,
                    button: Button::Mouse(sdl2_map_mouse(button)),
                    scancode: None,
                }), Some(timestamp)));
            }
            Event::MouseMotion { x, y, xrel: dx, yrel: dy, timestamp, .. } => {
                if self.is_capturing_cursor {
                    // Skip normal mouse movement and emit relative motion only.
                    return Some(input::Event::Input(
                        Input::Move(Motion::MouseRelative([dx as f64, dy as f64])),
                        Some(timestamp)));
                }
                // Send relative move movement next time.
                self.mouse_relative = Some((dx as f64, dy as f64, timestamp));
                return Some(input::Event::Input(
                    Input::Move(Motion::MouseCursor([x as f64, y as f64])),
                    Some(timestamp)));
            }
            Event::MouseWheel { x, y, timestamp, .. } => {
                return Some(input::Event::Input(
                    Input::Move(Motion::MouseScroll([x as f64, y as f64])), Some(timestamp)));
            }
            Event::JoyAxisMotion { which, axis_idx, value: val, timestamp, .. } => {
                // Axis motion is an absolute value in the range
                // [-32768, 32767]. Normalize it down to a float.
                use std::i16::MAX;
                let normalized_value = val as f64 / MAX as f64;
                return Some(input::Event::Input(Input::Move(
                    Motion::ControllerAxis(ControllerAxisArgs::new(
                    which, axis_idx, normalized_value))), Some(timestamp)));
            }
            Event::JoyButtonDown { which, button_idx, timestamp, .. } => {
                return Some(input::Event::Input(Input::Button(ButtonArgs {
                    state: ButtonState::Press,
                    button: Button::Controller(ControllerButton::new(which, button_idx)),
                    scancode: None,
                }), Some(timestamp)))
            }
            Event::JoyButtonUp { which, button_idx, timestamp, .. } => {
                return Some(input::Event::Input(Input::Button(ButtonArgs {
                    state: ButtonState::Release,
                    button: Button::Controller(ControllerButton::new(which, button_idx)),
                    scancode: None,
                }), Some(timestamp)))
            }
            Event::JoyHatMotion { which, hat_idx, state, timestamp, .. } => {
              let state = match state {
                HatState::Centered => PistonHat::Centered,
                HatState::Up => PistonHat::Up,
                HatState::Right => PistonHat::Right,
                HatState::Down => PistonHat::Down,
                HatState::Left => PistonHat::Left,
                HatState::RightUp => PistonHat::RightUp,
                HatState::RightDown => PistonHat::RightDown,
                HatState::LeftUp => PistonHat::LeftUp,
                HatState::LeftDown => PistonHat::LeftDown,
              };
              return Some(input::Event::Input(Input::Button(ButtonArgs {
                    state: ButtonState::Release,
                    button: Button::Hat(ControllerHat::new(which, hat_idx, state)),
                    scancode: None,
                }), Some(timestamp)))
            }
            Event::FingerDown { touch_id, finger_id, x, y, pressure, timestamp, .. } => {
                return Some(input::Event::Input(Input::Move(Motion::Touch(TouchArgs::new(touch_id,
                                                                     finger_id,
                                                                     [x as f64, y as f64],
                                                                     pressure as f64,
                                                                     Touch::Start))),
                             Some(timestamp)))
            }
            Event::FingerMotion { touch_id, finger_id, x, y, pressure, timestamp, .. } => {
                return Some(input::Event::Input(Input::Move(Motion::Touch(TouchArgs::new(touch_id,
                                                                     finger_id,
                                                                     [x as f64, y as f64],
                                                                     pressure as f64,
                                                                     Touch::Move))),
                             Some(timestamp)))
            }
            Event::FingerUp { touch_id, finger_id, x, y, pressure, timestamp, .. } => {
                return Some(input::Event::Input(Input::Move(Motion::Touch(TouchArgs::new(touch_id,
                                                                     finger_id,
                                                                     [x as f64, y as f64],
                                                                     pressure as f64,
                                                                     Touch::End))),
                             Some(timestamp)))
            }
            Event::Window { win_event: sdl2::event::WindowEvent::Resized(w, h), timestamp, .. } => {
                let draw_size = self.draw_size();
                return Some(input::Event::Input(Input::Resize(ResizeArgs {
                    window_size: [w as f64, h as f64],
                    draw_size: draw_size.into(),
                }), Some(timestamp)));
            }
            Event::Window { win_event: WindowEvent::FocusGained, timestamp, .. } => {
                return Some(input::Event::Input(Input::Focus(true), Some(timestamp)));
            }
            Event::Window { win_event: WindowEvent::FocusLost, timestamp, .. } => {
                return Some(input::Event::Input(Input::Focus(false), Some(timestamp)));
            }
            Event::Window { win_event: WindowEvent::Enter, timestamp, .. } => {
                return Some(input::Event::Input(Input::Cursor(true), Some(timestamp)));
            }
            Event::Window { win_event: WindowEvent::Leave, timestamp, .. } => {
                return Some(input::Event::Input(Input::Cursor(false), Some(timestamp)));
            }
            _ => {
                *unknown = true;
                return None;
            }
        }
        None
    }

    fn fake_capture(&mut self) {
        // Fake capturing of cursor.
        let (w, h) = self.window.size();
        let cx = (w / 2) as i32;
        let cy = (h / 2) as i32;
        let s = self.sdl_context.event_pump().unwrap().mouse_state();
        let dx = cx - s.x();
        let dy = cy - s.y();
        if dx != 0 || dy != 0 {
            self.ignore_relative_event = Some((dx, dy));
            self.sdl_context.mouse().warp_mouse_in_window(&self.window, cx as i32, cy as i32);
        }
    }
}

impl BuildFromWindowSettings for Sdl2Window {
    fn build_from_window_settings(settings: &WindowSettings) -> Result<Self, Box<Error>> {
        Sdl2Window::new(settings)
    }
}

impl Drop for Sdl2Window {
    fn drop(&mut self) {
        self.set_capture_cursor(false);
    }
}

impl Window for Sdl2Window {
    fn should_close(&self) -> bool {
        self.should_close
    }
    fn set_should_close(&mut self, value: bool) {
        self.should_close = value;
    }
    fn swap_buffers(&mut self) {
        self.window.gl_swap_window();
    }
    fn size(&self) -> Size {
        let (w, h) = self.window.size();
        Size {width: w as f64, height: h as f64}
    }
    fn wait_event(&mut self) -> Event {
        self.wait_event()
    }
    fn wait_event_timeout(&mut self, timeout: Duration) -> Option<Event> {
        self.wait_event_timeout(timeout)
    }
    fn poll_event(&mut self) -> Option<Event> {
        self.poll_event()
    }
    fn draw_size(&self) -> Size {
        let (w, h) = self.window.drawable_size();
        Size {width: w as f64, height: h as f64}
    }
}

impl AdvancedWindow for Sdl2Window {
    fn get_title(&self) -> String {
        self.title.clone()
    }
    fn set_title(&mut self, value: String) {
        let _ = self.window.set_title(&value);
        self.title = value
    }
    fn get_automatic_close(&self) -> bool {
        self.automatic_close
    }
    fn set_automatic_close(&mut self, value: bool) {
        self.automatic_close = value;
    }
    fn get_exit_on_esc(&self) -> bool {
        self.exit_on_esc
    }
    fn set_exit_on_esc(&mut self, value: bool) {
        self.exit_on_esc = value;
    }
    fn set_capture_cursor(&mut self, value: bool) {
        // Normally it should call `.set_relative_mouse_mode(value)`,
        // but since it does not emit relative mouse events,
        // we have to fake it by hiding the cursor and warping it
        // back to the center of the window.
        self.is_capturing_cursor = value;
        self.sdl_context.mouse().show_cursor(!value);
        if value {
            // Move cursor to center of window now,
            // to get right relative mouse motion to ignore.
            self.fake_capture();
        }
    }
    fn show(&mut self) {
        self.window.show();
    }
    fn hide(&mut self) {
        self.window.hide();
    }
    fn get_position(&self) -> Option<Position> {
        let (x, y) = self.window.position();
        Some(Position { x: x, y: y })
    }
    fn set_position<P: Into<Position>>(&mut self, pos: P) {
        use sdl2::video::WindowPos;

        let pos: Position = pos.into();
        self.window.set_position(WindowPos::Positioned(pos.x), WindowPos::Positioned(pos.y));
    }
    fn set_size<S: Into<Size>>(&mut self, size: S) {
        let size: Size = size.into();
        let _ = self.window.set_size(size.width as u32, size.height as u32);
    }
}

impl OpenGLWindow for Sdl2Window {
    fn get_proc_address(&mut self, proc_name: &str) -> ProcAddress {
        self.video_subsystem.gl_get_proc_address(proc_name) as *const _
    }

    fn is_current(&self) -> bool {
        self.context.is_current()
    }

    fn make_current(&mut self) {
        self.window.gl_make_current(&self.context).unwrap();
    }
}

/// Maps a SDL2 key to piston-input key.
pub fn sdl2_map_key(keycode: sdl2::keyboard::Keycode) -> keyboard::Key {
    (keycode as u32).into()
}

/// Maps a SDL2 mouse button to piston-input button.
pub fn sdl2_map_mouse(button: sdl2::mouse::MouseButton) -> MouseButton {
    use sdl2::mouse::MouseButton as MB;

    match button {
        MB::Left => MouseButton::Left,
        MB::Right => MouseButton::Right,
        MB::Middle => MouseButton::Middle,
        MB::X1 => MouseButton::X1,
        MB::X2 => MouseButton::X2,
        MB::Unknown => MouseButton::Unknown,
    }
}