Best Python code snippet using fMBT_python
fmbtuinput.py
Source:fmbtuinput.py
...636 return sendInputEvent(self._fd, typeCode, codeCode, value)637 def sync(self):638 if self._fd < 1:639 raise InputDeviceError("InputDevice is not open")640 return sendInputSync(self._fd)641class InputDeviceError(Exception):642 pass643class Mouse(InputDevice):644 def __init__(self, absoluteMove=False):645 """646 Parameters:647 absoluteMove (boolean, optional)648 force move(x,y) to send absolute coordinates instead649 of standard relative movement. This helps avoiding650 mouse pointer drift in some occasions. The default651 is False.652 """653 InputDevice.__init__(self)654 self._x = 0655 self._y = 0656 self._sendAbs = absoluteMove657 def create(self, name="Virtual fMBT Mouse",658 vendor=0xf4b7, product=0x4053, version=1):659 self.startCreating(name, vendor, product, version)660 self.addEvent("EV_KEY")661 self.addEvent("EV_REL")662 if self._sendAbs:663 self.addEvent("EV_ABS")664 self.addEvent("EV_SYN")665 self.addRel("REL_X")666 self.addRel("REL_Y")667 self.addRel("REL_HWHEEL")668 self.addRel("REL_WHEEL")669 self.addKey("BTN_LEFT")670 self.addKey("BTN_RIGHT")671 self.addKey("BTN_MIDDLE")672 self.addKey("BTN_SIDE")673 self.addKey("BTN_EXTRA")674 self.addKey("BTN_FORWARD")675 self.addKey("BTN_BACK")676 self.addKey("BTN_TASK")677 if self._sendAbs:678 self.addAbs("ABS_X")679 self.addAbs("ABS_Y")680 self.finishCreating()681 return self682 def move(self, x, y):683 """684 Move mouse cursor to coordinates x, y.685 """686 if self._sendAbs:687 self.send("EV_ABS", "ABS_X", x)688 self.send("EV_ABS", "ABS_Y", y)689 else:690 deltaX = x - self._x691 deltaY = y - self._y692 self.send("EV_REL", "REL_X", deltaX)693 self.send("EV_REL", "REL_Y", deltaY)694 self.sync()695 self.setXY(x, y)696 def moveRel(self, deltaX, deltaY):697 self.send("EV_REL", "REL_X", deltaX)698 self.send("EV_REL", "REL_Y", deltaY)699 self.sync()700 self.setXY(self._x + deltaX, self._y + deltaY)701 def press(self, button):702 buttonCode = toButtonCode(button)703 self.send("EV_KEY", buttonCode, 1)704 self.sync()705 def release(self, button):706 buttonCode = toButtonCode(button)707 self.send("EV_KEY", buttonCode, 0)708 self.sync()709 def setXY(self, x, y):710 """711 Resets relative mouse position to (x, y), does not synthesize712 event. Example: disable possible mouse pointer drift:713 mouse.moveRel(-4096, -4096) # move to the top-left corner714 mouse.setXY(0, 0) # set current pointer coordinates to 0, 0715 After this, mouse.move(x, y) will synthesize relative mouse716 move event which will drive cursor to coordinates x, y.717 """718 self._x = x719 self._y = y720 def xy(self):721 return (self._x, self._y)722 def tap(self, x, y, button):723 self.move(x, y)724 self.press(button)725 self.release(button)726class Touch(InputDevice):727 """728 Simulates touchpanel and touchpad729 """730 def __init__(self, maxX = None, maxY = None,731 screenWidth = None, screenHeight = None, screenAngle = None):732 InputDevice.__init__(self)733 self._maxX = maxX734 self._maxY = maxY735 self._screenW = screenWidth736 self._screenH = screenHeight737 self._screenA = screenAngle738 self._maxPressure = None739 self._multiTouch = True740 self._mtTrackingId = 0741 self._mtTracking = {}742 self._hoover = (0, 0)743 def create(self, name="Virtual fMBT Touch",744 vendor=0xf4b7, product=0x70c5, version=1,745 maxX=0xffff, maxY=0xffff, maxPressure=None,746 multiTouch = True):747 absmin = [0 for _ in xrange(abs_count)]748 absmax = [0 for _ in xrange(abs_count)]749 absmax[absCodes["ABS_X"]] = maxX750 absmax[absCodes["ABS_Y"]] = maxY751 if maxPressure != None:752 self._maxPressure = maxPressure753 absmax[absCodes["ABS_PRESSURE"]] = self._maxPressure754 absmax[absCodes["ABS_MT_SLOT"]] = 16755 absmax[absCodes["ABS_MT_TRACKING_ID"]] = 0x0fffffff756 absmax[absCodes["ABS_MT_POSITION_X"]] = maxX757 absmax[absCodes["ABS_MT_POSITION_Y"]] = maxY758 self._maxX = maxX759 self._maxY = maxY760 self._multiTouch = multiTouch761 self.startCreating(name, vendor, product, version,762 absmin=absmin, absmax=absmax)763 self.addEvent("EV_KEY")764 self.addEvent("EV_ABS")765 self.addEvent("EV_SYN")766 self.addKey("BTN_TOUCH")767 self.addAbs("ABS_X")768 self.addAbs("ABS_Y")769 if self._maxPressure != None:770 self.addAbs("ABS_PRESSURE")771 if self._multiTouch:772 self.addAbs("ABS_MT_SLOT")773 self.addAbs("ABS_MT_TRACKING_ID")774 self.addAbs("ABS_MT_POSITION_X")775 self.addAbs("ABS_MT_POSITION_Y")776 self.finishCreating()777 return self778 def open(self, filename):779 InputDevice.open(self, filename)780 # detect touch device capabilities and max values781 # nfo is struct input_absinfo782 nfo = array.array('i', range(6))783 fcntl.ioctl(self._fd, EVIOCGABS(absCodes["ABS_X"]), nfo, 1)784 self._maxX = nfo[2]785 fcntl.ioctl(self._fd, EVIOCGABS(absCodes["ABS_Y"]), nfo, 1)786 self._maxY = nfo[2]787 return self788 def setScreenSize(self, (width, height)):789 self._screenW, self._screenH = (width, height)790 def setScreenAngle(self, angle):791 self._screenA = angle792 def _angleXY(self, x, y, angle=None):793 """return x, y in screen without rotation"""794 if angle == None:795 angle = self._screenA796 sw, sh = self._screenW, self._screenH797 if angle:798 while angle < 0:799 angle += 360800 while angle > 360:801 angle -= 360802 if angle == 90:803 ax = self._screenH - y804 ay = x805 sw, sh = self._screenH, self._screenW806 elif angle == 180:807 ax = self._screenH - x808 ay = self._screenW - y809 elif angle == 270:810 ax = y811 ay = self._screenW - x812 sw, sh = self._screenH, self._screenW813 else:814 raise ValueError('Illegal screen rotation angle %s' %815 (self._screenA,))816 else:817 ax, ay = x, y818 return (sw, sh, ax, ay)819 def _tXY(self, x, y):820 """convert x, y to touch screen coordinates"""821 if self._screenW and self._maxX and self._screenH and self._maxY:822 w, h, x, y = self._angleXY(x, y)823 x = int((self._maxX * x) / w)824 y = int((self._maxY * y) / h)825 return (x, y)826 else:827 return (x, y)828 def absToScreenXY(self, absX, absY):829 if self._screenW and self._maxX and self._screenH and self._maxY:830 x = int(self._screenW * absX / self._maxX)831 y = int(self._screenH * absY / self._maxY)832 if self._screenA:833 _, _, x, y = self._angleXY(x, y, -self._screenA)834 return (x, y)835 else:836 return (absX, absY)837 def _startTracking(self, finger, x, y):838 self._mtTrackingId += 1839 usedSlots = set([self._mtTracking[fngr][0]840 for fngr in self._mtTracking])841 for freeSlot in xrange(16):842 if not freeSlot in usedSlots:843 break844 else:845 raise ValueError("No free slots for multitouch")846 self._mtTracking[finger] = [freeSlot, self._mtTrackingId, x, y]847 self._sendSlot(finger)848 self.send("EV_ABS", "ABS_MT_TRACKING_ID", self._mtTrackingId)849 tx, ty = self._tXY(x, y)850 self.send("EV_ABS", "ABS_MT_POSITION_X", tx)851 self.send("EV_ABS", "ABS_MT_POSITION_Y", ty)852 return self._mtTrackingId853 def _stopTracking(self, finger):854 self._sendSlot(finger)855 self.send("EV_ABS", "ABS_MT_TRACKING_ID", -1)856 del self._mtTracking[finger]857 def _sendSlot(self, finger):858 slot = self._mtTracking[finger][0]859 self.send("EV_ABS", "ABS_MT_SLOT", slot)860 def tap(self, x, y, pressure=None):861 self.pressFinger(-1, x, y, pressure)862 self.releaseFinger(-1)863 # Compatibility API to allow using a Touch almost like a Mouse864 def move(self, x, y):865 if len(self._mtTracking.keys()) == 0:866 self._hoover = (x, y)867 else:868 finger = sorted(self._mtTracking.keys())[0]869 return self.moveFinger(finger, x, y)870 def press(self, finger):871 return self.pressFinger(finger, *self._hoover)872 def release(self, finger):873 return self.releaseFinger(finger)874 # end of compatibility API875 # Multi-touch API876 def pressFinger(self, finger, x, y, pressure=None):877 """Add a finger to current multitouch gesture. If multitouch gesture878 is not started, it starts automatically.879 """880 if self._multiTouch and not finger in self._mtTracking:881 self._startTracking(finger, x, y)882 if pressure != None and self._maxPressure != None:883 self.send("EV_ABS", "ABS_PRESSURE", pressure)884 self.send("EV_KEY", "BTN_TOUCH", 1)885 tx, ty = self._tXY(x, y)886 self.send("EV_ABS", "ABS_X", tx)887 self.send("EV_ABS", "ABS_Y", ty)888 self.sync()889 def releaseFinger(self, finger):890 """Remove a finger from current multitouch gesture. When last finger891 is raised from the screen, multitouch gesture ends."""892 if self._multiTouch:893 self._stopTracking(finger)894 self.send("EV_KEY", "BTN_TOUCH", 0)895 for fngr in self._mtTracking:896 # still some finger pressed, non-multitouch reader gets897 # coordinates from one of those898 tx, ty = self._tXY(self._mtTracking[fngr][2],899 self._mtTracking[fngr][3])900 self.send("EV_ABS", "ABS_X", tx)901 self.send("EV_ABS", "ABS_Y", ty)902 break # only one coordinates will be sent.903 self.sync()904 def moveFinger(self, finger, x, y):905 """Move a finger in current multitouch gesture"""906 self._sendSlot(finger)907 tx, ty = self._tXY(x, y)908 if self._multiTouch:909 self.send("EV_ABS", "ABS_MT_POSITION_X", tx)910 self.send("EV_ABS", "ABS_MT_POSITION_Y", ty)911 self.send("EV_ABS", "ABS_X", tx)912 self.send("EV_ABS", "ABS_Y", ty)913 self._mtTracking[finger][2] = x # last X914 self._mtTracking[finger][3] = y # last Y915 self.sync()916class Keyboard(InputDevice):917 def __init__(self):918 InputDevice.__init__(self)919 def create(self, name="Virtual fMBT Keyboard",920 vendor=0xf4b7, product=0x4ebd, version=1):921 self.startCreating(name, vendor, product, version)922 self.addEvent("EV_KEY")923 self.addEvent("EV_SYN")924 for keyName in keyCodes:925 if keyName.startswith("KEY_"):926 self.addKey(keyCodes[keyName])927 self.finishCreating()928 return self929 def press(self, keyCodeOrName):930 self.send("EV_KEY", toKeyCode(keyCodeOrName), 1)931 self.sync()932 def release(self, keyCodeOrName):933 self.send("EV_KEY", toKeyCode(keyCodeOrName), 0)934 self.sync()935 def tap(self, keyCodeOrName):936 keyCode = toKeyCode(keyCodeOrName)937 self.press(keyCode)938 self.release(keyCode)939def sendInputSync(devFd):940 return sendInputEvent(devFd, 0, 0, 0)941def sendInputEvent(devFd, type_, code, value):942 t = time.time()943 t_sec = int(t)944 t_usec = int(1000000*(t-t_sec))945 rv = os.write(devFd,946 struct.pack(struct_input_event,947 t_sec, t_usec,948 type_,949 code,950 value))951 return rv == sizeof_input_event952def eventToString(inputEvent):953 tim, tus, typ, cod, val = struct.unpack(struct_input_event, inputEvent)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!