Skip to content

Commit

Permalink
Use GAppInfo to launch applications
Browse files Browse the repository at this point in the history
This removes the need to rely on hacks like double-fork. It also fixes
an issue where certain windows launched through mate-menu are not
immediately in focus.
  • Loading branch information
vkareh authored and flexiondotorg committed Mar 23, 2020
1 parent 88e846c commit 6e9dce4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
45 changes: 24 additions & 21 deletions mate_menu/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import shlex
import subprocess

from gi.repository import Gdk, Gtk, Gio

def RemoveArgs(Execline):
NewExecline = []
Expand All @@ -33,21 +33,34 @@ def RemoveArgs(Execline):
NewExecline.append(elem)
return NewExecline

# Actually execute the command
def ExecuteCommand(cmd , commandCwd=None):
if not commandCwd:
cwd = os.path.expanduser( "~" );
else:
# Actually launch the application
def Launch(cmd, cwd=None):
if cwd:
cmd = cwd + ' ' + cmd

app_info = Gio.AppInfo.create_from_commandline(cmd,
None,
Gio.AppInfoCreateFlags.SUPPORTS_STARTUP_NOTIFICATION)

display = Gdk.Display.get_default()
context = display.get_app_launch_context()
context.set_desktop(-1) # use default screen & desktop
context.set_timestamp(Gtk.get_current_event_time())

app_info.launch(None, context)

def Execute(cmd , commandCwd=None):
if commandCwd:
tmpCwd = os.path.expanduser( commandCwd );
if (os.path.exists(tmpCwd)):
cwd = tmpCwd
else:
cwd = None

if isinstance( cmd, str ):
if (cmd.find("/home/") >= 0) or (cmd.find("xdg-su") >= 0) or (cmd.find("\"") >= 0):
print("running manually...")
try:
os.chdir(cwd)
subprocess.Popen(shlex.split(cmd))
Launch(cmd, cwd)
return True
except Exception as detail:
print(detail)
Expand All @@ -56,19 +69,9 @@ def ExecuteCommand(cmd , commandCwd=None):
cmd = RemoveArgs(cmd)

try:
os.chdir( cwd )
string = ' '.join(cmd)
subprocess.Popen(shlex.split(string))
Launch(string, cwd)
return True
except Exception as detail:
print(detail)
return False

# Execute cmd using the double fork method
def Execute(cmd, commandCwd=None):
child_pid = os.fork()
if child_pid == 0:
ExecuteCommand(cmd, commandCwd)
os._exit(0)
else:
os.wait()
1 change: 0 additions & 1 deletion mate_menu/plugins/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import subprocess
import filecmp
from mate_menu.easybuttons import *
from mate_menu.execute import Execute
from mate_menu.easygsettings import EasyGSettings
from mate_menu.easyfiles import *

Expand Down

0 comments on commit 6e9dce4

Please sign in to comment.