@@ -15,7 +15,7 @@ use mio::net::UdpSocket;
1515use mio_signals:: { Signal , Signals , SignalSet } ;
1616use udp_connections:: { MAX_PACKET_SIZE , Server , ServerEvent , Transport } ;
1717use vec_map:: VecMap ;
18- use inputshare_common:: { HidButtonCode , HidKeyCode , IDENTIFIER } ;
18+ use inputshare_common:: { ConsumerDeviceCode , HidButtonCode , HidKeyCode , IDENTIFIER } ;
1919use crate :: receiver:: { InputEvent , InputReceiver } ;
2020
2121/// The server for inputshare
@@ -109,8 +109,6 @@ fn server(args: Args) -> Result<()> {
109109 }
110110 }
111111
112- consumer_device. press_key ( 1 << 5 ) ?;
113- consumer_device. release_key ( 1 << 5 ) ?;
114112 socket. update ( ) ;
115113 loop {
116114 match socket. next_event ( & mut buffer) {
@@ -138,6 +136,8 @@ fn server(args: Args) -> Result<()> {
138136 InputEvent :: KeyRelease ( key) => keyboard. release_key ( key) ?,
139137 InputEvent :: MouseButtonPress ( button) => mouse. press_button ( button) ?,
140138 InputEvent :: MouseButtonRelease ( button) => mouse. release_button ( button) ?,
139+ InputEvent :: ConsumerDevicePress ( button) => consumer_device. press_key ( button) ?,
140+ InputEvent :: ConsumerDeviceRelease ( button) => consumer_device. release_key ( button) ?,
141141 InputEvent :: HorizontalScrolling ( amount) => mouse. scroll_horizontal ( amount) ?,
142142 InputEvent :: VerticalScrolling ( amount) => mouse. scroll_vertical ( amount) ?,
143143 InputEvent :: Reset => {
@@ -260,7 +260,7 @@ impl Keyboard {
260260#[ derive( Debug ) ]
261261pub struct ConsumerDevice {
262262 device : File ,
263- pressed_keys : u16 ,
263+ pressed_keys : ConsumerDeviceButtons ,
264264}
265265
266266impl ConsumerDevice {
@@ -269,27 +269,37 @@ impl ConsumerDevice {
269269 let device = OpenOptions :: new ( ) . write ( true ) . append ( true ) . open ( "/dev/hidg2" ) ?;
270270 Ok ( Self {
271271 device,
272- pressed_keys : 0
272+ pressed_keys : ConsumerDeviceButtons :: empty ( )
273273 } )
274274 }
275275
276276 fn send_report ( & mut self ) -> std:: io:: Result < ( ) > {
277- self . device . write_all ( & self . pressed_keys . to_le_bytes ( ) )
277+ self . device . write_all ( & self . pressed_keys . bits ( ) . to_le_bytes ( ) )
278278 }
279279
280280 pub fn reset ( & mut self ) -> std:: io:: Result < ( ) > {
281- self . pressed_keys = 0 ;
281+ self . pressed_keys = ConsumerDeviceButtons :: empty ( ) ;
282282 self . send_report ( )
283283 }
284284
285- pub fn press_key ( & mut self , key : u16 ) -> std:: io:: Result < ( ) > {
286- self . pressed_keys |= key;
287- self . send_report ( )
285+ pub fn press_key ( & mut self , key : ConsumerDeviceCode ) -> std:: io:: Result < ( ) > {
286+ match key. try_into ( ) {
287+ Ok ( key) => {
288+ self . pressed_keys . insert ( key) ;
289+ self . send_report ( )
290+ } ,
291+ Err ( ( ) ) => Ok ( ( ) )
292+ }
288293 }
289294
290- pub fn release_key ( & mut self , key : u16 ) -> std:: io:: Result < ( ) > {
291- self . pressed_keys &= !key;
292- self . send_report ( )
295+ pub fn release_key ( & mut self , key : ConsumerDeviceCode ) -> std:: io:: Result < ( ) > {
296+ match key. try_into ( ) {
297+ Ok ( key) => {
298+ self . pressed_keys . remove ( key) ;
299+ self . send_report ( )
300+ } ,
301+ Err ( ( ) ) => Ok ( ( ) )
302+ }
293303 }
294304
295305}
@@ -388,7 +398,7 @@ fn abs_min(a: i16, b: i16) -> i16 {
388398 }
389399}
390400
391- pub use flags:: { HidMouseButtons , HidModifierKeys } ;
401+ pub use flags:: { HidMouseButtons , HidModifierKeys , ConsumerDeviceButtons } ;
392402
393403#[ allow( non_upper_case_globals) ]
394404pub mod flags {
@@ -412,6 +422,51 @@ pub mod flags {
412422 const Button4 = 0x08 ;
413423 const Button5 = 0x10 ;
414424 }
425+
426+ pub struct ConsumerDeviceButtons : u16 {
427+ const NextTrack = 0x0001 ;
428+ const PreviousTrack = 0x0002 ;
429+ const Stop = 0x0004 ;
430+ const PlayPause = 0x0008 ;
431+ const Mute = 0x0010 ;
432+ const VolumeUp = 0x0020 ;
433+ const VolumeDown = 0x0040 ;
434+ const BrowserHome = 0x0080 ;
435+ const MyComputer = 0x0100 ;
436+ const Calculator = 0x0200 ;
437+ const BrowserFavorites = 0x0400 ;
438+ const BrowserSearch = 0x0800 ;
439+ const BrowserStop = 0x1000 ;
440+ const BrowserBack = 0x2000 ;
441+ const MediaSelect = 0x4000 ;
442+ const Mail = 0x8000 ;
443+ }
444+ }
445+ }
446+
447+ impl TryFrom < ConsumerDeviceCode > for ConsumerDeviceButtons {
448+ type Error = ( ) ;
449+
450+ fn try_from ( value : ConsumerDeviceCode ) -> std:: result:: Result < Self , Self :: Error > {
451+ match value {
452+ ConsumerDeviceCode :: NextTrack => Ok ( ConsumerDeviceButtons :: NextTrack ) ,
453+ ConsumerDeviceCode :: PreviousTrack => Ok ( ConsumerDeviceButtons :: PreviousTrack ) ,
454+ ConsumerDeviceCode :: Stop => Ok ( ConsumerDeviceButtons :: Stop ) ,
455+ ConsumerDeviceCode :: PlayPause => Ok ( ConsumerDeviceButtons :: PlayPause ) ,
456+ ConsumerDeviceCode :: Mute => Ok ( ConsumerDeviceButtons :: Mute ) ,
457+ ConsumerDeviceCode :: VolumeUp => Ok ( ConsumerDeviceButtons :: VolumeUp ) ,
458+ ConsumerDeviceCode :: VolumeDown => Ok ( ConsumerDeviceButtons :: VolumeDown ) ,
459+ ConsumerDeviceCode :: MediaSelect => Ok ( ConsumerDeviceButtons :: MediaSelect ) ,
460+ ConsumerDeviceCode :: Mail => Ok ( ConsumerDeviceButtons :: Mail ) ,
461+ ConsumerDeviceCode :: Calculator => Ok ( ConsumerDeviceButtons :: Calculator ) ,
462+ ConsumerDeviceCode :: MyComputer => Ok ( ConsumerDeviceButtons :: MyComputer ) ,
463+ ConsumerDeviceCode :: BrowserSearch => Ok ( ConsumerDeviceButtons :: BrowserSearch ) ,
464+ ConsumerDeviceCode :: BrowserHome => Ok ( ConsumerDeviceButtons :: BrowserHome ) ,
465+ ConsumerDeviceCode :: BrowserBack => Ok ( ConsumerDeviceButtons :: BrowserBack ) ,
466+ ConsumerDeviceCode :: BrowserStop => Ok ( ConsumerDeviceButtons :: BrowserStop ) ,
467+ ConsumerDeviceCode :: BrowserFavorites => Ok ( ConsumerDeviceButtons :: BrowserFavorites ) ,
468+ _ => Err ( ( ) )
469+ }
415470 }
416471}
417472
0 commit comments