Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- # Python
- import sys
- import os
- import subprocess
- import time
- def getOutputs():
- """
- For the first screen, get the names of the outputs and whether they're currently connected.
- Returns:
- (list)
- Each element is:
- (tuple)
- Tuple has elements:
- 0:
- (str)
- Name of output
- 1:
- (bool)
- Whether the output is currently connected.
- """
- # Run xrandr
- popen = subprocess.Popen(["xrandr", "--screen", "0", "--query"], stdout=subprocess.PIPE)
- popen.wait()
- (out, err) = popen.communicate()
- # Parse output
- lines = out.split("\n")
- if lines[0][:9] != "Screen 0:":
- raise RuntimeError("Unexpected preamble from xrandr --query")
- outputs = []
- for line in lines[1:]:
- print line
- if len(line) > 0 and line[1] != " ":
- lineFields = line.split(" ")
- outputs.append((lineFields[0], lineFields[1] == "connected"))
- return outputs
- def generateTimings(i_timingCommand, i_width, i_height, i_fieldRate, i_interlaced=False):
- """
- Params:
- i_timingCommand:
- (str)
- i_width, i_height:
- Either (int)
- or (str)
- String representations of an integer.
- i_fieldRate:
- (str)
- String representations of a float.
- i_interlaced:
- (bool)
- True: Make an interlaced mode
- Returns:
- (list)
- eg. ['Modeline', '1920x1080i60.00', '86.5', '1920', '2048', '2248', '2576', '1080', '1083', '1088', '1120', '+hsync', '+vsync', 'Interlace']
- """
- # Run cvt or gtf
- popen = subprocess.Popen([i_timingCommand, str(i_width), str(i_height), i_fieldRate], stdout=subprocess.PIPE)
- popen.wait()
- (out, err) = popen.communicate()
- # Parse output
- lines = out.split("\n")
- modespec = None
- for line in lines:
- line = line.strip()
- if line.startswith("Modeline "):
- parts = line.split(" ")
- modespec = [p for p in parts if p != ""]
- break
- # Remove enclosing quotes in mode name
- modespec[1] = modespec[1].replace('"', '')
- # Modify mode name (which was generated by cvt/gtf)
- # to show whether progressive or interlaced,
- # and add actual interlacing options if desired
- if not i_interlaced:
- modespec[1] = modespec[1].replace('_', 'p')
- else:
- modespec[1] = modespec[1].replace('_', 'i')
- modespec.append("Interlace")
- modespec[2] = str(float(modespec[2]) / 2.0)
- #
- return modespec
- def recreateMode(i_outputName, i_modespec, i_printXrandrCommands = False):
- """
- Params:
- i_outputName:
- (str)
- i_modespec:
- (list)
- i_printXrandrCommands:
- (bool)
- """
- #print "i_modespec: " + str(i_modespec)
- xrandrCommand = ["xrandr", "--delmode", i_outputName]
- xrandrCommand += [i_modespec[1]]
- if i_printXrandrCommands:
- print(subprocess.list2cmdline(xrandrCommand))
- subprocess.Popen(xrandrCommand)
- time.sleep(0.25)
- xrandrCommand = ["xrandr", "--rmmode"]
- xrandrCommand += [i_modespec[1]]
- if i_printXrandrCommands:
- print(subprocess.list2cmdline(xrandrCommand))
- subprocess.Popen(xrandrCommand)
- time.sleep(0.25)
- xrandrCommand = ["xrandr", "--newmode"]
- xrandrCommand += i_modespec[1:]
- if i_printXrandrCommands:
- print(subprocess.list2cmdline(xrandrCommand))
- subprocess.Popen(xrandrCommand)
- time.sleep(0.25)
- xrandrCommand = ["xrandr", "--addmode", i_outputName]
- xrandrCommand += [i_modespec[1]]
- if i_printXrandrCommands:
- print(subprocess.list2cmdline(xrandrCommand))
- subprocess.Popen(xrandrCommand)
- def setMode(i_outputName, i_modespec, i_printXrandrCommands = False):
- """
- Params:
- i_outputName:
- (str)
- i_modespec:
- (list)
- i_printXrandrCommands:
- (bool)
- """
- xrandrCommand = ["xrandr", "--output", i_outputName]
- xrandrCommand += ["--mode", i_modespec[1]]
- if i_printXrandrCommands:
- print(subprocess.list2cmdline(xrandrCommand))
- subprocess.Popen(xrandrCommand)
- if __name__ == "__main__":
- # + Parse command line {{{
- COMMAND_NAME = "easy_xrandr"
- def printUsage(i_outputStream):
- i_outputStream.write('''\
- ''' + COMMAND_NAME + ''' by Daniel Lopez, 04/12/2016
- xrandr wrapper
- Usage:
- ======
- ''' + COMMAND_NAME + ''' <action> <width> <height> <field rate> [options...]
- Params:
- action:
- Either 'add'
- Just add the new mode to xrandr's list
- or 'set'
- Add the new mode and also switch to it now.
- width, height:
- Desired resolution width and height in integer pixels
- eg. 1024 and 768
- field rate:
- Desired field rate in floating point Hz
- eg. 60.1
- In non-interlaced modes, this is the same as the frame rate,
- and in interlaced modes, this is double the actual full frame rate.
- Options:
- Output selection
- --output <name>
- Choose which output to control
- Interlace
- --interlace
- Create an interlaced mode.
- In this case the actual full frame rate will turn out to be half of
- the number given for the <field rate> parameter.
- Generating timings
- --cvt
- Use the 'cvt' command to generate the timings
- (This is the default).
- --gtf
- Use the 'gtf' command to generate the timings.
- ''')
- #
- param_action = None # "add" or "set"
- param_width = None
- param_height = None
- param_fieldRate = None
- interlaced = False
- timingCommand = "cvt"
- outputName = "DVI-I-1"
- import sys
- argNo = 1
- while argNo < len(sys.argv):
- arg = sys.argv[argNo]
- argNo += 1
- if arg[0] == "-":
- if arg == "--help":
- printUsage(sys.stdout)
- sys.exit(0)
- if arg == "--cvt":
- timingCommand = "cvt"
- elif arg == "--gtf":
- timingCommand = "gtf"
- elif arg == "--interlace":
- interlaced = True
- elif arg == "--output":
- if argNo >= len(sys.argv):
- print("--output requires an argument")
- sys.exit(-1)
- arg = sys.argv[argNo]
- argNo += 1
- outputName = arg
- else:
- print("Unrecognised option: " + arg)
- sys.exit(-1)
- else:
- # Collect command-line arguments
- if param_action == None:
- param_action = arg
- elif param_width == None:
- param_width = arg
- elif param_height == None:
- param_height = arg
- elif param_fieldRate == None:
- param_fieldRate = arg
- else:
- print("Too many arguments.")
- sys.exit(-1)
- if param_fieldRate == None:
- print("Insufficient arguments.")
- sys.exit(-1)
- # + }}}
- # + Generate timings {{{
- print("Generating timings with " + timingCommand)
- modespec = generateTimings(timingCommand, param_width, param_height, param_fieldRate, interlaced)
- #print modespec
- if modespec == None:
- print("Failed to parse timings.")
- sys.exit(1)
- ##
- #modespec = modespec[:-2]
- #modespec.append("-hsync")
- #modespec.append("-vsync")
- #
- print modespec
- #sys.exit(0)
- # + }}}
- recreateMode(outputName, modespec, True)
- if param_action == "set":
- time.sleep(0.25)
- setMode(outputName, modespec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement