Merge pull request #13 from CodePanter/master
Updated sdlvloed.py so it has the same functionality as vloed.py.
This commit is contained in:
commit
7bfbdb6bdc
2 changed files with 50 additions and 41 deletions
80
sdlvloed.py
80
sdlvloed.py
|
|
@ -12,17 +12,19 @@ __author__ = "Jan Klopper <jan@underdark.nl>"
|
||||||
|
|
||||||
# import gevent monkeypatching and perform patch_all before anything else to
|
# import gevent monkeypatching and perform patch_all before anything else to
|
||||||
# avoid nasty eception on python closing time
|
# avoid nasty eception on python closing time
|
||||||
from gevent import spawn, monkey
|
if __name__ == '__main__':
|
||||||
monkey.patch_all()
|
from gevent import spawn, monkey
|
||||||
|
monkey.patch_all()
|
||||||
|
|
||||||
|
try:
|
||||||
|
import gtk
|
||||||
|
except ImportError:
|
||||||
|
gtk = None
|
||||||
|
|
||||||
import sdl2.ext
|
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from gevent.server import DatagramServer
|
|
||||||
from gevent.queue import Queue
|
|
||||||
|
|
||||||
UDP_IP = "127.0.0.1"
|
UDP_IP = "127.0.0.1"
|
||||||
UDP_PORT = 5005
|
UDP_PORT = 5005
|
||||||
DISCOVER_PORT = 5006
|
DISCOVER_PORT = 5006
|
||||||
|
|
@ -30,8 +32,8 @@ PROTOCOL_VERSION = 1
|
||||||
MAX_PROTOCOL_VERSION = 1
|
MAX_PROTOCOL_VERSION = 1
|
||||||
PROTOCOL_PREAMBLE = "pixelvloed"
|
PROTOCOL_PREAMBLE = "pixelvloed"
|
||||||
MAX_PIXELS = 140
|
MAX_PIXELS = 140
|
||||||
DEFAULT_WIDTH = 786
|
DEFAULT_WIDTH = 1366
|
||||||
DEFAULT_HEIGHT = 1366
|
DEFAULT_HEIGHT = 786
|
||||||
|
|
||||||
class Canvas(object):
|
class Canvas(object):
|
||||||
"""PixelVloed display class"""
|
"""PixelVloed display class"""
|
||||||
|
|
@ -42,11 +44,9 @@ class Canvas(object):
|
||||||
self.pixeloffset = 2
|
self.pixeloffset = 2
|
||||||
self.fps = 30
|
self.fps = 30
|
||||||
self.screen = None
|
self.screen = None
|
||||||
self.udp_ip = UDP_IP
|
self.udp_ip = options.ip if options.ip else UDP_IP
|
||||||
self.udp_port = UDP_PORT
|
self.udp_port = options.port if options.port else UDP_PORT
|
||||||
self.factor = options.factor if options.factor else 1
|
self.factor = options.factor if options.factor else 1
|
||||||
self.width = options.width if options.width else DEFAULT_WIDTH
|
|
||||||
self.height = options.height if options.height else DEFAULT_HEIGHT
|
|
||||||
self.canvas()
|
self.canvas()
|
||||||
|
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
|
|
@ -67,6 +67,13 @@ class Canvas(object):
|
||||||
def canvas(self):
|
def canvas(self):
|
||||||
"""Init the pygame canvas"""
|
"""Init the pygame canvas"""
|
||||||
sdl2.ext.init()
|
sdl2.ext.init()
|
||||||
|
if gtk:
|
||||||
|
window = gtk.Window()
|
||||||
|
screen = window.get_screen()
|
||||||
|
self.width = gtk.gdk.screen_width() if gtk else DEFAULT_WIDTH
|
||||||
|
self.height = gtk.gdk.screen_height() if gtk else DEFAULT_HEIGHT
|
||||||
|
self.width = options.width if options.width else self.width
|
||||||
|
self.height = options.height if options.height else self.height
|
||||||
self.screen = sdl2.ext.Window(self.set_title(),
|
self.screen = sdl2.ext.Window(self.set_title(),
|
||||||
size=(self.width, self.height))
|
size=(self.width, self.height))
|
||||||
self.screen.show()
|
self.screen.show()
|
||||||
|
|
@ -163,7 +170,7 @@ class Canvas(object):
|
||||||
self.broadcastsocket.sendto(
|
self.broadcastsocket.sendto(
|
||||||
'%s:%f %s:%d %d*%d' % (
|
'%s:%f %s:%d %d*%d' % (
|
||||||
PROTOCOL_PREAMBLE, PROTOCOL_VERSION,
|
PROTOCOL_PREAMBLE, PROTOCOL_VERSION,
|
||||||
UDP_IP, UDP_PORT,
|
self.udp_ip, self.udp_port,
|
||||||
self.width/self.factor, self.height/self.factor),
|
self.width/self.factor, self.height/self.factor),
|
||||||
('<broadcast>', DISCOVER_PORT))
|
('<broadcast>', DISCOVER_PORT))
|
||||||
if self.debug:
|
if self.debug:
|
||||||
|
|
@ -176,21 +183,6 @@ class Canvas(object):
|
||||||
"""Clean up any sockets we created"""
|
"""Clean up any sockets we created"""
|
||||||
self.broadcastsocket.close()
|
self.broadcastsocket.close()
|
||||||
|
|
||||||
class PixelVloedServer(DatagramServer):
|
|
||||||
"""PixelVloed server class"""
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
"""Set up some vars for this instance"""
|
|
||||||
self.queue = Queue()
|
|
||||||
pixelcanvas = Canvas(self.queue, kwargs['options'])
|
|
||||||
__request_processing_greenlet = spawn(pixelcanvas.CanvasUpdate)
|
|
||||||
del (kwargs['options'])
|
|
||||||
DatagramServer.__init__(self, *args, **kwargs)
|
|
||||||
|
|
||||||
def handle(self, data, _address):
|
|
||||||
"""Is called by the DataGramServer whenever an udp package is received"""
|
|
||||||
self.queue.put(data)
|
|
||||||
|
|
||||||
class PixelVloedClient(object):
|
class PixelVloedClient(object):
|
||||||
"""Sets up a client
|
"""Sets up a client
|
||||||
|
|
||||||
|
|
@ -237,16 +229,16 @@ class PixelVloedClient(object):
|
||||||
"""Sleeps the designated amount of time"""
|
"""Sleeps the designated amount of time"""
|
||||||
time.sleep(duration if duration else self.sleep)
|
time.sleep(duration if duration else self.sleep)
|
||||||
|
|
||||||
def SendPacket(self, message, sleep=True):
|
def SendPacket(self, message, sleep=0.01):
|
||||||
"""Sends the message to the udp server
|
"""Sends the message to the udp server
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
message: (str, 140)
|
message: (str, 140)
|
||||||
sleep: (bool) True, should the client sleep for a while?
|
sleep: (float) 0.01, duration of time the client should sleep
|
||||||
"""
|
"""
|
||||||
self.sock.sendto(message, (self.ipaddress, self.port))
|
self.sock.sendto(message, (self.ipaddress, self.port))
|
||||||
if sleep:
|
if sleep:
|
||||||
self.Sleep()
|
self.Sleep(duration=0.01)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def DiscoverServers(returnfirst=False, timeout=5):
|
def DiscoverServers(returnfirst=False, timeout=5):
|
||||||
|
|
@ -326,16 +318,34 @@ def RunServer(options):
|
||||||
options=options).serve_forever()
|
options=options).serve_forever()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
import sdl2.ext
|
||||||
|
|
||||||
|
from gevent.server import DatagramServer
|
||||||
|
from gevent.queue import Queue
|
||||||
|
|
||||||
|
class PixelVloedServer(DatagramServer):
|
||||||
|
"""PixelVloed server class"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
"""Set up some vars for this instance"""
|
||||||
|
self.queue = Queue()
|
||||||
|
pixelcanvas = Canvas(self.queue, kwargs['options'])
|
||||||
|
__request_processing_greenlet = spawn(pixelcanvas.CanvasUpdate)
|
||||||
|
del (kwargs['options'])
|
||||||
|
DatagramServer.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
|
def handle(self, data, _address):
|
||||||
|
"""Is called by the DataGramServer whenever an udp package is received"""
|
||||||
|
self.queue.put(data)
|
||||||
|
|
||||||
import optparse
|
import optparse
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser()
|
||||||
parser.add_option('-v', action="store_true", dest="debug", default=False)
|
parser.add_option('-v', action="store_true", dest="debug", default=False)
|
||||||
parser.add_option('-i', action="store", dest="ip", default=UDP_IP)
|
parser.add_option('-i', action="store", dest="ip", default=UDP_IP)
|
||||||
parser.add_option('-p', action="store", dest="port", default=UDP_PORT,
|
parser.add_option('-p', action="store", dest="port", default=UDP_PORT,
|
||||||
type="int")
|
type="int")
|
||||||
parser.add_option('-x', action="store", dest="width", default=DEFAULT_WIDTH,
|
parser.add_option('-x', action="store", dest="width", type="int")
|
||||||
type="int")
|
parser.add_option('-y', action="store", dest="height", type="int")
|
||||||
parser.add_option('-y', action="store", dest="height", default=DEFAULT_HEIGHT,
|
|
||||||
type="int")
|
|
||||||
parser.add_option('-m', action="store", dest="maxpixels", default=MAX_PIXELS,
|
parser.add_option('-m', action="store", dest="maxpixels", default=MAX_PIXELS,
|
||||||
type="int")
|
type="int")
|
||||||
parser.add_option('-f', action="store", dest="factor", default=1,
|
parser.add_option('-f', action="store", dest="factor", default=1,
|
||||||
|
|
|
||||||
3
vloed.py
3
vloed.py
|
|
@ -9,7 +9,7 @@ __version__ = 0.3
|
||||||
__author__ = "Jan Klopper <jan@underdark.nl>"
|
__author__ = "Jan Klopper <jan@underdark.nl>"
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# avoid nasty eception on python closing time
|
# avoid nasty exception on python closing time
|
||||||
from gevent import spawn, monkey
|
from gevent import spawn, monkey
|
||||||
monkey.patch_all()
|
monkey.patch_all()
|
||||||
|
|
||||||
|
|
@ -58,7 +58,6 @@ class Canvas(object):
|
||||||
"""Init the pygame canvas"""
|
"""Init the pygame canvas"""
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screeninfo = pygame.display.Info()
|
screeninfo = pygame.display.Info()
|
||||||
print options.width
|
|
||||||
self.width = options.width if options.width else screeninfo.current_w
|
self.width = options.width if options.width else screeninfo.current_w
|
||||||
self.height = options.height if options.height else screeninfo.current_h
|
self.height = options.height if options.height else screeninfo.current_h
|
||||||
pygame.mixer.quit()
|
pygame.mixer.quit()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue