Merge pull request #183 from eddyb/no-codegen

Replace codegen with DRY static code.
This commit is contained in:
Tony Aldridge
2014-10-03 09:49:16 +01:00
14 changed files with 562 additions and 1067 deletions
-2
View File
@@ -13,8 +13,6 @@
build/
bin/
lib/
src/sdl2/generated/
/rustpkg_db.json
*.swp
.project
src/demo/main
-1
View File
@@ -3,7 +3,6 @@
name = "sdl2"
version = "0.0.1"
authors = [ "Tony Aldridge<zaragopha@hotmail.com>" ]
build = "sh prebuild.sh"
[lib]
-9
View File
@@ -1,9 +0,0 @@
#!/bin/sh
codegen=src/codegen/target/codegen
src_dir=src/sdl2/generated
cargo build --manifest-path src/codegen/Cargo.toml
mkdir -p ${src_dir}
${codegen} keycode.rs ${src_dir}
${codegen} scancode.rs ${src_dir}
-11
View File
@@ -1,11 +0,0 @@
[package]
name = "codegen"
version = "0.0.1"
authors = ["Graydon Hoare"]
[[bin]]
name = "codegen"
path = "main.rs"
-26
View File
@@ -1,26 +0,0 @@
Copyright (c) 2006-2009 Graydon Hoare
Copyright (c) 2009-2013 Mozilla Foundation
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
-151
View File
@@ -1,151 +0,0 @@
#![macro_escape]
use std::io::BufferedWriter;
use std::io::{File, Writer};
use std::str::Chars;
use std::vec::Vec;
pub struct ParseBranch {
matches: Vec<u8>,
result: Option<String>,
children: Vec<ParseBranch>,
}
impl ParseBranch {
fn new() -> ParseBranch {
ParseBranch {
matches: Vec::new(),
result: None,
children: Vec::new(),
}
}
}
#[allow(visible_private_types)]
pub fn branchify(options: &[(&str, &str)], case_sensitive: bool) -> Vec<ParseBranch> {
let mut root = ParseBranch::new();
fn go_down_moses(branch: &mut ParseBranch, mut chariter: Chars, result: &str, case_sensitive: bool) {
match chariter.next() {
Some(c) => {
let first_case = if case_sensitive { c as u8 } else { c.to_ascii().to_uppercase().to_byte() };
for next_branch in branch.children.mut_iter() {
if next_branch.matches.as_slice()[0] == first_case {
go_down_moses(next_branch, chariter, result, case_sensitive);
return;
}
}
let mut subbranch = ParseBranch::new();
subbranch.matches.push(first_case);
if !case_sensitive {
let second_case = c.to_ascii().to_lowercase().to_byte();
if first_case != second_case {
subbranch.matches.push(second_case);
}
}
branch.children.push(subbranch);
let index = branch.children.len() -1;
go_down_moses(&mut branch.children.as_mut_slice()[index], chariter, result, case_sensitive);
},
None => {
assert!(branch.result.is_none());
branch.result = Some(result.to_string());
},
}
};
for &(key, result) in options.iter() {
go_down_moses(&mut root, key.chars(), result, case_sensitive);
}
root.children
}
macro_rules! branchify(
(case sensitive, $($key:expr => $value:ident),*) => (
::branchify::branchify([$(($key, stringify!($value))),*], true)
);
(case insensitive, $($key:expr => $value:ident),*) => (
branchify([$(($key, stringify!($value))),*], false)
);
)
/// Prints the contents to stdout.
///
/// :param branches: the branches to search through
/// :param indent: the level of indentation (each level representing four leading spaces)
/// :param read_call: the function call to read a byte
/// :param end: the byte which marks the end of the sequence
/// :param max_len: the maximum length a value may be before giving up and returning ``None``
/// :param valid: the function call to if a byte ``b`` is valid
/// :param unknown: the expression to call for an unknown value; in this string, ``{}`` will be
/// replaced with an expression (literal or non-literal) evaluating to a ``~str`` (it is
/// ``{}`` only, not arbitrary format strings)
#[allow(visible_private_types)]
pub fn generate_branchified_method(
writer: &mut BufferedWriter<File>,
branches: &[ParseBranch],
indent: uint,
read_call: &str,
end: &str,
max_len: &str,
valid: &str,
unknown: &str) {
// Write Formatted
let mut wf = |s: String| {
let indentstr = " ".repeat(indent * 4);
let result = writer.write(indentstr.as_bytes())
.and(writer.write(s.as_bytes()))
.and(writer.write(b"\n"));
match result {
Ok(_) => {},
Err(e) => fail!("write error: {:s}", e.desc),
}
};
fn r(branch: &ParseBranch, prefix: &str, read_call: &str,
valid: &str, unknown: &str, write_fmt: &mut |String|) {
for &c in branch.matches.iter() {
let next_prefix = format!("{}{}", prefix, c as char);
(*write_fmt)(format!("Some(b) if b == '{}' as u8 => match {} {{", c as char, read_call));
for b in branch.children.iter() {
r(b, next_prefix.as_slice(), read_call, valid, unknown, write_fmt);
}
match branch.result {
Some(ref result) => (*write_fmt)(format!(" Some(b) if b == SP => return Some({}),", *result)),
None => (*write_fmt)(format!(" Some(b) if b == SP => return Some({}),",
unknown.replace("{}", format!("~\"{}\"", next_prefix.as_slice()).as_slice()))),
}
(*write_fmt)(format!(" Some(b) if {} => (\"{}\", b),", valid, next_prefix.as_slice()));
(*write_fmt)(format!(" _ => return None,"));
(*write_fmt)(format!("}},"));
}
}
wf(format!("let (s, next_byte) = match {} {{", read_call));
for b in branches.iter() {
r(b, "", read_call, valid, unknown, &mut wf);
}
wf(format!(" Some(b) if {} => (\"\", b),", valid));
wf(format!(" _ => return None,"));
wf(format!("}};"));
wf(format!("// OK, that didn't pan out. Let's read the rest and see what we get."));
wf(format!("let mut s = s.to_string();"));
wf(format!("s.push_char(next_byte as char);"));
wf(format!("loop {{"));
wf(format!(" match {} {{", read_call));
wf(format!(" Some(b) if b == {} => return Some({}),", end, unknown.replace("{}", "s")));
wf(format!(" Some(b) if {} => {{", valid));
wf(format!(" if s.len() == {} {{", max_len));
wf(format!(" // Too long; bad request"));
wf(format!(" return None;"));
wf(format!(" }}"));
wf(format!(" s.push_char(b as char);"));
wf(format!(" }},"));
wf(format!(" _ => return None,"));
wf(format!(" }}"));
wf(format!("}}"));
}
-387
View File
@@ -1,387 +0,0 @@
use std::io::{IoResult,Writer};
use std::path::BytesContainer;
use super::get_writer;
struct Key {
code: uint,
ident: &'static str,
}
impl PartialOrd for Key {
fn partial_cmp(&self, other: &Key) -> Option<Ordering> {
match (!self.lt(other), !other.lt(self)) {
(false, false) => None,
(false, true) => Some(Less),
(true, false) => Some(Greater),
(true, true) => Some(Equal),
}
}
fn lt (&self, other: &Key) -> bool {
self.code < other.code
}
}
impl PartialEq for Key {
fn eq (&self, other: &Key) -> bool {
if self.code == other.code {
true
} else {
false
}
}
}
impl Ord for Key {
fn cmp(&self, other: &Key) -> Ordering {
if self.code < other.code {
Less
} else if self.code > other.code {
Greater
} else { Equal }
}
}
impl Eq for Key {
}
#[allow(non_snake_case_functions)]
fn Key(code: uint, ident: &'static str) -> Key {
Key { code: code, ident: ident }
}
impl Key {
fn ident(&self) -> String {
self.ident.to_string()
}
fn padded_ident(&self) -> String {
self.ident().append(" ".repeat(unsafe { longest_ident } - self.ident().len()).as_slice())
}
}
static mut longest_ident: uint = 0;
pub fn generate(output_dir: &Path) -> IoResult<()> {
let mut out = get_writer(output_dir, "keycode.rs");
let mut entries = [
Key(0, "UnknownKey"),
Key(13, "ReturnKey"),
Key(27, "EscapeKey"),
Key(8, "BackspaceKey"),
Key(9, "TabKey"),
Key(32, "SpaceKey"),
Key(33, "ExclaimKey"),
Key(34, "QuotedblKey"),
Key(35, "HashKey"),
Key(37, "PercentKey"),
Key(36, "DollarKey"),
Key(38, "AmpersandKey"),
Key(39, "QuoteKey"),
Key(40, "LeftParenKey"),
Key(41, "RightParenKey"),
Key(42, "AsteriskKey"),
Key(43, "PlusKey"),
Key(44, "CommaKey"),
Key(45, "MinusKey"),
Key(46, "PeriodKey"),
Key(47, "SlashKey"),
Key(48, "Num0Key"),
Key(49, "Num1Key"),
Key(50, "Num2Key"),
Key(51, "Num3Key"),
Key(52, "Num4Key"),
Key(53, "Num5Key"),
Key(54, "Num6Key"),
Key(55, "Num7Key"),
Key(56, "Num8Key"),
Key(57, "Num9Key"),
Key(58, "ColonKey"),
Key(59, "SemicolonKey"),
Key(60, "LessKey"),
Key(61, "EqualsKey"),
Key(62, "GreaterKey"),
Key(63, "QuestionKey"),
Key(64, "AtKey"),
Key(91, "LeftBracketKey"),
Key(92, "BackslashKey"),
Key(93, "RightBracketKey"),
Key(94, "CaretKey"),
Key(95, "UnderscoreKey"),
Key(96, "BackquoteKey"),
Key(97, "AKey"),
Key(98, "BKey"),
Key(99, "CKey"),
Key(100, "DKey"),
Key(101, "EKey"),
Key(102, "FKey"),
Key(103, "GKey"),
Key(104, "HKey"),
Key(105, "IKey"),
Key(106, "JKey"),
Key(107, "KKey"),
Key(108, "LKey"),
Key(109, "MKey"),
Key(110, "NKey"),
Key(111, "OKey"),
Key(112, "PKey"),
Key(113, "QKey"),
Key(114, "RKey"),
Key(115, "SKey"),
Key(116, "TKey"),
Key(117, "UKey"),
Key(118, "VKey"),
Key(119, "WKey"),
Key(120, "XKey"),
Key(121, "YKey"),
Key(122, "ZKey"),
Key(1073741881, "CapsLockKey"),
Key(1073741882, "F1Key"),
Key(1073741883, "F2Key"),
Key(1073741884, "F3Key"),
Key(1073741885, "F4Key"),
Key(1073741886, "F5Key"),
Key(1073741887, "F6Key"),
Key(1073741888, "F7Key"),
Key(1073741889, "F8Key"),
Key(1073741890, "F9Key"),
Key(1073741891, "F10Key"),
Key(1073741892, "F11Key"),
Key(1073741893, "F12Key"),
Key(1073741894, "PrintScreenKey"),
Key(1073741895, "ScrollLockKey"),
Key(1073741896, "PauseKey"),
Key(1073741897, "InsertKey"),
Key(1073741898, "HomeKey"),
Key(1073741899, "PageUpKey"),
Key(127, "DeleteKey"),
Key(1073741901, "EndKey"),
Key(1073741902, "PageDownKey"),
Key(1073741903, "RightKey"),
Key(1073741904, "LeftKey"),
Key(1073741905, "DownKey"),
Key(1073741906, "UpKey"),
Key(1073741907, "NumLockClearKey"),
Key(1073741908, "KpDivideKey"),
Key(1073741909, "KpMultiplyKey"),
Key(1073741910, "KpMinusKey"),
Key(1073741911, "KpPlusKey"),
Key(1073741912, "KpEnterKey"),
Key(1073741913, "Kp1Key"),
Key(1073741914, "Kp2Key"),
Key(1073741915, "Kp3Key"),
Key(1073741916, "Kp4Key"),
Key(1073741917, "Kp5Key"),
Key(1073741918, "Kp6Key"),
Key(1073741919, "Kp7Key"),
Key(1073741920, "Kp8Key"),
Key(1073741921, "Kp9Key"),
Key(1073741922, "Kp0Key"),
Key(1073741923, "KpPeriodKey"),
Key(1073741925, "ApplicationKey"),
Key(1073741926, "PowerKey"),
Key(1073741927, "KpEqualsKey"),
Key(1073741928, "F13Key"),
Key(1073741929, "F14Key"),
Key(1073741930, "F15Key"),
Key(1073741931, "F16Key"),
Key(1073741932, "F17Key"),
Key(1073741933, "F18Key"),
Key(1073741934, "F19Key"),
Key(1073741935, "F20Key"),
Key(1073741936, "F21Key"),
Key(1073741937, "F22Key"),
Key(1073741938, "F23Key"),
Key(1073741939, "F24Key"),
Key(1073741940, "ExecuteKey"),
Key(1073741941, "HelpKey"),
Key(1073741942, "MenuKey"),
Key(1073741943, "SelectKey"),
Key(1073741944, "StopKey"),
Key(1073741945, "AgainKey"),
Key(1073741946, "UndoKey"),
Key(1073741947, "CutKey"),
Key(1073741948, "CopyKey"),
Key(1073741949, "PasteKey"),
Key(1073741950, "FindKey"),
Key(1073741951, "MuteKey"),
Key(1073741952, "VolumeUpKey"),
Key(1073741953, "VolumeDownKey"),
Key(1073741957, "KpCommaKey"),
Key(1073741958, "KpEqualsAS400Key"),
Key(1073741977, "AltEraseKey"),
Key(1073741978, "SysreqKey"),
Key(1073741979, "CancelKey"),
Key(1073741980, "ClearKey"),
Key(1073741981, "PriorKey"),
Key(1073741982, "Return2Key"),
Key(1073741983, "SeparatorKey"),
Key(1073741984, "OutKey"),
Key(1073741985, "OperKey"),
Key(1073741986, "ClearAgainKey"),
Key(1073741987, "CrSelKey"),
Key(1073741988, "ExSelKey"),
Key(1073742000, "Kp00Key"),
Key(1073742001, "Kp000Key"),
Key(1073742002, "ThousandsSeparatorKey"),
Key(1073742003, "DecimalSeparatorKey"),
Key(1073742004, "CurrencyUnitKey"),
Key(1073742005, "CurrencySubUnitKey"),
Key(1073742006, "KpLeftParenKey"),
Key(1073742007, "KpRightParenKey"),
Key(1073742008, "KpLeftBraceKey"),
Key(1073742009, "KpRightBraceKey"),
Key(1073742010, "KpTabKey"),
Key(1073742011, "KpBackspaceKey"),
Key(1073742012, "KpAKey"),
Key(1073742013, "KpBKey"),
Key(1073742014, "KpCKey"),
Key(1073742015, "KpDKey"),
Key(1073742016, "KpEKey"),
Key(1073742017, "KpFKey"),
Key(1073742018, "KpXorKey"),
Key(1073742019, "KpPowerKey"),
Key(1073742020, "KpPercentKey"),
Key(1073742021, "KpLessKey"),
Key(1073742022, "KpGreaterKey"),
Key(1073742023, "KpAmpersandKey"),
Key(1073742024, "KpDblAmpersandKey"),
Key(1073742025, "KpVerticalBarKey"),
Key(1073742026, "KpDblVerticalBarKey"),
Key(1073742027, "KpColonKey"),
Key(1073742028, "KpHashKey"),
Key(1073742029, "KpSpaceKey"),
Key(1073742030, "KpAtKey"),
Key(1073742031, "KpExclamKey"),
Key(1073742032, "KpMemStoreKey"),
Key(1073742033, "KpMemRecallKey"),
Key(1073742034, "KpMemClearKey"),
Key(1073742035, "KpMemAddKey"),
Key(1073742036, "KpMemSubtractKey"),
Key(1073742037, "KpMemMultiplyKey"),
Key(1073742038, "KpMemDivideKey"),
Key(1073742039, "KpPlusMinusKey"),
Key(1073742040, "KpCearKey"),
Key(1073742041, "KpClearEntryKey"),
Key(1073742042, "KpBinaryKey"),
Key(1073742043, "KpOctalKey"),
Key(1073742044, "KpDecimalKey"),
Key(1073742045, "KpHexadecimalKey"),
Key(1073742048, "LCtrlKey"),
Key(1073742049, "LShiftKey"),
Key(1073742050, "LAltKey"),
Key(1073742051, "LGuiKey"),
Key(1073742052, "RCtrlKey"),
Key(1073742053, "RShiftKey"),
Key(1073742054, "RAltKey"),
Key(1073742055, "RGuiKey"),
Key(1073742081, "ModeKey"),
Key(1073742082, "AudioNextKey"),
Key(1073742083, "AudioPrevKey"),
Key(1073742084, "AudioStopKey"),
Key(1073742085, "AudioPlayKey"),
Key(1073742086, "AudioMuteKey"),
Key(1073742087, "MediaSelectKey"),
Key(1073742088, "WwwKey"),
Key(1073742089, "MailKey"),
Key(1073742090, "CalculatorKey"),
Key(1073742091, "ComputerKey"),
Key(1073742092, "AcSearchKey"),
Key(1073742093, "AcHomeKey"),
Key(1073742094, "AcBackKey"),
Key(1073742095, "AcForwardKey"),
Key(1073742096, "AcStopKey"),
Key(1073742097, "AcRefreshKey"),
Key(1073742098, "AcBookmarksKey"),
Key(1073742099, "BrightnessDownKey"),
Key(1073742100, "BrightnessUpKey"),
Key(1073742101, "DisplaySwitchKey"),
Key(1073742102, "KbdIllumToggleKey"),
Key(1073742103, "KbdIllumDownKey"),
Key(1073742104, "KbdIllumUpKey"),
Key(1073742105, "EjectKey"),
Key(1073742106, "SleepKey"),
];
entries.sort();
unsafe {
longest_ident = entries.iter().map(|&key| key.ident().len()).max_by(|&i| i).unwrap();
}
try!(out.write("// This automatically generated file is used as sdl2::keycode.
use std::hash::Hash;
use std::hash::sip::SipState;
use std::num::FromPrimitive;
use std::num::ToPrimitive;
#[deriving(PartialEq, Eq, Show)]
pub enum KeyCode {
".as_bytes()));
for &entry in entries.iter() {
try!(out.write(format!(" {} = {},\n", entry.padded_ident(), entry.code).container_as_bytes()));
}
try!(out.write("
}
impl Hash for KeyCode {
#[inline]
fn hash(&self, state: &mut SipState) {
self.code().hash(state);
}
}
impl KeyCode {
/// Get the code
pub fn code(&self) -> i32 {
match *self {
".as_bytes()));
for &entry in entries.iter() {
try!(out.write(format!(" {} => {},\n", entry.padded_ident(), entry.code).container_as_bytes()));
}
try!(out.write("
}
}
}
impl ToPrimitive for KeyCode {
/// Equivalent to `self.code()`
".as_bytes()));
let types = vec!("i64", "u64", "int");
for primitive_type in types.iter() {
try!(out.write(format!("fn to_{}(&self) -> Option<{}> {{
Some(self.code() as {})
}}\n", *primitive_type, *primitive_type, *primitive_type).container_as_bytes()));
}
try!(out.write("
}
impl FromPrimitive for KeyCode {
/// Get a *registered* key code.
///
/// This will return UnknownKey if an unknown code is passed.
///
/// For example, `from_int(13)` will return `ReturnKey`.
".as_bytes()));
for primitive_type in types.iter() {
try!(out.write(format!("
fn from_{}(n: {}) -> Option<KeyCode> {{
match n {{
", *primitive_type, *primitive_type).container_as_bytes()));
for &entry in entries.iter() {
try!(out.write(format!(" {} => Some({}),\n", entry.code, entry.ident()).container_as_bytes()));
}
try!(out.write("
_ => { Some(UnknownKey) }
}
}\n".as_bytes()));
}
try!(out.write("
}".as_bytes()));
try!(out.flush());
Ok(())
}
-57
View File
@@ -1,57 +0,0 @@
#![feature(macro_rules)]
#![crate_name = "codegen"]
use std::os;
use std::io::BufferedWriter;
use std::io::File;
use std::io;
use std::io::UserDir;
use std::io::stdio::println;
use std::io::fs::mkdir_recursive;
use std::path::GenericPath;
pub mod branchify;
pub mod keycode;
pub mod scancode;
fn main() {
let args = os::args();
match args.len() {
0 => {
println("usage: codegen [keycode|scancode].rs destdir");
os::set_exit_status(1);
},
3 => {
let output_dir = GenericPath::new(args[2].as_slice());
match mkdir_recursive(&output_dir, UserDir) {
Err(e) => fail!("Could not create directory for generated sources: {:s}", e.desc),
Ok(_) => {},
};
if "keycode.rs" == args[1].as_slice() {
match keycode::generate(&output_dir) {
Ok(_) => {},
Err(e) => fail!("Could not automatically generate sources for keycodes: {:s}", e.desc),
};
} else if "scancode.rs" == args[1].as_slice() {
match scancode::generate(&output_dir) {
Ok(_) => {},
Err(e) => fail!("Could not automatically generate sources for scancodes: {:s}", e.desc),
};
} else {
println!("unknown thing-to-generate '{}'", args.get(1));
os::set_exit_status(1);
}
},
_ => {
println!("usage: {} [keycode|scancode].rs destdir", args.get(0));
os::set_exit_status(1);
}
}
}
pub fn get_writer(output_dir: &Path, filename: &str) -> BufferedWriter<File> {
match File::open_mode(&output_dir.join(filename), io::Truncate, io::Write) {
Ok(writer) => BufferedWriter::new(writer),
Err(e) => fail!("Unable to write file: {:s}", e.desc),
}
}
-401
View File
@@ -1,401 +0,0 @@
use std::io::{IoResult,Writer};
use std::path::BytesContainer;
use super::get_writer;
struct ScanCode {
code: uint,
ident: &'static str,
}
impl PartialOrd for ScanCode {
fn partial_cmp(&self, other: &ScanCode) -> Option<Ordering> {
match (!self.lt(other), !other.lt(self)) {
(false, false) => None,
(false, true) => Some(Less),
(true, false) => Some(Greater),
(true, true) => Some(Equal),
}
}
fn lt (&self, other: &ScanCode) -> bool {
self.code < other.code
}
}
impl PartialEq for ScanCode {
fn eq (&self, other: &ScanCode) -> bool {
if self.code == other.code {
true
} else {
false
}
}
}
impl Ord for ScanCode {
fn cmp(&self, other: &ScanCode) -> Ordering {
if self.code < other.code {
Less
} else if self.code > other.code {
Greater
} else { Equal }
}
}
impl Eq for ScanCode {
}
#[allow(non_snake_case_functions)]
fn ScanCode(code: uint, ident: &'static str) -> ScanCode {
ScanCode { code: code, ident: ident }
}
impl ScanCode {
fn ident(&self) -> String {
self.ident.to_string()
}
fn padded_ident(&self) -> String {
self.ident().append(" ".repeat(unsafe { longest_ident } - self.ident().len()).as_slice())
}
}
static mut longest_ident: uint = 0;
pub fn generate(output_dir: &Path) -> IoResult<()> {
let mut out = get_writer(output_dir, "scancode.rs");
let mut entries = [
ScanCode(0, "UnknownScanCode"),
ScanCode(4, "AScanCode"),
ScanCode(5, "BScanCode"),
ScanCode(6, "CScanCode"),
ScanCode(7, "DScanCode"),
ScanCode(8, "EScanCode"),
ScanCode(9, "FScanCode"),
ScanCode(10, "GScanCode"),
ScanCode(11, "HScanCode"),
ScanCode(12, "IScanCode"),
ScanCode(13, "JScanCode"),
ScanCode(14, "KScanCode"),
ScanCode(15, "LScanCode"),
ScanCode(16, "MScanCode"),
ScanCode(17, "NScanCode"),
ScanCode(18, "OScanCode"),
ScanCode(19, "PScanCode"),
ScanCode(20, "QScanCode"),
ScanCode(21, "RScanCode"),
ScanCode(22, "SScanCode"),
ScanCode(23, "TScanCode"),
ScanCode(24, "UScanCode"),
ScanCode(25, "VScanCode"),
ScanCode(26, "WScanCode"),
ScanCode(27, "XScanCode"),
ScanCode(28, "YScanCode"),
ScanCode(29, "ZScanCode"),
ScanCode(30, "Num1ScanCode"),
ScanCode(31, "Num2ScanCode"),
ScanCode(32, "Num3ScanCode"),
ScanCode(33, "Num4ScanCode"),
ScanCode(34, "Num5ScanCode"),
ScanCode(35, "Num6ScanCode"),
ScanCode(36, "Num7ScanCode"),
ScanCode(37, "Num8ScanCode"),
ScanCode(38, "Num9ScanCode"),
ScanCode(39, "Num0ScanCode"),
ScanCode(40, "ReturnScanCode"),
ScanCode(41, "EscapeScanCode"),
ScanCode(42, "BackspaceScanCode"),
ScanCode(43, "TabScanCode"),
ScanCode(44, "SpaceScanCode"),
ScanCode(45, "MinusScanCode"),
ScanCode(46, "EqualsScanCode"),
ScanCode(47, "LeftBracketScanCode"),
ScanCode(48, "RightBracketScanCode"),
ScanCode(49, "BackslashScanCode"),
ScanCode(50, "NonUsHashScanCode"),
ScanCode(51, "SemicolonScanCode"),
ScanCode(52, "ApostropheScanCode"),
ScanCode(53, "GraveScanCode"),
ScanCode(54, "CommaScanCode"),
ScanCode(55, "PeriodScanCode"),
ScanCode(56, "SlashScanCode"),
ScanCode(57, "CapsLockScanCode"),
ScanCode(58, "F1ScanCode"),
ScanCode(59, "F2ScanCode"),
ScanCode(60, "F3ScanCode"),
ScanCode(61, "F4ScanCode"),
ScanCode(62, "F5ScanCode"),
ScanCode(63, "F6ScanCode"),
ScanCode(64, "F7ScanCode"),
ScanCode(65, "F8ScanCode"),
ScanCode(66, "F9ScanCode"),
ScanCode(67, "F10ScanCode"),
ScanCode(68, "F11ScanCode"),
ScanCode(69, "F12ScanCode"),
ScanCode(70, "PrintScreenScanCode"),
ScanCode(71, "ScrollLockScanCode"),
ScanCode(72, "PauseScanCode"),
ScanCode(73, "InsertScanCode"),
ScanCode(74, "HomeScanCode"),
ScanCode(75, "PageUpScanCode"),
ScanCode(76, "DeleteScanCode"),
ScanCode(77, "EndScanCode"),
ScanCode(78, "PageDownScanCode"),
ScanCode(79, "RightScanCode"),
ScanCode(80, "LeftScanCode"),
ScanCode(81, "DownScanCode"),
ScanCode(82, "UpScanCode"),
ScanCode(83, "NumLockClearScanCode"),
ScanCode(84, "KpDivideScanCode"),
ScanCode(85, "KpMultiplyScanCode"),
ScanCode(86, "KpMinusScanCode"),
ScanCode(87, "KpPlusScanCode"),
ScanCode(88, "KpEnterScanCode"),
ScanCode(89, "Kp1ScanCode"),
ScanCode(90, "Kp2ScanCode"),
ScanCode(91, "Kp3ScanCode"),
ScanCode(92, "Kp4ScanCode"),
ScanCode(93, "Kp5ScanCode"),
ScanCode(94, "Kp6ScanCode"),
ScanCode(95, "Kp7ScanCode"),
ScanCode(96, "Kp8ScanCode"),
ScanCode(97, "Kp9ScanCode"),
ScanCode(98, "Kp0ScanCode"),
ScanCode(99, "KpPeriodScanCode"),
ScanCode(100, "NonUsBackslashScanCode"),
ScanCode(101, "ApplicationScanCode"),
ScanCode(102, "PowerScanCode"),
ScanCode(103, "KpEqualsScanCode"),
ScanCode(104, "F13ScanCode"),
ScanCode(105, "F14ScanCode"),
ScanCode(106, "F15ScanCode"),
ScanCode(107, "F16ScanCode"),
ScanCode(108, "F17ScanCode"),
ScanCode(109, "F18ScanCode"),
ScanCode(110, "F19ScanCode"),
ScanCode(111, "F20ScanCode"),
ScanCode(112, "F21ScanCode"),
ScanCode(113, "F22ScanCode"),
ScanCode(114, "F23ScanCode"),
ScanCode(115, "F24ScanCode"),
ScanCode(116, "ExecuteScanCode"),
ScanCode(117, "HelpScanCode"),
ScanCode(118, "MenuScanCode"),
ScanCode(119, "SelectScanCode"),
ScanCode(120, "StopScanCode"),
ScanCode(121, "AgainScanCode"),
ScanCode(122, "UndoScanCode"),
ScanCode(123, "CutScanCode"),
ScanCode(124, "CopyScanCode"),
ScanCode(125, "PasteScanCode"),
ScanCode(126, "FindScanCode"),
ScanCode(127, "MuteScanCode"),
ScanCode(128, "VolumeUpScanCode"),
ScanCode(129, "VolumeDownScanCode"),
ScanCode(133, "KpCommaScanCode"),
ScanCode(134, "KpEqualsAS400ScanCode"),
ScanCode(135, "International1ScanCode"),
ScanCode(136, "International2ScanCode"),
ScanCode(137, "International3ScanCode"),
ScanCode(138, "International4ScanCode"),
ScanCode(139, "International5ScanCode"),
ScanCode(140, "International6ScanCode"),
ScanCode(141, "International7ScanCode"),
ScanCode(142, "International8ScanCode"),
ScanCode(143, "International9ScanCode"),
ScanCode(144, "Lang1ScanCode"),
ScanCode(145, "Lang2ScanCode"),
ScanCode(146, "Lang3ScanCode"),
ScanCode(147, "Lang4ScanCode"),
ScanCode(148, "Lang5ScanCode"),
ScanCode(149, "Lang6ScanCode"),
ScanCode(150, "Lang7ScanCode"),
ScanCode(151, "Lang8ScanCode"),
ScanCode(152, "Lang9ScanCode"),
ScanCode(153, "AltEraseScanCode"),
ScanCode(154, "SysReqScanCode"),
ScanCode(155, "CancelScanCode"),
ScanCode(156, "ClearScanCode"),
ScanCode(157, "PriorScanCode"),
ScanCode(158, "Return2ScanCode"),
ScanCode(159, "SeparatorScanCode"),
ScanCode(160, "OutScanCode"),
ScanCode(161, "OperScanCode"),
ScanCode(162, "ClearAgainScanCode"),
ScanCode(163, "CrseScanCode"),
ScanCode(164, "ExseLScanCode"),
ScanCode(176, "Kp00ScanCode"),
ScanCode(177, "Kp000ScanCode"),
ScanCode(178, "ThousandsSeparatorScanCode"),
ScanCode(179, "DecimalSeparatorScanCode"),
ScanCode(180, "CurrencyUnitScanCode"),
ScanCode(181, "CurrencySubUnitScanCode"),
ScanCode(182, "KpLeftParenScanCode"),
ScanCode(183, "KpRightParenScanCode"),
ScanCode(184, "KpLeftBraceScanCode"),
ScanCode(185, "KpRightBraceScanCode"),
ScanCode(186, "KpTabScanCode"),
ScanCode(187, "KpBackspaceScanCode"),
ScanCode(188, "KpAScanCode"),
ScanCode(189, "KpBScanCode"),
ScanCode(190, "KpCScanCode"),
ScanCode(191, "KpDScanCode"),
ScanCode(192, "KpEScanCode"),
ScanCode(193, "KpFScanCode"),
ScanCode(194, "KpXorScanCode"),
ScanCode(195, "KpPowerScanCode"),
ScanCode(196, "KpPercentScanCode"),
ScanCode(197, "KpLessScanCode"),
ScanCode(198, "KpGreaterScanCode"),
ScanCode(199, "KpAmpersandScanCode"),
ScanCode(200, "KpDblAmpersandScanCode"),
ScanCode(201, "KpVerticalBarScanCode"),
ScanCode(202, "KpDblVerticalBarScanCode"),
ScanCode(203, "KpColonScanCode"),
ScanCode(204, "KpHashScanCode"),
ScanCode(205, "KpSpaceScanCode"),
ScanCode(206, "KpAtScanCode"),
ScanCode(207, "KpExclamScanCode"),
ScanCode(208, "KpMemStoreScanCode"),
ScanCode(209, "KpMemRecallScanCode"),
ScanCode(210, "KpMemClearScanCode"),
ScanCode(211, "KpMemAddScanCode"),
ScanCode(212, "KpMemSubtractScanCode"),
ScanCode(213, "KpMemMultiplyScanCode"),
ScanCode(214, "KpMemDivideScanCode"),
ScanCode(215, "KpPlusMinusScanCode"),
ScanCode(216, "KpClearScanCode"),
ScanCode(217, "KpClearEntryScanCode"),
ScanCode(218, "KpBinaryScanCode"),
ScanCode(219, "KpOoctalScanCode"),
ScanCode(220, "KpDecimalScanCode"),
ScanCode(221, "KpHexadecimalScanCode"),
ScanCode(224, "LCtrlScanCode"),
ScanCode(225, "LShiftScanCode"),
ScanCode(226, "LAltScanCode"),
ScanCode(227, "LGuiScanCode"),
ScanCode(228, "RCtrlScanCode"),
ScanCode(229, "RShiftScanCode"),
ScanCode(230, "RAltScanCode"),
ScanCode(231, "RGuiScanCode"),
ScanCode(257, "ModeScanCode"),
ScanCode(258, "AudioNextScanCode"),
ScanCode(259, "AudioPrevScanCode"),
ScanCode(260, "AudioStopScanCode"),
ScanCode(261, "AudioPlayScanCode"),
ScanCode(262, "AudioMuteScanCode"),
ScanCode(263, "MediaSelectScanCode"),
ScanCode(264, "WwwScanCode"),
ScanCode(265, "MailScanCode"),
ScanCode(266, "CalculatorScanCode"),
ScanCode(267, "ComputerScanCode"),
ScanCode(268, "AcSearchScanCode"),
ScanCode(269, "AcHomeScanCode"),
ScanCode(270, "AcBackScanCode"),
ScanCode(271, "AcForwardScanCode"),
ScanCode(272, "AcStopScanCode"),
ScanCode(273, "AcRefreshScanCode"),
ScanCode(274, "AcBookmarksScanCode"),
ScanCode(275, "BrightnessDownScanCode"),
ScanCode(276, "BrightnessUpScanCode"),
ScanCode(277, "DisplaySwitchScanCode"),
ScanCode(278, "KbdIllumToggleScanCode"),
ScanCode(279, "KbdIllumDownScanCode"),
ScanCode(280, "KbdIllumUpScanCode"),
ScanCode(281, "EjectScanCode"),
ScanCode(282, "SleepScanCode"),
ScanCode(283, "App1ScanCode"),
ScanCode(284, "App2ScanCode"),
ScanCode(512, "NumScanCode"),
];
entries.sort();
unsafe {
longest_ident = entries.iter().map(|&key| key.ident().len()).max_by(|&i| i).unwrap();
}
try!(out.write("// This automatically generated file is used as sdl2::scancode.
use std::hash::Hash;
use std::hash::sip::SipState;
use std::num::FromPrimitive;
use std::num::ToPrimitive;
#[deriving(PartialEq, Eq, Show)]
pub enum ScanCode {
".as_bytes()));
for &entry in entries.iter() {
try!(out.write(format!(" {} = {},\n", entry.padded_ident(), entry.code).container_as_bytes()));
}
try!(out.write("
}
impl Hash for ScanCode {
#[inline]
fn hash(&self, state: &mut SipState) {
self.code().hash(state);
}
}
impl ScanCode {
/// Get the code
pub fn code(&self) -> i32 {
match *self {
".as_bytes()));
for &entry in entries.iter() {
try!(out.write(format!(" {} => {},\n", entry.padded_ident(), entry.code).container_as_bytes()));
}
try!(out.write("
}
}
}
impl ToPrimitive for ScanCode {
/// Equivalent to `self.code()`
".as_bytes()));
let types = vec!("i64", "u64", "int");
for primitive_type in types.iter() {
try!(out.write(format!("fn to_{}(&self) -> Option<{}> {{
Some(self.code() as {})
}}\n", *primitive_type, *primitive_type, *primitive_type).container_as_bytes()));
}
try!(out.write("
}
impl FromPrimitive for ScanCode {
/// Get a *registered* scan code.
///
/// This will return UnknownScanCode if an unknown code is passed.
///
/// For example, `from_int(4)` will return `AScanCode`.
".as_bytes()));
for primitive_type in types.iter() {
try!(out.write(format!("
fn from_{}(n: {}) -> Option<ScanCode> {{
match n {{
", *primitive_type, *primitive_type).container_as_bytes()));
for &entry in entries.iter() {
try!(out.write(format!(" {} => Some({}),\n", entry.code, entry.ident()).container_as_bytes()));
}
try!(out.write("
_ => { Some(UnknownScanCode) }
}
}\n".as_bytes()));
}
try!(out.write("
}".as_bytes()));
try!(out.flush());
Ok(())
}
+10 -6
View File
@@ -15,10 +15,10 @@ use joystick::HatState;
use keyboard;
use keyboard::Mod;
use keyboard::ll::SDL_Keymod;
use keycode::KeyCode;
use keycode::{KeyCode, UnknownKey};
use mouse;
use mouse::{Mouse, MouseState};
use scancode::ScanCode;
use scancode::{ScanCode, UnknownScanCode};
use video;
use get_error;
use SdlResult;
@@ -760,8 +760,10 @@ impl Event {
};
KeyDownEvent(event.timestamp as uint, window,
FromPrimitive::from_int(event.keysym.sym as int).unwrap(),
FromPrimitive::from_int(event.keysym.scancode as int).unwrap(),
FromPrimitive::from_int(event.keysym.sym as int)
.unwrap_or(UnknownKey),
FromPrimitive::from_int(event.keysym.scancode as int)
.unwrap_or(UnknownScanCode),
keyboard::Mod::from_bits(event.keysym._mod as SDL_Keymod).unwrap())
}
KeyUpEventType => {
@@ -774,8 +776,10 @@ impl Event {
};
KeyUpEvent(event.timestamp as uint, window,
FromPrimitive::from_int(event.keysym.sym as int).unwrap(),
FromPrimitive::from_int(event.keysym.scancode as int).unwrap(),
FromPrimitive::from_int(event.keysym.sym as int)
.unwrap_or(UnknownKey),
FromPrimitive::from_int(event.keysym.scancode as int)
.unwrap_or(UnknownScanCode),
keyboard::Mod::from_bits(event.keysym._mod as SDL_Keymod).unwrap())
}
TextEditingEventType => {
+14 -11
View File
@@ -4,9 +4,9 @@ use std::ptr;
use std::string;
use std::vec;
use keycode::KeyCode;
use keycode::{KeyCode, UnknownKey};
use rect::Rect;
use scancode::ScanCode;
use scancode::{ScanCode, UnknownScanCode};
use video::Window;
#[allow(non_camel_case_types)]
@@ -86,7 +86,8 @@ pub fn get_keyboard_state() -> HashMap<ScanCode, bool> {
let mut current = 0;
while current < raw.len() {
state.insert(FromPrimitive::from_int(current as int).unwrap(),
state.insert(FromPrimitive::from_int(current as int)
.unwrap_or(UnknownScanCode),
raw[current] == 1);
current += 1;
}
@@ -104,21 +105,21 @@ pub fn set_mod_state(flags: Mod) {
pub fn get_key_from_scancode(scancode: ScanCode) -> KeyCode {
unsafe {
FromPrimitive::from_int(ll::SDL_GetKeyFromScancode(scancode.code()
as u32) as int).unwrap()
FromPrimitive::from_int(ll::SDL_GetKeyFromScancode(scancode as u32) as int)
.unwrap_or(UnknownKey)
}
}
pub fn get_scancode_from_key(key: KeyCode) -> ScanCode {
unsafe {
FromPrimitive::from_int(ll::SDL_GetScancodeFromKey(key.code())
as int).unwrap()
FromPrimitive::from_int(ll::SDL_GetScancodeFromKey(key as i32) as int)
.unwrap_or(UnknownScanCode)
}
}
pub fn get_scancode_name(scancode: ScanCode) -> String {
unsafe {
let scancode_name = ll::SDL_GetScancodeName(scancode.code() as u32);
let scancode_name = ll::SDL_GetScancodeName(scancode as u32);
string::raw::from_buf(scancode_name as *const u8)
}
}
@@ -126,14 +127,15 @@ pub fn get_scancode_name(scancode: ScanCode) -> String {
pub fn get_scancode_from_name(name: &str) -> ScanCode {
unsafe {
name.with_c_str(|name| {
FromPrimitive::from_int(ll::SDL_GetScancodeFromName(name) as int).unwrap()
FromPrimitive::from_int(ll::SDL_GetScancodeFromName(name) as int)
.unwrap_or(UnknownScanCode)
})
}
}
pub fn get_key_name(key: KeyCode) -> String {
unsafe {
let key_name = ll::SDL_GetKeyName(key.code());
let key_name = ll::SDL_GetKeyName(key as i32);
string::raw::from_buf(key_name as *const u8)
}
}
@@ -141,7 +143,8 @@ pub fn get_key_name(key: KeyCode) -> String {
pub fn get_key_from_name(name: &str) -> KeyCode {
unsafe {
name.with_c_str(|name| {
FromPrimitive::from_int(ll::SDL_GetKeyFromName(name) as int).unwrap()
FromPrimitive::from_int(ll::SDL_GetKeyFromName(name) as int)
.unwrap_or(UnknownKey)
})
}
}
+265
View File
@@ -0,0 +1,265 @@
use std::hash::{mod, Hash};
#[deriving(PartialEq, Eq, FromPrimitive, Show)]
pub enum KeyCode {
UnknownKey = 0,
BackspaceKey = 8,
TabKey = 9,
ReturnKey = 13,
EscapeKey = 27,
SpaceKey = 32,
ExclaimKey = 33,
QuotedblKey = 34,
HashKey = 35,
DollarKey = 36,
PercentKey = 37,
AmpersandKey = 38,
QuoteKey = 39,
LeftParenKey = 40,
RightParenKey = 41,
AsteriskKey = 42,
PlusKey = 43,
CommaKey = 44,
MinusKey = 45,
PeriodKey = 46,
SlashKey = 47,
Num0Key = 48,
Num1Key = 49,
Num2Key = 50,
Num3Key = 51,
Num4Key = 52,
Num5Key = 53,
Num6Key = 54,
Num7Key = 55,
Num8Key = 56,
Num9Key = 57,
ColonKey = 58,
SemicolonKey = 59,
LessKey = 60,
EqualsKey = 61,
GreaterKey = 62,
QuestionKey = 63,
AtKey = 64,
LeftBracketKey = 91,
BackslashKey = 92,
RightBracketKey = 93,
CaretKey = 94,
UnderscoreKey = 95,
BackquoteKey = 96,
AKey = 97,
BKey = 98,
CKey = 99,
DKey = 100,
EKey = 101,
FKey = 102,
GKey = 103,
HKey = 104,
IKey = 105,
JKey = 106,
KKey = 107,
LKey = 108,
MKey = 109,
NKey = 110,
OKey = 111,
PKey = 112,
QKey = 113,
RKey = 114,
SKey = 115,
TKey = 116,
UKey = 117,
VKey = 118,
WKey = 119,
XKey = 120,
YKey = 121,
ZKey = 122,
DeleteKey = 127,
CapsLockKey = 1073741881,
F1Key = 1073741882,
F2Key = 1073741883,
F3Key = 1073741884,
F4Key = 1073741885,
F5Key = 1073741886,
F6Key = 1073741887,
F7Key = 1073741888,
F8Key = 1073741889,
F9Key = 1073741890,
F10Key = 1073741891,
F11Key = 1073741892,
F12Key = 1073741893,
PrintScreenKey = 1073741894,
ScrollLockKey = 1073741895,
PauseKey = 1073741896,
InsertKey = 1073741897,
HomeKey = 1073741898,
PageUpKey = 1073741899,
EndKey = 1073741901,
PageDownKey = 1073741902,
RightKey = 1073741903,
LeftKey = 1073741904,
DownKey = 1073741905,
UpKey = 1073741906,
NumLockClearKey = 1073741907,
KpDivideKey = 1073741908,
KpMultiplyKey = 1073741909,
KpMinusKey = 1073741910,
KpPlusKey = 1073741911,
KpEnterKey = 1073741912,
Kp1Key = 1073741913,
Kp2Key = 1073741914,
Kp3Key = 1073741915,
Kp4Key = 1073741916,
Kp5Key = 1073741917,
Kp6Key = 1073741918,
Kp7Key = 1073741919,
Kp8Key = 1073741920,
Kp9Key = 1073741921,
Kp0Key = 1073741922,
KpPeriodKey = 1073741923,
ApplicationKey = 1073741925,
PowerKey = 1073741926,
KpEqualsKey = 1073741927,
F13Key = 1073741928,
F14Key = 1073741929,
F15Key = 1073741930,
F16Key = 1073741931,
F17Key = 1073741932,
F18Key = 1073741933,
F19Key = 1073741934,
F20Key = 1073741935,
F21Key = 1073741936,
F22Key = 1073741937,
F23Key = 1073741938,
F24Key = 1073741939,
ExecuteKey = 1073741940,
HelpKey = 1073741941,
MenuKey = 1073741942,
SelectKey = 1073741943,
StopKey = 1073741944,
AgainKey = 1073741945,
UndoKey = 1073741946,
CutKey = 1073741947,
CopyKey = 1073741948,
PasteKey = 1073741949,
FindKey = 1073741950,
MuteKey = 1073741951,
VolumeUpKey = 1073741952,
VolumeDownKey = 1073741953,
KpCommaKey = 1073741957,
KpEqualsAS400Key = 1073741958,
AltEraseKey = 1073741977,
SysreqKey = 1073741978,
CancelKey = 1073741979,
ClearKey = 1073741980,
PriorKey = 1073741981,
Return2Key = 1073741982,
SeparatorKey = 1073741983,
OutKey = 1073741984,
OperKey = 1073741985,
ClearAgainKey = 1073741986,
CrSelKey = 1073741987,
ExSelKey = 1073741988,
Kp00Key = 1073742000,
Kp000Key = 1073742001,
ThousandsSeparatorKey = 1073742002,
DecimalSeparatorKey = 1073742003,
CurrencyUnitKey = 1073742004,
CurrencySubUnitKey = 1073742005,
KpLeftParenKey = 1073742006,
KpRightParenKey = 1073742007,
KpLeftBraceKey = 1073742008,
KpRightBraceKey = 1073742009,
KpTabKey = 1073742010,
KpBackspaceKey = 1073742011,
KpAKey = 1073742012,
KpBKey = 1073742013,
KpCKey = 1073742014,
KpDKey = 1073742015,
KpEKey = 1073742016,
KpFKey = 1073742017,
KpXorKey = 1073742018,
KpPowerKey = 1073742019,
KpPercentKey = 1073742020,
KpLessKey = 1073742021,
KpGreaterKey = 1073742022,
KpAmpersandKey = 1073742023,
KpDblAmpersandKey = 1073742024,
KpVerticalBarKey = 1073742025,
KpDblVerticalBarKey = 1073742026,
KpColonKey = 1073742027,
KpHashKey = 1073742028,
KpSpaceKey = 1073742029,
KpAtKey = 1073742030,
KpExclamKey = 1073742031,
KpMemStoreKey = 1073742032,
KpMemRecallKey = 1073742033,
KpMemClearKey = 1073742034,
KpMemAddKey = 1073742035,
KpMemSubtractKey = 1073742036,
KpMemMultiplyKey = 1073742037,
KpMemDivideKey = 1073742038,
KpPlusMinusKey = 1073742039,
KpCearKey = 1073742040,
KpClearEntryKey = 1073742041,
KpBinaryKey = 1073742042,
KpOctalKey = 1073742043,
KpDecimalKey = 1073742044,
KpHexadecimalKey = 1073742045,
LCtrlKey = 1073742048,
LShiftKey = 1073742049,
LAltKey = 1073742050,
LGuiKey = 1073742051,
RCtrlKey = 1073742052,
RShiftKey = 1073742053,
RAltKey = 1073742054,
RGuiKey = 1073742055,
ModeKey = 1073742081,
AudioNextKey = 1073742082,
AudioPrevKey = 1073742083,
AudioStopKey = 1073742084,
AudioPlayKey = 1073742085,
AudioMuteKey = 1073742086,
MediaSelectKey = 1073742087,
WwwKey = 1073742088,
MailKey = 1073742089,
CalculatorKey = 1073742090,
ComputerKey = 1073742091,
AcSearchKey = 1073742092,
AcHomeKey = 1073742093,
AcBackKey = 1073742094,
AcForwardKey = 1073742095,
AcStopKey = 1073742096,
AcRefreshKey = 1073742097,
AcBookmarksKey = 1073742098,
BrightnessDownKey = 1073742099,
BrightnessUpKey = 1073742100,
DisplaySwitchKey = 1073742101,
KbdIllumToggleKey = 1073742102,
KbdIllumDownKey = 1073742103,
KbdIllumUpKey = 1073742104,
EjectKey = 1073742105,
SleepKey = 1073742106,
}
impl<S: hash::Writer> Hash<S> for KeyCode {
#[inline]
fn hash(&self, state: &mut S) {
(*self as i32).hash(state);
}
}
impl ToPrimitive for KeyCode {
#[inline]
fn to_i64(&self) -> Option<i64> {
Some(*self as i64)
}
#[inline]
fn to_u64(&self) -> Option<u64> {
Some(*self as u64)
}
#[inline]
fn to_int(&self) -> Option<int> {
Some(*self as int)
}
}
+2 -5
View File
@@ -4,18 +4,15 @@
#![desc = "SDL2 bindings"]
#![license = "MIT"]
#![feature(globs)]
#![feature(macro_rules)]
#![feature(unsafe_destructor)]
#![feature(default_type_params, globs, macro_rules, unsafe_destructor)]
extern crate libc;
extern crate collections;
extern crate debug;
pub use sdl::*;
#[path = "generated/keycode.rs"]
pub mod keycode;
#[path = "generated/scancode.rs"]
pub mod scancode;
pub mod clipboard;
+271
View File
@@ -0,0 +1,271 @@
use std::hash::{mod, Hash};
#[deriving(PartialEq, Eq, FromPrimitive, Show)]
pub enum ScanCode {
UnknownScanCode = 0,
AScanCode = 4,
BScanCode = 5,
CScanCode = 6,
DScanCode = 7,
EScanCode = 8,
FScanCode = 9,
GScanCode = 10,
HScanCode = 11,
IScanCode = 12,
JScanCode = 13,
KScanCode = 14,
LScanCode = 15,
MScanCode = 16,
NScanCode = 17,
OScanCode = 18,
PScanCode = 19,
QScanCode = 20,
RScanCode = 21,
SScanCode = 22,
TScanCode = 23,
UScanCode = 24,
VScanCode = 25,
WScanCode = 26,
XScanCode = 27,
YScanCode = 28,
ZScanCode = 29,
Num1ScanCode = 30,
Num2ScanCode = 31,
Num3ScanCode = 32,
Num4ScanCode = 33,
Num5ScanCode = 34,
Num6ScanCode = 35,
Num7ScanCode = 36,
Num8ScanCode = 37,
Num9ScanCode = 38,
Num0ScanCode = 39,
ReturnScanCode = 40,
EscapeScanCode = 41,
BackspaceScanCode = 42,
TabScanCode = 43,
SpaceScanCode = 44,
MinusScanCode = 45,
EqualsScanCode = 46,
LeftBracketScanCode = 47,
RightBracketScanCode = 48,
BackslashScanCode = 49,
NonUsHashScanCode = 50,
SemicolonScanCode = 51,
ApostropheScanCode = 52,
GraveScanCode = 53,
CommaScanCode = 54,
PeriodScanCode = 55,
SlashScanCode = 56,
CapsLockScanCode = 57,
F1ScanCode = 58,
F2ScanCode = 59,
F3ScanCode = 60,
F4ScanCode = 61,
F5ScanCode = 62,
F6ScanCode = 63,
F7ScanCode = 64,
F8ScanCode = 65,
F9ScanCode = 66,
F10ScanCode = 67,
F11ScanCode = 68,
F12ScanCode = 69,
PrintScreenScanCode = 70,
ScrollLockScanCode = 71,
PauseScanCode = 72,
InsertScanCode = 73,
HomeScanCode = 74,
PageUpScanCode = 75,
DeleteScanCode = 76,
EndScanCode = 77,
PageDownScanCode = 78,
RightScanCode = 79,
LeftScanCode = 80,
DownScanCode = 81,
UpScanCode = 82,
NumLockClearScanCode = 83,
KpDivideScanCode = 84,
KpMultiplyScanCode = 85,
KpMinusScanCode = 86,
KpPlusScanCode = 87,
KpEnterScanCode = 88,
Kp1ScanCode = 89,
Kp2ScanCode = 90,
Kp3ScanCode = 91,
Kp4ScanCode = 92,
Kp5ScanCode = 93,
Kp6ScanCode = 94,
Kp7ScanCode = 95,
Kp8ScanCode = 96,
Kp9ScanCode = 97,
Kp0ScanCode = 98,
KpPeriodScanCode = 99,
NonUsBackslashScanCode = 100,
ApplicationScanCode = 101,
PowerScanCode = 102,
KpEqualsScanCode = 103,
F13ScanCode = 104,
F14ScanCode = 105,
F15ScanCode = 106,
F16ScanCode = 107,
F17ScanCode = 108,
F18ScanCode = 109,
F19ScanCode = 110,
F20ScanCode = 111,
F21ScanCode = 112,
F22ScanCode = 113,
F23ScanCode = 114,
F24ScanCode = 115,
ExecuteScanCode = 116,
HelpScanCode = 117,
MenuScanCode = 118,
SelectScanCode = 119,
StopScanCode = 120,
AgainScanCode = 121,
UndoScanCode = 122,
CutScanCode = 123,
CopyScanCode = 124,
PasteScanCode = 125,
FindScanCode = 126,
MuteScanCode = 127,
VolumeUpScanCode = 128,
VolumeDownScanCode = 129,
KpCommaScanCode = 133,
KpEqualsAS400ScanCode = 134,
International1ScanCode = 135,
International2ScanCode = 136,
International3ScanCode = 137,
International4ScanCode = 138,
International5ScanCode = 139,
International6ScanCode = 140,
International7ScanCode = 141,
International8ScanCode = 142,
International9ScanCode = 143,
Lang1ScanCode = 144,
Lang2ScanCode = 145,
Lang3ScanCode = 146,
Lang4ScanCode = 147,
Lang5ScanCode = 148,
Lang6ScanCode = 149,
Lang7ScanCode = 150,
Lang8ScanCode = 151,
Lang9ScanCode = 152,
AltEraseScanCode = 153,
SysReqScanCode = 154,
CancelScanCode = 155,
ClearScanCode = 156,
PriorScanCode = 157,
Return2ScanCode = 158,
SeparatorScanCode = 159,
OutScanCode = 160,
OperScanCode = 161,
ClearAgainScanCode = 162,
CrseScanCode = 163,
ExseLScanCode = 164,
Kp00ScanCode = 176,
Kp000ScanCode = 177,
ThousandsSeparatorScanCode = 178,
DecimalSeparatorScanCode = 179,
CurrencyUnitScanCode = 180,
CurrencySubUnitScanCode = 181,
KpLeftParenScanCode = 182,
KpRightParenScanCode = 183,
KpLeftBraceScanCode = 184,
KpRightBraceScanCode = 185,
KpTabScanCode = 186,
KpBackspaceScanCode = 187,
KpAScanCode = 188,
KpBScanCode = 189,
KpCScanCode = 190,
KpDScanCode = 191,
KpEScanCode = 192,
KpFScanCode = 193,
KpXorScanCode = 194,
KpPowerScanCode = 195,
KpPercentScanCode = 196,
KpLessScanCode = 197,
KpGreaterScanCode = 198,
KpAmpersandScanCode = 199,
KpDblAmpersandScanCode = 200,
KpVerticalBarScanCode = 201,
KpDblVerticalBarScanCode = 202,
KpColonScanCode = 203,
KpHashScanCode = 204,
KpSpaceScanCode = 205,
KpAtScanCode = 206,
KpExclamScanCode = 207,
KpMemStoreScanCode = 208,
KpMemRecallScanCode = 209,
KpMemClearScanCode = 210,
KpMemAddScanCode = 211,
KpMemSubtractScanCode = 212,
KpMemMultiplyScanCode = 213,
KpMemDivideScanCode = 214,
KpPlusMinusScanCode = 215,
KpClearScanCode = 216,
KpClearEntryScanCode = 217,
KpBinaryScanCode = 218,
KpOoctalScanCode = 219,
KpDecimalScanCode = 220,
KpHexadecimalScanCode = 221,
LCtrlScanCode = 224,
LShiftScanCode = 225,
LAltScanCode = 226,
LGuiScanCode = 227,
RCtrlScanCode = 228,
RShiftScanCode = 229,
RAltScanCode = 230,
RGuiScanCode = 231,
ModeScanCode = 257,
AudioNextScanCode = 258,
AudioPrevScanCode = 259,
AudioStopScanCode = 260,
AudioPlayScanCode = 261,
AudioMuteScanCode = 262,
MediaSelectScanCode = 263,
WwwScanCode = 264,
MailScanCode = 265,
CalculatorScanCode = 266,
ComputerScanCode = 267,
AcSearchScanCode = 268,
AcHomeScanCode = 269,
AcBackScanCode = 270,
AcForwardScanCode = 271,
AcStopScanCode = 272,
AcRefreshScanCode = 273,
AcBookmarksScanCode = 274,
BrightnessDownScanCode = 275,
BrightnessUpScanCode = 276,
DisplaySwitchScanCode = 277,
KbdIllumToggleScanCode = 278,
KbdIllumDownScanCode = 279,
KbdIllumUpScanCode = 280,
EjectScanCode = 281,
SleepScanCode = 282,
App1ScanCode = 283,
App2ScanCode = 284,
NumScanCode = 512,
}
impl<S: hash::Writer> Hash<S> for ScanCode {
#[inline]
fn hash(&self, state: &mut S) {
(*self as i32).hash(state);
}
}
impl ToPrimitive for ScanCode {
#[inline]
fn to_i64(&self) -> Option<i64> {
Some(*self as i64)
}
#[inline]
fn to_u64(&self) -> Option<u64> {
Some(*self as u64)
}
#[inline]
fn to_int(&self) -> Option<int> {
Some(*self as int)
}
}