Repository: psychopy/psychopy

Issue:
iohub mouse setPosition not setting y position correctly
I'm trying to center the mouse cursor on an object, and can't use `event.Mouse.setPos()` since I'm using pyglet. The following should center the mouse cursor in the middle of the screen:

``` python
from psychopy import iohub
io = iohub.launchHubServer()
io.devices.mouse.setPosition((0, 0))
```

However this puts the cursor at the middle of the top edge of the screen. Looking in `setPosition()`, [`display._displayCoord2Pixel()`](https://github.com/psychopy/psychopy/blob/master/psychopy/iohub/devices/mouse/__init__.py#L285) returns a negative y component, which when checked with `self._validateMousePosition()` zeros out the y component, thus putting the cursor at the top of the screen.

I could fix this by changing [`iohub.devices.display.Display:_calculateCoordMappingFunctions:psychopy2displayPix()`](https://github.com/psychopy/psychopy/blob/master/psychopy/iohub/devices/display/__init__.py#L557) to `return l+(cx+w/2),t+(cy+h/2)`, but I'm almost certain I'm missing something obvious.



Discussion:
created bug in isolver/psychopy for this so I fix it. https://github.com/isolver/psychopy/issues/12


Resolving patch:
diff --git a/psychopy/iohub/devices/mouse/__init__.py b/psychopy/iohub/devices/mouse/__init__.py
--- a/psychopy/iohub/devices/mouse/__init__.py
+++ b/psychopy/iohub/devices/mouse/__init__.py
@@ -161,7 +161,7 @@ def setPosition(self, pos, display_index=None):
             print2err(' mouse.setPos did not update mouse pos')
         else:
             self._lastPosition = self._position
-            self._position = px, py
+            self._position = pos[0], pos[1]
 
             self._last_display_index = self._display_index
             self._display_index = mouse_display_index
diff --git a/psychopy/iohub/devices/mouse/win32.py b/psychopy/iohub/devices/mouse/win32.py
--- a/psychopy/iohub/devices/mouse/win32.py
+++ b/psychopy/iohub/devices/mouse/win32.py
@@ -143,11 +143,32 @@ def _initialMousePos(self):
         this method gets the current system cursor pos.
         """
         if self._position is None:
-            self._position = 0.0, 0.0
-            self._lastPosition = 0.0, 0.0
+            p = 0.0, 0.0
             mpos = ctypes.wintypes.POINT()
             if self._user32.GetCursorPos(ctypes.byref(mpos)):
-                self._position = [mpos.x, mpos.y]
+                display_index = self.getDisplayIndexForMousePosition(
+                    (mpos.x,mpos.y))
+    
+                if display_index == -1 and self._last_display_index is not None:
+                    display_index = self._last_display_index
+
+                if display_index != self._display_device.getIndex():
+                    # sys mouse is currently not in psychopy window
+                    # so keep pos to window center.
+                    display_index = -1
+        
+                if display_index == -1:
+                    self._display_index = self._display_device.getIndex()
+                    self._last_display_index = self._display_index
+                    wm_pix = self._display_device._displayCoord2Pixel(p[0],
+                                                                      p[1],
+                                                                      self._display_index)
+                    self._nativeSetMousePos(*wm_pix)
+                else:
+                    p = self._display_device._pixel2DisplayCoord(
+                        mpos.x, mpos.y, display_index)
+                
+                self._position = p
                 self._lastPosition = self._position
 
     def _nativeSetMousePos(self, px, py):
diff --git a/psychopy/visual/form.py b/psychopy/visual/form.py
--- a/psychopy/visual/form.py
+++ b/psychopy/visual/form.py
@@ -779,7 +779,15 @@ def _drawCtrls(self):
                                element._baseY - self._getScrollOffset())
                 if self._inRange(element):
                     element.draw()
-
+                    
+    def setAutoDraw(self, value, log=None):
+        """Sets autoDraw for Form and any responseCtrl contained within
+        """
+        for i in self.items:
+            if i['responseCtrl']:
+                i['responseCtrl'].setAutoDraw(value)        
+        BaseVisualStim.setAutoDraw(self, value, log)
+        
     def draw(self):
         """Draw all form elements"""
         # Check mouse wheel


Test patch: