#! /usr/bin/env python
#
#    <one line to give the program's name and a brief idea of what it does.>
#    Copyright (C) 2002  Michael Urman
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import pygame
from pygame.locals import *

pygame.joystick.init()
pygame.display.init()

w = 0
h = 0
joy = []
for joystick in range(pygame.joystick.get_count()):
    j = pygame.joystick.Joystick(joystick)
    joy.append(j)
    j.init()
    w = max(w, (j.get_numaxes()+j.get_numballs()+j.get_numbuttons()+j.get_numhats())*32)
    h += 66
w += 2

screen = pygame.display.set_mode((w,h))

def updateaxis(stick, axis):
    num = joy[stick].get_numaxes()
    axis = [0, 1, 2, 4, 3, 5][axis]
    if num & 1 and num == axis:
        x = (num-1) * 32 + 4
        y = stick * 66 + 4
        screen.fill((64,64,64), (x-4, y-4, 32, 64))
        screen.fill((0,0,0), (x, y, 24, 56))
        dy = (56-24) * (1 - joy[stick].get_axis(axis))
        screen.fill((80,255,100), (x, y+dy, 24, 24))
        pygame.display.update((x-4,y-4,32,64))
    else:
        xaxis = [0, 0, 4, 4, 5, 5][axis]# axis & 0xFFFE
        yaxis = [1, 1, 2, 2, 3, 3][axis]# xaxis | 1
        print xaxis, yaxis, '(%s)'%axis

        x = (axis&0xfffe) * 32 + 4
        y = stick * 66 + 4
        screen.fill((64,64,64), (x-2, y-2, 60, 60))
        screen.fill((0,0,0), (x, y, 56, 56))
        dx = (56-12) * (1+joy[stick].get_axis(xaxis)) / 2
        dy = (56-12) * (1+joy[stick].get_axis(yaxis)) / 2
        #screen.fill((80,255,100), (x+dx, y+dy, 24, 24))
        pygame.draw.circle(screen, (80,255,100), (x+dx+6, y+dy+6), 4)
        pygame.display.update((x-2,y-2,60,60))

while 1:
    event = pygame.event.wait()
    if event.type == QUIT:
        break
    elif event.type == JOYAXISMOTION:
        updateaxis(event.joy, event.axis)
    elif event.type == JOYBUTTONUP:
        x = (joy[event.joy].get_numaxes()+event.button)*32 + 2
        y = event.joy * 66 + 19
        screen.fill((64,64,64), (x, y, 28, 28))
        screen.fill((0,0,0), (x+2, y+2, 24, 24))
        pygame.display.update((x,y,28,28))
    elif event.type == JOYBUTTONDOWN:
        x = (joy[event.joy].get_numaxes()+event.button)*32 + 2
        y = event.joy * 66 + 19
        screen.fill((64,64,64), (x, y, 28, 28))
        screen.fill((80,255,100), (x+2, y+2, 24, 24))
        pygame.display.update((x,y,28,28))