summaryrefslogtreecommitdiffstats
path: root/monitor.py
blob: f7378f4c3d785ab9a49e7e41a6cab5454f497103 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#
# monitor.py - monitor probing and install data
#
# Mike Fulbright <msf@redhat.com>
#
# Copyright 2001 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import string
import kudzu
import iutil
import isys
from translate import _
from log import log

def isValidSyncRange(syncrange):

    def isValidPiece(piece):
        tmp = string.split(piece, "-")
        if len(tmp) > 2:
            return 0

        for i in tmp:
            try:
                tmp2 = float(i)
            except ValueError:
                return 0
            
        return 1

    pieces = string.split(syncrange, ",")
    for piece in pieces:
        if not isValidPiece(piece):
            return 0

    return 1



class MonitorInfo:

#
#  This class represents the monitor on the system. Values from ddcprobing
#  are provided if available.  LCDs are not currently probed.
#
#  Internal members (use methods to access):
#
#     monEisa     - probed monitor ID  (string)
#     monName     - human readable description (string)
#     monID       - human readable ID (string)
#     fbmonSect   - if framebuffer running, monitor ID (string)
#     monHoriz    - horizontal rating (kHz)
#     monVert     - vertical rating (Hz)
#
    def readMonitorsDB (self, lines = None):
        if self.monlist:
            return self.monlist
        if not lines:
            db = open ('/usr/X11R6/share/Xconfigurator/MonitorsDB')
            lines = db.readlines ()
            db.close ()

        for line in lines:
            line = string.strip (line)
            if not line:
                continue
            if line and line[0] == '#':
                continue
            fields = string.split (line, ';')
            man = string.strip(fields[0])
            model = string.strip(fields[1])
            eisa = string.lower(string.strip(fields[2]))
            horiz = string.strip(fields[3])
            vert = string.strip(fields[4])
            if self.monlist.has_key(man):
                self.monlist[man].append((model, eisa, vert, horiz))
            else:
                self.monlist[man] = [(model, eisa, vert, horiz)]
            self.monids[eisa] = (man, model, eisa, vert, horiz)
        return self.monlist

    def monitorsDB(self):
        if not self.monlist:
            self.readMonitorsDB()

        return self.monlist

    def monitorsEISADB(self):
        if not self.monlist:
            self.readMonitorsDB()

        return self.monids

    def lookupMonitor(self, monID):
        if not self.monlist:
            self.readMonitorsDB()

        for man in self.monlist.keys():
            for model in self.monlist[man]:
                if monID == model[0]:
                    return model

        return 0

    def __str__ (self):
        return  "monEisa: %s\nmonName: %s\nmonID: %s\nfbmonSect: %s\nmonHoriz: %s\nmonVert: %s\n" % (  self.monEisa, self.monName, self.monID, self.fbmonSect, self.monHoriz, self.monVert)

    def setSpecs(self, horiz, vert, id=None, name = None):
        self.monHoriz = horiz
        self.monVert = vert
        if id:
            self.monID = id
            
        if name:
            self.monName = name

    def getMonitorHorizSync(self, useProbed=0):
        if useProbed:
            return self.orig_monHoriz
        else:
            return self.monHoriz

    def getMonitorVertSync(self, useProbed=0):
        if useProbed:
            return self.orig_monVert
        else:
            return self.monVert

    def getFBMonitorSection(self):
        return self.fbmonSect

    def getFBMonitorMode(self):
        return self.fbmonMode

    def getMonitorID(self, useProbed = 0):
        if not useProbed:
            return self.monID
        else:
            return self.orig_monID

    def shortDescription(self):
        if self.monName and self.monName != "" and self.monName != "Unprobed Monitor":
            return self.monName
        else:
            return _("Unable to probe")
        

    def reset(self):
        self.monEisa = self.orig_monEisa
        self.monName = self.orig_monName
        self.monID = self.orig_monID
        self.fbmonSect = self.orig_fbmonSect
        self.monHoriz = self.orig_monHoriz
        self.monVert = self.orig_monVert

    def __init__ (self, skipDDCProbe = 0, fbDevice = None):

        self.monEisa = None
        self.monName = None
        self.monID = "Unprobed Monitor"

        self.fbmonSect = ""

        self.monHoriz = None
        self.monVert = None

        self.monlist = {}
        self.monids = {}

        # VESA probe for monitor/videoram, etc.
        if not skipDDCProbe:
	    try:
		probe = string.split (iutil.execWithCapture ("/usr/sbin/ddcprobe", ['ddcprobe']), '\n')
		for line in probe:
		    if line and line[:8] == "EISA ID:":
			self.monEisa = string.lower(line[9:])
			self.monID = line[9:]

		    if line and line[:6] == "\tName:":
			if not self.monName or len (self.monName) < len (line[7:]):
			    self.monName = line[7:]

		    if line and line[:15] == "\tTiming ranges:":
			ranges = string.split (line, ',')
			self.monHoriz = string.strip (string.split (ranges[0], '=')[1])
			self.monVert = string.strip (string.split (ranges[1], '=')[1])
                if self.monEisa:
                    # read the monitor DB
                    self.readMonitorsDB()
                    if self.monids.has_key (self.monEisa):
                        (man, model, eisa, vert, horiz) = self.monids[self.monEisa]
                        self.setSpecs(horiz, vert, id=model, name=model)
            except:
                log("ddcprobe failed")
                pass

        self.fbmonMode = {}
        if fbDevice != None:
            try:
                (vidram, depth, mode, monitor) = isys.fbconProbe("/dev/" + fbDevice)
                self.fbmonSect = monitor
                self.fbmonMode = {}
                self.fbmonMode[str(depth)] = [str(mode)]
            except:
                pass

        # save for reset() method
        self.orig_monEisa = self.monEisa
        self.orig_monName = self.monName
        self.orig_monID = self.monID

        self.orig_fbmonSect = self.fbmonSect

        self.orig_monHoriz = self.monHoriz
        self.orig_monVert = self.monVert