aboutsummaryrefslogtreecommitdiff
path: root/src/machine/key.rs
blob: 0a0978128ccf6f83f18f76ed895acfdef3c09fe9 (plain)
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
use self::super::kbd_defs::*;
use bitflags::bitflags;
use core::convert;

pub struct Key {
	asc: u8,
	scan: u8,
	modi: Modifiers,
	rawcode: u8, // this field not necessary, remove after testing
}

// Not implementing
// +operator char()
//

impl convert::Into<char> for Key {
	fn into(self) -> char {
		self.asc as char
	}
}

impl convert::Into<u8> for Key {
	fn into(self) -> u8 {
		self.asc
	}
}

bitflags! {
	pub struct Modifiers:u8 {
		const NONE			= 0;
		const SHIFT			= 1 << 0;
		const ALT_LEFT		= 1 << 1;
		const ALT_RIGHT		= 1 << 2;
		const CTRL_LEFT		= 1 << 3;
		const CTRL_RIGHT	= 1 << 4;
		const CAPSLOCK		= 1 << 5;
		const NUMLOCK		= 1 << 6;
		const SCROLL_LOCK	= 1 << 7;
	}
}

#[allow(dead_code)]
impl Key {
	pub fn new() -> Self {
		Self {
			asc: 0,
			scan: 0,
			modi: Modifiers::NONE,
			rawcode: 0,
		}
	}
	pub fn decode(&mut self) {
		// decode  key
	}
	pub fn valid(self) -> bool {
		self.scan != 0
	}

	pub fn invalidate(&mut self) {
		self.scan = 0;
	}

	pub fn set_raw(&mut self, code: u8) {
		self.rawcode = code;
	}

	pub fn get_raw(self) -> u8 {
		self.rawcode
	}

	// setter and getter for ascii and scancode
	pub fn set_ascii(&mut self, ascii: u8) {
		self.asc = ascii;
	}
	pub fn get_ascii(self) -> u8 {
		self.asc
	}
	pub fn set_scancode(&mut self, scancode: u8) {
		self.scan = scancode;
	}
	pub fn get_scancode(self) -> u8 {
		self.scan
	}

	// TODO the setters and getters should not be their own functions....

	#[inline(always)]
	pub fn mod_contains(&self, modi: Modifiers) -> bool {
		self.modi.contains(modi)
	}

	#[inline(always)]
	pub fn mod_set(&mut self, modi: Modifiers, pressed: bool) {
		if pressed {
			self.modi.insert(modi);
		} else {
			self.modi.remove(modi);
		}
	}
}