Created
June 16, 2021 15:05
-
-
Save pdc4444/425d9ad3cca72a1a0f90b1a2ba7aebac to your computer and use it in GitHub Desktop.
This script was written to quickly swap between desktops in GNOME2 (might work in GNOME3).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This script was written to quickly swap between desktops in GNOME2 (might work in GNOME3). | |
Logic is written to determine if the current desktop +1 or -1 exists so the user can pass the --next or --previous flags. | |
If the previous or next desktop does not exist then the desktop does not switch (it does not loop). | |
You might think "Why not use xdotool?" The reason I'm not using xdotool is so that the keys that it presses do no get passed to windows which grab keyboard input. | |
For example, using virt-viewer if you were to script xdotool to a hotkey, the actual key presses would be sent to the virtual machine instead of the hostmachine. | |
*/ | |
$longopts = ['next', 'previous']; | |
$cli_opts = getOpt('', $longopts); | |
if (!empty($cli_opts)) { | |
foreach ($cli_opts as $key => $value) { | |
if (in_array($key, $longopts) !== FALSE) { | |
switchDesktop($key); | |
} | |
exit(); //Only one option allowed | |
} | |
} | |
/** | |
* Takes a string and determines if the desktop switching should be +1 or -1, then switches to that desktop if it exists. | |
* | |
* @param string $direction - The way you want to move desktops | |
*/ | |
function switchDesktop($direction) | |
{ | |
$desktop_info = getCurrentDesktop(); | |
$modifier = $direction == 'next' ? 1 : -1; | |
$desktop_check = intval($desktop_info['current_desktop']) + intval($modifier); | |
if (array_key_exists($desktop_check, $desktop_info['desktops'])) { | |
$cmd = 'wmctrl -s ' . $desktop_check; | |
my_shell_exec($cmd); | |
} | |
} | |
/** | |
* Returns the current desktop information for use with the switchDesktop function. | |
* | |
* @return array Returns the current desktop and a sub array of all detected desktops. | |
*/ | |
function getCurrentDesktop() | |
{ | |
$cmd = "wmctrl -d"; | |
$result = my_shell_exec($cmd); | |
if (empty($result['stderr'])) { | |
$output_array = explode("\n", $result['stdout']); | |
$array_count = count($output_array) - 1; | |
if (empty($output_array[$array_count])) { | |
unset($output_array[$array_count]); | |
} | |
} | |
foreach ($output_array as $key => $line) { | |
if (strpos($line, "* DG: ") !== FALSE) { | |
$line_array = explode(" ", $line); | |
$current_desktop = $line_array[0]; | |
} | |
} | |
return ['current_desktop' => $current_desktop, 'desktops' => $output_array]; | |
} | |
/** | |
* Allows a shell command via a new process and captures the stdout and stderr for examination | |
* | |
* @param string $cmd - The command to be run | |
* @return array The result of the command run for both stdout and stderr | |
*/ | |
function my_shell_exec($cmd) | |
{ | |
$proc = proc_open($cmd,[ | |
1 => ['pipe','w'], | |
2 => ['pipe','w'], | |
],$pipes); | |
$stdout = stream_get_contents($pipes[1]); | |
fclose($pipes[1]); | |
$stderr = stream_get_contents($pipes[2]); | |
fclose($pipes[2]); | |
proc_close($proc); | |
$result = ['stdout' => $stdout, 'stderr' => $stderr, 'cmd' => $cmd]; | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment