#!/usr/bin/env python3 import subprocess import logging # Set up logging to see the output logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logger = logging.getLogger(__name__) def _get_best_monitor_selection(count): """Get the best monitor selection based on layout""" try: result = subprocess.run(['xfreerdp', '/monitor-list'], capture_output=True, text=True, timeout=5) if result.returncode == 0: lines = result.stdout.strip().split('\n') monitors = [] primary_monitor = None for line in lines: # Clean up the line and split by whitespace/tabs cleaned_line = line.strip() is_primary = cleaned_line.startswith('*') cleaned_line = cleaned_line.replace('*', '').strip() if '[' in cleaned_line and ']' in cleaned_line and 'x' in cleaned_line and '+' in cleaned_line: # Split by whitespace/tabs parts = cleaned_line.split() if len(parts) >= 3: id_part = parts[0] # [0] pos_part = parts[2] # +3840+0 if '[' in id_part and ']' in id_part: monitor_id = int(id_part.strip('[]')) x_pos = int(pos_part.split('+')[1]) monitor_info = (monitor_id, x_pos, is_primary) monitors.append(monitor_info) if is_primary: primary_monitor = monitor_id # Sort monitors by X position (left to right) monitors.sort(key=lambda x: x[1]) # For debugging, log the monitor layout logger.info(f"Monitor layout detected: {[(m[0], m[1], 'primary' if m[2] else 'secondary') for m in monitors]}") # Return the leftmost monitors (excluding position and primary flag) selected = [str(m[0]) for m in monitors[:count]] selected_str = ','.join(selected) logger.info(f"Selected {count} monitors: {selected_str}") return selected_str except Exception as e: logger.error(f"Error detecting monitors: {e}") # Fallback: simple sequential selection fallback = ','.join([str(i) for i in range(count)]) logger.warning(f"Using fallback monitor selection: {fallback}") return fallback if __name__ == "__main__": print("=== Testing Enhanced Monitor Selection Logic ===") print() for count in [2, 3]: print(f"Testing {count} monitors:") result = _get_best_monitor_selection(count) print(f"Result: {result}") print()