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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
#![deny( warnings, missing_debug_implementations, missing_copy_implementations, trivial_casts, trivial_numeric_casts, unstable_features, unused_import_braces, unused_qualifications, missing_docs, unused_extern_crates, unused_qualifications, unused_results )] //! # rust-wooting-sdk //! This crate provides Rust bindings to the Analog and RGB SDKs provided by Wooting for the Wooting //! One and Wooting Two. //! //! ## Example //! See the [`wooting-sdk/src/examples/`][examples] directory for more examples. //! //! [examples]: https://github.com/davidtwco/rust-wooting-sdk/tree/master/wooting-sdk/examples //! //! ```rust,no_run //! # fn test() -> Result<(), wooting_sdk::WootingError> { //! use wooting_sdk::{ //! analog::read_analog_key, //! rgb::RgbKeyboard, //! Key //! }; //! //! let min = u8::min_value(); //! let max = u8::max_value(); //! //! // Check how far down W has been pressed.. //! match read_analog_key(Key::W)? { //! min => { /* ..not pressed. */ }, //! max => { /* ..completely pressed. */ }, //! _ => { /* ..partially pressed. */ }, //! } //! //! let mut keyboard = RgbKeyboard::default(); //! //! // Modify the keyboard array so QWERTY will be set to white.. //! keyboard.array_set_full(&[ //! (Key::Q, (255, 255, 255)), //! (Key::W, (255, 255, 255)), //! (Key::E, (255, 255, 255)), //! (Key::R, (255, 255, 255)), //! (Key::T, (255, 255, 255)), //! (Key::Y, (255, 255, 255)), //! ]); //! //! // ..and apply the change. //! keyboard.array_update(); //! # Ok(()) //! # } //! ``` use std::error::Error; use std::fmt::{self, Display}; /// Represents an error that can occur when querying the state of a Wooting keyboard. #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] pub enum WootingError { /// Indicates that the keyboard is disconnected. Disconnected, /// Indicates that the requested number of analog key values was invalid. Must be non-zero /// and less than sixteen. InvalidBufferSize, } impl Display for WootingError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { WootingError::Disconnected => write!(fmt, "Wooting keyboard is not connected"), WootingError::InvalidBufferSize => { write!(fmt, "Requested analog value of too many keys") } } } } impl Error for WootingError {} /// Types that implement this trait can be transformed into a matrix row and column. pub trait IntoMatrixRowColumn { /// Return a tuple `(row, column)` that represents the matrix row and column for this type. fn into_matrix_row_and_column(&self) -> (u8, u8); } /// Types that implement this trait can be associated with a scan index. pub trait FromScanIndex: Sized { /// Return the instance of this type for the given scan index. fn from_scan_index(index: u8) -> Option<Self>; } /// Represents a key on the keyboard. #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] pub enum Key { /// Escape key (`Esc`). Generates the escape character (ASCII 27). Escape, /// Function key (`F1`). Normally programmed to cause an operating system or application to /// perform certain actions. F1, /// Function key (`F2`). Normally programmed to cause an operating system or application to /// perform certain actions. F2, /// Function key (`F3`). Normally programmed to cause an operating system or application to /// perform certain actions. F3, /// Function key (`F4`). Normally programmed to cause an operating system or application to /// perform certain actions. F4, /// Function key (`F5`). Normally programmed to cause an operating system or application to /// perform certain actions. F5, /// Function key (`F6`). Normally programmed to cause an operating system or application to /// perform certain actions. F6, /// Function key (`F7`). Normally programmed to cause an operating system or application to /// perform certain actions. F7, /// Function key (`F8`). Normally programmed to cause an operating system or application to /// perform certain actions. F8, /// Function key (`F9`). Normally programmed to cause an operating system or application to /// perform certain actions. F9, /// Function key (`F10`). Normally programmed to cause an operating system or application to /// perform certain actions. F10, /// Function key (`F11`). Normally programmed to cause an operating system or application to /// perform certain actions. F11, /// Function key (`F12`). Normally programmed to cause an operating system or application to /// perform certain actions. F12, /// Print screen key (`Prt Sc`). May share the same key as system request. Normally takes a /// screenshot. PrintScreen, /// Pause (or break) key. Has no well defined purpose. Pause, /// Scroll lock key (`Scr Lk`). Has different functions or purposes depending on the /// application or operating system. Originally intended to make arrow keys scroll the current /// window contents instead of moving the cursor. ScrollLock, /// Analog profile key (`A1`). Switches to analog profile one. A1, /// Analog profile key (`A2`). Switches to analog profile two. A2, /// Analog profile key (`A3`). Switches to analog profile three. A3, /// Mode key. Toggles between digital and analog modes. Mode, /// Tilde key (`~`). Tilde, /// Number one key (`1`). One, /// Number two key (`2`). Two, /// Number three key (`3`). Three, /// Number four key (`4`). Four, /// Number five key (`5`). Five, /// Number six key (`6`). Six, /// Number seven key (`7`). Seven, /// Number eight key (`8`). Eight, /// Number nine key (`9`). Nine, /// Number zero key (`0`). Zero, /// Dash or hyphen key (`-`). Dash, /// Equals key (`=`). Equals, /// Backspace key. Moves display cursor one position backwards, deleting the character at /// that position and shifting back the text after that position by one position. Backspace, /// Insert key (`Ins`). Switches between two text entry modes - overtype or insert. Overtype /// mode replaces the character present in the current location. Insert mode inserts a /// character at the current position, forcing all characters past it one position further. Insert, /// Home key. Has the opposite effect of the end key. Home, /// Page up key (`Pg Up`). Scrolls up in documents. PageUp, /// Number lock key (`Num`). Affects the function of the numeric keypad located to the right /// of the main keyboard. NumLock, /// Divide key on the numpad (`/`). Types a forward slash or acts as a divison key in /// calculator applications. NumDivide, /// Multiply key on the numpad (`*`). Types a star or acts as a multiplication key in /// calculator applications. NumMultiply, /// Subtract key on the numpad (`-`). Types a dash or acts as a subtraction key in /// calculator applications. NumSubtract, /// Tab key. Advances cursor to next tab stop. Tab, /// Letter `q` key. Q, /// Letter `w` key. W, /// Letter `e` key. E, /// Letter `r` key. R, /// Letter `t` key. T, /// Letter `y` key. Y, /// Letter `u` key. U, /// Letter `i` key. I, /// Letter `o` key. O, /// Letter `p` key. P, /// Left square bracket key (`[`). LeftBracket, /// Right square bracket key (`]`). RightBracket, /// Backslash key (`\`). Backslash, /// Delete key (`Del`). Deletes the character in the position after the cursor. Delete, /// End key. Has the opposite effect of the home key. End, /// Page down key (`Pg Dn`). Scrolls down in documents. PageDown, /// Number seven key (`7`) on the numpad. NumSeven, /// Number eight key (`8`) on the numpad. NumEight, /// Number nine key (`9`) on the numpad. NumNine, /// Addition key on the numpad (`+`). Types a plus or acts as a addition key in calculator /// applications. NumAddition, /// Capitalization lock key. Causes all letters in latin-based scripts to be generated in /// capitals. CapsLock, /// Letter `a` key. A, /// Letter `s` key. S, /// Letter `d` key. D, /// Letter `f` key. F, /// Letter `g` key. G, /// Letter `h` key. H, /// Letter `j` key. J, /// Letter `k` key. K, /// Letter `l` key. L, /// Semi-colon key (`;`). SemiColon, /// Apostrophe key (`'`). Apostrophe, /// Represents a key that is specific to the keyboard layout. This key is positioned above /// the return key or to the left of the return key. On UK layouts, this is a pound (`#`) key. /// On US layouts, this is a backslash key. This key has the same scan index as the backslash /// key. ISO1, /// Return (or enter) key. Return, /// Number four key (`4`) on the numpad. NumFour, /// Number five key (`5`) on the numpad. NumFive, /// Number six key (`6`) on the numpad. NumSix, /// Left shift modifier key. Used to type capital letters and other alternate "upper" /// characters. LeftShift, /// Represents a key that is specific to the keyboard layout. This key is positioned to the /// right of the left shift key. On UK layouts, this is a backslash key. On US layouts, this /// key does not exist. ISO2, /// Letter `z` key. Z, /// Letter `x` key. X, /// Letter `c` key. C, /// Letter `v` key. V, /// Letter `b` key. B, /// Letter `n` key. N, /// Letter `m` key. M, /// Comma key (`,`). Comma, /// Period key (`.`). Period, /// Forward slash key (`/`). ForwardSlash, /// Right shift modifier key. Performs the same function as left shift. RightShift, /// Up arrow key. Moves the cursor in the upwards direction. UpArrow, /// Number one key (`1`) on the numpad. NumOne, /// Number two key (`2`) on the numpad. NumTwo, /// Number three key (`3`) on the numpad. NumThree, /// Return (or enter) key on the numpad. Performs the same function as the normal return key. NumReturn, /// Left control modifier key. Performs a special operation when pressed in conjunction with /// another key. LeftControl, /// Left mod (or Windows) modifier key. Normally invokes the operating system's start menu. LeftMod, /// Left alt modifier key. Used to change (alternate) the function of other pressed keys. LeftAlt, /// Space key (` `). Space, /// Right alt modifier key. Performs the same function as left alt. RightAlt, /// Right mod modifier key. Performs the same function as left mod. RightMod, /// Function key (`Fn`). Performs an alternative operation for some keys, normally defined /// by the keyboard and indicated by symbols on the key. Fn, /// Right control modifier key. Performs the same function as left control. RightControl, /// Left arrow key. Moves the cursor in the left direction. LeftArrow, /// Down arrow key. Moves the cursor in the down direction. DownArrow, /// Right arrow key. Moves the cursor in the right direction. RightArrow, /// Number zero key (`0`) on the numpad. NumZero, /// Delete key (`Del`) on the numpad. Performs the same function as the normal delete key. NumDelete, } impl Display for Key { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { use Key::*; write!( fmt, "{}", match self { Escape => "Esc", F1 => "F1", F2 => "F2", F3 => "F3", F4 => "F4", F5 => "F5", F6 => "F6", F7 => "F7", F8 => "F8", F9 => "F9", F10 => "F10", F11 => "F11", F12 => "F12", PrintScreen => "Print Screen", Pause => "Pause", ScrollLock => "Scroll Lock", A1 => "A1", A2 => "A2", A3 => "A3", Mode => "Mode", Tilde => "~", One => "1", Two => "2", Three => "3", Four => "4", Five => "5", Six => "6", Seven => "7", Eight => "8", Nine => "9", Zero => "0", Dash => "-", Equals => "=", Backspace => "Backspace", Insert => "Insert", Home => "Home", PageUp => "Page Up", NumLock => "Num Lock", NumDivide => "\\", NumMultiply => "*", NumSubtract => "-", Tab => "Tab", Q => "Q", W => "W", E => "E", R => "R", T => "T", Y => "Y", U => "U", I => "I", O => "O", P => "P", LeftBracket => "[", RightBracket => "]", Backslash => "\\", Delete => "Delete", End => "End", PageDown => "Page Down", NumSeven => "7", NumEight => "8", NumNine => "9", NumAddition => "+", CapsLock => "Caps Lock", A => "A", S => "S", D => "D", F => "F", G => "G", H => "H", J => "J", K => "K", L => "L", SemiColon => ";", Apostrophe => "'", ISO1 => "ISO", Return => "Return", NumFour => "4", NumFive => "5", NumSix => "6", LeftShift => "Left Shift", ISO2 => "ISO", Z => "Z", X => "X", C => "C", V => "V", B => "B", N => "N", M => "M", Comma => "Comma", Period => "Period", ForwardSlash => "/", RightShift => "Right Shift", UpArrow => "Up Arrow", NumOne => "1", NumTwo => "2", NumThree => "3", NumReturn => "Return", LeftControl => "Left Control", LeftMod => "Left Mod", LeftAlt => "Left Alt", Space => "Space", RightAlt => "Right Alt", RightMod => "Right Mod", Fn => "Fn", RightControl => "Right Control", LeftArrow => "Left Arrow", DownArrow => "Down Arrow", RightArrow => "Right Arrow", NumZero => "0", NumDelete => "Delete", } ) } } impl FromScanIndex for Key { /// Return the key that corresponds to the provided scan index, if any. fn from_scan_index(index: u8) -> Option<Self> { use Key::*; Some(match index { 0 => Escape, 1 => F1, 2 => F2, 3 => F3, 4 => F4, 5 => F5, 6 => F6, 7 => F7, 8 => F8, 9 => F9, 10 => F10, 11 => F11, 12 => F12, 13 => PrintScreen, 14 => Pause, 15 => ScrollLock, 16 => Tilde, 17 => One, 18 => Two, 19 => Three, 20 => Four, 21 => Five, 22 => Six, 23 => Seven, 24 => Eight, 25 => Nine, 26 => Zero, 27 => Dash, 28 => Equals, 29 => Backspace, 30 => Insert, 31 => Home, 32 => Tab, 33 => Q, 34 => W, 35 => E, 36 => R, 37 => T, 38 => Y, 39 => U, 40 => I, 41 => O, 42 => P, 43 => LeftBracket, 44 => RightBracket, // 45 is also associated with `Key::Backslash` 45 => ISO1, 46 => Delete, 47 => End, 48 => CapsLock, 49 => A, 50 => S, 51 => D, 52 => F, 53 => G, 54 => H, 55 => I, 56 => J, 57 => K, 58 => L, 59 => SemiColon, 60 => Return, 61 => PageUp, 62 => PageDown, 63 => UpArrow, 64 => LeftShift, 65 => Z, 66 => X, 67 => C, 68 => V, 69 => B, 70 => N, 71 => M, 72 => Comma, 73 => Period, 74 => ForwardSlash, 75 => RightShift, 76 => LeftArrow, 77 => DownArrow, 78 => RightArrow, 79 => RightControl, 80 => LeftControl, 81 => LeftMod, 82 => LeftAlt, 83 => Space, 84 => RightAlt, 85 => RightMod, 86 => Fn, 87 => ISO2, 90 => NumOne, 91 => NumTwo, 92 => NumThree, 93 => NumReturn, 94 => NumDelete, 95 => NumZero, 96 => NumSix, 97 => NumFive, 98 => NumFour, 99 => NumAddition, 100 => NumNine, 101 => NumEight, 102 => NumSeven, 103 => NumSubtract, 104 => NumMultiply, 105 => NumDivide, 106 => NumLock, 107 => A1, 108 => A2, 109 => A3, 110 => Mode, // Invalid index should only ever be one of: 88 | 89 | 111 ..= 255 _ => return None, }) } } impl IntoMatrixRowColumn for Key { /// Returns a tuple `(row, column)` that represents the matrix row and column of the key. fn into_matrix_row_and_column(&self) -> (u8, u8) { use Key::*; match self { Escape => (0, 0), F1 => (0, 2), F2 => (0, 3), F3 => (0, 4), F4 => (0, 5), F5 => (0, 6), F6 => (0, 7), F7 => (0, 8), F8 => (0, 9), F9 => (0, 10), F10 => (0, 11), F11 => (0, 12), F12 => (0, 13), PrintScreen => (0, 14), Pause => (0, 15), ScrollLock => (0, 16), A1 => (0, 17), A2 => (0, 18), A3 => (0, 19), Mode => (0, 20), Tilde => (1, 0), One => (1, 1), Two => (1, 2), Three => (1, 3), Four => (1, 4), Five => (1, 5), Six => (1, 6), Seven => (1, 7), Eight => (1, 8), Nine => (1, 9), Zero => (1, 10), Dash => (1, 11), Equals => (1, 12), Backspace => (1, 13), Insert => (1, 14), Home => (1, 15), PageUp => (1, 16), NumLock => (1, 17), NumDivide => (1, 18), NumMultiply => (1, 19), NumSubtract => (1, 20), Tab => (2, 0), Q => (2, 1), W => (2, 2), E => (2, 3), R => (2, 4), T => (2, 5), Y => (2, 6), U => (2, 7), I => (2, 8), O => (2, 9), P => (2, 10), LeftBracket => (2, 11), RightBracket => (2, 12), Backslash => (2, 13), Delete => (2, 14), End => (2, 15), PageDown => (2, 16), NumSeven => (2, 17), NumEight => (2, 18), NumNine => (2, 19), NumAddition => (2, 20), CapsLock => (3, 0), A => (3, 1), S => (3, 2), D => (3, 3), F => (3, 4), G => (3, 5), H => (3, 6), J => (3, 7), K => (3, 8), L => (3, 9), SemiColon => (3, 10), Apostrophe => (3, 11), ISO1 => (3, 12), Return => (3, 13), NumFour => (3, 17), NumFive => (3, 18), NumSix => (3, 19), LeftShift => (4, 0), ISO2 => (4, 1), Z => (4, 2), X => (4, 3), C => (4, 4), V => (4, 5), B => (4, 6), N => (4, 7), M => (4, 8), Comma => (4, 9), Period => (4, 10), ForwardSlash => (4, 11), RightShift => (4, 13), UpArrow => (4, 15), NumOne => (4, 17), NumTwo => (4, 18), NumThree => (4, 19), NumReturn => (4, 20), LeftControl => (5, 0), LeftMod => (5, 1), LeftAlt => (5, 2), Space => (5, 6), RightAlt => (5, 10), RightMod => (5, 11), Fn => (5, 12), RightControl => (5, 13), LeftArrow => (5, 14), DownArrow => (5, 15), RightArrow => (5, 16), NumZero => (5, 18), NumDelete => (5, 19), } } } /// Contains functions from Wooting's Analog SDK. #[cfg(feature = "analog")] pub mod analog { use super::{FromScanIndex, IntoMatrixRowColumn, WootingError}; use std::sync::Mutex; use lazy_static::lazy_static; use wooting_analog_sdk_sys; lazy_static! { static ref CALLBACK: Mutex<Option<Box<dyn Fn() + Send>>> = Default::default(); } /// Is there a Wooting keyboard connected? /// /// ```rust,no_run /// // Assert that a Wooting keyboard is connected.. /// assert!(wooting_sdk::analog::is_wooting_keyboard_connected()); /// ``` pub fn is_wooting_keyboard_connected() -> bool { unsafe { wooting_analog_sdk_sys::wooting_kbd_connected() } } /// This is a trampoline function that is provided to the C function to be invoked which will /// in turn invoke the user provided callback. The user provided callback would normally be /// stored in userdata but due to the lack of any, we use a static instead. extern "C" fn set_disconnected_callback_handler() { if let Some(ref mut callback) = *CALLBACK.lock().unwrap() { callback(); } else { panic!("Callback static has not been set"); } } /// Set a callback to be invoked when a keyboard is disconnected. Currently only happens on a /// failed read. /// /// See [`analog_disconnected_callback`][example] example for usage. /// /// [example]: https://github.com/davidtwco/rust-wooting-sdk/blob/master/wooting-sdk/examples/analog_set_disconnected.rs pub fn set_disconnected_callback<F: 'static + Fn() + Send>(callback: F) { *CALLBACK.lock().unwrap() = Some(Box::new(callback)); unsafe { wooting_analog_sdk_sys::wooting_set_disconnected_cb(Some( set_disconnected_callback_handler, )); } } /// Read the analog value, represented by a `u8`, of the requested key. /// /// ```rust,no_run /// # fn test() -> Result<(), wooting_sdk::WootingError> { /// use wooting_sdk::{analog::read_analog_key, Key}; /// /// let min = u8::min_value(); /// let max = u8::max_value(); /// /// // Check how far down W has been pressed.. /// match read_analog_key(Key::W)? { /// min => { /* ..not pressed. */ }, /// max => { /* ..completely pressed. */ }, /// _ => { /* ..partially pressed. */ }, /// } /// # Ok(()) /// # } /// ``` pub fn read_analog_key<K: IntoMatrixRowColumn>(key: K) -> Result<u8, WootingError> { let (row, column) = key.into_matrix_row_and_column(); let ret = unsafe { wooting_analog_sdk_sys::wooting_read_analog(row, column) }; if ret == 0 && !is_wooting_keyboard_connected() { Err(WootingError::Disconnected) } else { Ok(ret) } } /// Read the analog value, represented by a `u8`, of pressed keys, up to a maximum of /// `n` keys (maximum of sixteen). /// /// This function will return `Err(WootingError::InvalidBufferSize)` if `n` is zero or larger /// than sixteen. /// /// ```rust,no_run /// # fn test() -> Result<(), wooting_sdk::WootingError> { /// use wooting_sdk::{analog::read_analog_keys, Key}; /// /// // Read the value of two pressed keys and check if they are CTRL and A.. /// if let &[(Key::LeftControl, ctrl), (Key::A, a)] = read_analog_keys(2)?.as_slice() { /// // ..if so, check if they are at least half pressed.. /// let is_half_pressed = |v: u8| v >= u8::max_value() / 2; /// if is_half_pressed(ctrl) && is_half_pressed(a) { /// // ..and if they are, select all. /// select_all(); /// } /// } /// # Ok(()) /// # } /// # fn select_all() {} /// ``` pub fn read_analog_keys<K: FromScanIndex>(n: u8) -> Result<Vec<(K, u8)>, WootingError> { if n == 0 || n > 16 { return Err(WootingError::InvalidBufferSize); } let buffer_length = n as usize * 2; let mut buffer: Vec<u8> = vec![0; buffer_length]; let ret: i32 = unsafe { wooting_analog_sdk_sys::wooting_read_full_buffer( buffer.as_mut_ptr(), buffer_length as u32, ) }; if ret == -1 { Err(WootingError::Disconnected) } else if ret < -1 { panic!("Invalid return code from Wooting Analog SDK"); } else { Ok(buffer .chunks(2) .take(ret as usize) .filter_map(|chunk| match chunk { &[scan_index, analog_value] => { K::from_scan_index(scan_index).map(|key| (key, analog_value)) } _ => unreachable!(), }) .collect()) } } } /// Contains functions from Wooting's RGB SDK. #[cfg(feature = "rgb")] pub mod rgb { use super::IntoMatrixRowColumn; use std::sync::Mutex; use lazy_static::lazy_static; use wooting_rgb_sdk_sys; /// How many columns are there? const COLUMNS: usize = 21; /// How many rows are there? const ROWS: usize = 6; /// How many components are there in a color? const COMPONENTS: usize = 3; lazy_static! { static ref CALLBACK: Mutex<Option<Box<dyn Fn() + Send>>> = Default::default(); } /// Is there a Wooting keyboard connected? /// /// ```rust,no_run /// // Assert that a Wooting keyboard is connected.. /// assert!(wooting_sdk::rgb::is_wooting_keyboard_connected()); /// ``` pub fn is_wooting_keyboard_connected() -> bool { unsafe { wooting_rgb_sdk_sys::wooting_rgb_kbd_connected() } } /// This is a trampoline function that is provided to the C function to be invoked which will /// in turn invoke the user provided callback. The user provided callback would normally be /// stored in userdata but due to the lack of any, we use a static instead. extern "C" fn set_disconnected_callback_handler() { if let Some(ref mut callback) = *CALLBACK.lock().unwrap() { callback(); } else { panic!("Callback static has not been set"); } } /// Set a callback to be invoked when a keyboard is disconnected. Currently only happens on a /// failed read. /// /// See [`rgb_disconnected_callback`][example] example for usage. /// /// [example]: https://github.com/davidtwco/rust-wooting-sdk/blob/master/wooting-sdk/examples/rgb_set_disconnected.rs pub fn set_disconnected_callback<F: 'static + Fn() + Send>(callback: F) { *CALLBACK.lock().unwrap() = Some(Box::new(callback)); unsafe { wooting_rgb_sdk_sys::wooting_rgb_set_disconnected_cb(Some( set_disconnected_callback_handler, )); } } /// Represents the connected keyboard to perform RGB operations. This struct is empty and /// only exists to enforce that `reset` is called on drop. #[derive(Clone, Debug, Default)] pub struct RgbKeyboard; impl RgbKeyboard { /// Set the color of a single key. This will not influence the keyboard color array. Use /// this function for simple amplifications, like a notification. Use the array functions /// if you want to change the entire keyboard. Returns `true` if the color is set. /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Set the A key to white... /// keyboard.direct_set_key(Key::A, 255, 255, 255); /// ``` pub fn direct_set_key<K: IntoMatrixRowColumn>( &mut self, key: K, red: u8, green: u8, blue: u8, ) -> bool { let (row, column) = key.into_matrix_row_and_column(); unsafe { wooting_rgb_sdk_sys::wooting_rgb_direct_set_key(row, column, red, green, blue) } } /// Directly reset the color of a single key on the keyboard. This will not influence the /// keyboard color array. Use this function for simple amplifications, like a notification. /// Use the array functions if you want to change the entire keyboard. Returns `true` if /// the color is reset. /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Set the A key to white... /// keyboard.direct_set_key(Key::A, 255, 255, 255); /// // ..and then reset it back! /// keyboard.direct_reset_key(Key::A); /// ``` pub fn direct_reset_key<K: IntoMatrixRowColumn>(&mut self, key: K) -> bool { let (row, column) = key.into_matrix_row_and_column(); unsafe { wooting_rgb_sdk_sys::wooting_rgb_direct_reset_key(row, column) } } /// Apply any updates made by the `array_set_single` and `array_set_full` functions. /// Returns `true` if the colors are updated. /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Modify keyboard array so A will be set to white.. /// keyboard.array_set_single(Key::A, 255, 255, 255); /// // ..and apply the change. /// keyboard.array_update(); /// ``` pub fn array_update(&mut self) -> bool { unsafe { wooting_rgb_sdk_sys::wooting_rgb_array_update_keyboard() } } /// Set an auto-update trigger after every change with the `array_set_single` and /// `array_set_full` functions. By default, no auto-update trigger is set. /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Make keyboard array changes apply automatically.. /// keyboard.array_auto_update(true); /// // ..and then modify the array so QWERTY are set to white... /// // ..with no need for a call to `array_update`! /// keyboard.array_set_full(&[ /// (Key::Q, (255, 255, 255)), /// (Key::W, (255, 255, 255)), /// (Key::E, (255, 255, 255)), /// (Key::R, (255, 255, 255)), /// (Key::T, (255, 255, 255)), /// (Key::Y, (255, 255, 255)), /// ]); /// ``` pub fn array_auto_update(&mut self, auto_update: bool) { unsafe { wooting_rgb_sdk_sys::wooting_rgb_array_auto_update(auto_update) } } /// Set a single color in the color array. This will not directly update the keyboard /// unless the auto update flag is set (see `array_auto_update`), so it can be called /// frequently (i.e. in a loop that updates the entire keyboard). Returns `true` if the /// colors have changed. /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Modify the keyboard array so QWERTY will be set to white.. /// keyboard.array_set_single(Key::Q, 255, 255, 255); /// keyboard.array_set_single(Key::W, 255, 255, 255); /// keyboard.array_set_single(Key::E, 255, 255, 255); /// keyboard.array_set_single(Key::R, 255, 255, 255); /// keyboard.array_set_single(Key::T, 255, 255, 255); /// keyboard.array_set_single(Key::Y, 255, 255, 255); /// // ..and apply the change. /// keyboard.array_update(); /// ``` pub fn array_set_single<K: IntoMatrixRowColumn>( &mut self, key: K, red: u8, green: u8, blue: u8, ) -> bool { let (row, column) = key.into_matrix_row_and_column(); unsafe { wooting_rgb_sdk_sys::wooting_rgb_array_set_single(row, column, red, green, blue) } } /// Set a complete color array. This will not directly update the keyboard unless the auto /// update flag is set (see `array_auto_update`). Returns `true` if the colors have /// changed. /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Modify the keyboard array so QWERTY will be set to white.. /// keyboard.array_set_full(&[ /// (Key::Q, (255, 255, 255)), /// (Key::W, (255, 255, 255)), /// (Key::E, (255, 255, 255)), /// (Key::R, (255, 255, 255)), /// (Key::T, (255, 255, 255)), /// (Key::Y, (255, 255, 255)), /// ]); /// // ..and apply the change. /// keyboard.array_update(); /// ``` pub fn array_set_full<K: IntoMatrixRowColumn>( &mut self, array: &[(K, (u8, u8, u8))], ) -> bool { let mut flattened: [u8; COMPONENTS * COLUMNS * ROWS] = [0; COMPONENTS * COLUMNS * ROWS]; for (key, (red, green, blue)) in array { let (row, column) = key.into_matrix_row_and_column(); let index: usize = (row as usize) * (COLUMNS * COMPONENTS) + (column as usize) * COMPONENTS; flattened[index] = *red; flattened[index + 1] = *green; flattened[index + 2] = *blue; } unsafe { wooting_rgb_sdk_sys::wooting_rgb_array_set_full(flattened.as_ptr()) } } /// Restore all colors to those that were originally on the keyboard. Must be called when /// application is closed (this will be invoked when this struct is dropped). /// /// ```rust,no_run /// use wooting_sdk::{rgb::RgbKeyboard, Key}; /// /// let mut keyboard = RgbKeyboard::default(); /// // Set ABC to white.. /// keyboard.direct_set_key(Key::A, 255, 255, 255); /// keyboard.direct_set_key(Key::B, 255, 255, 255); /// keyboard.direct_set_key(Key::C, 255, 255, 255); /// // ..and then reset the entire keyboard back to how it was previously. /// keyboard.reset_all(); /// ``` pub fn reset_all(&mut self) -> bool { unsafe { wooting_rgb_sdk_sys::wooting_rgb_reset() } } } impl Drop for RgbKeyboard { fn drop(&mut self) { // By restricting all rgb functions to get performed on a struct then we can ensure // that there is something to be dropped and therefore force a reset. let _ = self.reset_all(); // Also, make sure that the auto update has been reset. self.array_auto_update(false); } } }