Best Python code snippet using fMBT_python
fmbtuinput.py
Source:fmbtuinput.py
...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)954 styp = eventTypesInv.get(typ, "?")955 if styp == "EV_KEY":956 scod = keyCodesInv.get(cod, "?")957 elif styp == "EV_REL":958 scod = relCodesInv.get(cod, "?")959 elif styp == "EV_ABS":960 scod = absCodesInv.get(cod, "?")961 else:962 scod = "N/A"963 if typ == 0:964 return styp965 else:966 return "%8s.%s type: %4s (%5s), code: %5s (%15s) value: %8s" % \967 (tim, str(tus).zfill(6), typ, styp, cod, scod, val)968def queueEventsFromFile(filename, queue, lock, filterOpts):969 if isinstance(filterOpts, dict) and "type" in filterOpts:970 allowedTypes = set()971 for t in filterOpts["type"]:972 if isinstance(t, str):973 allowedTypes.add(eventTypes[t])974 else:975 allowedTypes.add(t)976 else:977 allowedTypes = set(eventTypes.values())978 if ("touchScreen" in filterOpts and979 filename == filterOpts["touchScreen"].filename()):980 touchScreen = filterOpts["touchScreen"]981 absXCodes = set([absCodes["ABS_X"], absCodes["ABS_MT_POSITION_X"]])982 absYCodes = set([absCodes["ABS_Y"], absCodes["ABS_MT_POSITION_Y"]])983 absType = eventTypes["EV_ABS"]984 else:985 touchScreen = None986 fd = os.open(filename, os.O_RDONLY)987 try:988 while 1:989 eventData = os.read(fd, sizeof_input_event)990 if not lock.locked():991 return992 if not eventData:993 break994 (ts, tus, typ, cod, val) = struct.unpack(struct_input_event, eventData)995 if touchScreen and typ == absType:996 if cod in absXCodes:997 val, _ = touchScreen.absToScreenXY(val, 0)998 elif cod in absYCodes:999 _, val = touchScreen.absToScreenXY(0, val)1000 if typ in allowedTypes:1001 queue.put((ts, tus, typ, cod, val))1002 finally:1003 os.close(fd)1004# _g_recQL dictionary contains events being actively recorded1005# - key: filename, like "/dev/input/event0"1006# - value: (eventQueue, lock)1007# A thread is filling eventQueue with events from filename.1008# Once the lock is released, the thread will quit without writing1009# anything to the eventQueue anymore.1010_g_recQL = {}1011_g_unfetchedEvents = []1012def queueEventsFromFiles(listOfFilenames, filterOpts):1013 global _g_recQL...
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!!