# python-webos example usage / demo script
# Copyright (c) 2011-05-11 Thomas Perl <thp.io/about>

import webos
import pygame
import time

from pygame.locals import *

pygame.init()
webos.init()

screen = pygame.display.set_mode()

colors = [
        (255, 0, 0),
        (0, 255, 0),
        (0, 0, 255),
        (0, 255, 255),
        (255, 0, 255),
        (255, 255, 0),
]

color_index = 0

# Get some status information from the system
print '='*40
print 'Language:', webos.get_language()
print 'Unique ID:', webos.get_unique_id()
print 'Device name:', webos.get_device_name()
print 'Data file path:', webos.get_data_file_path('')
print 'Hardware:', webos.get_hardware()
print 'PDK Version:', webos.get_pdk_version()
print '='*40

while True:
    event = pygame.event.wait()
    if event.type == KEYDOWN:
        # Switch between colors and vibrate by swiping the gesture area
        if event.key == webos.KEY_GESTURE_BACK:
            webos.vibrate(20, 100)
            color_index -= 1
        elif event.key == webos.KEY_GESTURE_FORWARD:
            webos.vibrate(20, 100)
            color_index += 1
        elif event.key == webos.KEY_GESTURE_AREA:
            # Gesture area has been touched (even when swiping)
            print 'gesture area touched'
        elif event.key == K_m:
            # Press "m" to minimize the card (show task switcher)
            webos.minimize()
        elif event.key == K_s:
            # Press "s" to create a new email with subject and body
            webos.launch_email('Python rocks', 'Even on webOS!')
        elif event.key == K_b:
            # Press "b" to open the browser with a given URL
            webos.launch_browser('http://thp.io/2011/webos/')
        elif event.key == K_p:
            # Press "p" to start the camera application
            webos.service_call('palm://com.palm.applicationManager/launch',
                    '{"id": "com.palm.app.camera"}')
        elif event.key == K_x:
            # Press "x" to play the camera shutter sound
            webos.service_call('palm://com.palm.audio/systemsounds/playFeedback',
                    '{"name": "shutter"}')
        elif event.key == K_k:
            # Press "k" to open Maps routing to "PythonLand, Earth"
            webos.service_call('palm://com.palm.applicationManager/open',
                    '{"target": "mapto: PythonLand, Earth"}')
        elif event.key == K_q:
            # Press "q" to quit the example app (or flick the card)
            break
        else:
            # All other key events should be logged on the console
            print 'key pressed:', event.key

    # Fill the whole screen with the current color
    current_color = colors[color_index%len(colors)]
    pygame.draw.rect(screen, current_color, screen.get_rect())
    pygame.display.flip()


# Cleanup
webos.quit()