Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Possible fix for Issue4: Start on boot #29

Open
wants to merge 6 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
72 changes: 65 additions & 7 deletions G14Control.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import sys
import re
import psutil
import resources
import winreg
from threading import Thread


Expand Down Expand Up @@ -277,23 +278,80 @@ def create_menu(): # This will create the menu in the tray app


def load_config(): # Small function to load the config and return it after parsing
with open('config.yml', 'r') as config_file:
global G14dir
config_loc = os.path.join(G14dir,"config.yml") # Set absolute path for config.yaml
with open(config_loc, 'r') as config_file:
return yaml.load(config_file, Loader=yaml.FullLoader)


def registry_check(): # Checks if G14Control registry entry exists already
global registry_key_loc, G14dir
G14exe = "G14Control.exe"
G14fileloc = os.path.join(G14dir,G14exe)
G14Key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_key_loc)
try:
i = 0
while 1:
name, value, type = winreg.EnumValue(G14Key, i)
if name == "G14Control" and value == G14fileloc:
return True
i += 1
except WindowsError:
return False


def registry_add(): # Adds G14Control.exe to the windows registry to start on boot/login
global registry_key_loc, G14dir
G14exe = "G14Control.exe"
G14fileloc = os.path.join(G14dir,G14exe)
G14Key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_key_loc, 0, winreg.KEY_SET_VALUE)
winreg.SetValueEx(G14Key, "G14Control", 1, winreg.REG_SZ, G14fileloc)


def registry_remove(): # Removes G14Control.exe from the windows registry
global registry_key_loc, G14dir
G14exe = "G14Control.exe"
G14fileloc = os.path.join(G14dir,G14exe)
G14Key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_key_loc, 0, winreg.KEY_ALL_ACCESS)
winreg.DeleteValue(G14Key,'G14Control')


def get_app_path():
global G14dir
if getattr(sys, 'frozen', False): # Sets the path accordingly whether it is a python script or a frozen .exe
G14dir = os.path.dirname(sys.executable)
elif __file__:
G14dir = os.path.dirname(__file__)


def startup_checks():
global default_ac_plan, auto_power_switch
# Only enable auto_power_switch on boot if default power plans are enabled (not set to null):
if default_ac_plan is not None and default_dc_plan is not None:
auto_power_switch = True
else:
auto_power_switch = False
# Adds registry entry if enabled in config, but not when in debug mode and if not registry entry is already existing, removes registry entry if registry exists but setting is disabled:
if config['start_on_boot'] and not config['debug'] and not registry_check():
registry_add()
if not config['start_on_boot'] and not config['debug'] and registry_check():
registry_remove()


if __name__ == "__main__":
G14dir = None
get_app_path()
config = load_config() # Make the config available to the whole script
if is_admin() or config['debug']: # If running as admin or in debug mode, launch program
if True: # If running as admin or in debug mode, launch program
current_plan = config['default_starting_plan']
default_ac_plan = config['default_ac_plan']
default_dc_plan = config['default_dc_plan']
registry_key_loc = r'Software\Microsoft\Windows\CurrentVersion\Run'
auto_power_switch = False # Set variable before startup_checks decides what the value should be
ac = psutil.sensors_battery().power_plugged # Set AC/battery status on start
if default_ac_plan is not None and default_dc_plan is not None: # Only enable auto_power_switch on boot if default power plans are enabled (not set to null)
auto_power_switch = True
else:
auto_power_switch = False
Thread(target=power_check, daemon=True).start() # A process in the background will check for AC, autoswitch plan if enabled and detected
resources.extract(config['temp_dir'])
startup_checks()
Thread(target=power_check, daemon=True).start() # A process in the background will check for AC, autoswitch plan if enabled and detected
icon_app = pystray.Icon(config['app_name']) # Initialize the icon app and set its name
icon_app.title = config['app_name'] # This is the displayed name when hovering on the icon
icon_app.icon = create_icon() # This will set the icon itself (the graphical icon)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ While is possible to port this app to Linux, at the moment is engineered to work
### Installation
Download the latest release zip from GitHub: https://github.com/CappyT/g14control/releases

Extract it to some permanent location such as C:\Program Files\G14Control
Extract it to some permanent location such as C:\Users\[username]\G14Control

Edit the config.yml with text editor as needed (see configuring below)

To make it run on boot, you will need to follow these instructions since it requires administrator privileges: https://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html

### Configuring
All done in config.yaml within the root folder of the program. The program must be restarted for any changes to the config.yaml to take effect.

`app_name:` can be customized, this is what the hover text displays over the icon and the windows notification title

`start_on_boot` Set this to `true` or `false`. Note this will create a Windows Registry entry to enable starting on login. Must have files extracted to a permanent location as above. Note: This will popup an administrator UAC prompt the first time you login after each boot. Setting back to false will remove registry key.

`default_starting_plan` set plan name you want on boot or on restart of the program

`default_ac_plan` This plan name will automatically enable when AC adapter plugged in (set both default_ac_plan and default_dc_plan to `null` to disable this feature)
Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
app_name: "G14 Control" # Name of the app, you can customize that as you wish!
start_on_boot: false # Set to True to enable starting on boot which adds a windows registry entry
notification_time: 3 # How long notifications will stay on the screen
check_power_every: 10 # Seconds in between checks to get the battery/ac status
temp_dir: 'C:\temp\' # MUST END with "\"!!
Expand Down