summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config/mv_screensaver.py
blob: 4068832fd5a81aa600b43d9d0aa54aa9fb2da309 (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
import logging, mv_common
import re
import os


def mplayer_saver_check (cmd,templatefile):
    logging.debug("    Checking for heartbeat command in mplayer.conf")
    mplayerconf = "/etc/mplayer/mplayer.conf"
    if not os.path.exists(mplayerconf):
        logging.debug("    copying in mplayer template file")
        mv_common.cp_and_log(templatefile, mplayerconf)

    try:
        f = open(mplayerconf, 'r')
        mplayerconf_contents = f.readlines()
        f.close()
    except:
        logging.debug("    Couldn't open %s for reading",mplayerconf)
        mplayerconf_contents = "#\n"

    try:
        f = open(mplayerconf, 'w')
    except:
        logging.debug("    Couldn't open %s for writing",mplayerconf)
        return

    for line in mplayerconf_contents:
        if re.match("^.*stop-xscreensaver",line):
            continue
        elif re.match("^.*heartbeat-cmd",line):
            continue
        else:
            f.write(line)

    f.write("stop-xscreensaver = 0\n")
    line='''heartbeat-cmd="%s"\n''' %cmd
    f.write(line)
    f.close()


def killxscreensaver():
    logging.info("   Stopping xscreensaver")
    cmd="xscreensaver-command --exit "
    mv_common.runcmd(cmd)
    cmd="killall xscreensaver "
    mv_common.runcmd(cmd)

def killgscreensaver():
    logging.info("    Stopping gnome-screensaver")
    cmd="gnome-screensaver-command --exit "
    mv_common.runcmd(cmd)
    cmd="killall gnome-screensaver "
    mv_common.runcmd(cmd)


def setup_x_screensaver(idle,theme,screenconfigfile,templatefile):
    logging.info("    Configuring X screensaver")
    killgscreensaver()
    logging.debug("    Why can't xscreensaver have a nice configuration !")

    if not os.path.exists(screenconfigfile):
        logging.debug("   copying in new template file")
        mv_common.cp_and_log(templatefile,screenconfigfile)

    try:
        f = open(screenconfigfile, 'r')
        screenconfigcontents=f.readlines()
        f.close()
    except:
        logging.info("Couldn't open %s for reading",screenconfigfile)
        return
    try:
        f = open(screenconfigfile, 'w')
    except:
        logging.info("Couldn't open %s for writing",screenconfigfile)
        return

    startline=0
    foundline=0

    for k, line in enumerate(screenconfigcontents):
        if re.match("programs:",line):
            startline = k
        if re.search(theme,line):
            foundline = k
    themenumber=foundline-startline-2

    for line in screenconfigcontents:
        if re.match("selected.*$",line):
            continue

        if re.match("timeout",line):
            logging.info("    Setting idletime to %s",idle)
            outline = "timeout:   0:%s:00 \n" %idle
            f.write(outline)
        elif re.match("mode",line):
            if theme == "Random":
                logging.info("    Setting theme to %s", "Random")
                outline = "mode:    random \n"
                f.write(outline)
                outline = "selected:    -1 \n"
                f.write(outline)
                continue

            elif theme == "Blank":
                logging.info("    Setting theme to %s", "blank")
                outline = "mode:    blank \n"
                f.write(outline)
                outline = "selected:    -1 \n"
                f.write(outline)
                continue

            else:
                logging.info("    Setting theme to %s", theme)
                outline = "mode:    one\n"
                f.write(outline)
                logging.debug("    Theme number is %s",themenumber)
                outline = "selected:    %s\n" %themenumber
                f.write(outline)
                continue

        else:
            outline = line
            f.write(outline)
    f.close()
    logging.debug("__End of xscreensaver")

def setup_gnome_screensaver(theme,idletime):
    logging.info("    Configuring gnome screensaver")

    logging.info("    Disabling lock screen")
    cmd='''gconftool-2 --direct   --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory  --type bool   --set /apps/gnome-screensaver/lock_enabled false'''
    mv_common.runcmd(cmd)

    logging.info("    Setting idletime to %s",idletime)
    cmd='''gconftool-2 --direct  --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory  --type int -s "/apps/gnome-screensaver/idle_delay" %s''' %idletime
    mv_common.runcmd(cmd)

    logging.info("    Setting mode to single")
    cmd='''gconftool-2 --direct  --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory  --type string -s "/apps/gnome-screensaver/mode" single'''
    mv_common.runcmd(cmd)

    if theme == "Random":
        logging.info("    setting theme to random")
        cmd='''gconftool-2 --direct  --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory  --type string -s "/apps/gnome-screensaver/mode" random'''
        mv_common.runcmd(cmd)

    elif theme == "Blank":
        logging.info("    setting theme to blank")
        cmd='''gconftool-2 --direct  --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory  --type string -s "/apps/gnome-screensaver/themes" blank'''
        mv_common.runcmd(cmd)

    else:
        logging.info("    setting theme to %s",theme)
        cmd='''gconftool-2  --direct  --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory --set --type list --list-type=string /apps/gnome-screensaver/themes [screensavers-%s]''' %theme
        mv_common.runcmd(cmd)

    killxscreensaver()
    logging.info("__End gnome screensaver")




def setup_screensaver (systemconfig,data_config):
    if  mv_common.read_config(mv_common.module_config,"screensaver")  == False  :
        logging.info("____Skipping of Screensaver, config disabled____")
        return
    logging.info("____Start of screensaver configuration____")
    screensavertype = systemconfig["Screensavertype"]
    theme = systemconfig["Screensavertheme"]
    idletime = systemconfig["Screensaveridle"]
    configfile = data_config.MYTHHOME+"/.xscreensaver"
    templatefile = systemconfig["TEMPLATES"]+"/xscreensaver.template"
    mtemplate = systemconfig["TEMPLATES"]+"/mplayer.conf.template"

    if screensavertype == "xscreensaver":
        mv_common.pacinstall("xscreensaver")
        mv_common.pacremove("gnome-screensaver")
        setup_x_screensaver(idletime, theme, configfile, templatefile)
        mplayer_saver_check("/usr/bin/xscreensaver-command -deactivate &",mtemplate)

    elif screensavertype == "gscreensaver":
        mv_common.pacinstall("xscreensaver")
        mv_common.pacinstall("gnome-screensaver")
        setup_gnome_screensaver(theme,idletime)
        mplayer_saver_check("/usr/bin/gnome-screensaver-command -p &",mtemplate)

    elif screensavertype == "none":
        mv_common.pacremove("xscreensaver")
        mv_common.pacremove("gnome-screensaver")
        killgscreensaver()
        killxscreensaver()

    elif screensavertype == "tinker" :
        logging.debug("    screensaver tinker mode detected")
    else:
        logging.debug("   Unknown screensaver")
    logging.info("__End of screensaver\n")