Skip to content

Add Linux Support #22

@NakaokaRei

Description

@NakaokaRei

Overview

Add support for Linux platforms to SwiftAutoGUI, extending the library's cross-platform capabilities beyond macOS.

Motivation

Linux is a major platform for developers and automation tools. Supporting Linux would significantly expand SwiftAutoGUI's user base and make it a truly cross-platform automation library.

Technical Requirements

1. Platform APIs

  • Replace CoreGraphics with Linux-compatible alternatives
  • Investigate X11/Wayland APIs for:
    • Mouse control (XWarpPointer, XTestFakeMotionEvent)
    • Keyboard control (XTestFakeKeyEvent)
    • Screen capture (XGetImage, XShmGetImage)

2. Dependencies

  • Swift on Linux compatibility
  • OpenCV for Linux (already cross-platform)
  • X11 development libraries (libx11-dev, libxtst-dev)
  • Potential Wayland support for modern systems

3. Implementation Tasks

Mouse Control

  • Implement mouse movement using X11 APIs
  • Implement click events (left, right, middle)
  • Implement drag operations
  • Implement scroll events

Keyboard Control

  • Map Swift Key enum to X11 keycodes
  • Implement key press/release events
  • Support for keyboard shortcuts
  • Handle different keyboard layouts

Screen Capture

  • Implement screenshot functionality
  • Support for capturing specific regions
  • Handle multiple monitors
  • Pixel color detection

Image Recognition

  • Ensure OpenCV integration works on Linux
  • Test template matching performance
  • Verify coordinate system compatibility

4. Build System

  • Update Package.swift for Linux targets
  • Add conditional compilation directives
  • Handle platform-specific dependencies
  • CI/CD pipeline for Linux testing

5. Testing

  • Port existing tests to Linux
  • Add Linux-specific test cases
  • Test on various Linux distributions (Ubuntu, Fedora, etc.)
  • Verify Wayland vs X11 compatibility

Example Implementation Structure

#if os(Linux)
import X11
import Glibc

extension SwiftAutoGUI {
    public static func moveMouse(dx: Int32, dy: Int32) {
        guard let display = XOpenDisplay(nil) else { return }
        defer { XCloseDisplay(display) }
        
        XWarpPointer(display, 0, 0, 0, 0, 0, 0, dx, dy)
        XFlush(display)
    }
}
#endif

Challenges

  1. Window System Diversity: Supporting both X11 and Wayland
  2. Permissions: Linux security models may require different approaches
  3. Distribution Differences: Various Linux distros handle displays differently
  4. Swift on Linux: Limited ecosystem compared to macOS

Success Criteria

  • All core SwiftAutoGUI functions work on Linux
  • Tests pass on Ubuntu LTS and at least one other major distribution
  • Documentation includes Linux-specific setup instructions
  • Performance is comparable to macOS implementation

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions