final JComboBox ggComboAdd, ggComboActivate, ggComboRemove;
final int result;
JScrollPane ggScroll;
// JSeparator ggSep;
int row, action;
AudioBoxConfig cfg;
final List collDetected = Collections.synchronizedList( new ArrayList() );
final List collAdd = new ArrayList();
final List collActivate = new ArrayList();
final List collRemove = new ArrayList();
// this doesn't show up obivously, so we add it manually...
collDetected.add( new AudioBoxConfig( AudioBoxConfig.ID_DEFAULT, "Default", 8, 8, true ));
scsynthPath = audioPrefs.get( PrefsUtil.KEY_SUPERCOLLIDERAPP, null );
if( scsynthPath == null ) {
System.err.println( getResourceString( "errSCSynthAppNotFound" ));
return;
}
pane = new JPanel( new BorderLayout() );
//pane.setBackground( Color.white );
ggSpinner = new IndeterminateSpinner( 24 );
b = Box.createHorizontalBox();
// pane.gridAdd( ggSpinner, 0, 0 );
b.add( ggSpinner );
b.add( Box.createHorizontalStrut( 4 ));
ggLabel = new JLabel( "Searching for Audio Devices..." );
// ggLabel.setBorder( BorderFactory.createEmptyBorder( 0, 4, 0, 0 ));
// pane.gridAdd( ggLabel, 1, 0 );
b.add( ggLabel );
pane.add( b, BorderLayout.NORTH );
pane2 = new SpringPanel( 0, 0, 4, 2 );
row = 0;
ggListAdd = new JList();
ggScroll = new JScrollPane( ggListAdd );
pane2.gridAdd( new JLabel( "Detected new devices:" ), 0, row++, 2, 1 );
pane2.gridAdd( ggScroll, 0, row++, 2, 1 );
pane2.gridAdd( new JLabel( "Action:", RIGHT ), 0, row );
ggComboAdd = new JComboBox( new Object[] { "Ignore", "Add to list" });
pane2.gridAdd( ggComboAdd, 1, row++ );
pane2.gridAdd( createVSep(), 0, row++, 2, 1 );
ggListActivate = new JList();
ggScroll = new JScrollPane( ggListActivate );
pane2.gridAdd( new JLabel( "Detected deactivated devices:" ), 0, row++, 2, 1 );
pane2.gridAdd( ggScroll, 0, row++, 2, 1 );
pane2.gridAdd( new JLabel( "Action:", RIGHT ), 0, row );
ggComboActivate = new JComboBox( new Object[] { "Ignore", "Make active" });
pane2.gridAdd( ggComboActivate, 1, row++ );
pane2.gridAdd( createVSep(), 0, row++, 2, 1 );
// ggSep = new JSeparator();
// ggSep.setBorder( BorderFactory.createEmptyBorder( 8, 0, 8, 0 ));
// pane2.gridAdd( ggSep, 0, row++, 2, 1 );
ggListRemove = new JList();
ggScroll = new JScrollPane( ggListRemove );
pane2.gridAdd( new JLabel( "Devices not found:" ), 0, row++, 2, 1 );
pane2.gridAdd( ggScroll, 0, row++, 2, 1 );
pane2.gridAdd( new JLabel( "Action:", RIGHT ), 0, row );
ggComboRemove = new JComboBox( new Object[] { "Ignore", "Make inactive", "Remove from list" });
pane2.gridAdd( ggComboRemove, 1, row++ );
pane2.setVisible( false );
pane.add( pane2, BorderLayout.CENTER );
AbstractWindowHandler.setDeepFont( pane );
// op = new JOptionPane( pane, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION,
// new ImageIcon( GUIUtil.class.getResource( "assistent_64x64.png" )));
// dlg = op.createDialog( getWindow(), getResourceString( "prefsAudioDevicesAssistent" ) );
runResult = new Runnable() {
public void run()
{
// String name;
AudioBoxConfig cfg3, cfg2;
for( Iterator iter = collDetected.iterator(); iter.hasNext(); ) {
cfg2 = (AudioBoxConfig) iter.next();
if( !setAudioBoxNames.contains( cfg2.name )) {
collAdd.add( cfg2 );
} else {
cfg3 = boxForName( cfg2.name );
if( (cfg3 != null) && !cfg3.active ) collActivate.add( cfg3 );
}
}
iterRemove: for( Iterator iter = collAudioBoxConfigs.iterator(); iter.hasNext(); ) {
cfg3 = (AudioBoxConfig) iter.next();
for( Iterator iter2 = collDetected.iterator(); iter2.hasNext(); ) {
cfg2 = (AudioBoxConfig) iter2.next();
if( cfg2.name.equals( cfg3.name )) continue iterRemove;
}
collRemove.add( cfg3 );
}
Collections.sort( collAdd );
Collections.sort( collActivate );
Collections.sort( collRemove );
ggListAdd.setListData( collAdd.toArray() );
ggListAdd.setVisibleRowCount( Math.max( 1, Math.min( collAdd.size(), 6 )));
if( collAdd.isEmpty() ) {
ggComboAdd.setEnabled( false );
} else {
ggComboAdd.setSelectedIndex( 1 );
}
ggListActivate.setListData( collActivate.toArray() );
ggListActivate.setVisibleRowCount( Math.max( 1, Math.min( collActivate.size(), 6 )));
ggListAdd.setVisibleRowCount( Math.max( 1, Math.min( collAdd.size(), 6 )));
if( collActivate.isEmpty() ) {
ggComboActivate.setEnabled( false );
} else {
ggComboActivate.setSelectedIndex( 1 );
}
ggListRemove.setListData( collRemove.toArray() );
ggListRemove.setVisibleRowCount( Math.max( 1, Math.min( collRemove.size(), 6 )));
if( collRemove.isEmpty() ) {
ggComboRemove.setEnabled( false );
} else {
ggComboRemove.setSelectedIndex( 1 );
}
pane2.makeCompactGrid();
b.setVisible( false );
pane2.setVisible( true );
// pane.makeCompactGrid();
// dlg.pack();
((JDialog) SwingUtilities.getAncestorOfClass( JDialog.class, pane2 )).pack();
}
};
testThread = new Thread( new Runnable() {
public void run()
{
final InputStream inStream; // , errStream;
final byte[] inBuf = new byte[128];
// final byte[] errBuf = new byte[128];
final File cwd = new File( scsynthPath ).getParentFile();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Pattern ptrn1 = Pattern.compile( "Number of Devices: \\d+\\n( \\d+ : \".+\" \\(\\d+ ins, \\d+ outs\\)\\n)+\\n" );
// final Pattern ptrn2 = Pattern.compile( "\".+\"" );
final Pattern ptrn2 = Pattern.compile( "\"(.+)\" \\((\\d+) ins, (\\d+) outs()" ); // the trailing () makes the numOuts not disappear (why?!)
final Matcher m1 = ptrn1.matcher( "" );
Matcher m2;
String name;
Process p = null;
int len, ins, outs; // , resultCode = -1;
boolean pRunning = true;
try {
p = Runtime.getRuntime().exec( new String[] { scsynthPath, "-u", "0" }, null, cwd );
// "Implementation note: It is a good idea for the input stream to be buffered."
inStream = new BufferedInputStream( p.getInputStream() );
// errStream = new BufferedInputStream( p.getErrorStream() );
while( keepThreadRunning.isSet() && pRunning ) {
try {
Thread.sleep( 250 ); // a kind of cheesy way to wait for the program to end
}
catch( InterruptedException e5 ) { /* ignore */ }
if( inStream.available() > 0 ) {
try {
while( inStream.available() > 0 ) {
len = Math.min( inBuf.length, inStream.available() );
inStream.read( inBuf, 0, len );
baos.write( inBuf, 0, len );
}
}
catch( IOException e1 ) { /* ignore for now XXX */ }
m1.reset( baos.toString() );
if( m1.find() ) {
pRunning = false;
// keepThreadRunning.set( false );
m2 = ptrn2.matcher( m1.group() );
while( m2.find() ) {
// s = m2.group();
// System.out.println( "'" + s.substring( 1, s.length() - 1 ) + "'" );
// collDetected.add( s.substring( 1, s.length() - 1 ));
if( m2.groupCount() == 4 ) {
name = m2.group( 1 );
ins = Integer.parseInt( m2.group( 2 ));
outs = Integer.parseInt( m2.group( 3 ));
if( (ins > 0) && (outs > 0) ) { // filter out single direction devices (like mac os x built-in mic and output)
collDetected.add( new AudioBoxConfig(
null, name, ins, outs, true ));
}
}
}
}