From 585a3c2a6055487c6abea4ea7aff1e725036a18a Mon Sep 17 00:00:00 2001 From: npc-strider Date: Wed, 8 Jan 2020 13:56:50 +0800 Subject: [PATCH] Added -p and -b flag --- Makefile | 5 ++-- README.md | 14 ++++++---- wmctrl.cs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index a5d3be1..a34e1a5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CSC=csc CSC=mcs -PROG=wmctrl.exe +PROG=./wmctrl.exe @@ -15,4 +15,5 @@ $(PROG): wmctrl.cs test: $(PROG) -h $(PROG) -l - $(PROG) -a gvim + $(PROG) -a devenv + #you can test the others yourself :D diff --git a/README.md b/README.md index cb9bbaf..a1f6095 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,27 @@ # wmctrl-for-windows +This is a fork of https://github.com/ebranlard/wmctrl-for-windows with two new flags added for PID and process title :D + Command line implementation of wmctrl for windows (pseudo-equivalent). Only few features of the linux tool wmctrl are implemented so far, that is: - List the window titles, process name and ID - - Switch the focus to a given window + - Switch the focus to a given window (Based on process title, process name or PID) The program is written in C-sharp. This repository contains the latest binary and the source code (compatible Mono or Microsoft). ## Binaries -The latest binary is available [here](https://github.com/elmanuelito/wmctrl-for-windows/raw/master/_bin/wmctrl.exe) +You can get the binaries from the releases tab of this repo ## Features/ usage usage: wmctrl [options] [args] options: - -h : show this help - -l : list windows - -a : switch to the window of the process name + -h : show this help + -l : list windows + -a : switch to the window of the process name + -b : switch to the window of the process title + -p : switch to the window of the process ID diff --git a/wmctrl.cs b/wmctrl.cs index 067c6b0..2f89fd6 100644 --- a/wmctrl.cs +++ b/wmctrl.cs @@ -33,7 +33,63 @@ public class wmctrl } } + // -------------------------------------------------------------------------------- + // --- Switch to Window (TITLE) + // -------------------------------------------------------------------------------- + public static int SwitchToWindowTitle(string procTitle) + { + int ret = -1; + Process[] procs = Process.GetProcesses(); + foreach (Process proc in procs) + { + if (! String.IsNullOrEmpty(proc.MainWindowTitle) && string.Compare(procTitle, proc.MainWindowTitle) == 0) + { + Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle); + SwitchToThisWindow(proc.MainWindowHandle); //Just going to use the first window as per usual + ret = 0; + break; + } + } + if (ret == -1) + { + Console.WriteLine("Error: No process found for title: {0}", procTitle); + } + return ret; + } + + // -------------------------------------------------------------------------------- + // --- Switch to Window (PID) + // -------------------------------------------------------------------------------- + + public static int SwitchToWindowPID(string procPIDstr) + { + int ret = -1; + int procPID; + if (Int32.TryParse(procPIDstr, out procPID)){ + Process[] procs = Process.GetProcesses(); + foreach (Process proc in procs) + { + if (procPID == proc.Id) //Can processes have no PID in MS windows? I have no idea. + { + Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle); + SwitchToThisWindow(proc.MainWindowHandle); //Just going to use the first window as per usual + ret = 0; + break; + } + } + if (ret == -1) + { + Console.WriteLine("Error: No process found for PID: {0}", procPIDstr); + } + } + else + { + Console.WriteLine("Error: Invalid PID: {0}", procPIDstr); + } + return ret; + } + // -------------------------------------------------------------------------------- // --- List Windows info // -------------------------------------------------------------------------------- @@ -59,9 +115,11 @@ public class wmctrl Console.WriteLine("usage: wmctrl [options] [args]"); Console.WriteLine(""); Console.WriteLine("options:"); - Console.WriteLine(" -h : show this help"); - Console.WriteLine(" -l : list windows"); - Console.WriteLine(" -a : switch to the window of the process name "); + Console.WriteLine(" -h : show this help"); + Console.WriteLine(" -l : list windows"); + Console.WriteLine(" -a : switch to the window of the process name "); + Console.WriteLine(" -b : switch to the window of the process title "); + Console.WriteLine(" -p : switch to the window of the process ID "); Console.WriteLine(""); } @@ -99,6 +157,24 @@ public class wmctrl status=-1; } break; + case "-b": // Switch to window (TITLE) + if (i+1