xorg.display.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import comar
  4. from pardus.sysutils import *
  5. from zorg.config import *
  6. from zorg.consts import *
  7. from zorg.probe import *
  8. from zorg.utils import *
  9. kernel_module_file = os.path.join(config_dir, "kernel_module")
  10. def getDevice():
  11. bus = configuredBus()
  12. if not bus:
  13. return None
  14. return getDeviceInfo(bus)
  15. def getDeviceOutput(output):
  16. dev = getDevice()
  17. if not dev:
  18. return None, None
  19. if output not in dev.outputs:
  20. dev.outputs[output] = Output(output)
  21. return dev, dev.outputs[output]
  22. # Model methods
  23. def ready(boot):
  24. opts = {}
  25. if boot:
  26. opts = get_kernel_option("xorg")
  27. if "keymap" in opts:
  28. keymap = opts["keymap"].split("/", 1)
  29. setKeymap(*keymap)
  30. if "safe" in opts:
  31. return False
  32. driver = opts.get("driver")
  33. if driver or "probe" in opts:
  34. return initialConfig(driver)
  35. busId = configuredBus()
  36. if not busId:
  37. return initialConfig()
  38. if boot:
  39. # Check if the active card is changed
  40. try:
  41. device = getDeviceInfo(busId)
  42. except IOError:
  43. # Device is not present on specified bus.
  44. # Check if the new device is configured before.
  45. newBus = getPrimaryCard()
  46. if newBus:
  47. device = getDeviceInfo(VideoDevice(newBus).bus_id)
  48. if device and not device.isChanged():
  49. # Yes, it is configured before.
  50. # Reenable driver and write xorg.conf.
  51. device.enableDriver()
  52. saveXorgConfig(device)
  53. else:
  54. # This is a different card.
  55. return initialConfig()
  56. else:
  57. # No card is present. Return True in order to
  58. # start with a manually edited xorg.conf.
  59. return True
  60. else:
  61. if not device or device.isChanged():
  62. return initialConfig()
  63. # Check kernel module. Later, this should be done
  64. # with Xorg.Driver.ready method.
  65. if os.path.exists(kernel_module_file):
  66. kernelModule = file(kernel_module_file).read()
  67. if run("/sbin/modprobe", "-i", kernelModule) and kernelModule != "fglrx":
  68. return False
  69. return True
  70. def initialConfig(preferredDriver=None):
  71. bus = getPrimaryCard()
  72. if bus:
  73. device = VideoDevice(bus)
  74. else:
  75. # This machine might be a terminal server with no video cards.
  76. # We start X and leave the decision to the user.
  77. log_debug("No video card found.")
  78. return True
  79. if preferredDriver is None:
  80. preferredDriver = device.preferredDriver()
  81. device.setDriver(preferredDriver)
  82. saveDeviceInfo(device)
  83. saveXorgConfig(device)
  84. return True
  85. def safeConfig():
  86. bus = getPrimaryCard()
  87. if bus:
  88. device = VideoDevice(bus)
  89. else:
  90. # See the comment in initialConfig
  91. return True
  92. device.setDriver("vesa")
  93. device.depth = 16
  94. device.outputs["default"] = Output("default")
  95. device.monitors["default"] = Monitor()
  96. saveDeviceInfo(device)
  97. saveXorgConfig(device)
  98. return True
  99. # FIXME Confusing method name
  100. def activeDeviceID():
  101. return configuredBus()
  102. def listDrivers():
  103. drivers = listAvailableDrivers()
  104. link = comar.Link()
  105. packages = list(link.Xorg.Driver)
  106. for package in packages:
  107. try:
  108. info = link.Xorg.Driver[package].getInfo()
  109. except dbus.DBusException:
  110. continue
  111. alias = str(info["alias"])
  112. driver = str(info["xorg-module"])
  113. drivers.append(alias)
  114. if driver in drivers:
  115. drivers.remove(driver)
  116. return sorted(drivers)
  117. def setDriver(driver):
  118. dev = getDevice()
  119. if dev:
  120. dev.setDriver(driver)
  121. saveDeviceInfo(dev)
  122. def setDepth(depth):
  123. dev = getDevice()
  124. if dev:
  125. dev.depth = depth
  126. saveDeviceInfo(dev)
  127. def setOutput(output, enabled, ignored):
  128. dev, out = getDeviceOutput(output)
  129. if dev:
  130. out.setEnabled(enabled)
  131. out.setIgnored(ignored)
  132. saveDeviceInfo(dev)
  133. def setMode(output, resolution, rate):
  134. dev, out = getDeviceOutput(output)
  135. if dev:
  136. out.setMode(resolution, rate)
  137. saveDeviceInfo(dev)
  138. def setOrientation(output, rotation, reflection):
  139. dev, out = getDeviceOutput(output)
  140. if dev:
  141. out.setOrientation(rotation, reflection)
  142. saveDeviceInfo(dev)
  143. def setPosition(output, position, arg):
  144. dev, out = getDeviceOutput(output)
  145. if dev:
  146. out.setPosition(position, arg)
  147. saveDeviceInfo(dev)
  148. def setMonitor(output, vendor, model, horizSync, vertRefresh):
  149. dev, out = getDeviceOutput(output)
  150. if dev:
  151. if model:
  152. mon = Monitor()
  153. mon.vendor = vendor
  154. mon.model = model
  155. mon.hsync = horizSync
  156. mon.vref = vertRefresh
  157. dev.monitors[output] = mon
  158. elif output in dev.monitors:
  159. del dev.monitors[output]
  160. else:
  161. return
  162. saveDeviceInfo(dev)
  163. def syncConfigs():
  164. dev = getDevice()
  165. if dev:
  166. saveXorgConfig(dev)
  167. def setKeymap(layout, variant=""):
  168. saveKeymap(layout, variant)