summaryrefslogtreecommitdiffstats
path: root/FusionIcon/environment.py
blob: 9423098e6aeb3d4a5874c80a200704eb90fd2d4f (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
# This file is part of Fusion-icon.

# Fusion-icon 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.
#
# Fusion-icon 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, see <http://www.gnu.org/licenses/>.
#
# Author(s): crdlb, nesl247
#
# Copyright 2007 Christopher Williams <christopherw@verizon.net> 

import os
from execute import run

tfp = 'GLX_EXT_texture_from_pixmap'
GDSID = 'GNOME_DESKTOP_SESSION_ID'

class Environment:

	'''Detects properties of the enviroment, and provides a set() method that uses this information to export environment variables needed to start compiz.'''
	

	def __init__(self):

		'''desktop: current desktop enviroment used to choose interface, fallback wm, and default decorator
	
failsafe: boolean, True if in a failsafe session, currently only supports gnome failsafe mode.

glxinfo: output of glxinfo command

indirect_glxinfo: output of glxinfo with LIBGL_ALWAYS_INDIRECT

xvinfo: output of xvinfo

glx_vendor: 'client glx vendor:' usually one of SGI (for mesa-based drivers), NVIDIA Corporation, or ATI.

tfp: 'direct' if texture_from_pixmap is present with direct rendering (implying presence with indirect as well), 'indirect' if only present with indirect context, False if not present at all

Xgl: True in Xgl'''

		# Check gnome- and kde-specific vars, then try generic 'DESKTOP_SESSION'
		if GDSID in os.environ:
			self.desktop = 'gnome'

		elif 'KDE_FULL_SESSION' in os.environ: 
			self.desktop = 'kde'

		else:
			self.desktop = os.environ.get('DESKTOP_SESSION', 'unknown')

		self.failsafe = False
		if self.desktop == 'gnome' and GDSID in os.environ and os.environ[GDSID] == 'failsafe':
			self.failsafe = True
		
		if self.failsafe:
			failsafe_str = 'failsafe '
		else:
			failsafe_str = ''

		print ' * Detected Session: %s%s' %(failsafe_str, self.desktop)


		## Save the output of glxinfo and xvinfo for later use:

		# don't try to run glxinfo unless it's installed
		if run(['which', 'glxinfo'], 'call', quiet=True) == 0:
			self.glxinfo = run('glxinfo', 'output')
		else:
			raise SystemExit, ' * Error: glxinfo not installed!'

		# make a temp environment 
		indirect_environ = os.environ.copy()
		indirect_environ['LIBGL_ALWAYS_INDIRECT'] = '1'
		self.indirect_glxinfo = run('glxinfo', 'output', env=indirect_environ)

		if run(['which', 'xvinfo'], 'call', quiet=True) == 0:
			self.xvinfo = run('xvinfo', 'output')
		else:
			raise SystemExit, ' * Error: xvinfo not installed!'

		line = [l for l in self.glxinfo.splitlines() if 'client glx vendor string:' in l]
		if line:
			self.glx_vendor = ' '.join(line[0].split()[4:])
		else:
			self.glx_vendor = ''

		## Texture From Pixmap / Indirect
		self.tfp = False
		if self.glxinfo.count(tfp) < 3:
			if self.indirect_glxinfo.count(tfp) == 3:
				self.tfp = 'indirect'
		else:
			self.tfp = 'direct'

		## Xgl
		if 'Xgl' in self.xvinfo:
			self.Xgl = True

		else:
			self.Xgl = False

	def set(self):

		'''Trigger all environment checks'''

		# Check for Intel and export INTEL_BATCH
		if 'Intel' in self.glxinfo:
			print ' * Intel detected, exporting: INTEL_BATCH=1'
			os.environ['INTEL_BATCH'] = '1'

		# Check TFP and export LIBGL_ALWAYS_INDIRECT if needed
		if self.tfp != 'direct':
			print ' * No %s with direct rendering context' %tfp
			if self.tfp == 'indirect':
				print ' ... present with indirect rendering, exporting: LIBGL_ALWAYS_INDIRECT=1'
				os.environ['LIBGL_ALWAYS_INDIRECT'] = '1'
			else:
				print ' ... nor with indirect rendering, this isn\'t going to work!'

		# If using Xgl with a proprietary driver, exports LD_PRELOAD=<mesa libGL>
		if self.Xgl and self.glx_vendor != 'SGI':
			print ' * Non-mesa driver on Xgl detected'
			from data import mesa_libgl_locations
			location = [l for l in mesa_libgl_locations if os.path.exists(l)]
			if location:
				print ' ... exporting: LD_PRELOAD=%s' %location[0]
				os.environ['LD_PRELOAD'] = location[0]
			else:
				# kindly let the user know... but don't abort (maybe it will work :> )
				print ' ... no mesa libGL found for preloading, this may not work!'

		# Check for nvidia on Xorg
		if not self.Xgl and self.glx_vendor == 'NVIDIA Corporation':
			print ' * NVIDIA on Xorg detected, exporting: __GL_YIELD=NOTHING'
			os.environ['__GL_YIELD'] = 'NOTHING'

env = Environment()