/usr/lib/python3/dist-packages/sgtlauncher/SgtSocketLauncher.py is in sgt-launcher 0.2.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import struct
import subprocess
import sys
import time
import xcffib
import xcffib.xproto
from gi.repository import Gtk
class SgtSocketLauncher:
def __init__(self):
self.socket = Gtk.Socket.new()
self.socket.set_can_focus(True)
self.socket.set_focus_on_click(True)
self.socket.set_receives_default(True)
def get_socket(self):
return self.socket
def launch(self, path, wm_class=None):
if wm_class is None:
wm_class = path
retry_count = 10
retry_timer = 0.05
proc = subprocess.Popen([path])
count = 0
window_id = 0
while window_id == 0:
proc.poll()
if proc.returncode is not None:
return False
window_id = get_window_id("_NET_WM_PID", proc.pid)
if window_id == 0:
window_id = get_window_id("WM_CLASS", wm_class)
if window_id == 0:
count += 1
if count == retry_count:
return False
time.sleep(retry_timer)
count = 0
while True:
proc.poll()
if proc.returncode is not None:
return False
plug_count = 0
while self.socket.get_plug_window() is None:
self.socket.add_id(window_id)
if self.socket.get_plug_window() is None:
plug_count += 1
if plug_count == retry_count:
return False
time.sleep(retry_timer)
if self.socket.get_plug_window().get_xid() == window_id:
proc.poll()
if proc.returncode is not None:
return
return True
count += 1
if count == retry_count:
return False
time.sleep(retry_timer)
return False
def get_window_id(prop, value):
window_id = 0
c = xcffib.connect()
root = c.get_setup().roots[0].root
_NET_CLIENT_LIST = c.core.InternAtom(True, len('_NET_CLIENT_LIST'),
'_NET_CLIENT_LIST').reply().atom
raw_clientlist = c.core.GetProperty(False, root, _NET_CLIENT_LIST,
xcffib.xproto.GetPropertyType.Any,
0, 2 ** 32 - 1).reply()
clientlist = get_property_value(raw_clientlist)
cookies = {}
for ident in clientlist:
if prop in dir(xcffib.xproto.Atom):
atom = getattr(xcffib.xproto.Atom, prop)
else:
atom = c.core.InternAtom(True, len(prop), prop).reply().atom
cookies[ident] = c.core.GetProperty(False, ident, atom,
xcffib.xproto.GetPropertyType.Any,
0, 2 ** 32 - 1)
for ident in cookies:
winclass = get_property_value(cookies[ident].reply())
if isinstance(winclass, list):
if value in winclass:
window_id = ident
break
c.disconnect()
return window_id
def get_property_value(property_reply):
assert isinstance(property_reply, xcffib.xproto.GetPropertyReply)
if property_reply.format == 8:
if 0 in property_reply.value:
ret = []
s = ''
for o in property_reply.value:
if o == 0:
ret.append(s)
s = ''
else:
s += chr(o)
else:
ret = str(property_reply.value.buf().replace(b'\x00', b'\t'),
"utf-8")
ret = ret.split("\t")
return ret
elif property_reply.format in (16, 32):
return list(struct.unpack('I' * property_reply.value_len,
property_reply.value.buf()))
return None
|