Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iMac powerbutton support #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions acpi/README.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions acpi/powerbutton
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
event=button/power PBTN
action=/etc/acpi/powerbutton.sh
30 changes: 30 additions & 0 deletions acpi/powerbutton.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions acpi/rc.local
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tdm_off.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
rm -f tdm_started
./SmcDumpKey MVHR 0
sleep 1
./SmcDumpKey MVMR 2
Expand Down
1 change: 1 addition & 0 deletions tdm_on.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ sleep 1
./SmcDumpKey MVMR 2
sleep 2
DISPLAY=:0.0 xrandr --output eDP --off
touch tdm_started
13 changes: 13 additions & 0 deletions tdm_toggle.sh
Original file line number Diff line number Diff line change
@@ -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