#!/usr/bin/env python
#coding:utf-8
'''
Created on Sep 17, 2014

@author: root
'''

from  monitorI import  MonitorI
from copy import deepcopy
import os,sys
if os.name == 'posix' and sys.version_info[0] < 3:
    import subprocess32 as subprocess
else:
    import subprocess

class MonitorResolution(MonitorI):

    Right = "--right-of"
    Left  = "--left-of"

    #def __init__(self):
    #    pass

    # 获取屏幕分辨率 信息
    @classmethod
    def getMonitorsInfo(cls):
        monitorinfo_list = []
        monitorinfo_tmpList = cls.getScreenResolution_List()  
        for monitor_I in monitorinfo_tmpList:
            #print ":" ,monitor_I
            monitorinfo = {
                "name":"" ,
                "curResolution":"",
                "resolutionList":[]
            }
            res_tmp = monitor_I[0].split("--")
            monitorinfo["name"] = res_tmp[0] 
            if len(res_tmp) > 1:
                monitorinfo["curResolution"] = res_tmp[1]

            #print monitorinfo
            monitor_I.pop(0)
            monitor_I.reverse()
            monitorinfo["resolutionList"]= monitor_I
            
            monitorinfo_list.append(deepcopy(monitorinfo))
            del  monitorinfo

        #print monitorinfo_list
        return monitorinfo_list

    # 关闭屏幕输出
    @classmethod
    def closeMonitor(cls,monitorName):
        args  = ["xrandr","--output"]
        args.append(str(monitorName))
        args.append("--off")
        status = subprocess.call(args)
        return status


    # 启动屏幕输出, 同显
    @classmethod
    def openMonitorSameOutput(cls,monitorName):
        args  = ["xrandr","--output"]
        args.append(str(monitorName))
        args.append("--auto")

        status = subprocess.call(args)
        return status

    # "xrandr --output VGA1 --auto --right-of LVDS1 --primary" # 主屏幕VGA1  ,
    # "xrandr --output VGA1 --auto --right-of LVDS1"            # 主屏幕LVDS1, VGA1 位于 右边
    #  xrandr --output VGA1 --auto --right-of LVDS1 --mode 800x600
    #  xrandr --output VGA1 --mode 800x600

    # 启动屏幕扩展输出
    @classmethod
    def openMonitorExtendOutput(cls,extendMonitorName,originalMonitorName,\
                                direction="--right-of"):
        args  = ["xrandr","--output"]
        args.append(str(extendMonitorName))
        args.append("--auto")
        args.append(direction)
        args.append(str(originalMonitorName))

        status = subprocess.call(args)
        return status

    # 设置屏幕分辨率
    #  xrandr --output VGA1 --mode 800x600
    @classmethod
    def setMonitorResolution(cls,monitorName,resolution):
        args  = ["xrandr","--output"]
        args.append(str(monitorName))
        args.append("--mode")
        args.append(str(resolution))
        status = subprocess.call(args)
        return status

#########################################################

def test_main_01():

    mutimonitor = MonitorResolution()
    monitorinfos =  mutimonitor.getMonitorsInfo()
    for info in monitorinfos:
        print  info
    print "close: ",MonitorResolution.closeMonitor("VGA1")
    # print "open: ",MonitorResolution.openMonitor("VGA1")
    # print "open: ",MonitorResolution.setMonitorResolution("LVDS1","1360x768")
    # print "open: ",MonitorResolution.setMonitorResolution("LVDS1","1440x900")
    # print "open: ",MonitorResolution.openMonitorExtendOutput("VGA1","LVDS1")
    print "open: ",MonitorResolution.openMonitorExtendOutput("VGA1","LVDS1",MonitorResolution.Right)
    # print "open: ",MonitorResolution.openMonitorExtendOutput("VGA1","LVDS1",MonitorResolution.Left)


if __name__ == "__main__":
    print "\033[0;31;1m --- test main --- \033[0m"
    test_main_01()
    print "\033[0;32;1m --- test end ---\n \033[0m"

