#!/usr/bin/python

"""base class for writing xscreensaver modules
"""

import Xlib.display
import random
import time
import os
import sys

import Image

# Sample usage:
"""
import xss_base

class myxss(xss_base.xss_base):
    def render(self):
        use self.window, self.gc, etc...
"""

# used as
# options, args = xss_base.parse_args(parser)
def parse_args(parser, args=None):
    """fix up -window-id for arg parser"""
    if args is None:
        args = sys.argv[1:]
    args = map(lambda arg: arg.replace("-window-id",
                                       "--window-id"),
               args)
    parser.add_option("--window-id")
    return parser.parse_args(args=args)


class xss_base: # base on object?
    """xscreensaver module base.

    children can expect self.display, self.window, and a self.gc
    also self.rootscreen
    children can of course store their own stuff
    
    self.delay defaults to 0.1 seconds between calls to render()
    """
    delay = 0.1
    def __init__(self):
        self.display = Xlib.display.Display()
        self.rootscreen = self.display.screen()

        window = os.getenv('XSCREENSAVER_WINDOW')
        if window:
            window = int(window, 16)
            window = self.display.create_resource_object('window', window)
        else:
            window = self.rootscreen.root

        self.window = window
        self.gc = self.window.create_gc(
            foreground = self.rootscreen.white_pixel,
            background = self.rootscreen.black_pixel,
            )

    def run(self):
        """run the main loop"""
        while True:
            self.render()
            self.display.flush()
            time.sleep(self.delay)

    def render(self):
        """replace this with your rendering function"""
        pass

class colorsquare_demo(xss_base):
    delay = 0.01

    def __init__(self):
        xss_base.__init__(self)
        # load it
        self.myface = Image.open(os.path.expanduser("~/.zpix/eichin.jpg"))
        assert self.myface.mode == "RGB", "wrong PIL mode, only ZPixmap implemented"

    def render(self):
        if random.randint(0, 10) < 6:
            # random color
            new_pixel = random.randint(0, 2**24)
            self.gc.change(foreground=new_pixel)
            self.window.rectangle(self.gc,
                             random.randint(0, self.rootscreen.width_in_pixels), # x
                             random.randint(0, self.rootscreen.height_in_pixels), # y
                             50, # width
                             40, # height
                             )
        else:
            self.window.put_pil_image(self.gc,
                             random.randint(0, self.rootscreen.width_in_pixels), # x
                             random.randint(0, self.rootscreen.height_in_pixels), # y
                             self.myface)
        if random.randint(0, 500) < 1:
            self.window.clear_area(0, 0, 0, 0) # clear_window


if __name__ == "__main__":
    demo = colorsquare_demo()
    demo.run()

