'''
@date 2015-02-21
@author Hong-She Liang <starofrainnight@gmail.com>
'''

import os
import re
import subprocess
from .mouse_constant import *

class Mouse(object):
    ## return current mouse absolute position
    @classmethod
    def position(cls):
        p = subprocess.Popen(["xdotool", "getmouselocation"], stdout=subprocess.PIPE)
        output = p.communicate()[0]
        matched = re.match(".*x:(\d+)\s*y:(\d+)\s*.*", output)
        return [int(matched.group(1)), int(matched.group(2))]
    
    @classmethod
    def move(cls, position):
        os.system("xdotool mousemove --sync %s %s" % (int(position[0]), int(position[1])))
    
    ##  
    @classmethod
    def button_up(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            os.system("xdotool mouseup 1") 
        elif ButtonType.RIGHT == button_type:
            os.system("xdotool mouseup 3")
        elif ButtonType.MIDDLE == button_type:
            os.system("xdotool mouseup 2")
            
    @classmethod
    def button_down(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            os.system("xdotool mousedown 1")
        elif ButtonType.RIGHT == button_type:
            os.system("xdotool mousedown 3")
        elif ButtonType.MIDDLE == button_type:
            os.system("xdotool mousedown 2")
            
            
'''
@date 2015-02-21
@author Hong-She Liang <starofrainnight@gmail.com>
'''

import os
import re
import subprocess
from .mouse_constant import *

class Mouse(object):
    ## return current mouse absolute position
    @classmethod
    def position(cls):
        output = subprocess.check_output(["xdotool", "getmouselocation"])
        matched = re.match(".*x:(\d+)\s*y:(\d+)\s*.*", output)
        return [int(matched.group(1)), int(matched.group(2))]
    
    @classmethod
    def move(cls, position):
        subprocess.call(["xdotool", "mousemove", "--sync", position[0], position[1]])
    
    ##  
    @classmethod
    def button_up(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            subprocess.call(["xdotool", "mouseup", "1"]) 
        elif ButtonType.RIGHT == button_type:
            subprocess.call(["xdotool", "mouseup", "3"])
        elif ButtonType.MIDDLE == button_type:
            subprocess.call(["xdotool", "mouseup", "2"])
            
    @classmethod
    def button_down(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            subprocess.call(["xdotool", "mousedown", "1"])
        elif ButtonType.RIGHT == button_type:
            subprocess.call(["xdotool", "mousedown", "3"])
        elif ButtonType.MIDDLE == button_type:
            subprocess.call(["xdotool", "mousedown", "2"])
            
            
Convert all os.system() to subprocess.check_output().
Convert all os.system() to subprocess.check_output().


  '''
  @date 2015-02-21
  @author Hong-She Liang <starofrainnight@gmail.com>
  '''
  
  import os
  import re
  import subprocess
  from .mouse_constant import *
  
  class Mouse(object):
      ## return current mouse absolute position
      @classmethod
      def position(cls):
+         output = subprocess.check_output(["xdotool", "getmouselocation"])
-         p = subprocess.Popen(["xdotool", "getmouselocation"], stdout=subprocess.PIPE)
-         output = p.communicate()[0]
          matched = re.match(".*x:(\d+)\s*y:(\d+)\s*.*", output)
          return [int(matched.group(1)), int(matched.group(2))]
      
      @classmethod
      def move(cls, position):
-         os.system("xdotool mousemove --sync %s %s" % (int(position[0]), int(position[1])))
+         subprocess.call(["xdotool", "mousemove", "--sync", position[0], position[1]])
      
      ##  
      @classmethod
      def button_up(cls, button_type = ButtonType.LEFT ):
          if ButtonType.LEFT == button_type:
-             os.system("xdotool mouseup 1") 
+             subprocess.call(["xdotool", "mouseup", "1"]) 
          elif ButtonType.RIGHT == button_type:
-             os.system("xdotool mouseup 3")
+             subprocess.call(["xdotool", "mouseup", "3"])
          elif ButtonType.MIDDLE == button_type:
-             os.system("xdotool mouseup 2")
+             subprocess.call(["xdotool", "mouseup", "2"])
              
      @classmethod
      def button_down(cls, button_type = ButtonType.LEFT ):
          if ButtonType.LEFT == button_type:
-             os.system("xdotool mousedown 1")
+             subprocess.call(["xdotool", "mousedown", "1"])
          elif ButtonType.RIGHT == button_type:
-             os.system("xdotool mousedown 3")
+             subprocess.call(["xdotool", "mousedown", "3"])
          elif ButtonType.MIDDLE == button_type:
-             os.system("xdotool mousedown 2")
+             subprocess.call(["xdotool", "mousedown", "2"])
              
              
Convert all os.system() to subprocess.check_output().
## Code Before:
'''
@date 2015-02-21
@author Hong-She Liang <starofrainnight@gmail.com>
'''

import os
import re
import subprocess
from .mouse_constant import *

class Mouse(object):
    ## return current mouse absolute position
    @classmethod
    def position(cls):
        p = subprocess.Popen(["xdotool", "getmouselocation"], stdout=subprocess.PIPE)
        output = p.communicate()[0]
        matched = re.match(".*x:(\d+)\s*y:(\d+)\s*.*", output)
        return [int(matched.group(1)), int(matched.group(2))]
    
    @classmethod
    def move(cls, position):
        os.system("xdotool mousemove --sync %s %s" % (int(position[0]), int(position[1])))
    
    ##  
    @classmethod
    def button_up(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            os.system("xdotool mouseup 1") 
        elif ButtonType.RIGHT == button_type:
            os.system("xdotool mouseup 3")
        elif ButtonType.MIDDLE == button_type:
            os.system("xdotool mouseup 2")
            
    @classmethod
    def button_down(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            os.system("xdotool mousedown 1")
        elif ButtonType.RIGHT == button_type:
            os.system("xdotool mousedown 3")
        elif ButtonType.MIDDLE == button_type:
            os.system("xdotool mousedown 2")
            
            
## Instruction:
Convert all os.system() to subprocess.check_output().
## Code After:
'''
@date 2015-02-21
@author Hong-She Liang <starofrainnight@gmail.com>
'''

import os
import re
import subprocess
from .mouse_constant import *

class Mouse(object):
    ## return current mouse absolute position
    @classmethod
    def position(cls):
        output = subprocess.check_output(["xdotool", "getmouselocation"])
        matched = re.match(".*x:(\d+)\s*y:(\d+)\s*.*", output)
        return [int(matched.group(1)), int(matched.group(2))]
    
    @classmethod
    def move(cls, position):
        subprocess.call(["xdotool", "mousemove", "--sync", position[0], position[1]])
    
    ##  
    @classmethod
    def button_up(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            subprocess.call(["xdotool", "mouseup", "1"]) 
        elif ButtonType.RIGHT == button_type:
            subprocess.call(["xdotool", "mouseup", "3"])
        elif ButtonType.MIDDLE == button_type:
            subprocess.call(["xdotool", "mouseup", "2"])
            
    @classmethod
    def button_down(cls, button_type = ButtonType.LEFT ):
        if ButtonType.LEFT == button_type:
            subprocess.call(["xdotool", "mousedown", "1"])
        elif ButtonType.RIGHT == button_type:
            subprocess.call(["xdotool", "mousedown", "3"])
        elif ButtonType.MIDDLE == button_type:
            subprocess.call(["xdotool", "mousedown", "2"])
            
            