|
4 | 4 | # This file is part of the batocera distribution (https://batocera.org). |
5 | 5 | # Copyright (c) 2025+. |
6 | 6 | # |
7 | | -# This program is free software: you can redistribute it and/or modify |
8 | | -# it under the terms of the GNU General Public License as published by |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
9 | 9 | # the Free Software Foundation, version 3. |
10 | 10 | # |
11 | | -# You should have received a copy of the GNU General Public License |
| 11 | +# You should have received a copy of the GNU General Public License |
12 | 12 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
13 | 13 | # |
14 | 14 | # YOU MUST KEEP THIS HEADER AS IT IS |
|
22 | 22 | # users can set a higher or lower manufacturer TDP accordingly. |
23 | 23 |
|
24 | 24 | log="/userdata/system/logs/amd-tdp.log" |
| 25 | +STATE_FILE="/var/run/amd-tdp.changed" |
25 | 26 |
|
26 | 27 | # Check we have a max system TDP value |
27 | 28 | CPU_TDP=$(/usr/bin/batocera-settings-get system.cpu.tdp) |
|
34 | 35 |
|
35 | 36 | # Set the final tdp value |
36 | 37 | set_tdp() { |
37 | | - echo "Game ${2} requested setting AMD Mobile Processor TDP to ${1} Watts" >> $log |
38 | | - /usr/bin/batocera-amd-tdp $1 |
| 38 | + local TDP_VALUE=$1 |
| 39 | + local ROM_NAME=$2 |
| 40 | + |
| 41 | + echo "Game ${ROM_NAME} requested setting AMD Processor TDP to ${TDP_VALUE} Watts" >> $log |
| 42 | + |
| 43 | + /usr/bin/batocera-amd-tdp "$TDP_VALUE" |
39 | 44 | } |
40 | 45 |
|
41 | 46 | # Determine the new TDP value based on max TDP |
42 | 47 | handle_tdp() { |
43 | | - TDP_PERCENTAGE=$1 |
44 | | - ROM_NAME=$2 |
| 48 | + local TDP_PERCENTAGE=$1 |
| 49 | + local ROM_NAME=$2 |
| 50 | + |
| 51 | + local MAX_TDP |
45 | 52 | MAX_TDP=$(/usr/bin/batocera-settings-get system.cpu.tdp) |
46 | | - # Check if MAX_TDP is defined and non-empty |
47 | | - if [ -n "$MAX_TDP" ]; then |
48 | | - # round the value up or down to make bash happy |
49 | | - TDP_VALUE=$(awk -v max_tdp="$MAX_TDP" -v tdp_percentage="$TDP_PERCENTAGE" 'BEGIN { printf("%.0f\n", max_tdp * tdp_percentage / 100) }') |
50 | | - set_tdp "${TDP_VALUE}" "${ROM_NAME}" |
51 | | - else |
| 53 | + |
| 54 | + # Check if TDP is defined and non-empty |
| 55 | + if [ -z "$MAX_TDP" ]; then |
52 | 56 | echo "A maximum TDP is not defined, cannot set TDP." >> $log |
53 | 57 | exit 1 |
54 | 58 | fi |
| 59 | + |
| 60 | + # Round the value up or down to make bash happy |
| 61 | + local TDP_VALUE |
| 62 | + TDP_VALUE=$(awk -v max_tdp="$MAX_TDP" -v tdp_percentage="$TDP_PERCENTAGE" 'BEGIN { printf("%.0f\n", max_tdp * tdp_percentage / 100) }') |
| 63 | + set_tdp "${TDP_VALUE}" "${ROM_NAME}" |
55 | 64 | } |
56 | 65 |
|
57 | | -# Check for events |
58 | | -EVENT=$1 |
59 | | -SYSTEM_NAME=$2 |
60 | | -ROM_PATH=$5 |
| 66 | +do_game_start() { |
| 67 | + local SYSTEM_NAME="$1" |
| 68 | + local ROM_NAME="$2" |
| 69 | + local TDP_SETTING="" |
| 70 | + local RAW_GLOBAL="" |
61 | 71 |
|
62 | | -# Get the rom name from ROM_PATH |
63 | | -ROM_NAME=$(basename "$ROM_PATH") |
| 72 | + # Clear previous state file if present |
| 73 | + rm -f "$STATE_FILE" 2>/dev/null |
64 | 74 |
|
65 | | -# Exit accordingly if the event is neither gameStart nor gameStop |
66 | | -if [ "$EVENT" != "gameStart" ] && [ "$EVENT" != "gameStop" ]; then |
67 | | - exit 0 |
68 | | -fi |
| 75 | + # Check for user set rom or system specific setting |
| 76 | + if [ -n "${SYSTEM_NAME}" ]; then |
| 77 | + TDP_SETTING=$(/usr/bin/batocera-settings-get "${SYSTEM_NAME}[\"${ROM_NAME}\"].tdp") |
| 78 | + [ -z "$TDP_SETTING" ] && TDP_SETTING=$(/usr/bin/batocera-settings-get "${SYSTEM_NAME}.tdp") |
| 79 | + fi |
69 | 80 |
|
70 | | -# Handle gameStop event |
71 | | -if [ "$EVENT" == "gameStop" ]; then |
72 | | - RAW_TDP_SETTING=$(/usr/bin/batocera-settings-get global.tdp) |
73 | | - # Check if the raw setting is actually empty |
74 | | - if [ -z "${RAW_TDP_SETTING}" ]; then |
75 | | - # If it's empty, use the system default TDP |
76 | | - TDP_SETTING="$(/usr/bin/batocera-settings-get system.cpu.tdp)" |
77 | | - if [ -n "$TDP_SETTING" ]; then |
78 | | - set_tdp "${TDP_SETTING}" "STOP" |
79 | | - else |
80 | | - echo "No default TDP setting defined, cannot set TDP on game stop." >> $log |
81 | | - exit 1 |
| 81 | + # If no user set system specific setting check for user set global setting |
| 82 | + if [ -z "${TDP_SETTING}" ]; then |
| 83 | + RAW_GLOBAL=$(/usr/bin/batocera-settings-get global.tdp) |
| 84 | + if [ -n "${RAW_GLOBAL}" ]; then |
| 85 | + TDP_SETTING=$(printf "%.0f" "${RAW_GLOBAL}") |
82 | 86 | fi |
83 | | - else |
84 | | - # If it's not empty, NOW we can format it and handle it as a percentage |
85 | | - TDP_SETTING=$(printf "%.0f" "${RAW_TDP_SETTING}") |
86 | | - handle_tdp "${TDP_SETTING}" "STOP" |
87 | 87 | fi |
88 | | - exit 0 |
89 | | -fi |
90 | 88 |
|
91 | | -# Run through determining the desired TDP setting |
92 | | -# Check for user set system specific setting |
93 | | -if [ -n "${SYSTEM_NAME}" ]; then |
94 | | - # Check for rom specific config |
95 | | - TDP_SETTING=$(/usr/bin/batocera-settings-get "${SYSTEM_NAME}[\"${ROM_NAME}\"].tdp") |
96 | | - if [ -z "${TDP_SETTING}" ]; then |
97 | | - TDP_SETTING="$(/usr/bin/batocera-settings-get ${SYSTEM_NAME}.tdp)" |
| 89 | + # Now apply TDP percentage accordingly |
| 90 | + if [ -n "${TDP_SETTING}" ]; then |
| 91 | + handle_tdp "${TDP_SETTING}" "${ROM_NAME}" |
| 92 | + : > "$STATE_FILE" |
| 93 | + else |
| 94 | + echo "Game START, but no TDP setting defined. Leaving TDP unchanged." >> $log |
| 95 | + echo "" >> "$log" |
| 96 | + echo "*** ------------------------------------- ***" >> "$log" |
| 97 | + echo "" >> "$log" |
| 98 | + exit 0 |
98 | 99 | fi |
99 | | -fi |
| 100 | +} |
100 | 101 |
|
101 | | -# If no user set system specific setting check for user set global setting |
102 | | -if [ -z "${TDP_SETTING}" ]; then |
103 | | - RAW_TDP_SETTING=$(/usr/bin/batocera-settings-get global.tdp) |
104 | | - if [ -n "${RAW_TDP_SETTING}" ]; then |
105 | | - TDP_SETTING=$(printf "%.0f" "${RAW_TDP_SETTING}") |
| 102 | +do_game_stop() { |
| 103 | + # Check if we actually changed anything on game start |
| 104 | + if [ ! -e "$STATE_FILE" ]; then |
| 105 | + echo "Game STOP, but no prior TDP change. Nothing to do." >> "$log" |
| 106 | + echo "" >> "$log" |
| 107 | + echo "*** ------------------------------------- ***" >> "$log" |
| 108 | + echo "" >> "$log" |
| 109 | + exit 0 |
106 | 110 | fi |
107 | | -fi |
108 | 111 |
|
109 | | -# If no value is found after all checks, ensure tdp is default before exiting |
110 | | -if [ -z "${TDP_SETTING}" ]; then |
111 | | - TDP_SETTING="$(/usr/bin/batocera-settings-get-master system.cpu.tdp)" |
112 | | - if [ -n "${TDP_SETTING}" ]; then |
113 | | - set_tdp "${TDP_SETTING}" "${ROM_NAME}" |
| 112 | + local RAW_GLOBAL |
| 113 | + RAW_GLOBAL=$(/usr/bin/batocera-settings-get global.tdp) |
| 114 | + |
| 115 | + if [ -n "${RAW_GLOBAL}" ]; then |
| 116 | + TDP_SETTING=$(printf "%.0f" "${RAW_GLOBAL}") |
| 117 | + handle_tdp "$TDP_SETTING" "STOP" |
114 | 118 | else |
115 | | - echo "No TDP setting defined, cannot set TDP." >> $log |
116 | | - exit 1 |
| 119 | + local SYSTEM_TDP |
| 120 | + SYSTEM_TDP=$(/usr/bin/batocera-settings-get system.cpu.tdp) |
| 121 | + if [ -n "$SYSTEM_TDP" ]; then |
| 122 | + set_tdp "$SYSTEM_TDP" "STOP" |
| 123 | + else |
| 124 | + echo "No default TDP setting defined, cannot set TDP on game stop." >> $log |
| 125 | + exit 1 |
| 126 | + fi |
117 | 127 | fi |
| 128 | + |
| 129 | + rm -f "$STATE_FILE" 2>/dev/null |
118 | 130 | exit 0 |
119 | | -fi |
| 131 | +} |
| 132 | + |
| 133 | +# Check for events |
| 134 | +SYSTEM_NAME="$2" |
| 135 | +ROM_PATH="$5" |
| 136 | + |
| 137 | +# Get the rom name from ROM_PATH |
| 138 | +ROM_NAME=$(basename "$ROM_PATH") |
| 139 | + |
| 140 | +case "$1" in |
| 141 | + gameStart) |
| 142 | + do_game_start "$SYSTEM_NAME" "$ROM_NAME" |
| 143 | + ;; |
| 144 | + gameStop) |
| 145 | + do_game_stop |
| 146 | + ;; |
| 147 | + *) |
| 148 | + exit 0 |
| 149 | + ;; |
| 150 | +esac |
120 | 151 |
|
121 | | -# Now apply TDP percentage accordingly |
122 | | -handle_tdp "${TDP_SETTING}" "${ROM_NAME}" |
| 152 | +exit 0 |
0 commit comments