showpid 1.0.9

Cross-platform CLI utility to bring a window to foreground by PID (Windows, Linux, macOS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# showpid

[![Crates.io](https://img.shields.io/crates/v/showpid.svg)](https://crates.io/crates/showpid)
[![Documentation](https://docs.rs/showpid/badge.svg)](https://docs.rs/showpid)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://github.com/cumulus13/showpid/workflows/CI/badge.svg)](https://github.com/cumulus13/showpid/actions)
[![Platform Support](https://img.shields.io/badge/platform-Windows%20%7C%20Linux%20%7C%20macOS-blue)]()

Bring a window of a specified process ID (PID) to the foreground. Cross-platform support for Windows, Linux, and macOS.

## Features

- ๐Ÿš€ **Fast & Lightweight** - Pure Rust implementation with minimal dependencies
- ๐ŸŒ **Cross-Platform** - Supports Windows, Linux (X11), and macOS
- ๐Ÿ”„ **Retry Logic** - Configurable retry mechanism for transient failures
- ๐ŸชŸ **Multiple Windows** - Handles processes with multiple windows
- ๐Ÿ”ง **Platform-Native** - Uses native APIs for optimal performance on each platform
- ๐Ÿ“Š **Verbose Mode** - Detailed logging for troubleshooting
- ๐Ÿ›ก๏ธ **Error Handling** - Comprehensive error handling with meaningful messages
- โšก **Zero-Cost Abstractions** - Safe Rust wrapper over platform-specific APIs
- ๐Ÿ“ฆ **Standalone Binary** - Single executable, no runtime dependencies required
- ๐Ÿงช **Well Tested** - Unit tests and cross-platform CI testing

## Platform Support

| Platform | API Used | Minimum Version | Notes |
|----------|----------|-----------------|--------|
| Windows  | Windows API (Win32) | Windows 10+ | Native Win32 API through `windows-rs` |
| Linux    | X11 (Xlib) | X11 compatible | Requires `libx11-dev` on some distributions |
| macOS    | CoreGraphics + AppleScript | macOS 10.12+ | Uses `osascript` for reliable window activation |

## Installation

### From Crates.io

```bash
cargo install showpid
```

### From Source

```bash
git clone https://github.com/cumulus13/showpid.git
cd showpid

# Linux: Install dependencies first
sudo apt-get install libx11-dev  # Ubuntu/Debian
# or
sudo dnf install libX11-devel     # Fedora/RHEL

# Build and install
cargo install --path .
```

### Pre-built Binaries

Download the latest release for your platform from the [Releases](https://github.com/cumulus13/showpid/releases) page:

- **Windows**: `showpid-windows-x86_64.zip`
- **Linux**: `showpid-linux-x86_64.tar.gz`
- **macOS**: `showpid-macos-x86_64.tar.gz`

### Platform-Specific Requirements

#### Linux
```bash
# Ubuntu/Debian
sudo apt-get install libx11-dev

# Fedora/RHEL
sudo dnf install libX11-devel

# Arch Linux
sudo pacman -S libx11
```

#### macOS
No additional dependencies required. Uses built-in CoreGraphics and AppleScript frameworks.

#### Windows
No additional dependencies required. Uses built-in Windows API.

## Usage

### Basic Usage

```bash
# Bring window of PID 1234 to foreground
showpid 1234

# Verbose mode with detailed output
showpid -v 1234
showpid --verbose 1234

# Show help
showpid -h
showpid --help
```

### Command Line Options

```
Usage: showpid [OPTIONS] <PID>

Brings the window of the specified PID to the foreground.

Arguments:
  <PID>         Process ID to bring to foreground

Options:
  -v, --verbose  Enable verbose output
  -h, --help     Show this help message
  -V, --version  Print version information
```

### Platform-Specific Examples

#### Windows
```bash
# Find notepad PID
tasklist | findstr notepad.exe

# Bring to foreground
showpid 1234

# With verbose output
showpid -v 1234
```

#### Linux
```bash
# Find Firefox PID
pgrep firefox

# Bring to foreground
showpid 1234

# With verbose output
showpid -v 1234
```

#### macOS
```bash
# Find Safari PID
pgrep Safari

# Bring to foreground
showpid 1234

# With verbose output
showpid -v 1234
```

### Using as a Library

```rust
use showpid::{Config, WindowActivator};

fn main() {
    let config = Config::new(1234)
        .with_verbose(true);
    
    let mut activator = WindowActivator::new(config);
    
    match activator.execute() {
        Ok(()) => println!("Window activated successfully"),
        Err(e) => eprintln!("Error: {}", e),
    }
}
```

## How It Works

### Windows
1. Enumerates all visible windows using `EnumWindows`
2. Filters windows by process ID using `GetWindowThreadProcessId`
3. Restores window if minimized with `ShowWindow`
4. Attaches thread input for reliable foreground activation
5. Sets window as foreground with `SetForegroundWindow`

### Linux (X11)
1. Connects to X display and queries all windows
2. Reads `_NET_WM_PID` property to match process IDs
3. Raises window with `XRaiseWindow`
4. Sets input focus with `XSetInputFocus`
5. Maps window if unmapped with `XMapWindow`

### macOS
1. Uses `CGWindowListCopyWindowInfo` to enumerate windows
2. Filters by owner PID from window properties
3. Uses AppleScript to bring process to foreground
4. Handles permissions and accessibility requirements

## Exit Codes

- `0` - Success: Window brought to foreground
- `1` - Error: No window found, operation failed, or invalid input

## Environment Variables

- `RUST_LOG` - Set logging level (e.g., `RUST_LOG=debug showpid 1234`)
  - Levels: `error`, `warn`, `info`, `debug`, `trace`

## Troubleshooting

### Linux
- **Error: "Failed to open X display"**: Make sure you're running in an X11 session (not Wayland)
- **No windows found**: Ensure the process has visible windows and you have proper permissions

### macOS
- **Permission denied**: Grant accessibility permissions in System Preferences > Security & Privacy > Privacy > Accessibility
- **osascript errors**: Ensure you have AppleScript permissions enabled

### Windows
- **Access denied**: Run as administrator if targeting system processes
- **No windows found**: Some processes may not have visible windows

## Development

### Prerequisites

- Rust 1.70 or later
- Platform-specific dependencies (see Installation section)

### Building

```bash
# Debug build
cargo build

# Release build (optimized)
cargo build --release

# Run tests
cargo test

# Run clippy lints
cargo clippy -- -D warnings

# Format code
cargo fmt --all -- --check
```

### Cross-Compilation

```bash
# Cross-compile for Windows from Linux
rustup target add x86_64-pc-windows-gnu
cargo build --release --target x86_64-pc-windows-gnu

# Cross-compile for macOS from Linux
rustup target add x86_64-apple-darwin
cargo build --release --target x86_64-apple-darwin
```

### Project Structure

```
showpid/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ main.rs          # Main application with platform modules
โ”‚   โ”œโ”€โ”€ platform/
โ”‚   โ”‚   โ”œโ”€โ”€ windows.rs   # Windows-specific implementation
โ”‚   โ”‚   โ”œโ”€โ”€ linux.rs     # Linux/X11-specific implementation
โ”‚   โ”‚   โ””โ”€โ”€ macos.rs     # macOS-specific implementation
โ”œโ”€โ”€ Cargo.toml           # Package configuration
โ”œโ”€โ”€ Cargo.lock           # Dependency lock file
โ”œโ”€โ”€ README.md            # Project documentation
โ”œโ”€โ”€ LICENSE              # MIT License
โ”œโ”€โ”€ CHANGELOG.md         # Version history
โ”œโ”€โ”€ .gitignore           # Git ignore rules
โ””โ”€โ”€ .github/
    โ””โ”€โ”€ workflows/
        โ”œโ”€โ”€ ci.yml       # Multi-platform CI workflow
        โ””โ”€โ”€ publish.yml  # Multi-platform publish workflow
```

## Comparison with Original Python Version

| Feature | Python (showpid.py) | Rust (showpid) |
|---------|-------------------|----------------|
| Speed | Moderate | Fast (compiled) |
| Binary Size | ~0 KB + Python | ~2-5 MB standalone |
| Dependencies | win32gui, win32process | Platform-native APIs |
| Platform Support | Windows only | Windows, Linux, macOS |
| Error Handling | Basic | Comprehensive |
| Retry Logic | โŒ | โœ… (configurable) |
| Multiple Windows | First only | All windows tried |
| Verbose Mode | โŒ | โœ… |
| Logging | Print statements | Structured logging (log/env_logger) |
| Thread Safety | N/A | โœ… |
| Testing | โŒ | โœ… (unit tests + CI) |
| Cross-compilation | N/A | โœ… |
| CI/CD | N/A | โœ… (GitHub Actions) |
| Package Registry | N/A | โœ… (crates.io) |

## Changelog

See [CHANGELOG.md](CHANGELOG.md) for version history.

## API Documentation

Full API documentation is available on [docs.rs](https://docs.rs/showpid).

### Main API Types

- `Config` - Configuration for window activation
- `WindowActivator` - Main window finder and activator
- `WindowInfo` - Platform-specific window information

### Example: Programmatic Usage

```rust
use showpid::{Config, WindowActivator};

// Create configuration
let config = Config::new(1234)
    .with_verbose(true)
    .with_retries(5)
    .with_retry_delay(std::time::Duration::from_millis(200));

// Create activator
let mut activator = WindowActivator::new(config);

// Execute with custom error handling
match activator.execute() {
    Ok(()) => {
        println!("Window activated successfully");
        std::process::exit(0);
    }
    Err(e) => {
        eprintln!("Failed to activate window: {}", e);
        std::process::exit(1);
    }
}
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Ensure cross-platform compatibility
4. Add tests for new features
5. Commit your changes (`git commit -m 'Add some amazing feature'`)
6. Push to the branch (`git push origin feature/amazing-feature`)
7. Open a Pull Request

### Adding Support for New Platforms

To add support for a new platform:

1. Create a new conditionally compiled module in `src/main.rs`
2. Implement `WindowActivator` for the new platform
3. Add platform-specific dependencies to `Cargo.toml`
4. Update CI workflow to test the new platform
5. Update this README with new platform requirements

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ‘ค Author
        
**Hadi Cahyadi** - [cumulus13@gmail.com](mailto:cumulus13@gmail.com)
    

[![Buy Me a Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/cumulus13)

[![Donate via Ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/cumulus13)
 
[Support me on Patreon](https://www.patreon.com/cumulus13)

- GitHub: [@cumulus13]https://github.com/cumulus13

## Acknowledgments

- Original Python version by Hadi Cahyadi
- Windows API bindings provided by [windows-rs]https://github.com/microsoft/windows-rs
- X11 bindings provided by the Rust community
- CoreGraphics bindings for macOS support
- All contributors and users of this project

## Support

If you encounter any issues or have questions:

1. Check the [Troubleshooting]#troubleshooting section
2. Search [existing issues]https://github.com/cumulus13/showpid/issues
3. [Open a new issue]https://github.com/cumulus13/showpid/issues/new with:
   - Your operating system and version
   - Steps to reproduce
   - Verbose output (`showpid -v <PID>`)
   - Expected vs actual behavior

## Roadmap

- [ ] Wayland support for Linux
- [ ] Apple Silicon (ARM64) native builds
- [ ] Window selection by title pattern matching
- [ ] Multiple window selection interface
- [ ] Configuration file support
- [ ] Shell completion scripts
- [ ] System tray integration
- [ ] Web interface for remote control