forked from NG-SD-Plugins/PowerTools
Add per-game profile loading and fix stuff
This commit is contained in:
parent
d023f42ac5
commit
7158ac3596
3 changed files with 193 additions and 76 deletions
111
main.py
111
main.py
|
@ -21,7 +21,7 @@ logging.basicConfig(
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
logging.info(f"PowerTools v{VERSION} https://github.com/NGnius/PowerTools")
|
logging.info(f"PowerTools v{VERSION} https://github.com/NGnius/PowerTools")
|
||||||
logging.info(f"CWD: {os.getcwd()} HOME:{HOME_DIR}")
|
logging.debug(f"CWD: {os.getcwd()} HOME:{HOME_DIR}")
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
#import pathlib
|
#import pathlib
|
||||||
|
@ -129,8 +129,7 @@ class Plugin:
|
||||||
auto_fan = True
|
auto_fan = True
|
||||||
persistent = True
|
persistent = True
|
||||||
modified_settings = False
|
modified_settings = False
|
||||||
current_game = None # None means main menu
|
current_gameid = None
|
||||||
last_recognised_game = None
|
|
||||||
|
|
||||||
async def get_version(self) -> str:
|
async def get_version(self) -> str:
|
||||||
return VERSION
|
return VERSION
|
||||||
|
@ -265,47 +264,44 @@ class Plugin:
|
||||||
if settings is None or settings["persistent"] == False:
|
if settings is None or settings["persistent"] == False:
|
||||||
logging.debug("Ignoring settings from file")
|
logging.debug("Ignoring settings from file")
|
||||||
self.persistent = False
|
self.persistent = False
|
||||||
self.cpus = []
|
self.guess_settings(self)
|
||||||
|
self.modified_settings = True
|
||||||
for cpu_number in range(0, Plugin.CPU_COUNT):
|
|
||||||
self.cpus.append(CPU(cpu_number))
|
|
||||||
|
|
||||||
# If any core has two threads, smt is True
|
|
||||||
self.smt = self.cpus[1].status()
|
|
||||||
if(not self.smt):
|
|
||||||
for cpu_number in range(2, len(self.cpus), 2):
|
|
||||||
if(self.cpus[cpu_number].status()):
|
|
||||||
self.smt = True
|
|
||||||
break
|
|
||||||
logging.info(f"SMT state is guessed to be {self.smt}")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# apply settings
|
# apply settings
|
||||||
logging.debug("Restoring settings from file")
|
logging.debug("Restoring settings from file")
|
||||||
self.persistent = True
|
self.persistent = True
|
||||||
# CPU
|
self.apply_settings(self, settings)
|
||||||
self.cpus = []
|
# self.modified_settings = False
|
||||||
|
|
||||||
for cpu_number in range(0, Plugin.CPU_COUNT):
|
|
||||||
self.cpus.append(CPU(cpu_number, settings=settings["cpu"]["threads"][cpu_number]))
|
|
||||||
self.smt = settings["cpu"]["smt"]
|
|
||||||
write_cpu_boost(settings["cpu"]["boost"])
|
|
||||||
# GPU
|
|
||||||
write_gpu_ppt(1, settings["gpu"]["slowppt"])
|
|
||||||
write_gpu_ppt(2, settings["gpu"]["fastppt"])
|
|
||||||
# Fan
|
|
||||||
if not (os.path.exists(FANTASTIC_INSTALL_DIR) or settings["fan"]["auto"]):
|
|
||||||
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 1)
|
|
||||||
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", settings["fan"]["target"])
|
|
||||||
self.dirty = False
|
|
||||||
logging.info("Handled saved settings, back-end startup complete")
|
logging.info("Handled saved settings, back-end startup complete")
|
||||||
# server setup
|
# server setup
|
||||||
await pt_server.start(VERSION)
|
await pt_server.start(VERSION)
|
||||||
# work loop
|
# work loop
|
||||||
while True:
|
while True:
|
||||||
|
# persistence
|
||||||
if self.modified_settings and self.persistent:
|
if self.modified_settings and self.persistent:
|
||||||
self.save_settings(self)
|
self.save_settings(self)
|
||||||
self.modified_settings = False
|
self.modified_settings = False
|
||||||
|
if self.persistent:
|
||||||
|
# per-game profiles
|
||||||
|
current_game = pt_server.http_server.game()
|
||||||
|
old_gameid = self.current_gameid
|
||||||
|
if current_game is not None and current_game.has_settings():
|
||||||
|
self.current_gameid = current_game.gameid
|
||||||
|
if old_gameid != self.current_gameid:
|
||||||
|
logging.info(f"Applying custom settings for {current_game.name()} {current_game.appid()}")
|
||||||
|
# new game; apply settings
|
||||||
|
settings = current_game.load_settings()
|
||||||
|
if settings is not None:
|
||||||
|
self.apply_settings(self, settings)
|
||||||
|
else:
|
||||||
|
self.current_gameid = None
|
||||||
|
if old_gameid != self.current_gameid:
|
||||||
|
logging.info("Reapplying default settings; game without custom settings found")
|
||||||
|
# game without custom settings; apply defaults
|
||||||
|
settings = read_json(DEFAULT_SETTINGS_LOCATION)
|
||||||
|
self.apply_settings(self, settings)
|
||||||
|
logging.debug(f"gameid update: {old_gameid} -> {self.current_gameid}")
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
await pt_server.shutdown()
|
await pt_server.shutdown()
|
||||||
|
|
||||||
|
@ -356,8 +352,44 @@ class Plugin:
|
||||||
|
|
||||||
def save_settings(self):
|
def save_settings(self):
|
||||||
settings = self.current_settings(self)
|
settings = self.current_settings(self)
|
||||||
logging.info(f"Saving settings to file: {settings}")
|
logging.debug(f"Saving settings to file: {settings}")
|
||||||
write_json(DEFAULT_SETTINGS_LOCATION, settings)
|
current_game = pt_server.http_server.game()
|
||||||
|
if current_game is not None and self.current_gameid is not None:
|
||||||
|
save_location = current_game.settings_path()
|
||||||
|
else:
|
||||||
|
save_location = DEFAULT_SETTINGS_LOCATION
|
||||||
|
write_json(save_location, settings)
|
||||||
|
logging.info(f"Saved settings to {save_location}")
|
||||||
|
|
||||||
|
def apply_settings(self, settings: dict):
|
||||||
|
# CPU
|
||||||
|
self.cpus = []
|
||||||
|
|
||||||
|
for cpu_number in range(0, Plugin.CPU_COUNT):
|
||||||
|
self.cpus.append(CPU(cpu_number, settings=settings["cpu"]["threads"][cpu_number]))
|
||||||
|
self.smt = settings["cpu"]["smt"]
|
||||||
|
write_cpu_boost(settings["cpu"]["boost"])
|
||||||
|
# GPU
|
||||||
|
write_gpu_ppt(1, settings["gpu"]["slowppt"])
|
||||||
|
write_gpu_ppt(2, settings["gpu"]["fastppt"])
|
||||||
|
# Fan
|
||||||
|
if not (os.path.exists(FANTASTIC_INSTALL_DIR) or settings["fan"]["auto"]):
|
||||||
|
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 1)
|
||||||
|
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", settings["fan"]["target"])
|
||||||
|
|
||||||
|
def guess_settings(self):
|
||||||
|
self.cpus = []
|
||||||
|
for cpu_number in range(0, Plugin.CPU_COUNT):
|
||||||
|
self.cpus.append(CPU(cpu_number))
|
||||||
|
|
||||||
|
# If any core has two threads, smt is True
|
||||||
|
self.smt = self.cpus[1].status()
|
||||||
|
if(not self.smt):
|
||||||
|
for cpu_number in range(2, len(self.cpus), 2):
|
||||||
|
if(self.cpus[cpu_number].status()):
|
||||||
|
self.smt = True
|
||||||
|
break
|
||||||
|
logging.info(f"SMT state is guessed to be {self.smt}")
|
||||||
|
|
||||||
# per-game profiles
|
# per-game profiles
|
||||||
|
|
||||||
|
@ -368,6 +400,17 @@ class Plugin:
|
||||||
else:
|
else:
|
||||||
return f"{current_game.name()} ({current_game.appid()})"
|
return f"{current_game.name()} ({current_game.appid()})"
|
||||||
|
|
||||||
|
async def set_per_game_profile(self, enabled: bool):
|
||||||
|
current_game = pt_server.http_server.game()
|
||||||
|
if enabled and self.persistent and current_game is not None:
|
||||||
|
self.current_gameid = current_game.gameid
|
||||||
|
self.modified_settings = True
|
||||||
|
else:
|
||||||
|
self.current_gameid = None
|
||||||
|
|
||||||
|
async def get_per_game_profile(self) -> bool:
|
||||||
|
return self.current_gameid is not None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# these are stateless (well, the state is not saved internally) functions, so there's no need for these to be called like a class method
|
# these are stateless (well, the state is not saved internally) functions, so there's no need for these to be called like a class method
|
||||||
|
|
106
main_view.html
106
main_view.html
|
@ -82,6 +82,14 @@
|
||||||
return call_plugin_method("get_persistent", {});
|
return call_plugin_method("get_persistent", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setPerGameProfile(value) {
|
||||||
|
return call_plugin_method("set_per_game_profile", {"enabled": value});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPerGameProfile() {
|
||||||
|
return call_plugin_method("get_per_game_profile", {});
|
||||||
|
}
|
||||||
|
|
||||||
function getCurrentGame() {
|
function getCurrentGame() {
|
||||||
return call_plugin_method("get_current_game", {});
|
return call_plugin_method("get_current_game", {});
|
||||||
}
|
}
|
||||||
|
@ -94,7 +102,8 @@
|
||||||
// detect game starts and exits
|
// detect game starts and exits
|
||||||
console.log("Injecting game detection code into main window (SP)");
|
console.log("Injecting game detection code into main window (SP)");
|
||||||
await execute_in_tab("SP", false,
|
await execute_in_tab("SP", false,
|
||||||
`SteamClient.Apps.RegisterForGameActionStart((actionType, data) => {
|
`console.log("Hey PowerTools is over here now too!");
|
||||||
|
SteamClient.Apps.RegisterForGameActionStart((actionType, data) => {
|
||||||
console.log("start game", appStore.GetAppOverviewByGameID(data));
|
console.log("start game", appStore.GetAppOverviewByGameID(data));
|
||||||
fetch("http://127.0.0.1:5030/on_game_start/" + data.toString(), {method: "POST", body: JSON.stringify(appStore.GetAppOverviewByGameID(data))}).then((_) => {});
|
fetch("http://127.0.0.1:5030/on_game_start/" + data.toString(), {method: "POST", body: JSON.stringify(appStore.GetAppOverviewByGameID(data))}).then((_) => {});
|
||||||
});
|
});
|
||||||
|
@ -110,6 +119,35 @@
|
||||||
});*/`
|
});*/`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await updateCurrentGame();
|
||||||
|
/*let boostToggle = document.getElementById("boostToggle");
|
||||||
|
setToggleState(boostToggle, await getCPUBoost());
|
||||||
|
setToggleState(document.getElementById("smtToggle"), await getSMT());
|
||||||
|
selectNotch("cpuThreadsNotch", await getCPUs() - 1, 8);
|
||||||
|
selectNotch("frequencyNotch", await getMaxBoost(), 3);
|
||||||
|
await onReadyGPU();
|
||||||
|
let isFantasticInstalled = await getFantastic();
|
||||||
|
if (isFantasticInstalled) {
|
||||||
|
// Don't fight with Fantastic
|
||||||
|
let fanRoot = document.getElementById("fanRoot");
|
||||||
|
fanRoot.style.visibility = "hidden";
|
||||||
|
fanRoot.style.height = "0px";
|
||||||
|
} else {
|
||||||
|
selectNotch("fanNotch", await getFanTick(), 8);
|
||||||
|
}
|
||||||
|
await updateBatteryStats();
|
||||||
|
setToggleState(document.getElementById("persistToggle"), await getPersistent());
|
||||||
|
setToggleState(document.getElementById("gameProfileToggle"), await getPerGameProfile());
|
||||||
|
await updateCurrentGame();*/
|
||||||
|
// this is unimportant; always do it last
|
||||||
|
await updateVersion();
|
||||||
|
window.setInterval(function() {
|
||||||
|
updateBatteryStats().then(_ => {});
|
||||||
|
updateCurrentGame().then(_ => {});
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function reloadSettings() {
|
||||||
let boostToggle = document.getElementById("boostToggle");
|
let boostToggle = document.getElementById("boostToggle");
|
||||||
setToggleState(boostToggle, await getCPUBoost());
|
setToggleState(boostToggle, await getCPUBoost());
|
||||||
setToggleState(document.getElementById("smtToggle"), await getSMT());
|
setToggleState(document.getElementById("smtToggle"), await getSMT());
|
||||||
|
@ -127,12 +165,7 @@
|
||||||
}
|
}
|
||||||
await updateBatteryStats();
|
await updateBatteryStats();
|
||||||
setToggleState(document.getElementById("persistToggle"), await getPersistent());
|
setToggleState(document.getElementById("persistToggle"), await getPersistent());
|
||||||
// this is unimportant; always do it last
|
setToggleState(document.getElementById("gameProfileToggle"), await getPerGameProfile());
|
||||||
await updateVersion();
|
|
||||||
window.setInterval(function() {
|
|
||||||
updateBatteryStats().then(_ => {});
|
|
||||||
updateCurrentGame().then(_ => {});
|
|
||||||
}, 5000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setCPUNotch(index) {
|
async function setCPUNotch(index) {
|
||||||
|
@ -193,7 +226,7 @@
|
||||||
|
|
||||||
async function onSlideBoostNotch(e) {
|
async function onSlideBoostNotch(e) {
|
||||||
const ROOT_ID = "frequencyNotch";
|
const ROOT_ID = "frequencyNotch";
|
||||||
let closest = closestNotch(e, ROOT_ID, 8);
|
let closest = closestNotch(e, ROOT_ID, 3);
|
||||||
await setBoostNotch(closest);
|
await setBoostNotch(closest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,9 +351,23 @@
|
||||||
setToggleState(toggle, !isActive);
|
setToggleState(toggle, !isActive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function toggleGameProfile() {
|
||||||
|
let toggle = document.getElementById("gameProfileToggle");
|
||||||
|
let isActive = getToggleState(toggle);
|
||||||
|
await setPerGameProfile(!isActive);
|
||||||
|
setToggleState(toggle, await getPerGameProfile());
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastGameName = "";
|
||||||
|
|
||||||
async function updateCurrentGame() {
|
async function updateCurrentGame() {
|
||||||
let gameNow = document.getElementById("gameNow");
|
let gameNow = document.getElementById("gameNow");
|
||||||
let gameNameNow = await getCurrentGame();
|
let gameNameNow = await getCurrentGame();
|
||||||
|
if (lastGameName != gameNameNow) {
|
||||||
|
setToggleState(document.getElementById("gameProfileToggle"), await getPerGameProfile());
|
||||||
|
await reloadSettings();
|
||||||
|
}
|
||||||
|
lastGameName = gameNameNow
|
||||||
gameNow.innerText = gameNameNow;
|
gameNow.innerText = gameNameNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,7 +416,7 @@
|
||||||
|
|
||||||
|
|
||||||
<!-- CPUs selector -->
|
<!-- CPUs selector -->
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">Threads</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">Threads</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -440,12 +487,12 @@
|
||||||
|
|
||||||
|
|
||||||
<!-- Frequency selector -->
|
<!-- Frequency selector -->
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">Max Frequency</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">Max Frequency</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
<div id="frequencyNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.5;">
|
<div id="frequencyNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.5;" onmousemove="onSlideBoostNotch(event)">
|
||||||
<div class="gamepadslider_SliderControl_3o137">
|
<div class="gamepadslider_SliderControl_3o137">
|
||||||
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
||||||
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
||||||
|
@ -485,7 +532,7 @@
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">GPU SlowPPT Power</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">GPU SlowPPT Power</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
<div id="slowPPTNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.33;">
|
<div id="slowPPTNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.33;" onmousemove="onSlideSlowPPTNotch(event)">
|
||||||
<div class="gamepadslider_SliderControl_3o137">
|
<div class="gamepadslider_SliderControl_3o137">
|
||||||
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
||||||
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
||||||
|
@ -511,12 +558,12 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- FastPPT power limit (number 2) -->
|
<!-- FastPPT power limit (number 2) -->
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">GPU FastPPT Power</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">GPU FastPPT Power</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
<div id="fastPPTNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.33;">
|
<div id="fastPPTNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.33;" onmousemove="onSlideFastPPTNotch(event)">
|
||||||
<div class="gamepadslider_SliderControl_3o137">
|
<div class="gamepadslider_SliderControl_3o137">
|
||||||
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
||||||
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
||||||
|
@ -545,12 +592,12 @@
|
||||||
|
|
||||||
<!-- Fan RPM selector -->
|
<!-- Fan RPM selector -->
|
||||||
<!-- TODO: Make this non-notched slider when PluginLoader PR#41 is merged -->
|
<!-- TODO: Make this non-notched slider when PluginLoader PR#41 is merged -->
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" id="fanRoot">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_WithChildrenBelow_1u5FT gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_ChildrenWidthFixed_1ugIU gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" id="fanRoot">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">Fan RPM</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">Fan RPM</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
<div id="fanNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.33;">
|
<div id="fanNotch" class="gamepadslider_SliderControlAndNotches_1Cccx Focusable" tabindex="0" style="--normalized-slider-value:0.33;" onmousemove="onSlideFanNotch(event)">
|
||||||
<div class="gamepadslider_SliderControl_3o137">
|
<div class="gamepadslider_SliderControl_3o137">
|
||||||
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
<div class="gamepadslider_SliderTrack_Mq25N gamepadslider_SliderHasNotches_2XiAy "></div>
|
||||||
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
<div class="gamepadslider_SliderHandleContainer_1pQZi">
|
||||||
|
@ -605,7 +652,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="Panel Focusable" tabindex="0">
|
<div class="Panel Focusable" tabindex="0">
|
||||||
<div class="quickaccesscontrols_PanelSectionRow_2VQ88">
|
<div class="quickaccesscontrols_PanelSectionRow_2VQ88">
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">Now (Charge)</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">Now (Charge)</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
|
@ -615,7 +662,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="quickaccesscontrols_PanelSectionRow_2VQ88">
|
<div class="quickaccesscontrols_PanelSectionRow_2VQ88">
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">Max (Design)</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">Max (Design)</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
|
@ -640,13 +687,26 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="gamepaddialog_FieldDescription_2OJfk">Restores settings after a reboot</div>
|
<div class="gamepaddialog_FieldDescription_2OJfk">Restores settings after an app or OS restart</div>
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
</div>
|
||||||
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_WithDescription_3bMIS gamepaddialog_ExtraPaddingOnChildrenBelow_5UO-_ gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
||||||
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
|
<div class="gamepaddialog_FieldLabel_3b0U-">
|
||||||
|
Use per-game profile
|
||||||
|
</div>
|
||||||
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
|
<div id="gameProfileToggle" tabindex="0" class="gamepaddialog_Toggle_24G4g Focusable" onclick="toggleGameProfile()">
|
||||||
|
<div class="gamepaddialog_ToggleRail_2JtC3"></div>
|
||||||
|
<div class="gamepaddialog_ToggleSwitch_3__OD"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">Now Playing</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">Now Playing</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
<div class="gamepaddialog_LabelFieldValue_5Mylh" id="gameNow"> Littlewood (894940) </div>
|
<div class="gamepaddialog_LabelFieldValue_5Mylh" id="gameNow"> the bongos </div>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -654,7 +714,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="quickaccesscontrols_PanelSection_2C0g0" style="padding:0px 4px;">
|
<div class="quickaccesscontrols_PanelSection_2C0g0" style="padding:0px 4px;">
|
||||||
<div class="quickaccesscontrols_PanelSectionRow_2VQ88" onclick="updateVersion()">
|
<div class="quickaccesscontrols_PanelSectionRow_2VQ88" onclick="updateVersion()">
|
||||||
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparator_1lUZx gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
<div class="gamepaddialog_Field_S-_La gamepaddialog_WithFirstRow_qFXi6 gamepaddialog_VerticalAlignCenter_3XNvA gamepaddialog_InlineWrapShiftsChildrenBelow_pHUb6 gamepaddialog_WithBottomSeparatorStandard_3s1Rk gamepaddialog_StandardPadding_XRBFu gamepaddialog_HighlightOnFocus_wE4V6 Panel Focusable" style="--indent-level:0;">
|
||||||
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
<div class="gamepaddialog_FieldLabelRow_H9WOq">
|
||||||
<div class="gamepaddialog_FieldLabel_3b0U-">PowerTools</div>
|
<div class="gamepaddialog_FieldLabel_3b0U-">PowerTools</div>
|
||||||
<div class="gamepaddialog_FieldChildren_14_HB">
|
<div class="gamepaddialog_FieldChildren_14_HB">
|
||||||
|
|
48
server.py
48
server.py
|
@ -3,8 +3,9 @@ import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
import asyncio
|
# import asyncio
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
HOME_DIR = str(pathlib.Path(os.getcwd()).parent.parent.resolve())
|
HOME_DIR = str(pathlib.Path(os.getcwd()).parent.parent.resolve())
|
||||||
SETTINGS_DIR = HOME_DIR + "/.config/powertools"
|
SETTINGS_DIR = HOME_DIR + "/.config/powertools"
|
||||||
|
@ -14,6 +15,7 @@ if not os.path.exists(SETTINGS_DIR):
|
||||||
|
|
||||||
http_runner = None
|
http_runner = None
|
||||||
http_server = None
|
http_server = None
|
||||||
|
http_site = None
|
||||||
|
|
||||||
class GameInfo:
|
class GameInfo:
|
||||||
def __init__(self, gameid: int, game_info: dict):
|
def __init__(self, gameid: int, game_info: dict):
|
||||||
|
@ -27,17 +29,17 @@ class GameInfo:
|
||||||
return self.game_info["display_name"]
|
return self.game_info["display_name"]
|
||||||
|
|
||||||
def settings_path(self) -> str:
|
def settings_path(self) -> str:
|
||||||
return SETTINGS_DIR + os.pathsep + str(self.appid()) + ".json"
|
return SETTINGS_DIR + os.path.sep + str(self.appid()) + ".json"
|
||||||
|
|
||||||
def load_settings(self) -> dict:
|
def load_settings(self) -> dict:
|
||||||
settings_path = self.settings_path()
|
settings_path = self.settings_path()
|
||||||
if os.exists(settings_path):
|
if os.path.exists(settings_path):
|
||||||
with open(settings_path, mode="r") as f:
|
with open(settings_path, mode="r") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def has_settings(self) -> bool:
|
def has_settings(self) -> bool:
|
||||||
return os.exists(self.settings_path())
|
return os.path.exists(self.settings_path())
|
||||||
|
|
||||||
|
|
||||||
class Server(web.Application):
|
class Server(web.Application):
|
||||||
|
@ -46,28 +48,27 @@ class Server(web.Application):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.version = version
|
self.version = version
|
||||||
self.current_game = None
|
self.current_game = None
|
||||||
self.last_recognised_game = None
|
|
||||||
self.add_routes([
|
self.add_routes([
|
||||||
web.get("/", lambda req: self.index(req)),
|
web.get("/", lambda req: self.index(req)),
|
||||||
web.post("/on_game_start/{game_id}", lambda req: self.on_game_start(req)),
|
web.post("/on_game_start/{game_id}", lambda req: self.on_game_start(req)),
|
||||||
web.post("/on_game_exit/{game_id}", lambda req: self.on_game_exit(req)),
|
web.post("/on_game_exit/{game_id}", lambda req: self.on_game_exit(req)),
|
||||||
web.post("/on_game_exit_null", lambda req: self.on_game_exit_null(req))
|
web.post("/on_game_exit_null", lambda req: self.on_game_exit_null(req)),
|
||||||
|
web.get("/self_destruct", lambda req: self.self_destruct(req))
|
||||||
])
|
])
|
||||||
logging.debug("Server init complete")
|
logging.debug("Server init complete")
|
||||||
|
|
||||||
def game(self) -> GameInfo:
|
def game(self) -> GameInfo:
|
||||||
return self.current_game
|
return self.current_game
|
||||||
|
|
||||||
def recognised_game(self) -> GameInfo:
|
|
||||||
return self.last_recognised_game
|
|
||||||
|
|
||||||
async def index(self, request):
|
async def index(self, request):
|
||||||
logging.debug("Debug index page accessed")
|
logging.debug("Debug index page accessed")
|
||||||
|
current_game = None if self.current_game is None else self.current_game.gameid
|
||||||
|
game_info = None if self.current_game is None else self.current_game.game_info
|
||||||
return web.json_response({
|
return web.json_response({
|
||||||
"name": "PowerTools",
|
"name": "PowerTools",
|
||||||
"version": self.version,
|
"version": self.version,
|
||||||
"latest_game_id": self.current_game,
|
"latest_game_id": current_game,
|
||||||
"latest_recognised_game_id": self.last_recognised_game,
|
"game_info": game_info
|
||||||
}, headers={"Access-Control-Allow-Origin": "*"})
|
}, headers={"Access-Control-Allow-Origin": "*"})
|
||||||
|
|
||||||
async def on_game_start(self, request):
|
async def on_game_start(self, request):
|
||||||
|
@ -80,9 +81,8 @@ class Server(web.Application):
|
||||||
except:
|
except:
|
||||||
return web.Response(text="WTF", status=400)
|
return web.Response(text="WTF", status=400)
|
||||||
self.current_game = GameInfo(game_id, data)
|
self.current_game = GameInfo(game_id, data)
|
||||||
if True: # TODO check for game_id in existing profiles
|
if self.current_game.has_settings():
|
||||||
self.last_recognised_game = self.current_game # only set this when profile exists
|
self.last_recognised_game = self.current_game
|
||||||
# TODO apply profile
|
|
||||||
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||||
|
|
||||||
async def on_game_exit(self, request):
|
async def on_game_exit(self, request):
|
||||||
|
@ -107,9 +107,21 @@ class Server(web.Application):
|
||||||
# TODO change settings to default
|
# TODO change settings to default
|
||||||
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||||
|
|
||||||
|
async def self_destruct(self, request):
|
||||||
|
logging.warning("Geodude self-destructed")
|
||||||
|
await shutdown()
|
||||||
|
# unreachable \/ \/ \/
|
||||||
|
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||||
|
|
||||||
async def start(version):
|
async def start(version):
|
||||||
global http_runner, http_server
|
global http_runner, http_server, http_site
|
||||||
loop = asyncio.get_event_loop()
|
# make sure old server has shutdown
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get('http://127.0.0.1:5030/self_destruct') as response:
|
||||||
|
await response.text()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
http_server = Server(version)
|
http_server = Server(version)
|
||||||
http_runner = web.AppRunner(http_server)
|
http_runner = web.AppRunner(http_server)
|
||||||
await http_runner.setup()
|
await http_runner.setup()
|
||||||
|
@ -117,8 +129,10 @@ async def start(version):
|
||||||
await site.start()
|
await site.start()
|
||||||
|
|
||||||
async def shutdown(): # never really called
|
async def shutdown(): # never really called
|
||||||
global http_runner, http_server
|
global http_runner, http_server, http_site
|
||||||
if http_runner is not None:
|
if http_runner is not None:
|
||||||
await http_runner.cleanup()
|
await http_runner.cleanup()
|
||||||
http_runner = None
|
http_runner = None
|
||||||
|
http_site.stop()
|
||||||
|
http_site = None
|
||||||
http_server = None
|
http_server = None
|
||||||
|
|
Loading…
Reference in a new issue