Skip to content

Commit

Permalink
config should now be able to be created in exe directory instead of s…
Browse files Browse the repository at this point in the history
…ystem32
  • Loading branch information
F0903 committed Sep 10, 2024
1 parent 33e3840 commit 07f19d2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 135 deletions.
133 changes: 34 additions & 99 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,101 +1,36 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'autopower_shared'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=autopower_shared"
],
"filter": {
"name": "autopower_shared",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'autopower_notification_provider'",
"cargo": {
"args": [
"build",
"--bin=autopower_notification_provider",
"--package=autopower_notification_provider"
],
"filter": {
"name": "autopower_notification_provider",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'autopower_notification_provider'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=autopower_notification_provider",
"--package=autopower_notification_provider"
],
"filter": {
"name": "autopower_notification_provider",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'autopower'",
"cargo": {
"args": [
"build",
"--bin=autopower",
"--package=autopower"
],
"filter": {
"name": "autopower",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'autopower'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=autopower",
"--package=autopower"
],
"filter": {
"name": "autopower",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'autopower_proxy'",
"cargo": {
"args": ["build", "--bin=autopower_proxy", "--package=autopower_proxy"],
"filter": {
"name": "autopower_proxy",
"kind": "bin"
}
]
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'autopower'",
"cargo": {
"args": ["build", "--bin=autopower", "--package=autopower"],
"filter": {
"name": "autopower",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ windows = { version = "^0.58", features = [
"Win32_System_Com",
"Win32_System_Console",
"Win32_Graphics_Gdi",
"Win32_System_LibraryLoader",
] }

[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO

- Rewrite as a normal background process. The current way is too limiting due to session 0 isolation, and strange behaviour with some APIs.
- Start the proxy process from the service.
- Scrap the PS1 scripts and install/uninstall directly from the main exe.
14 changes: 0 additions & 14 deletions autopower_proxy/src/config/config_error.rs

This file was deleted.

32 changes: 14 additions & 18 deletions autopower_proxy/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
mod config_error;
mod power_scheme;
mod state_config;

pub use config_error::ConfigError;
pub use power_scheme::PowerScheme;
use state_config::StateConfig;

use crate::display::RefreshRateMode;
use autopower_shared::logging::Logger;
use autopower_shared::{logging::Logger, util::get_process_exe_path};
use serde::{Deserialize, Serialize};
use std::{
fs::File,
io::{BufReader, BufWriter, Write},
path::{Path, PathBuf},
path::Path,
};

const LOGGER: Logger = Logger::new("power_config", "autopower_proxy");

type Result<T> = crate::Result<T>;

#[derive(Serialize, Deserialize, Debug)]
pub struct PowerConfig {
wired_config: StateConfig,
Expand Down Expand Up @@ -45,30 +45,26 @@ impl Default for PowerConfig {
}

impl PowerConfig {
fn get(path: &Path) -> Result<Self, ConfigError> {
fn get(path: &Path) -> Result<Self> {
LOGGER.debug(format!("Reading power config at {}", path.display()));

let fs = File::open(path).map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
let fs = File::open(path)?;
let buf = BufReader::new(fs);
serde_json::from_reader(buf).map_err(|_| ConfigError::CouldNotLoadOrCreate)
serde_json::from_reader(buf).map_err(|e| e.into())
}

fn new(path: &Path) -> Result<Self, ConfigError> {
fn new(path: &Path) -> Result<Self> {
LOGGER.debug(format!("Writing new power config at {}", path.display()));
let new_config = PowerConfig::default();
let fs = File::create(path).map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
let fs = File::create(path)?;
let mut buf = BufWriter::new(fs);
serde_json::to_writer_pretty(&mut buf, &new_config)
.map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
buf.flush().map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
serde_json::to_writer_pretty(&mut buf, &new_config)?;
buf.flush()?;
Ok(new_config)
}

pub fn get_or_create() -> Result<Self, ConfigError> {
const CONFIG_PATH: &str = "./config.json";
let path = PathBuf::from(CONFIG_PATH)
.canonicalize()
.map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
pub fn get_or_create() -> Result<Self> {
const CONFIG_FILE: &str = "config.json";
let path = get_process_exe_path()?.with_file_name(CONFIG_FILE);
Self::get(&path).or_else(|_| Self::new(&path))
}

Expand Down
2 changes: 1 addition & 1 deletion autopower_proxy/src/config/state_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use serde::{Deserialize, Serialize};
use windows::Win32::System::Power::PowerSetActiveScheme;

type Result<T> = crate::Result<T>; // Perhaps make a custom error in the future.
type Result<T> = crate::Result<T>;

#[derive(Serialize, Deserialize, Debug)]
pub struct StateConfig {
Expand Down
25 changes: 24 additions & 1 deletion shared/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::{ffi::OsStr, path::PathBuf};

use windows::{
core::PWSTR,
Win32::{
Foundation::GetLastError,
System::Diagnostics::Debug::{FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM},
System::{
Diagnostics::Debug::{FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM},
LibraryLoader::GetModuleFileNameA,
},
},
};

Expand All @@ -24,3 +29,21 @@ pub fn get_last_win32_err() -> super::Result<String> {
let str = unsafe { buf.to_string()? };
return Ok(str[..count as usize].to_owned());
}

pub fn get_process_exe_path() -> super::Result<PathBuf> {
let mut buf = [0; 512];
unsafe {
let count = GetModuleFileNameA(None, &mut buf);
let count_usize = count as usize;
if count_usize == buf.len() {
return Err("Process path is too long!".into());
}
if buf[count_usize] != b'\0' {
return Err(
"Process path buffer did not end with a null terminator, possible overflow.".into(),
);
}
let os_str = OsStr::from_encoded_bytes_unchecked(&buf[0..(count_usize)]);
Ok(PathBuf::from(os_str))
}
}
2 changes: 1 addition & 1 deletion shared/src/winstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Win32StrPtr<u16> {
}

pub fn from_buffer(data: Vec<u16>) -> Self {
Self { data: data }
Self { data }
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/services/power_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ impl PowerService {
event_data: *mut c_void,
_context: *mut c_void,
) -> u32 {
// DO NOT DROP, will be dropped in service_main on exit.
let mut me = ManuallyDrop::new(_context.cast::<Self>().read());
let data = HandlerData {
event_type,
event_data,
};

// Win32 docs say to start new thread for any other work than returning immediately
std::thread::spawn(move || {
match ctrl_code {
Expand Down

0 comments on commit 07f19d2

Please sign in to comment.