Added alternate method to retrieve window geometry
This commit is contained in:
		
							
								
								
									
										9
									
								
								examples/alt_geometry.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								examples/alt_geometry.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\X11\Window;
 | 
			
		||||
 | 
			
		||||
$me = new Window();
 | 
			
		||||
 | 
			
		||||
print_r($me->getWindowGeometry(true));
 | 
			
		||||
@@ -99,13 +99,46 @@ class Window
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getWindowGeometry()
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     *
 | 
			
		||||
     * @param bool $alternative If true, xwininfo will be used instead of xdotool
 | 
			
		||||
     */
 | 
			
		||||
    public function getWindowGeometry($alternative=false)
 | 
			
		||||
    {
 | 
			
		||||
        exec("xdotool getwindowgeometry --shell {$this->windowId}", $output, $status);
 | 
			
		||||
        $ret = [];
 | 
			
		||||
        foreach ($output as $line) {
 | 
			
		||||
            list($k,$v) = explode("=",$line,2);
 | 
			
		||||
            $ret[strtolower($k)]=$v;
 | 
			
		||||
        if ($alternative==true) {
 | 
			
		||||
            exec("LANG=C xwininfo -id {$this->windowId} -int", $output, $status);
 | 
			
		||||
            foreach ($output as $line) {
 | 
			
		||||
                if (preg_match('/Window id:\s+([0-9]+)/', $line, $m)) {
 | 
			
		||||
                    $win_id = $m[1];
 | 
			
		||||
                } elseif (preg_match('/Absolute upper-left ([XY]):\s+([0-9]+)/', $line, $m)) {
 | 
			
		||||
                    if ($m[1] == 'X') {
 | 
			
		||||
                        $win_x = $m[2];
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $win_y = $m[2];
 | 
			
		||||
                    }
 | 
			
		||||
                } elseif (preg_match('/Width:\s+([0-9]+)/', $line, $m)) {
 | 
			
		||||
                    $win_width = $m[1];
 | 
			
		||||
                } elseif (preg_match('/Height:\s+([0-9]+)/', $line, $m)) {
 | 
			
		||||
                    $win_height = $m[1];
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            // return WINDOW, X, Y, WIDTH, HEIGHT, SCREEN
 | 
			
		||||
            $ret = [
 | 
			
		||||
                'window' => $win_id,
 | 
			
		||||
                'x' => $win_x,
 | 
			
		||||
                'y' => $win_y,
 | 
			
		||||
                'width' => $win_width,
 | 
			
		||||
                'height' => $win_height,
 | 
			
		||||
                'screen' => 0 // don't seem to get this from xwininfo
 | 
			
		||||
            ];
 | 
			
		||||
        } else {
 | 
			
		||||
            exec("xdotool getwindowgeometry --shell {$this->windowId}", $output, $status);
 | 
			
		||||
            $ret = [];
 | 
			
		||||
            foreach ($output as $line) {
 | 
			
		||||
                list($k,$v) = explode("=",$line,2);
 | 
			
		||||
                $ret[strtolower($k)]=$v;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return (object)$ret;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user