From 7cd29929d1474fe7d52ca8c419505bcb97875b8a Mon Sep 17 00:00:00 2001 From: Freek Mank Date: Wed, 22 Dec 2021 14:46:50 +0100 Subject: [PATCH 1/2] Add support to toggle TDM --- tdm_off.sh | 1 + tdm_on.sh | 1 + tdm_toggle.sh | 13 +++++++++++++ 3 files changed, 15 insertions(+) create mode 100755 tdm_toggle.sh diff --git a/tdm_off.sh b/tdm_off.sh index 8081460..bc5fb92 100755 --- a/tdm_off.sh +++ b/tdm_off.sh @@ -1,4 +1,5 @@ #!/bin/bash +rm -f tdm_started ./SmcDumpKey MVHR 0 sleep 1 ./SmcDumpKey MVMR 2 diff --git a/tdm_on.sh b/tdm_on.sh index 9fd8ffe..508f1c8 100755 --- a/tdm_on.sh +++ b/tdm_on.sh @@ -4,3 +4,4 @@ sleep 1 ./SmcDumpKey MVMR 2 sleep 2 DISPLAY=:0.0 xrandr --output eDP --off +touch tdm_started diff --git a/tdm_toggle.sh b/tdm_toggle.sh new file mode 100755 index 0000000..8a1a46a --- /dev/null +++ b/tdm_toggle.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +pushd $(dirname "${BASH_SOURCE[0]}") + +if [[ -f "tdm_started" ]]; then + echo "Switching off TDM" + source tdm_off.sh +else + echo "Switch on TDM" + source tdm_on.sh +fi + +popd From 1d919fd3cafd6aa142b383aa9c67e04079d14a83 Mon Sep 17 00:00:00 2001 From: Freek Mank Date: Wed, 22 Dec 2021 15:01:23 +0100 Subject: [PATCH 2/2] Acpi scripts --- acpi/README.md | 30 ++++++++++++++++++++++++++++++ acpi/powerbutton | 2 ++ acpi/powerbutton.sh | 30 ++++++++++++++++++++++++++++++ acpi/rc.local | 7 +++++++ 4 files changed, 69 insertions(+) create mode 100644 acpi/README.md create mode 100644 acpi/powerbutton create mode 100755 acpi/powerbutton.sh create mode 100755 acpi/rc.local diff --git a/acpi/README.md b/acpi/README.md new file mode 100644 index 0000000..12baf77 --- /dev/null +++ b/acpi/README.md @@ -0,0 +1,30 @@ +# Use iMac powerbutton to toggle TDM + +Since the powerbutton is the only physical button on the iMac, the following instructions allow using the powerbutton to toggle TDM (and to switch off the iMac as well, of course). No more keyboard/mouse are needed and the iMac can be used as an external monitor. + +How it works: +- After boot, iMac starts in TDM mode +- One press of the power button toggles TDM mode +- Two presses of the power button (within 1 second) switches off the iMac + +# How to use + +Install by following the instructions in the main README file + +Upon startup, switch TDM on: +``` +cp rc.local /etc/rc.local +``` + +Add acpi powerbutton event and script to handle event: +``` +cp powerbutton /etc/acpi/events +cp powerbutton.sh /etc/acpi +``` + +Restart acpid +``` +systemctl restart acpid +``` + +_Note_: This was tested on a end-2009 27" iMac running Ubuntu 20.10 diff --git a/acpi/powerbutton b/acpi/powerbutton new file mode 100644 index 0000000..9028e62 --- /dev/null +++ b/acpi/powerbutton @@ -0,0 +1,2 @@ +event=button/power PBTN +action=/etc/acpi/powerbutton.sh diff --git a/acpi/powerbutton.sh b/acpi/powerbutton.sh new file mode 100755 index 0000000..c49b318 --- /dev/null +++ b/acpi/powerbutton.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +pushd $(dirname "${BASH_SOURCE[0]}") + +FILE=powerbutton_pressed +NOW=$(date +%s) + +if [[ -f "$FILE" ]]; then + # Read timestamp of previous powerbutton press from file + echo "File exists" + typeset -i PREV=$(cat $FILE) + echo Compare $NOW and $PREV + + # if two powerbutton presses were <1 seconds apart -> shutdown + if [[ $(($NOW-$PREV)) -lt 2 ]]; then + # Shutdown + echo "Powerbutton pressed twice in a row: Shutting down" + shutdown now + else + echo "Toggle TDM" + /home/freek/smc_util/tdm_toggle.sh & + fi +else + echo "Toggle TDM" + /home/freek/smc_util/tdm_toggle.sh & +fi + +echo $NOW > $FILE + +popd diff --git a/acpi/rc.local b/acpi/rc.local new file mode 100755 index 0000000..50d5a74 --- /dev/null +++ b/acpi/rc.local @@ -0,0 +1,7 @@ +#!/bin/bash + +# Start Target Display Mode such that the pc is used as external monitor right away +# Note: The Power button toggles TDM (see /etc/acpi/events) +pushd /home/freek/smc_util +./tdm_on.sh +popd