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

@author: root
'''

import commands
import os,sys 

class MonitorI(object):
    def __init__(self):
        pass

    #***********************
    # 验证　xrandr 有没有
    #**********************
    @classmethod 
    def checkXrandr(cls,command):
        pass

    # #获取 命令行 的执行结果
    # @classmethod
    # def __execUnixCmd(cls,command):
    #     output = ""
    #     statusOutput = commands.getstatusoutput(command)    # 得到命令行标准输出
    #     if statusOutput[0] == 0:                            # 如果是0，则执行成功
    #         output = statusOutput[1]                        # 第二个元素就是命令行执行后的输出
    #     else:
    #         output = ""
    #
    #     result = output.split("\n")
    #     return result
    #
    #
    # #***********************
    # #获取当前连接的显示器
    # #**********************
    # @classmethod
    # def getCurActiveMonitors(cls):
    #     monitorList = []
    #     atemp = MonitorI.__execUnixCmd("xrandr |awk '/connected/'")
    #     temp = cls.__execUnixCmd("xrandr |awk '/connected/'")
    #
    #     for i in range(len(temp)):
    #         if(temp[i].split(" ")[1] == "connected"):
    #             monitorList.append(temp[i].split(" ")[0]);
    #     return monitorList
    #
    # # ********************************
    # # 获取当前连接的显示器名称
    # # 返回: 列表 ["name1","name2"....]
    # # *******************************
    # def getActiveMonitors(self):
    #     monitorList = []
    #     temp = MonitorI.__execUnixCmd("xrandr |awk '/connected/'")
    #     for i in range(len(temp)):
    #         if(temp[i].split(" ")[1] == "connected"):
    #             monitorList.append(temp[i].split(" ")[0]);
    #     return monitorList
    #
    # #获取当前屏幕分辨率
    # def getCurrentResolution(self):
    #     temp = MonitorI.__execUnixCmd("xrandr |awk '/connected/'");
    #     print "---:",temp
    #     for i in range(len(temp)):
    #         if(temp[i].split(" ")[1] == "connected"):
    #             resolution = temp[i].split(" ")[2]
    #     #print resolution,"----"
    #     return resolution
    #
    #
    #
    # def getMulCurrentResolution(self):
    #
    #     temp = MonitorI.__execUnixCmd("xrandr |awk '/connected/'");
    #     resolutionList = []
    #
    #     for i in range(len(temp)):
    #         if(temp[i].split(" ")[1] == "connected"):
    #             resolution = temp[i].split(" ")[2]
    #             resolutionList.append(resolution)
    #
    #     return resolutionList

         
    # 获取终端所支持分辨率列表
    @classmethod
    def getScreenResolutionlist(cls):
        screenResolution = []

        cmd = "xrandr | grep -v \"disconnected\" |grep -v \"Screen\"" \
            + "|grep \" .*x\" | sed  \"s/^\ \ \ //g\"" \
            + "| sed \"s/\ (normal.*$//\" | sed \"s/\ connected\ */--/g\"" \
            + "| sed \"s/primary //g\"| sed \"s/\ .*$//\""
        try:
            f = os.popen(cmd, "r")
            while True:
                x = f.readline().strip('\n')  # 去掉\n
                if x == "":
                    break
                screenResolution.append(x)
            f.close()
            return screenResolution
        except Exception:
            # raise Exception("getScreenResolution err")
            return []

    # ********************************************************
    # 获取 分辨率列表,
    # 返回　[ 分辨率列表1,[分辨率列表2]]
    #
    #      eg:
    #      screen_01 = ['LVDS1--1440x900+0+0', '1440x900', '1360x768', '1280x800', '1152x864', '1024x768', '800x600', '720x450', '640x480']
    #      screen_02 = ['VGA1--', '1440x900', '1280x1024', '1152x864', '1280x720', '1024x768', '800x600', '640x480', '720x400']
    #      return [ screen_01,screen_02 ]
    # ********************************************************
    @classmethod
    def getScreenResolution_List(cls):
        resolution_list = cls.getScreenResolutionlist()
        #print resolution_list
        resolution_tmp=[]
        tmp = [] 
        for resolution in  resolution_list :
            if resolution.find("--") >= 0:
                if len(tmp) > 0:
                    resolution_tmp.append(tmp)
                tmp = [] 
            tmp.append(resolution)

        if len(tmp) > 0: 
            resolution_tmp.append(tmp)
        return  resolution_tmp                

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

def test_main_01():
 
    mutimonitor = MonitorI()
    # print "当前分辨率:", mutimonitor.getCurrentResolution() # 获取当前分辨率
    print "屏幕列表  :",mutimonitor.getActiveMonitors()
    
    #mutimonitor_list = mutimonitor.getScreenResolutionlist()
    #print mutimonitor_list

    mutimonitor_list = mutimonitor.getScreenResolution_List()
    print "分辨率列表"
    for mutimonitor in mutimonitor_list:
        print ":" ,mutimonitor

    #print "启用双屏异显:", mutimonitor.enableMonitorI() #启用多显示器(双平异显）
    #print mutimonitor.enableSameDoubleMonitor()  #启用多显示器(双平同显）
    #print mutimonitor.disableMonitorI()

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"

