From a8ccaca28a3abb2d240c018e7c80a18d78f592f7 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Mon, 26 Dec 2016 22:44:00 +0100 Subject: [PATCH] Window titles and windowlist visible matching --- examples/find_windows.php | 18 +++++++++++++----- examples/window_title.php | 9 +++++++++ src/Window.php | 21 ++++++++++----------- src/WindowList.php | 5 +++-- 4 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 examples/window_title.php diff --git a/examples/find_windows.php b/examples/find_windows.php index 9dbd4cc..4f12d15 100644 --- a/examples/find_windows.php +++ b/examples/find_windows.php @@ -8,13 +8,21 @@ use NoccyLabs\X11\WindowList; $windows = new WindowList(); $windows->dump(); +// Select only visible windows +$visible = $windows->visible(); +$visible->dump(); + // Select all the windows with a title matching *code* -$windows = $windows->visible()->find("*code*"); -$windows->dump(); +$codewindows = $visible->find("*code*"); +if (count($codewindows)==0) { + printf("Sorry, couldn't find any vscode instances running!\n"); + exit; +} +$codewindows->dump(); // Grab the first window -$window = $windows->first(); -$window->dump(); +$codewindow = $codewindows->first(); +$codewindow->dump(); // Focus the window and simulate F1 being pressed -$window->focus()->sendKeys("F1"); +$codewindow->focus()->activate()->sendKeys("F1"); diff --git a/examples/window_title.php b/examples/window_title.php new file mode 100644 index 0000000..3b959a5 --- /dev/null +++ b/examples/window_title.php @@ -0,0 +1,9 @@ +setWindowTitle("Awesome Terminal"); diff --git a/src/Window.php b/src/Window.php index 03dc213..2a3e5a7 100644 --- a/src/Window.php +++ b/src/Window.php @@ -26,11 +26,6 @@ class Window foreach ($output as $line) { if (preg_match('/^(.+?): (.+?)$/', trim($line), $match)) { switch ($match[1]) { - case 'xwininfo': - if (preg_match('/Window id: .+? "(.+?)"/', $match[2], $m)) { - $this->windowTitle = $m[1]; - } - break; case 'Map State': $this->isVisible = ($match[2]=='IsViewable'); break; @@ -54,16 +49,20 @@ class Window public function getWindowTitle() { - return $this->windowTitle; + return trim(exec("xdotool getwindowname {$this->windowId}")); } - public function sendKeys($keys, $forceFocus=false) + public function setWindowTitle($title) { - if ($forceFocus) { - $this->focus(); - $cmdl = sprintf("xdotool key %s", $keys); - } else { + exec("xdotool set_window --name ".escapeshellarg($title)." {$this->windowId}"); + } + + public function sendKeys($keys, $forceWindow=false) + { + if ($forceWindow) { $cmdl = sprintf("xdotool key --delay 100 --window %d %s", $this->windowId, $keys); + } else { + $cmdl = sprintf("xdotool key %s", $keys); } echo "$ {$cmdl}\n"; exec($cmdl); diff --git a/src/WindowList.php b/src/WindowList.php index a5ea0c8..7148801 100644 --- a/src/WindowList.php +++ b/src/WindowList.php @@ -63,11 +63,12 @@ class WindowList implements IteratorAggregate, Countable } - public function visible() + public function visible($match=true) { $found = []; + $match = (bool)$match; foreach ($this->windows as $window) { - if ($window->isVisible()) + if ($window->isVisible() == $match) $found[] = $window; } return new WindowList($found);