Skip to content

Commit

Permalink
updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
perara committed Nov 28, 2017
1 parent fdaec09 commit c57c3dc
Show file tree
Hide file tree
Showing 5 changed files with 1,360 additions and 1,332 deletions.
28 changes: 23 additions & 5 deletions pyVNC/Client.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from threading import Thread
from twisted.internet import reactor
from twisted.internet import reactor, task
import pygame
import time
from pyVNC import constants
from pyVNC.Buffer import DisplayBuffer, ArrayBuffer
from pyVNC.VNCFactory import VNCFactory

import logging
logger = logging.getLogger("pyVNC")

class Client(Thread):
def __init__(self, host="127.0.0.1", password=None, port=5902, depth=32, fast=False, shared=True, gui=False, gui_with_array=False):
def __init__(self, host="127.0.0.1", password=None, port=5902, depth=32, fast=False, shared=True, gui=False, array=False, callbacks=[]):
Thread.__init__(self)
pygame.init()
self.has_gui = gui
self.screen = DisplayBuffer(gui_with_array) if gui else ArrayBuffer()
self.screen = DisplayBuffer(array) if gui else ArrayBuffer()
self.host = host
self.password = password
self.port = port
self.depth = depth
self.fast = fast
self.shared = shared
self.callbacks = callbacks

def send_key(self, key, duration=0.001):
if key in constants.MODIFIERS:
Expand All @@ -28,7 +30,7 @@ def send_key(self, key, duration=0.001):
elif type(key) == str:
self.screen.protocol.key_event(ord(key))

time.sleep(.001)
time.sleep(duration)

if key in constants.MODIFIERS:
self.screen.protocol.key_event(constants.MODIFIERS[key], down=0)
Expand All @@ -48,6 +50,11 @@ def send_mouse(self, event="Left", position=(0, 0)):
self.screen.protocol.pointer_event(position[0], position[1], 0)
self.screen.protocol.pointer_event(position[0], position[1], button_id)

def add_callback(self, interval, cb):
l = task.LoopingCall(cb)
l.start(interval)


def run_block(self):
reactor.connectTCP(
self.host, # remote hostname
Expand All @@ -61,6 +68,17 @@ def run_block(self):
)
)

# Create callbacks
for cb_pair in self.callbacks:
try:
fps, cb = cb_pair
interval = fps / 1000
self.add_callback(interval, cb)

except:
logger.error("Callbacks must be formed as (fps, callback_fn)")


# run the application
reactor.callLater(0.1, self.screen.loop)
reactor.run(installSignalHandlers=False)
Expand Down
16 changes: 9 additions & 7 deletions pyVNC/VNCFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from twisted.internet import reactor
from pyVNC.RFBToGUI import RFBToGUIeightbits, RFBToGUI
from pyVNC.rfb import *
import logging
logger = logging.getLogger("pyVNC")


class VNCFactory(RFBFactory):
Expand All @@ -10,6 +12,7 @@ class VNCFactory(RFBFactory):
def __init__(self, buffer, depth, fast, *args, **kwargs):
RFBFactory.__init__(self, *args, **kwargs)
self.buffer = buffer

if depth == 32:
self.protocol = RFBToGUI
elif depth == 8:
Expand All @@ -32,15 +35,14 @@ def __init__(self, buffer, depth, fast, *args, **kwargs):
]

def buildProtocol(self, addr):
display = addr.port - 5900
pygame.display.set_caption('pyVNC on %s:%s' % (addr.host, display))

pygame.display.set_caption('pyVNC on %s:%s' % (addr.host, addr.port))
return RFBFactory.buildProtocol(self, addr)

def clientConnectionLost(self, connector, reason):
log.msg("connection lost: %r" % reason.getErrorMessage())
#reactor.stop()
logging.error("Connection lost: %r" % reason.getErrorMessage())
logging.error("Attempting to reconnect")
connector.connect()

def clientConnectionFailed(self, connector, reason):
log.msg("cannot connect to server: %r\n" % reason.getErrorMessage())
reactor.stop()
logging.error("Could not connect to the server with reason: %r" % reason.getErrorMessage())
reactor.stop()
Loading

0 comments on commit c57c3dc

Please sign in to comment.