# Path: resources/lib/model/inputmap.py
# class InputMap:
#     STATUS_DONE = 'done'
#     STATUS_PENDING = 'pending'
#     STATUS_ERROR = 'error'
# 
#     def __init__(self, file_name):
#         self.file_name = file_name
#         self.abs_x = -1
#         self.abs_y = -1
#         self.abs_z = -1
#         self.reverse_x = 'false'
#         self.reverse_y = 'false'
#         self.abs_rx = -1
#         self.abs_ry = -1
#         self.abs_rz = -1
#         self.reverse_rx = 'false'
#         self.reverse_ry = 'false'
#         # TODO: How to calculate deadzone properly? moonlight's default config uses 0 all the time ...
#         self.abs_deadzone = 0
#         self.abs_dpad_x = -1
#         self.abs_dpad_y = -1
#         self.reverse_dpad_x = 'false'
#         self.reverse_dpad_y = 'false'
#         self.btn_north = -1
#         self.btn_east = -1
#         self.btn_south = -1
#         self.btn_west = -1
#         self.btn_select = -1
#         self.btn_start = -1
#         self.btn_mode = -1
#         self.btn_thumbl = -1
#         self.btn_thumbr = -1
#         self.btn_tl = -1
#         self.btn_tr = -1
#         self.btn_tl2 = -1
#         self.btn_tr2 = -1
#         self.btn_dpad_up = -1
#         self.btn_dpad_down = -1
#         self.btn_dpad_left = -1
#         self.btn_dpad_right = -1
#         self.status = self.STATUS_PENDING
# 
#     def __iter__(self):
#         for attr, value in self.__dict__.iteritems():
#             if attr not in ['status', 'file_name']:
#                 yield attr, value
# 
#     def set_btn(self, attr, btn_no):
#         setattr(self, attr, btn_no)
# 
#     def write(self):
#         mapping = ConfigParser.ConfigParser()
#         print dict(self)
#         for attr, value in dict(self).iteritems():
#             mapping.set('', attr, value)
# 
#         if self.status == self.STATUS_DONE:
#             with open(self.file_name, 'wb') as mapping_file:
#                 mapping.write(mapping_file)
#             mapping_file.close()


import array
import os
import struct
from fcntl import ioctl
from resources.lib.model.inputmap import InputMap

        buf = array.array('B', [0])
        ioctl(js_dev, 0x80016a11, buf)
        self.num_axes = buf[0]
        print self.num_axes

        buf = array.array('B', [0])
        ioctl(js_dev, 0x80016a12, buf)
        self.num_buttons = buf[0]
        print self.num_buttons

        buf = array.array('B', [0] * 0x40)
        ioctl(js_dev, 0x80406a32, buf)
        for axis in buf[:self.num_axes]:
            axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
            self.axis_map.append(axis_name)
            self.axis_states[axis_name] = 0.0
            self.reverse_axis_map[axis_name] = axis

        buf = array.array('H', [0] * 200)
        ioctl(js_dev, 0x80406a34, buf)
        for btn in buf[:self.num_buttons]:
            btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
            self.btn_map.append(btn_name)
            self.button_states[btn_name] = 0
            self.reverse_btn_map[btn_name] = btn

        js_dev.close()

    def capture_input_events(self, js_dev):

# Path: resources/lib/model/inputmap.py
# class InputMap:
#     STATUS_DONE = 'done'
#     STATUS_PENDING = 'pending'
#     STATUS_ERROR = 'error'
# 
#     def __init__(self, file_name):
#         self.file_name = file_name
#         self.abs_x = -1
#         self.abs_y = -1
#         self.abs_z = -1
#         self.reverse_x = 'false'
#         self.reverse_y = 'false'
#         self.abs_rx = -1
#         self.abs_ry = -1
#         self.abs_rz = -1
#         self.reverse_rx = 'false'
#         self.reverse_ry = 'false'
#         # TODO: How to calculate deadzone properly? moonlight's default config uses 0 all the time ...
#         self.abs_deadzone = 0
#         self.abs_dpad_x = -1
#         self.abs_dpad_y = -1
#         self.reverse_dpad_x = 'false'
#         self.reverse_dpad_y = 'false'
#         self.btn_north = -1
#         self.btn_east = -1
#         self.btn_south = -1
#         self.btn_west = -1
#         self.btn_select = -1
#         self.btn_start = -1
#         self.btn_mode = -1
#         self.btn_thumbl = -1
#         self.btn_thumbr = -1
#         self.btn_tl = -1
#         self.btn_tr = -1
#         self.btn_tl2 = -1
#         self.btn_tr2 = -1
#         self.btn_dpad_up = -1
#         self.btn_dpad_down = -1
#         self.btn_dpad_left = -1
#         self.btn_dpad_right = -1
#         self.status = self.STATUS_PENDING
# 
#     def __iter__(self):
#         for attr, value in self.__dict__.iteritems():
#             if attr not in ['status', 'file_name']:
#                 yield attr, value
# 
#     def set_btn(self, attr, btn_no):
#         setattr(self, attr, btn_no)
# 
#     def write(self):
#         mapping = ConfigParser.ConfigParser()
#         print dict(self)
#         for attr, value in dict(self).iteritems():
#             mapping.set('', attr, value)
# 
#         if self.status == self.STATUS_DONE:
#             with open(self.file_name, 'wb') as mapping_file:
#                 mapping.write(mapping_file)
#             mapping_file.close()


# Path: resources/lib/util/inputwrapper.py
import array
import os
import struct
from fcntl import ioctl
from resources.lib.model.inputmap import InputMap


        buf = array.array('B', [0])
        ioctl(js_dev, 0x80016a11, buf)
        self.num_axes = buf[0]
        print self.num_axes

        buf = array.array('B', [0])
        ioctl(js_dev, 0x80016a12, buf)
        self.num_buttons = buf[0]
        print self.num_buttons

        buf = array.array('B', [0] * 0x40)
        ioctl(js_dev, 0x80406a32, buf)
        for axis in buf[:self.num_axes]:
            axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
            self.axis_map.append(axis_name)
            self.axis_states[axis_name] = 0.0
            self.reverse_axis_map[axis_name] = axis

        buf = array.array('H', [0] * 200)
        ioctl(js_dev, 0x80406a34, buf)
        for btn in buf[:self.num_buttons]:
            btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
            self.btn_map.append(btn_name)
            self.button_states[btn_name] = 0
            self.reverse_btn_map[btn_name] = btn

        js_dev.close()

    def capture_input_events(self, js_dev):

        if self.input_map.status == InputMap.STATUS_PENDING: