onInputSourcesChange method

void onInputSourcesChange(
  1. Event event
)

Implementation

void onInputSourcesChange(Event event ) {
  // Notify disconnected
  for ( int i = 0; i < event.removed.length; i ++ ) {
    final inputSource = event.removed[ i ];
    final index = controllerInputSources.indexOf( inputSource );

    if ( index >= 0 ) {
      controllerInputSources[ index ] = null;
      controllers[ index ]?.disconnect( inputSource );
    }
  }

  // Notify connected
  for ( int i = 0; i < event.added.length; i ++ ) {
    final inputSource = event.added[ i ];
    int controllerIndex = controllerInputSources.indexOf( inputSource );

    if ( controllerIndex == - 1 ) {
      // Assign input source a controller that currently has no input source
      for ( int i = 0; i < controllers.length; i ++ ) {
        if ( i >= controllerInputSources.length ) {
          controllerInputSources.add( inputSource );
          controllerIndex = i;
          break;
        }
        else if ( controllerInputSources[ i ] == null ) {
          controllerInputSources[ i ] = inputSource;
          controllerIndex = i;
          break;
        }
      }

      // If all controllers do currently receive input we ignore new ones
      if ( controllerIndex == - 1 ) break;
    }

    final controller = controllers[ controllerIndex ];
    controller?.connect( inputSource );
  }
}