Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/scripts/xinitrc
blob: bf99b9060ce76145ebab806026e8a0353215bddb (plain)
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
#!/usr/bin/python

import os
import subprocess
import signal
import sys
import shlex

from gi.repository import SugarRunner

devnull = open("/dev/null")
main_loop = None

_SUGAR_ARGS = ["sugar"]


def _setup_outputs():
    """Sugar doesn't handle multiple outputs properly. To avoid issues
       we keep the first output in the list returned by xrandr and turn
       off everything else.
    """
    outputs = SugarRunner.list_outputs()
    if not outputs:
        return

    on_output = os.environ.get("SUGAR_RUNNER_OUTPUT", outputs[0])
    for output in outputs:
        if output != on_output:
            subprocess.check_call(["xrandr", "--output", output, "--off"])


def _load_xkb_config():
    if "SUGAR_RUNNER_XKBCONFIG" not in os.environ:
        return

    with open(os.environ["SUGAR_RUNNER_XKBCONFIG"]) as f:
        config = f.read()

    process = subprocess.Popen(["xkbcomp", "-", os.environ["DISPLAY"]],
                               stdin=subprocess.PIPE, stdout=devnull)
    process.communicate(input=config)


def _child_watch_cb(pid, condition):
    window_pid = os.environ.get("SUGAR_RUNNER_WINDOW_PID", None)
    if window_pid:
        os.kill(int(window_pid), signal.SIGTERM)

    global main_loop
    main_loop.quit()


def _add_output_to_environ(output):
    for line in output.strip().split("\n"):
        splitted = line.strip().split("=", 1)
        if len(splitted) == 2:
            os.environ[splitted[0]] = splitted[1]


def _start_dbus():
    output = subprocess.check_output(["dbus-launch", "--exit-with-session"])
    _add_output_to_environ(output)


def _start_keyring():
    output = subprocess.check_output(["gnome-keyring-daemon",
                                      "--start", "-d",
                                      "--components=secrets,pkcs11,ssh,gpg"])
    _add_output_to_environ(output)


def _run_sugar():
    subprocess.check_call(_SUGAR_ARGS)


def _run_sugar_and_test(test_command):
    sugar_process = subprocess.Popen(_SUGAR_ARGS)
    result = subprocess.call(shlex.split(test_command))
    sugar_process.terminate()

    return result == 0


def _setup_xdg_user_dirs():
    subprocess.check_call("xdg-user-dirs-update")


_setup_outputs()
_setup_xdg_user_dirs()
_load_xkb_config()
_start_dbus()
_start_keyring()

test_command = os.environ.get("SUGAR_RUNNER_TEST_COMMAND", None)
if test_command:
    if not _run_sugar_and_test(test_command):
        os.kill(int(os.environ["SUGAR_RUNNER_PID"]), signal.SIGUSR1)
else:
    _run_sugar()

os.kill(int(os.environ["GNOME_KEYRING_PID"]), signal.SIGTERM)