def find_on_screen(image): needle = cv2.imread(image) screen = display.Display().screen() width, height = screen.width_in_pixels, screen.height_in_pixels haystack = numpy.array(screencap(0, 0, width, height)) return needle.shape[1::-1], find_best(needle, haystack)
def mousePixel(): data = display.Display().screen().root.query_pointer()._data return str(data["root_x"]) + "," + str(data["root_y"])
elif e.event_x == 0: self.window.warp_pointer(geometry.width - 2, e.event_y) prev_x = geometry.width - 2 else: prev_x = e.event_x if e.event_y == geometry.height - 1: self.window.warp_pointer(e.event_x, 1) prev_y = 1 elif e.event_y == 0: self.window.warp_pointer(e.event_x, geometry.height - 2) prev_y = geometry.height - 2 else: prev_y = e.event_y if __name__ == '__main__': DBusGMainLoop(set_as_default=True) service_record = open("sdp_record_kbd.xml").read() d = display.Display() d.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOff) try: bthid_srv = BluetoothHIDService(service_record) Window(d).loop(bthid_srv.send) #Window(d).loop(print) finally: d.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOn) d.get_keyboard_control() d.ungrab_keyboard(X.CurrentTime) d.ungrab_pointer(X.CurrentTime) print("Exit")
def mouse_move(x, y): disp = display.Display() root = disp.screen().root root.warp_pointer(x, y) disp.sync()
import ctypes import os, sys from PIL import Image from Xlib import display LibName = 'prtscn_dll_x11.so' AbsLibPath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + LibName grab = ctypes.CDLL(AbsLibPath) dsp = display.Display(None) geo = dsp.screen().root.get_geometry() def grabScreen(x1,y1,x2,y2): x1,y1 = max(x1, 0), max(y1, 0) if x2 > geo.width: x2 = geo.width if y2 > geo.height: y2 = geo.height W, H = min(x2-x1, geo.width), min(y2-y1, geo.height) if W < 0: W = 1 if H < 0: H = 1 size = W * H objlength = size * 3 grab.getScreen.argtypes = [] result = (ctypes.c_ubyte*objlength)() grab.getScreen(x1,y1, W, H, result) return Image.frombuffer('RGB', (W, H), result, 'raw', 'RGB', 0, 1)
default="%ld\t%s") parser.add_option( "-r", "--range", dest="range", help= "This option specifies the range of atom values to check. If low is not given, a value of 1 assumed. If high is not given, xlsatoms will stop at the first undefined atom at or above low.", metavar="[low]-[high]", default="1-") (options, args) = parser.parse_args() low = 1 high = 1 ec = error.CatchError(error.BadAtom) d = display.Display(options.display) def print_atom(print_format, atom, value): print(print_format % (atom, value)) def list_atoms(d, re_obj, low, high): while (low <= high): try: val = d.get_atom_name(low) if (re_obj == None): print_atom(options.format, low, val) elif re_obj.match(val) != None: print_atom(options.format, low, val) low += 1
def mouse_move(s): data = display.Display().screen().root.query_pointer()._data s.send_int(data["root_x"]) s.send_int(data["root_y"])
if ev.count == 0: self.first.draw() if self.last: # Redraw all the lines self.win.window.poly_line(self.win.gc, X.CoordModeOrigin, self.lines) self.last.draw() # A romboid, drawed around the Movement endpoints class Romboid(object): def __init__(self, win, ev): self.win = win self.x = ev.event_x self.y = ev.event_y self.draw() def draw(self): # Draw the segments of the romboid self.win.window.poly_line(self.win.gc, X.CoordModePrevious, [(self.x, self.y - 5), (5, 5), (-5, 5), (-5, -5), (5, -5)]) if __name__ == '__main__': Window(display.Display()).loop()
async def prepare_display(): """Fills up the display supplies.""" if len(PREPARED_DISPLAYS) < DISPLAY_SUPPLIES: PREPARED_DISPLAYS.append(Xdisplay.Display())
def on_init(self, controller): import subprocess, re from time import sleep # Get screen resolution displ = display.Display() s = displ.screen() HSNListener.screenx = s.width_in_pixels HSNListener.screeny = s.height_in_pixels print "Screen size is:", HSNListener.screenx, HSNListener.screeny # Initialize midi # subprocess.call(['aconnect',midiclientout,midiclientin]) print "If your keyboard doesn't play, try to find the device input and output" print 'aconnect -i' print 'aconnect -o' print 'And then:' print 'aconnect output_device input_device' port = -1 options = list() for tryport in range(40): try: name = pygame.midi.get_device_info(tryport)[1] if name != '': print 'Port ', tryport, ':', name options.append(tryport) except: donothing = 1 while port < 0: print 'Choose one of the above. Options are:', options try: port = int(raw_input()) except: print 'Answer a number from this list:', options if port == -1: print 'Error finding MIDI port' pygame.midi.quit() os._exit(1) del HSNListener.midi try: HSNListener.midi = pygame.midi.Output(port) except: print "Couldn't connect to port, trying the default" HSNListener.midi = pygame.midi.Output(0) # Open file with debug output if HSNListener.debug == 1: f = open('output.txt', 'w') for ind in range(5): f.write(str(ind) + '\n') f.close() # Create frame buffer print 'Initializing Leap Motion...' itry = 0 while (not controller.frame(2 * HSNListener.nframes).is_valid): itry += 1 if itry == 1e5: print "Something's not right. Make sure the Leap Motion is connected" print 'and the leapd daemon is running' pygame.midi.quit() os._exit(1) for iframe in range(HSNListener.nframes): HSNListener.frames.append(controller.frame(iframe)) HSNListener.prevframes.append( controller.frame(iframe + HSNListener.nframes)) print "Frame buffer sizes:", len(HSNListener.frames), len( HSNListener.prevframes) print "Initialized"
def configure(window): window.configure( width=100, height=100, x=0, y=0, border_width=1, ) for i in range(20): try: # Remove useless lib print stdout, sys.stdout = sys.stdout, StringIO() d = display.Display(sys.argv[1]) sys.stdout = stdout except error.DisplayConnectionError: time.sleep(0.1) continue except Exception as v: print >> sys.stderr, "Error opening test window: ", type(v), v sys.exit(1) break else: print >> sys.stderr, "Could not open window on display %s" % (sys.argv[1]) sys.exit(1) root = d.screen().root colormap = d.screen().default_colormap
def __init__(self, _display=None, root=None): self.display = _display or display.Display() self.root = root or self.display.screen().root
def keyboard_forward(window, forward): """ XXX: Keyboard events forwardind This is kind of hack needed to properly function inside Tk windows. Gtk app will receive _all_ events, even not needed. So we have to forward back things that left over after our widgets. Connect handlers _after_ all others and listen for key-presss and key-release events. If key is not in ignore list - forward it to window id found in evironment. """ if not forward: return try: forward = int(forward, 0) except: return from Xlib.protocol import event from Xlib import display, X from Xlib.xobject import drawable d = display.Display() fw = drawable.Window(d.display, forward, 0) ks = gtk.keysyms ignore = [ ks.Tab, ks.Page_Up, ks.Page_Down, ks.KP_Page_Up, ks.KP_Page_Down, ks.Left, ks.Right, ks.Up, ks.Down, ks.KP_Left, ks.KP_Right, ks.KP_Up, ks.KP_Down, ks.bracketleft, ks.bracketright ] def gtk2xlib(e, fw, g, type=None): if type is None: type = e.type if type == gtk.gdk.KEY_PRESS: klass = event.KeyPress elif type == gtk.gdk.KEY_RELEASE: klass = event.KeyRelease else: return kw = dict(window=fw, detail=e.hardware_keycode, state=e.state & 0xff, child=X.NONE, root=g._data['root'], root_x=g._data['x'], root_y=g._data['y'], event_x=0, event_y=0, same_screen=1) return klass(time=e.time, **kw) def forward(w, e, fw): if e.keyval in ignore: return g = fw.get_geometry() fe = gtk2xlib(e, fw, g) if not fe: return fw.send_event(fe) window.connect_after("key-press-event", forward, fw) window.connect("key-release-event", forward, fw) window.add_events(gtk.gdk.KEY_PRESS_MASK) window.add_events(gtk.gdk.KEY_RELEASE_MASK)
def mouse_location(self): display = ds.Display() coord = display.screen().root.query_pointer()._data print("mouse at:\t\t %d,%d" % (coord["root_x"], coord["root_y"]))
""" Colors, that are used for drawing contours ACTIVE_TRACKING_DISTANCE - limit of active zone Xlib.display - displaying for mouse control """ from Xlib import display BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) PURPLE = (255, 0, 255) BLUE = (0, 0, 255) WHITE = (255, 255, 255) YELLOW = (255, 255, 0) ACTIVE_TRACKING_DISTANCE = 30 DISPLAY = display.Display()
def find_on_screen(image): needle = cv2.imread(image) screen = display.Display().screen() haystack = screencap(screen.width_in_pixels, screen.height_in_pixels) return needle.shape[1::-1], find_best(needle, haystack)
def _is_meeting_window_open() -> bool: """ Zoom opens window "zoom - free account" (on ubuntu, gnome-shell) """ list_windows = display.Display().screen().root.query_tree() window_names = [x.get_wm_name().lower() for x in list_windows.children] return 'zoom -' in window_names
class Backend(object): __metaclass__ = ABCMeta mouse_sensivity = 0.3 scroll_sensivity = 12 display_ = display.Display() screen = display_.screen() def get_current_workspace(self): p1 = subprocess.Popen(['wmctrl', '-d'], stdout=subprocess.PIPE) p2 = subprocess.Popen(['awk', "/\*/ {print $1}"], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() return int(''.join([i for i in p2.communicate()[0] if i.isdigit()])) def _find_in_haystack(self, haystack, needle): for k, v in enumerate(haystack): if needle == v: return k try: if needle in v: return k except TypeError: continue def get_position(self, haystack, needle): y = self._find_in_haystack(haystack, needle) return [self._find_in_haystack(haystack[y], needle), y] def generate_workspace_matrix(self, num_total, num_cols): def chunks(l, n): """ Yield successive n-sized chunks from l. """ for i in xrange(0, len(l), n): yield l[i:i + n] chunk_size = math.ceil(num_total / float(num_cols)) return list(chunks(range(num_total), int(chunk_size))) def find_new_position(self, workspaces, current_position, direction): """ Calculates new workspace position """ move = 0, 0 if direction[0] > self.mouse_sensivity: # move right move = 1, 0 elif direction[0] < -self.mouse_sensivity: # move left move = -1, 0 elif direction[1] > self.mouse_sensivity: # move up move = 0, -1 elif direction[1] < -self.mouse_sensivity: # move down move = 0, 1 new_position = copy(current_position) for k, v in enumerate(move): new_position[k] += v try: self.get_workspace_by_position(workspaces, new_position) except IndexError: del new_position return current_position else: del current_position return new_position def get_workspace_by_position(self, workspaces, position): """ Returns workspace id by given position """ return workspaces[position[1]][position[0]] def move_to_workspace(self, workspace_id): """ Calls WM move to """ subprocess.call(['wmctrl', '-s', str(workspace_id)]) @abstractmethod def lock_screen(self): pass def get_screen_size(self): return [self.screen.width_in_pixels, self.screen.height_in_pixels] def process_pointer(self, pos): x = (pos[0] * (self.screen.width_in_pixels / 250)) + ( self.screen.width_in_pixels / 2) x = min(x, self.screen.width_in_pixels) x = max(x, 0) y = self.screen.height_in_pixels - (pos[1] * (self.screen.height_in_pixels / 250)) \ + (self.screen.height_in_pixels / 2) y = min(y, self.screen.height_in_pixels) y = max(y, 0) self.screen.root.warp_pointer(x, y) self.display_.flush() def click(self): ext.xtest.fake_input(self.display_, X.ButtonPress, 1) ext.xtest.fake_input(self.display_, X.ButtonRelease, 1) self.display_.flush() def scroll(self, pitch): if pitch >= self.scroll_sensivity: ext.xtest.fake_input(self.display_, X.KeyPress, 112) ext.xtest.fake_input(self.display_, X.KeyRelease, 112) elif pitch <= -self.scroll_sensivity: ext.xtest.fake_input(self.display_, X.KeyPress, 117) ext.xtest.fake_input(self.display_, X.KeyRelease, 117) self.display_.flush() def start_task_switcher(self): ext.xtest.fake_input(self.display_, X.KeyPress, 64) ext.xtest.fake_input(self.display_, X.KeyPress, 23) ext.xtest.fake_input(self.display_, X.KeyRelease, 23) self.display_.flush() def release_task_switcher(self): ext.xtest.fake_input(self.display_, X.KeyRelease, 64) self.display_.flush() def switch_task(self, frame, gesture): the_hand = gesture.hands.frontmost # guesses which hand made the gesture (left or right) left_hand_gesture = False for hand in frame.hands: if hand != the_hand: if hand.stabilized_palm_position[ 0] >= the_hand.stabilized_palm_position[0]: # gesture with left hand, shift is pressed ext.xtest.fake_input(self.display_, X.KeyPress, 50) left_hand_gesture = True # tab is pressed anyway on both hands ext.xtest.fake_input(self.display_, X.KeyPress, 23) ext.xtest.fake_input(self.display_, X.KeyRelease, 23) if left_hand_gesture: ext.xtest.fake_input(self.display_, X.KeyRelease, 50) self.display_.flush()
# How do I find which monitor the mouse pointer is in with python? from Xlib import display data = display.Display().screen().root.query_pointer()._data locationtuple = (data["root_x"], data["root_y"])
def __init__(self): self.display = display.Display() self.root = self.display.screen().root self.keys = {}
def get_resolution(): d = display.Display().screen() return (d.width_in_pixels, d.height_in_pixels)
def actualise_camera(self, dt): self.painter.canvas.after.clear() self.painter.canvas.before.clear() (depth, _) = get_depth() # Get the depth from the kinect depth = depth.astype(np.float32) # Convert the depth to a 32 bit float _, depthThresh = cv2.threshold(depth, self.distance_hand_is_close, 255, cv2.THRESH_BINARY_INV) # Threshold the depth for a binary image. Thresholded at 600 arbitary units _, back = cv2.threshold(depth, self.distance_background, 255, cv2.THRESH_BINARY_INV) # Threshold the background in order to have an outlined background and segmented foreground _,wayBack = cv2.threshold(depth, 2000, 255, cv2.THRESH_BINARY_INV) blobData = BlobAnalysis(depthThresh) # Creates blobData object using BlobAnalysis class blobDataBack = BlobAnalysis(back) # Creates blobDataBack object using BlobAnalysis class blobDataWayBack = BlobAnalysis(wayBack) if(blobDataWayBack.counter != 0 and blobDataBack.counter==0): if(self.time==0): self.parent.check_users() self.time=100 else: self.time-=1 else: self.time=0 if(blobDataBack.counter==0):#not interractive self.parent.hide_overlay() for i in range(blobData.counter): # Iterate from 0 to the number of blobs minus 1 self.centroidList.append(blobData.centroid[i]) # Adds the centroid tuple to the centroidList --> used for drawing else: self.parent.show_overlay() # draw contours with self.painter.canvas.before: PushMatrix() Rotate(angle=180, axis=(0, 0, 1), origin=self.painter.center) Translate(self.painter.width / 2 - 320, self.painter.height / 2 - 240) for i in range(blobData.counter): # Iterate from 0 to the number of blobs minus 1 self.centroidList.append( blobData.centroid[i]) # Adds the centroid tuple to the centroidList --> used for drawing Color(self.RED[0], self.RED[1], self.RED[2], mode='rgb') Line(circle=(blobData.centroid[i][0], blobData.centroid[i][1], 20)) with self.painter.canvas.after: PopMatrix() del depth # Deletes depth --> opencv memory issue # Mouse Try statement try: centroidX = blobData.centroid[0][0] centroidY = blobData.centroid[0][1] if self.dummy: mousePtr = display.Display().screen().root.query_pointer()._data # Gets current mouse attributes dX = centroidX - self.strX # Finds the change in X dY = self.strY - centroidY # Finds the change in Y if abs(dX) > 1: # If there was a change in X greater than 1... mouseX = mousePtr["root_x"] - self.sensitivity_x * dX # New X coordinate of mouse if abs(dY) > 1: # If there was a change in Y greater than 1... mouseY = mousePtr["root_y"] - self.sensitivity_y * dY # New Y coordinate of mouse self.move_mouse(mouseX, mouseY) # Moves mouse to new location self.strX = centroidX # Makes the new starting X of mouse to current X of newest centroid self.strY = centroidY # Makes the new starting Y of mouse to current Y of newest centroid else: self.strX = centroidX # Initializes the starting X self.strY = centroidY # Initializes the starting Y self.dummy = True # Lets the function continue to the first part of the if statement except: # There may be no centroids and therefore blobData.centroid[0] will be out of range self.dummy = False # Waits for a new starting point
def get_cursor_pos(): d = display.Display().screen().root.query_pointer() return (d.root_x, d.root_y)
def screencap(x, y, w, h): root = display.Display().screen().root raw = root.get_image(x, y, w, h, X.ZPixmap, 0xffffffff) return numpy.fromstring(raw.data, dtype=numpy.uint8).reshape(h, w, 4)[:, :, :3]
def get_startup_notification_timestamp(self, bamf_window): atom = display.Display().intern_atom('_NET_WM_USER_TIME') atom_type = display.Display().intern_atom('CARDINAL') return bamf_window.x_win.get_property(atom, atom_type, 0, 1024).value[0]
def mouse_click(button=1): disp = display.Display() fake_input(disp, X.ButtonPress, button) disp.sync() fake_input(disp, X.ButtonRelease, button) disp.sync()
if keysym == VOLDOWN: change = -0.05 if keysym == VOLUP: change = 0.05 if keysym == VOLMUTE: change = None subprocess.check_output([ 'pactl', '--', 'set-sink-{}'.format('volume' if change else 'mute'), # NOTE: This could result in an argument of '+-0.5%' but they cancel sufficiently '0', '+{0:.0%}'.format(change) if change else 'toggle' ]) dpy = display.Display() assert dpy.has_extension("RECORD") ctx = dpy.record_create_context(0, [record.AllClients], [{ 'core_requests': (0, 0), 'core_replies': (0, 0), 'ext_requests': (0, 0, 0, 0), 'ext_replies': (0, 0, 0, 0), 'delivered_events': (0, 0), 'device_events': (X.KeyPress, X.MotionNotify), 'errors': (0, 0), 'client_started': False, 'client_died': False }])
class KeyServiceXlib(QThread): local_dpy = display.Display() record_dpy = display.Display() main_window = None ctx = None is_modify_key_press = False is_active = False signal_switch_select = pyqtSignal() signal_switch_confirm = pyqtSignal() signal_tip_capslock = pyqtSignal() signal_tip_numlock = pyqtSignal() def __init__(self, main_window): QThread.__init__(self) self.main_window = main_window self.signal_switch_select.connect(self.main_window.slot_switch_select) self.signal_switch_confirm.connect( self.main_window.slot_switch_confirm) self.signal_tip_capslock.connect(self.main_window.slot_tip_capslock) self.signal_tip_numlock.connect(self.main_window.slot_tip_numlock) def run(self): self.check_record_extension() # Create a recording context; we only want key and mouse events self.ctx = self.record_dpy.record_create_context( 0, [record.AllClients ], [{ 'core_requests': (0, 0), 'core_replies': (0, 0), 'ext_requests': (0, 0, 0, 0), 'ext_replies': (0, 0, 0, 0), 'delivered_events': (0, 0), 'device_events': (X.KeyPress, X.MotionNotify), 'errors': (0, 0), 'client_started': False, 'client_died': False, }]) # Enable the context; this only returns after a call to record_disable_context, # while calling the callback function in the meantime self.record_dpy.record_enable_context(self.ctx, self.record_callback) # Finally free the context self.record_dpy.record_free_context(self.ctx) def check_record_extension(self): if not self.record_dpy.has_extension("RECORD"): print("RECORD extension not found") sys.exit(1) def lookup_keysym(self, keysym): for name in dir(XK): if name[:3] == "XK_" and getattr(XK, name) == keysym: return name[3:] else: return keysym def record_callback(self, reply): if reply.category != record.FromServer: return if reply.client_swapped: return if not len(reply.data) or reply.data[0] < 2: return data = reply.data while len(data): event, data = rq.EventField(None).parse_binary_value( data, self.record_dpy.display, None, None) if event.type in [X.KeyPress, X.KeyRelease]: keysym = self.local_dpy.keycode_to_keysym(event.detail, 0) if event.type == X.KeyPress: if keysym == XK.XK_Super_L: self.is_modify_key_press = True if keysym == XK.XK_F3 or keysym == XK.XK_F7 or keysym == XK.XK_p: if self.is_modify_key_press == True: self.is_active = True self.signal_switch_select.emit() # dev, press ESC to exit # if keysym == XK.XK_Escape: # self.local_dpy.record_disable_context(self.ctx) # self.local_dpy.flush() # sys.exit(0) elif event.type == X.KeyRelease: if (keysym == XK.XK_Super_L): self.is_modify_key_press = False if (self.is_active == True): self.is_active = False self.signal_switch_confirm.emit() if keysym == XK.XK_Caps_Lock: self.signal_tip_capslock.emit() if keysym == XK.XK_Num_Lock: self.signal_tip_numlock.emit()
def get_features(data_path): current_brightness = get_current_brightness() brightess_change = change_brightness() # local time (does not have microsecond) now = datetime.strptime(time.strftime(DATE_FORMAT, time.localtime()), DATE_FORMAT) microsecond = datetime.now().microsecond # add microsecond for uniqueness now = now.replace(microsecond=microsecond) datetime_full = str(now) # paths data_path = os.path.expanduser(data_path) image_path = os.path.join(data_path, "images") disp = display.Display() root = disp.screen().root geo = root.get_geometry() # active window active_window = disp.get_input_focus().focus active_window_geo = active_window.get_geometry() # image file path image_file_path = get_image_file_path(image_path, datetime_full) save_image(root, geo, image_file_path) # mouse mouse_data = root.query_pointer() all_data = { "ambient_light": get_ambient_light(), "current_brightness": current_brightness, "brightness_change": brightess_change, "max_brightness": get_max_brightness(), "mouse_x": mouse_data.root_x, "mouse_y": mouse_data.root_y, "active_window_name": active_window.get_wm_name(), # Out[60]: '*IPython3*' "active_window_class": " ".join(active_window.get_wm_class()), # Out: ('emacs', 'Emacs'), "image_file_path": image_file_path, # just because we can "whereami": get_whereami(), # seems wrong "active_window_x": active_window_geo.x, "active_window_y": active_window_geo.y, "active_window_width": active_window_geo.width, "active_window_height": active_window_geo.height, "root_width": geo.width, "root_height": geo.height, "datetime_full": datetime_full, # TODO: do something with hour + timezone w.r.t. sunrize / sunset "datetime_hour": now.hour, "datetime_timezone": str(now.tzinfo), "datetime_time": str(now.time()), "datetime_date": str(now.date()), } with open(data_path + "/data.jsonl", "a") as f: f.write(json.dumps(all_data) + "\n") print(json.dumps(all_data, indent=4))