I have a routine that uses conky to place an image on the middle of my primary monitor. To close it, I wish to click on the image, so I acquired a bash script that defines a square area surrounding the image, and waits for the mouse to be clicked while inside that defined space.
Unfortunately, the script is registering clicks anywhere on primary monitor as if I had clicked on the image inside the designated area. Can anyone please tell me what's making this script ignore the set boundaries?
clickbox_any.sh
Code: Select all
#!/bin/bash
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
export XDG_CURRENT_DESKTOP=Cinnamon
export XDG_RUNTIME_DIR=/run/user/$(id -u)
# Play a beep sound at max volume
paplay /usr/share/sounds/freedesktop/stereo/complete.oga
# Get the mouse ID
MOUSE_ID=$(xinput --list | grep -i -m 1 'mouse' | grep -o 'id=[0-9]\+' | grep -o '[0-9]\+')
# Define the coordinates of the clickable box
box_x=780
box_y=385
box_width=300
box_height=260
# Function to check if the mouse is inside the box
mouse_inside_box() {
mouse_x=$(xdotool getmouselocation --shell | grep 'X' | awk -F'=' '{print $2}')
mouse_y=$(xdotool getmouselocation --shell | grep 'Y' | awk -F'=' '{print $2}')
if [ "$mouse_x" -ge "$box_x" ] && [ "$mouse_x" -le "$((box_x + box_width))" ] && \
[ "$mouse_y" -ge "$box_y" ] && [ "$mouse_y" -le "$((box_y + box_height))" ]; then
return 0 # Mouse is inside the box
else
return 1 # Mouse is outside the box
fi
}
# Wait for a mouse click event within the specified box
while true; do
# Get the current state of the mouse buttons
STATE1=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
if mouse_inside_box; then
# Wait for a change in mouse button state
while true; do
sleep 0.2
STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
# Check for a change in mouse button states
if [ "$(comm -13 <(echo "$STATE1") <(echo "$STATE2"))" != "" ]; then
# Play a beep sound at max volume
paplay /usr/share/sounds/freedesktop/stereo/complete.oga
# Close the conky picture display
pkill -f "conky -c /home/logansfury/.conky/Biohazzard/conkyrc"
# Exit the loop after the script is executed
break 2 # Break out of both loops
fi
STATE1=$STATE2
done
fi
sleep 0.1 # Adjust the sleep duration as needed
done
The export bit at the beginning is so that this can be launched by crontab.
Thank you for reading,
Logan