From e46a32ab43f1dc4bd7399e1dcfabb880c223ea27 Mon Sep 17 00:00:00 2001 From: project-repo Date: Fri, 6 Jan 2023 20:15:28 +0100 Subject: [PATCH] Add example scripts for socket interaction --- example_scripts/cb_script_header.sh | 16 ++++++++++++++++ example_scripts/focus_follows_cursor.sh | 21 +++++++++++++++++++++ example_scripts/show_workspace_views.sh | 23 +++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 example_scripts/cb_script_header.sh create mode 100644 example_scripts/focus_follows_cursor.sh create mode 100644 example_scripts/show_workspace_views.sh diff --git a/example_scripts/cb_script_header.sh b/example_scripts/cb_script_header.sh new file mode 100644 index 0000000..0e7bda6 --- /dev/null +++ b/example_scripts/cb_script_header.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +named_pipe_send="$(mktemp -u)" +named_pipe_recv="$(mktemp -u)" +mkfifo "${named_pipe_send}" +mkfifo "${named_pipe_recv}" +nc -U "${CAGEBREAK_SOCKET}" < "${named_pipe_send}" > "${named_pipe_recv}"& +# The file descriptor 3 is set up to send commands to cagebreak and file +# descriptor 4 can be used to read events. Notice that events will pile up in +# file descriptor 4, so it is a good idea to continuously read from it or to +# clear it before starting a new transaction. +exec 3>"${named_pipe_send}" +exec 4<"${named_pipe_recv}" +# When the script exits, the os will clean up the pipe +rm "${named_pipe_recv}" +rm "${named_pipe_send}" diff --git a/example_scripts/focus_follows_cursor.sh b/example_scripts/focus_follows_cursor.sh new file mode 100644 index 0000000..e193e28 --- /dev/null +++ b/example_scripts/focus_follows_cursor.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# This script uses the cagebreak socket to modify the compositor's behaviour in +# such a way that the currently focussed tile follows the cursor, i.e. that the +# currently focussed tile is always the one within which the cursor is located. + +# Start up the ipc link +source "$(dirname "${BASH_SOURCE[0]}")/cb_script_header.sh" + +while IFS= read -r -d $'\0' event +do + # Remove the cg-ipc header + event="${event:6}" + if [[ "$(echo "${event}" | jq ".event_name")" = "\"cursor_switch_tile\"" ]] + then + new_tile="$(echo "${event}"|jq -r ".new_tile")" + new_output_id="$(echo "${event}"|jq -r ".new_output_id")" + # Send the new tile to focus to cagebreak + echo -e "screen ${new_output_id}\nfocus_tile ${new_tile}" >&3 + fi +done <&4 diff --git a/example_scripts/show_workspace_views.sh b/example_scripts/show_workspace_views.sh new file mode 100644 index 0000000..0c86b56 --- /dev/null +++ b/example_scripts/show_workspace_views.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# This script displays the process names of the views on the current workspace. + +# Start up the ipc link +source "$(dirname "${BASH_SOURCE[0]}")/cb_script_header.sh" + +echo "dump" >&3 + +while IFS= read -r -d $'\0' event +do + # Remove the cg-ipc header + event="${event:6}" + if [[ "$(echo "${event}" | jq ".event_name")" = "\"dump\"" ]] + then + curr_output="$(echo "${event}"|jq ".curr_output")" + curr_workspace="$(echo "${event}"|jq -r ".outputs.${curr_output}.curr_workspace")" + # Print the process names of the view on the current workspace. jq retrieves + # their PID and ps is then used to retrieve the process names. + (echo -n "message ";ps -o comm=Command -p $(echo "${event}"|jq -r ".outputs.${curr_output}.workspaces[$((curr_workspace-1))].views[].pid")|tail +2|sed ':a; N; $!ba; s/\n/||/g') >&3 + break + fi +done <&4