#!/usr/bin/env python

import sys
import gtk.gdk

FILL = 1
CENTERED = 2
SCALE = 3
TILED = 4

def set_wallpaper(filename,mode):
    root=gtk.gdk.get_default_root_window()
    width, height = root.get_size()
    pixmap=gtk.gdk.Pixmap(root,width,height,root.get_depth())
    if mode == FILL:
        image=gtk.gdk.pixbuf_new_from_file(filename)
        pixbuf=image.scale_simple(width,height,gtk.gdk.INTERP_BILINEAR)
        pixmap.draw_pixbuf(None,pixbuf,0,0,0,0,pixbuf.get_width(),pixbuf.get_height())
    root.property_change(
            gtk.gdk.atom_intern("_XROOTPMAP_ID",False),
            gtk.gdk.atom_intern("PIXMAP",False),
            32,
            gtk.gdk.PROP_MODE_REPLACE,
            [pixmap.xid])
    root.set_back_pixmap(pixmap,False)
    root.clear()
    gtk.gdk.flush()

def run(args):
    if len(args) > 0:
        mode=FILL
        filename=None
        for arg in args:
            if arg == '--fill':
                mode=FILL
            elif arg == '--centered':
                mode=CENTERED
            elif arg == '--scaled':
                mode=SCALED
            elif arg == '--tiled':
                mode=TILED
            else:
                filename=arg
        if filename == None:
            print "error: No image specified."
        else:
            set_wallpaper(filename,mode)
    else:
        print "Usage: pywpp [--fill] [--centered] [--scaled] [--tiled] filename"

if __name__ == "__main__":
    run(sys.argv[1:])
