Window titles and windowlist visible matching

This commit is contained in:
Chris 2016-12-26 22:44:00 +01:00
parent 8c2d752780
commit a8ccaca28a
4 changed files with 35 additions and 18 deletions

View File

@ -8,13 +8,21 @@ use NoccyLabs\X11\WindowList;
$windows = new WindowList(); $windows = new WindowList();
$windows->dump(); $windows->dump();
// Select only visible windows
$visible = $windows->visible();
$visible->dump();
// Select all the windows with a title matching *code* // Select all the windows with a title matching *code*
$windows = $windows->visible()->find("*code*"); $codewindows = $visible->find("*code*");
$windows->dump(); if (count($codewindows)==0) {
printf("Sorry, couldn't find any vscode instances running!\n");
exit;
}
$codewindows->dump();
// Grab the first window // Grab the first window
$window = $windows->first(); $codewindow = $codewindows->first();
$window->dump(); $codewindow->dump();
// Focus the window and simulate F1 being pressed // Focus the window and simulate F1 being pressed
$window->focus()->sendKeys("F1"); $codewindow->focus()->activate()->sendKeys("F1");

View File

@ -0,0 +1,9 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\X11\Window;
$me = new Window();
$me->setWindowTitle("Awesome Terminal");

View File

@ -26,11 +26,6 @@ class Window
foreach ($output as $line) { foreach ($output as $line) {
if (preg_match('/^(.+?): (.+?)$/', trim($line), $match)) { if (preg_match('/^(.+?): (.+?)$/', trim($line), $match)) {
switch ($match[1]) { switch ($match[1]) {
case 'xwininfo':
if (preg_match('/Window id: .+? "(.+?)"/', $match[2], $m)) {
$this->windowTitle = $m[1];
}
break;
case 'Map State': case 'Map State':
$this->isVisible = ($match[2]=='IsViewable'); $this->isVisible = ($match[2]=='IsViewable');
break; break;
@ -54,16 +49,20 @@ class Window
public function getWindowTitle() 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) { exec("xdotool set_window --name ".escapeshellarg($title)." {$this->windowId}");
$this->focus(); }
$cmdl = sprintf("xdotool key %s", $keys);
} else { public function sendKeys($keys, $forceWindow=false)
{
if ($forceWindow) {
$cmdl = sprintf("xdotool key --delay 100 --window %d %s", $this->windowId, $keys); $cmdl = sprintf("xdotool key --delay 100 --window %d %s", $this->windowId, $keys);
} else {
$cmdl = sprintf("xdotool key %s", $keys);
} }
echo "$ {$cmdl}\n"; echo "$ {$cmdl}\n";
exec($cmdl); exec($cmdl);

View File

@ -63,11 +63,12 @@ class WindowList implements IteratorAggregate, Countable
} }
public function visible() public function visible($match=true)
{ {
$found = []; $found = [];
$match = (bool)$match;
foreach ($this->windows as $window) { foreach ($this->windows as $window) {
if ($window->isVisible()) if ($window->isVisible() == $match)
$found[] = $window; $found[] = $window;
} }
return new WindowList($found); return new WindowList($found);