Skip to content

Instantly share code, notes, and snippets.

@ervitis
Created February 25, 2018 16:33
Show Gist options
  • Save ervitis/a6c7eb62a4cdadb474b225cfe786b237 to your computer and use it in GitHub Desktop.
Save ervitis/a6c7eb62a4cdadb474b225cfe786b237 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from argparse import ArgumentParser
LAPTOP_POSITIONS = dict(LAPTOP_SCREEN_HOME_POSITION='1920x0', LAPTOP_SCREEN_WORK_POSITION='0x1080')
SCREENS_OPTIONS = dict(
LAPTOP_SCREEN = {
'output': 'eDP-1',
'primary': 'true',
'resolution': '1920x1080',
'frecuency': 60.00,
'rotation': 'normal',
'position': '',
},
HOME_SCREEN = {
'output': 'HDMI-1',
'primary': 'false',
'resolution': '1920x1080',
'frecuency': 60.00,
'rotation': 'normal',
'position': '0x0'
},
WORK_SCREEN = {
'output': 'HDMI-1',
'primary': 'false',
'resolution': '2560x1440',
'frecuency': 60.00,
'rotation': 'normal',
'position': '0x0'
}
)
def parser():
argparser = ArgumentParser()
argparser.add_argument('--location', choices=['home', 'work'], help='Set where you are using two screens')
return argparser.parse_args()
class Screen(object):
def __init__(self, properties):
for (k, v) in properties.items():
setattr(self, k, v)
def change_screen_option(self):
command = 'xrandr {}'.format(self._transform_properties())
print(command)
import subprocess
try:
subprocess.call(command.split())
except Exception as e:
raise e
def _transform_properties(self):
# example: xrandr --output HDMI-1 --mode 1920x1080 --primary --rate 60.00 --pos 0x0 --rotate normal
args = '--output {0} --mode {1} --rate {2} --pos {3} --rotate {4}'.format(
self.output, self.resolution, self.frecuency, self.position, self.rotation
)
if self.primary == 'true':
args += ' --primary'
return args
def __repr__(self):
return '{output}: {resolution}_{frecuency} {position}'.format(output=self.output, resolution=self.resolution,
frecuency=self.frecuency, position=self.position)
def factory_screen(location):
screen = Screen(SCREENS_OPTIONS['{}_SCREEN'.format(location.upper())])
return screen
def laptop_screen(location):
screen = factory_screen('LAPTOP')
screen.position = LAPTOP_POSITIONS['LAPTOP_SCREEN_{}_POSITION'.format(location.upper())]
return screen
def main():
argsparser = parser()
laptop = laptop_screen(argsparser.location)
secondary = factory_screen(argsparser.location)
screens = [laptop, secondary]
for screen in screens:
screen.change_screen_option()
return 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment