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
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

use allegro_util::{from_c_str, Flag};
use core::Core;

use ffi::*;
use libc::*;
use std::mem;

flag_type! {
	StickFlags
	{
		DIGITAL = ALLEGRO_JOYFLAG_DIGITAL,
		ANALOGUE = ALLEGRO_JOYFLAG_ANALOGUE
	}
}

#[allow(missing_copy_implementations)]
pub struct Joystick
{
	allegro_joystick: *mut ALLEGRO_JOYSTICK,
}

impl Joystick
{
	pub fn new(core: &Core, idx: i32) -> Result<Joystick, ()>
	{
		assert!(
			core.is_joystick_installed(),
			"Joystick support not installed!"
		);
		let ptr = unsafe { al_get_joystick(idx as i32) };
		if !ptr.is_null()
		{
			Ok(Joystick {
				allegro_joystick: ptr,
			})
		}
		else
		{
			Err(())
		}
	}

	pub fn is_active(&self) -> bool
	{
		unsafe { al_get_joystick_active(self.allegro_joystick) != 0 }
	}

	pub fn get_name(&self) -> Result<String, ()>
	{
		unsafe {
			let ptr = al_get_joystick_name(self.allegro_joystick);
			if !ptr.is_null()
			{
				Ok(from_c_str(ptr))
			}
			else
			{
				Err(())
			}
		}
	}

	pub fn get_num_sticks(&self) -> i32
	{
		unsafe { al_get_joystick_num_sticks(self.allegro_joystick) as i32 }
	}

	pub fn get_stick_flags(&self, stick: i32) -> StickFlags
	{
		unsafe {
			mem::transmute(al_get_joystick_stick_flags(
				self.allegro_joystick,
				stick as c_int,
			))
		}
	}

	pub fn get_stick_name(&self, stick: i32) -> Result<String, ()>
	{
		unsafe {
			let ptr = al_get_joystick_stick_name(self.allegro_joystick, stick as c_int);
			if !ptr.is_null()
			{
				Ok(from_c_str(ptr))
			}
			else
			{
				Err(())
			}
		}
	}

	pub fn get_num_axes(&self, stick: i32) -> i32
	{
		unsafe { al_get_joystick_num_axes(self.allegro_joystick, stick as c_int) as i32 }
	}

	pub fn get_axis_name(&self, stick: i32, axis: i32) -> Result<String, ()>
	{
		unsafe {
			let ptr =
				al_get_joystick_axis_name(self.allegro_joystick, stick as c_int, axis as c_int);
			if !ptr.is_null()
			{
				Ok(from_c_str(ptr))
			}
			else
			{
				Err(())
			}
		}
	}

	pub fn get_num_buttons(&self) -> i32
	{
		unsafe { al_get_joystick_num_buttons(self.allegro_joystick) as i32 }
	}

	pub fn get_button_name(&self, button: i32) -> Result<String, ()>
	{
		unsafe {
			let ptr = al_get_joystick_button_name(self.allegro_joystick, button as c_int);
			if !ptr.is_null()
			{
				Ok(from_c_str(ptr))
			}
			else
			{
				Err(())
			}
		}
	}

	pub fn get_allegro_joystick(&self) -> *mut ALLEGRO_JOYSTICK
	{
		self.allegro_joystick
	}
}