Skip to content

Instantly share code, notes, and snippets.

@flexiondotorg
Created January 22, 2018 00:44
Show Gist options
  • Save flexiondotorg/c5df13cca7ef1a23702be899059a040d to your computer and use it in GitHub Desktop.
Save flexiondotorg/c5df13cca7ef1a23702be899059a040d to your computer and use it in GitHub Desktop.
A meta window manager to set the default window manager for Ubuntu MATE. Runs standalone but not when the session is starting up.
#!/usr/bin/env python3
# Copyright (C) 2018 Martin Wimpress <code@ubuntu-mate.org>
#
# 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 2 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 warranty of
# MERCHANTABILITY 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, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
import gi
import os
import platform
import psutil
import setproctitle
import signal
import subprocess
import sys
from subprocess import DEVNULL, PIPE
from gi.repository import GLib
from gi.repository import Gio
from gi.repository import GObject
# Workaround introspection bug, gnome bug 622084
signal.signal(signal.SIGINT, signal.SIG_DFL)
def schema_has_key(schema, key):
settings=Gio.Settings.new(schema)
schema = settings.get_property('settings-schema')
return schema.has_key(key)
def set_string(schema, path, key, value):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
try:
settings.set_string(key, value)
except:
print('Unable set ' + key + ' with ' + value + ' in ' + schema)
pass
def get_string(schema, path, key):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
return settings.get_string(key)
def get_int(schema, path, key):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
return settings.get_int(key)
def set_int(schema, path, key, value):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
settings.set_int(key, value)
def get_enum(schema, path, key):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
return settings.get_enum(key)
def set_enum(schema, path, key, value):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
settings.set_enum(key, value)
def get_bool(schema, path, key):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
return settings.get_boolean(key)
def set_bool(schema, path, key, value):
if path:
settings = Gio.Settings.new_with_path(schema, path)
else:
settings = Gio.Settings.new(schema)
settings.set_boolean(key, value)
def reset_dconf_path(path):
subprocess.call(['dconf', 'reset', '-f', path], stdout=DEVNULL, stderr=DEVNULL)
def set_dconf_value(path, value):
subprocess.call(['dconf', 'write', path, value], stdout=DEVNULL, stderr=DEVNULL)
def get_dconf_value(path):
dconf = subprocess.Popen(['dconf', 'read', path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
dconf_output, dconf_error = dconf.communicate()
return dconf_output.decode().strip()
def process_running(name):
uid = os.getuid()
for process in psutil.process_iter():
try:
proc = process.as_dict(attrs=['name', 'uids'])
except psutil.NoSuchProcess:
pass
else:
if name == proc['name'] and uid == proc['uids'].real:
return True
return False
def kill_process(name):
uid = os.getuid()
for process in psutil.process_iter():
try:
proc = process.as_dict(attrs=['name', 'pid', 'uids'])
except psutil.NoSuchProcess:
pass
else:
if name == proc['name'] and uid == proc['uids'].real:
try:
target = psutil.Process(proc['pid'])
target.kill()
except psutil.NoSuchProcess:
pass
def find_on_path(command):
"""Is command on the executable search path?"""
if 'PATH' not in os.environ:
return False
path = os.environ['PATH']
for element in path.split(os.pathsep):
if not element:
continue
filename = os.path.join(element, command)
if os.path.isfile(filename) and os.access(filename, os.X_OK):
return True
return False
def replace_windowmanager(new_wm):
print('Enabling ' + new_wm)
#if new_wm == 'marco':
# wm_params = '--composite'
#elif new_wm == 'compiz':
# wm_params = '--keep-desktop-hints'
# Make sure Compiz use the same theme as Marco to
# prevent theme/xcursor changes when switching themes.
if (new_wm == 'compiz'):
mate_theme = get_string('org.mate.interface', None, 'gtk-theme')
mate_cursor_theme = get_string('org.mate.peripherals-mouse', None, 'cursor-theme')
mate_cursor_size = get_int('org.mate.peripherals-mouse', None, 'cursor-size')
schemasource = Gio.SettingsSchemaSource.get_default()
gnome_desktop_schema = schemasource.lookup('org.gnome.desktop', False)
if gnome_desktop_schema:
set_string('org.gnome.desktop.wm.preferences', None, 'theme', mate_theme)
set_string('org.gnome.desktop.interface', None, 'cursor-theme', mate_cursor_theme)
set_int('org.gnome.desktop.interface', None, 'cursor-size', mate_cursor_size)
# metacity >= 3.20 - this schema may not be installed
metacity_schema = schemasource.lookup('org.gnome.metacity.theme', False)
if metacity_schema:
set_string('org.gnome.metacity.theme', None, 'name', mate_theme)
# FIXME! Don't assume 'metacity' is 1
set_enum('org.gnome.metacity.theme', None, 'type', 1)
if process_running(new_wm):
kill_process(new_wm)
set_string('org.mate.session.required-components', None, 'windowmanager', new_wm)
pid = subprocess.Popen([new_wm, '--replace'], stdout=DEVNULL, stderr=DEVNULL).pid
return pid
def check_glx_features():
if find_on_path('glxinfo'):
process = subprocess.Popen(['glxinfo'], stdout=PIPE)
out = process.communicate()[0].decode("UTF-8")
if out.count("Software Rasterizer") == 0:
software_rasterizer = False
else:
software_rasterizer = True
if out.count("texture_from_pixmap") > 2:
texture_from_pixmap = True
else:
texture_from_pixmap = False
else:
software_rasterizer = False
texture_from_pixmap = False
return software_rasterizer, texture_from_pixmap
if __name__ == "__main__":
setproctitle.setproctitle('mate-meta-wm')
# The most basic check, can we actually change the default window manager?
if schema_has_key('org.mate.session.required-components', 'windowmanager'):
# Only change things if mate-meta-wm is the configured window manager.
current_wm = get_string('org.mate.session.required-components', None, 'windowmanager')
if current_wm == 'mate-meta-wm':
software_rasterizer, texture_from_pixmap = check_glx_features()
compiz_capable = False
marco_capable = False
marco_no_composite_capable = False
if find_on_path('compiz'):
if not software_rasterizer and texture_from_pixmap:
compiz_capable = True
if find_on_path('marco'):
marco_capable = True
if compiz_capable:
pid = replace_windowmanager('compiz')
print('Compiz is PID: ' + str(pid))
else:
pid = replace_windowmanager('marco')
print('Marco is PID: ' + str(pid))
else:
print(current_wm + ' is already configured. Doing nothing.')
else:
print('Could not find the MATE schema. I have no power here.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment