Buchempfehlung
Visual Basic 6 Kochbuch
Visual Basic 6 Kochbuch
Viele praktische Tipps zum Programmieren mit Visual Basic 6, die sich oft auch auf FB übertragen lassen. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

xid_usdriver.bas

Uploader:MitgliedThePuppetMaster
Datum/Zeit:12.12.2013 03:14:37
Hinweis: Dieser Quelltext ist Bestandteil des Projekts LINUX XID Userspace Treiber, zu dem es auf FreeBASIC-Portal.de eine Projektseite gibt.

'##############################################################################################################################################################
Dim Shared XIDUSD_Controler_TypesD()        as String
Dim Shared XIDUSD_Controler_TypesC          as UInteger



'##############################################################################################################################################################
#Include Once "xidusd_usb.bi"



'##############################################################################################################################################################
Enum XIDUSD_ControlerInputMaskType_Enum
    XIDUSD_CIMT_Unknown                 = -1
    XIDUSD_CIMT_LenCheck                = -2

    XIDUSD_CIMT_GET_Button_XBOX         = -3
    XIDUSD_CIMT_GET_Button_START        = -5
    XIDUSD_CIMT_GET_Button_BACK         = -6
    XIDUSD_CIMT_GET_Button_FOR          = -7
    XIDUSD_CIMT_GET_Button_SELECT       = -8
    XIDUSD_CIMT_GET_Button_X            = -9
    XIDUSD_CIMT_GET_Button_Y            = -10
    XIDUSD_CIMT_GET_Button_Z            = -11
    XIDUSD_CIMT_GET_Button_A            = -12
    XIDUSD_CIMT_GET_Button_B            = -13
    XIDUSD_CIMT_GET_Button_C            = -14
    XIDUSD_CIMT_GET_Button_Axis1        = -15
    XIDUSD_CIMT_GET_Button_Axis2        = -16
    XIDUSD_CIMT_GET_Button_Axis3        = -17
    XIDUSD_CIMT_GET_Button_Axis4        = -18
    XIDUSD_CIMT_GET_Button_Axis5        = -19
    XIDUSD_CIMT_GET_Button_Axis6        = -20
    XIDUSD_CIMT_GET_Button_Axis7        = -21
    XIDUSD_CIMT_GET_Button_Axis8        = -22
    XIDUSD_CIMT_GET_Button_TL           = -23
    XIDUSD_CIMT_GET_Button_TR           = -24
    XIDUSD_CIMT_GET_Button_BL           = -25
    XIDUSD_CIMT_GET_Button_BR           = -26
    XIDUSD_CIMT_GET_Button_Up           = -27
    XIDUSD_CIMT_GET_Button_Down         = -28
    XIDUSD_CIMT_GET_Button_Left         = -29
    XIDUSD_CIMT_GET_Button_Right        = -30

    XIDUSD_CIMT_GET_Axis_1              = -31
    XIDUSD_CIMT_GET_Axis_2              = -32
    XIDUSD_CIMT_GET_Axis_3              = -33
    XIDUSD_CIMT_GET_Axis_4              = -34
    XIDUSD_CIMT_GET_Axis_5              = -35
    XIDUSD_CIMT_GET_Axis_6              = -36
    XIDUSD_CIMT_GET_Axis_7              = -37
    XIDUSD_CIMT_GET_Axis_8              = -38

    XIDUSD_CIMT_GET_LED_1               = -39

    XIDUSD_CIMT_GET_Button_A1           = -40
    XIDUSD_CIMT_GET_Button_A2           = -41

End Enum
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Type XIDUSD_ControlerInputMaskItem_Type Field = 1
    V_Next                      as XIDUSD_ControlerInputMaskItem_Type Ptr
    V_Prev                      as XIDUSD_ControlerInputMaskItem_Type Ptr

    V_ByteIndex                 as Integer
    V_ByteValue                 as UInteger
    V_BitIndex                  as Integer

End Type
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Type XIDUSD_ControlerInputMask_Type Field = 1
    V_Next                      as XIDUSD_ControlerInputMask_Type Ptr
    V_Prev                      as XIDUSD_ControlerInputMask_Type Ptr

    V_IsGet                     as Byte
    V_Style                     as Byte

    V_ValueType                 as XIDUSD_ControlerInputMaskType_Enum

    V_DataLen                   as UByte

    V_Min                       as Integer
    V_Max                       as Integer
    V_Invers                    as Integer

    V_Mask(0 to 255)            as UByte
    V_Value(0 to 255)           as Integer
    V_Type(0 to 255)            as UByte
    V_Set(0 to 255)             as Integer
    V_Order(0 to 255, 0 to 4)   as UByte
End Type
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Type XIDUSD_Controler_Type
    V_Next                      as XIDUSD_Controler_Type Ptr
    V_Prev                      as XIDUSD_Controler_Type Ptr
    V_MarkDel                   as Integer

    V_Name                      as String
    V_Device                    as usb_device Ptr
    V_DeviceFN                  as usb_dev_handle Ptr
    V_EP_AdrW                   as Integer
    V_EP_AdrR                   as Integer

    V_UInput_FID                as long

    V_Masks_F                   as XIDUSD_ControlerInputMask_Type Ptr
    V_Masks_L                   as XIDUSD_ControlerInputMask_Type Ptr
End Type
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Dim Shared XIDUSD_Controler_F   as XIDUSD_Controler_Type Ptr
Dim Shared XIDUSD_Controler_L   as XIDUSD_Controler_Type Ptr



'##############################################################################################################################################################
#Include Once "xidusd_uinput.bi"



'##############################################################################################################################################################
Function XIDUSD_DevCheckExist(V_Device as usb_device Ptr) as Integer
Dim TCPtr as XIDUSD_Controler_Type Ptr = XIDUSD_Controler_F
Do Until TCPtr = 0
    If TCPtr->V_Device = V_Device Then Return 1
    TCPtr = TCPtr->V_Next
Loop
Return -1
End Function



'##############################################################################################################################################################
Function XIDUSD_DevAddXIDParseParam(ByRef RV_Mask_F as XIDUSD_ControlerInputMask_Type Ptr, ByRef RV_Mask_L as XIDUSD_ControlerInputMask_Type Ptr, V_Data as String) as Integer
Dim DD() as String
Dim DC as Integer
Dim TPos as Integer
Dim T as String = V_Data
Dim T1 as String
Do
    TPos = InStr(1, T, ";")
    If TPos > 0 Then
        T1 = Trim(Left(T, TPos - 1))
        T = Trim(Mid(T, TPos + 1))
    Else: T1 = T: T = ""
    End If
    If T1 <> "" Then
        DC += 1
        Redim Preserve DD(DC) as String
        DD(DC) = UCase(T1)
    End If
    If T = "" Then Exit Do
Loop
If DC < 3 Then Return -1
Dim TIDXStart as Integer
If RV_Mask_L <> 0 Then
    RV_Mask_L->V_Next = CAllocate(SizeOf(XIDUSD_ControlerInputMask_Type))
    RV_Mask_L->V_Next->V_Prev = RV_Mask_L
    RV_Mask_L = RV_Mask_L->V_Next
Else
    RV_Mask_L = CAllocate(SizeOf(XIDUSD_ControlerInputMask_Type))
    RV_Mask_F = RV_Mask_L
End If
With *RV_Mask_L
    .V_IsGet        = 1
    TPos = InStr(1, DD(1), ",")
    If TPos > 0 Then
        T = Mid(DD(1), TPos + 1)
        DD(1) = Left(DD(1), TPos - 1)
    End If
    Select Case DD(1)
        Case "D": .V_Style = 1
        Case Else
            Select Case DD(1)
                Case "A": .V_Style = 0
                Case "AI": .V_Style = 0: .V_Invers = 1
                Case "S": .V_Style = 2
            End Select
            TPos = InStr(1, T, ",")
            If TPos <= 0 Then Return -1
            T1 = Mid(T, TPos + 1)
            T = Left(T, TPos - 1)
            If Left(T, 1) = "-" Then
                .V_Min = -CInt("&H" & Mid(T, 2))
            Else: .V_Min = CInt("&H" & T)
            End If
            If Left(T1, 1) = "-" Then
                .V_Max = -CInt("&H" & Mid(T1, 2))
            Else: .V_Max = CInt("&H" & T1)
            End If

    End Select
    .V_DataLen = CUByte(DD(2))
    Dim TByteSet as String
    Dim TByteIndex as String
    Dim TByteType as String
    Dim TByteBit as String
    Dim TByteValue as String
    Dim TByteOrder as String
    Dim TByteValueX as Integer
    For X as UInteger = 3 to DC
        T = DD(X)
        TPos = InStr(1, T, ",")
        If TPos > 0 Then
            TByteSet = Left(T, TPos - 1)
            T = Mid(T, TPos + 1)
        Else: TByteSet = "1"
        End If
        TPos = InStr(1, T, ":")
        If TPos = 0 Then Continue For
        TByteIndex = Left(T, TPos - 1)
        TByteValue = Mid(T, TPos + 1)
        TPos = InStr(1, TByteIndex, ".")
        If TPos > 0 Then
            TByteType = Mid(TByteIndex, TPos + 1, 1)
            TByteBit = Mid(TByteIndex, TPos + 2)
            TByteIndex = Left(TByteIndex, TPos - 1)
        Else: TByteType = "B": TByteBit = ""
        End If
        If TByteBit = "" Then TByteBit = "FF"
        If (CInt(TByteIndex) < 0) or (CInt(TByteIndex) > 255) Then Continue For
        If Left(TByteSet, 1) <> "-" Then
            .V_Set(CInt(TByteIndex)) = CInt("&H" & TByteSet)
        Else: .V_Set(CInt(TByteIndex)) = -CInt("&H" & Mid(TByteSet, 2))
        End If
        TByteOrder = Mid(TByteType, 2)
        TByteType = Left(TByteType, 1)
        Select Case TByteType
            Case "B": .V_Type(CInt(TByteIndex)) = 0
            Case "W": .V_Type(CInt(TByteIndex)) = 1
            Case "I": .V_Type(CInt(TByteIndex)) = 2
        End Select
        If TByteOrder <> "" Then
            .V_Order(CInt(TByteIndex), 0) = 1
            For X as Integer = 1 to Len(TByteOrder)
                .V_Order(CInt(TByteIndex), X) = CUByte(Mid(TByteOrder, X, 1))
            Next
        End If
        If .V_Style = 1 Then
            .V_Mask(CInt(TByteIndex)) = CUByte("&H" & TByteBit)
            Select Case TByteValue
                Case "LC"               : TByteValueX = XIDUSD_CIMT_LenCheck
                Case "BUTTON_XBOX"      : TByteValueX = XIDUSD_CIMT_GET_Button_XBOX
                Case "BUTTON_START"     : TByteValueX = XIDUSD_CIMT_GET_Button_START
                Case "BUTTON_BACK"      : TByteValueX = XIDUSD_CIMT_GET_Button_BACK
                Case "BUTTON_FOR"       : TByteValueX = XIDUSD_CIMT_GET_Button_FOR
                Case "BUTTON_SELECT"    : TByteValueX = XIDUSD_CIMT_GET_Button_SELECT
                Case "BUTTON_X"         : TByteValueX = XIDUSD_CIMT_GET_Button_X
                Case "BUTTON_Y"         : TByteValueX = XIDUSD_CIMT_GET_Button_Y
                Case "BUTTON_Z"         : TByteValueX = XIDUSD_CIMT_GET_Button_Z
                Case "BUTTON_A"         : TByteValueX = XIDUSD_CIMT_GET_Button_A
                Case "BUTTON_B"         : TByteValueX = XIDUSD_CIMT_GET_Button_B
                Case "BUTTON_C"         : TByteValueX = XIDUSD_CIMT_GET_Button_C
                Case "BUTTON_A1"        : TByteValueX = XIDUSD_CIMT_GET_Button_A1
                Case "BUTTON_A2"        : TByteValueX = XIDUSD_CIMT_GET_Button_A2
                Case "BUTTON_LT"        : TByteValueX = XIDUSD_CIMT_GET_Button_TL
                Case "BUTTON_RT"        : TByteValueX = XIDUSD_CIMT_GET_Button_TR
                Case "BUTTON_LB"        : TByteValueX = XIDUSD_CIMT_GET_Button_BL
                Case "BUTTON_RB"        : TByteValueX = XIDUSD_CIMT_GET_Button_BR
                Case "BUTTON_UP"        : TByteValueX = XIDUSD_CIMT_GET_Button_Up
                Case "BUTTON_DOWN"      : TByteValueX = XIDUSD_CIMT_GET_Button_Down
                Case "BUTTON_LEFT"      : TByteValueX = XIDUSD_CIMT_GET_Button_Left
                Case "BUTTON_RIGHT"     : TByteValueX = XIDUSD_CIMT_GET_Button_Right
                Case "LED_1"            : TByteValueX = XIDUSD_CIMT_GET_LED_1
                Case Else               : TByteValueX = CInt("&H" & TByteValue)
            End Select
            .V_Value(CInt(TByteIndex)) = TByteValueX
            Print "D: >" & Bin(.V_Mask(CInt(TByteIndex)), 8) & "<___>" & .V_Value(CInt(TByteIndex)) & "<"
        Else
            .V_Mask(CInt(TByteIndex)) = CUByte("&H" & TByteBit)
            Select Case TByteValue
                Case "LC"               : TByteValueX = XIDUSD_CIMT_LenCheck
                Case "AXIS_1"           : TByteValueX = XIDUSD_CIMT_GET_Axis_1
                Case "AXIS_2"           : TByteValueX = XIDUSD_CIMT_GET_Axis_2
                Case "AXIS_3"           : TByteValueX = XIDUSD_CIMT_GET_Axis_3
                Case "AXIS_4"           : TByteValueX = XIDUSD_CIMT_GET_Axis_4
                Case "AXIS_5"           : TByteValueX = XIDUSD_CIMT_GET_Axis_5
                Case "AXIS_6"           : TByteValueX = XIDUSD_CIMT_GET_Axis_6
                Case "AXIS_7"           : TByteValueX = XIDUSD_CIMT_GET_Axis_7
                Case "AXIS_8"           : TByteValueX = XIDUSD_CIMT_GET_Axis_8
                Case Else               : TByteValueX = CInt("&H" & TByteValue)
            End Select
            .V_Value(CInt(TByteIndex)) = TByteValueX
            Print "A: >" & Bin(.V_Mask(CInt(TByteIndex)), 8) & "<___>" & .V_Type(CInt(TByteIndex)) & "<___>" & .V_Order(CInt(TByteIndex), 0) & "<___>" & .V_Value(CInt(TByteIndex)) & "<___>" & .V_Set(CInt(TByteIndex)) & "<"
        End If
    Next
End With
Return 1
End Function



'##############################################################################################################################################################
Function XIDUSD_DevAdd(V_Device as usb_device Ptr) as Integer
If V_Device = 0 Then Return -1
Dim TCName as String = UCase(Hex(V_Device->descriptor.idVendor, 4)) & UCase(Hex(V_Device->descriptor.idProduct, 4))
Dim TFN as Integer = FreeFile()
If Open("controler/" & TCName & ".xid" for Input as #TFN) <> 0 Then Return -1
Dim TName as String
Dim T as String
Dim TPos as Integer
Dim TMaskF as XIDUSD_ControlerInputMask_Type Ptr
Dim TMaskL as XIDUSD_ControlerInputMask_Type Ptr
Do Until EOF(TFN)
    Line Input #TFN, T
    T = Trim(T)
    If Left(T, 1) = "#" Then Continue Do
    If T = "" Then Continue Do
    TPos = InStr(T, "=")
    If TPos <= 0 Then Continue Do
    Select Case LCase(Trim(Left(T, TPos - 1)))
        Case "name": TName = Trim(Mid(T, TPos + 1))
        Case "get": XIDUSD_DevAddXIDParseParam(TMaskF, TMaskL, Trim(Mid(T, TPos + 1)))
    End Select
Loop
Close #TFN

Dim TDevFN as usb_dev_handle Ptr = usb_open(V_Device)
If TDevFN = 0 Then Return -1
If usb_set_configuration(TDevFN, 1) <> 0 Then usb_close(TDevFN): Return -1
If usb_claim_interface(TDevFN, 0) <> 0 Then usb_close(TDevFN): Return -1

With *TDevFN
    If .device->descriptor.bNumConfigurations <= 0 Then Print "bNumConfigurations <= 0!": Return -1
    If .device->config[0].bNumInterfaces <= 0 Then Print "bNumInterfaces <= 0!": Return -1
    If .device->config[0].interface[0].num_altsetting <= 0 Then Print "num_altsetting <= 0!": Return -1
    If .device->config[0].interface[0].altsetting[0].bNumEndpoints <= 0 Then Print "bNumEndpoints <= 0!": Return -1
    If .device->config[0].interface[0].altsetting[0].bNumEndpoints <> 2 Then Print "bNumEndpoints <> 2!": Return -1
End With

If XIDUSD_Controler_L <> 0 Then
    XIDUSD_Controler_L->V_Next = CAllocate(SizeOf(XIDUSD_Controler_Type))
    XIDUSD_Controler_L->V_Next->V_Prev = XIDUSD_Controler_L
    XIDUSD_Controler_L = XIDUSD_Controler_L->V_Next
Else
    XIDUSD_Controler_L = CAllocate(SizeOf(XIDUSD_Controler_Type))
    XIDUSD_Controler_F = XIDUSD_Controler_L
End If
With *XIDUSD_Controler_L
    .V_Name             = TName
    .V_Device           = V_Device
    .V_DeviceFN         = TDevFN
    If .V_DeviceFN->device->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress > &HF Then
        .V_EP_AdrR = .V_DeviceFN->device->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress
    Else: .V_EP_AdrW = .V_DeviceFN->device->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress
    End If
    If .V_DeviceFN->device->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress > &HF Then
        .V_EP_AdrR = .V_DeviceFN->device->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress
    Else: .V_EP_AdrW = .V_DeviceFN->device->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress
    End If
    .V_Masks_F          = TMaskF
    .V_Masks_L          = TMaskL
End With
If XIDUSD_UInput_Init(XIDUSD_Controler_L) <> 1 Then
    Print "Cant init UInput!"
End If
Print "NEW DEVICE!"
Return 1
End Function



'##############################################################################################################################################################
Function XIDUSD_DevDel(V_Controler as XIDUSD_Controler_Type Ptr) as Integer
XIDUSD_UInput_Term(V_Controler)
If V_Controler = 0 Then Return -1
If V_Controler->V_Next <> 0 Then V_Controler->V_Next->V_Prev = V_Controler->V_Prev
If V_Controler->V_Prev <> 0 Then V_Controler->V_Prev->V_Next = V_Controler->V_Next
If XIDUSD_Controler_F = V_Controler Then XIDUSD_Controler_F = V_Controler->V_Next
If XIDUSD_Controler_L = V_Controler Then XIDUSD_Controler_L = V_Controler->V_Prev
DeAllocate(V_Controler)
Print "DEVICE REMOVED!"
End Function



'##############################################################################################################################################################
Function XIDUSD_DevReadData(V_Controler as XIDUSD_Controler_Type Ptr, V_Data as String, V_DataLen as Integer) as Integer
'return -1
'If V_DataLen = 20 Then Print "BUTTONS:" & Bin(V_Data[2], 8) & " - " & Bin(V_Data[3], 8)
Dim TInEVD(1 to 255) as input_event
Dim TInEVC as Integer
Dim TCode as Integer
Dim TValue as Integer
Dim TMPtr as XIDUSD_ControlerInputMask_Type Ptr = V_Controler->V_Masks_F
Do Until TMPtr = 0
    With *TMPtr
        For X as Integer = 0 to 255
            If TInEVC >= 255 Then Exit For
            If X > V_DataLen Then Exit For
            If .V_Mask(X) > 0 Then
                If .V_Value(X) < 0 Then
                    If .V_Value(X) = XIDUSD_CIMT_LenCheck Then
                        If V_Data[X] <> V_DataLen Then Exit For
                        Continue For
                    End If
                    If .V_Style = 1 Then
                        If (V_Data[X] and .V_Mask(X)) <> 0 Then TValue = .V_Set(X) Else TValue = 0
                        Select Case .V_Value(X)
                            Case XIDUSD_CIMT_GET_Button_XBOX        : TCode = BTN_MODE
                            Case XIDUSD_CIMT_GET_Button_START       : TCode = BTN_START
                            Case XIDUSD_CIMT_GET_Button_BACK        : TCode = BTN_BACK
                            Case XIDUSD_CIMT_GET_Button_FOR         : TCode = BTN_FORWARD
                            Case XIDUSD_CIMT_GET_Button_SELECT      : TCode = BTN_SELECT
                            Case XIDUSD_CIMT_GET_Button_X           : TCode = BTN_X
                            Case XIDUSD_CIMT_GET_Button_Y           : TCode = BTN_Y
                            Case XIDUSD_CIMT_GET_Button_A           : TCode = BTN_A
                            Case XIDUSD_CIMT_GET_Button_B           : TCode = BTN_B
                            Case XIDUSD_CIMT_GET_Button_A1          : TCode = BTN_C
                            Case XIDUSD_CIMT_GET_Button_A2          : TCode = BTN_Z
                            Case XIDUSD_CIMT_GET_Button_TL          : TCode = BTN_TL
                            Case XIDUSD_CIMT_GET_Button_TR          : TCode = BTN_TR
                            Case XIDUSD_CIMT_GET_Button_BL          : TCode = BTN_TL2
                            Case XIDUSD_CIMT_GET_Button_BR          : TCode = BTN_TR2
                            Case XIDUSD_CIMT_GET_Button_Up          : TCode = BTN_TRIGGER_HAPPY3
                            Case XIDUSD_CIMT_GET_Button_Down        : TCode = BTN_TRIGGER_HAPPY4
                            Case XIDUSD_CIMT_GET_Button_Left        : TCode = BTN_TRIGGER_HAPPY1
                            Case XIDUSD_CIMT_GET_Button_Right       : TCode = BTN_TRIGGER_HAPPY2
                        End Select
                        TInEVC += 1
                        With TInEVD(TInEVC)
                            .type = EV_KEY
                            .code = TCode
                            .value = TValue '*Cast(Short Ptr, @V_Data[6])
                        End With
                        'Print "EVD: >" & X & "<___>" & TCode & "<___>" & TValue & "<"
                    Else
                        Select Case .V_Value(X)
                            Case XIDUSD_CIMT_GET_Axis_1             : TCode = ABS_X + 1
                            Case XIDUSD_CIMT_GET_Axis_2             : TCode = ABS_Y + 1
                            Case XIDUSD_CIMT_GET_Axis_3             : TCode = ABS_RX + 1
                            Case XIDUSD_CIMT_GET_Axis_4             : TCode = ABS_RY + 1
                            Case XIDUSD_CIMT_GET_Axis_5             : TCode = ABS_GAS + 1
                            Case XIDUSD_CIMT_GET_Axis_6             : TCode = ABS_BRAKE + 1
                            Case XIDUSD_CIMT_GET_Axis_7             : TCode = ABS_Z + 1
                            Case XIDUSD_CIMT_GET_Axis_8             : TCode = ABS_RZ + 1
                        End Select
                        Select Case .V_Style
                            Case 0
                                Select Case .V_Type(X)
                                    Case 0: TValue = CInt(*Cast(UByte Ptr, @V_Data[X]))
                                    Case 1: TValue = CInt(*Cast(Short Ptr, @V_Data[X]))
                                    Case 2: TValue = CInt(*Cast(Integer Ptr, @V_Data[X]))
                                End Select
                                If .V_Invers = 1 Then TValue = -TValue
                            Case 2
                                If (V_Data[X] and .V_Mask(X)) <> 0 Then TValue = .V_Set(X) Else TValue = 0
                                For XX as Integer = 1 to TInEVC
                                    If TInEVD(XX).code = (TCode - 1) Then
                                        If TValue <> 0 Then TInEVD(XX).value = TValue
                                        TCode = 0
                                        Exit For
                                    End If
                                Next
                        End Select
                        If TCode <> 0 Then
                            TInEVC += 1
                            With TInEVD(TInEVC)
                                .type = EV_ABS
                                .code = TCode - 1
                                .value = TValue
                            End With
                        End If
                        'Print "EVA: >" & X & "<___>" & TInEVC & "<___>" & TCode & "<___>" & TValue & "<"
                        'Print "     >" & X & "<___>" & .V_Value(X) & "<___>" & .V_Type(X) & "<___>" & V_Data[X] & "<"
                    End If
                Else: If V_Data[X] <> .V_Value(X) Then Exit For
                End If
            End If
        Next
    End With
    TMPtr = TMPtr->V_Next
Loop
'Print ""
If TInEVC <= 0 Then Return 1
'Print "WRITE:" & write_(V_Controler->V_UInput_FID, @TInEVD(1), TInEVC * SizeOf(input_event)) ' <> SizeOf(TUInputDev) Then XIDUSD_UInput_Term(V_Controler): Print "Failed write DeviceHeader!": Return -1
For X as integer = 1 to TInEVC
    write_(V_Controler->V_UInput_FID, @TInEVD(X), SizeOf(input_event))
next
Return 1
End Function



'##############################################################################################################################################################
Sub Main()
Print "2013 By.: ThePuppetMaster"
Print "init..."
Dim TCMDC as Integer
Dim DD() as String
Dim DC as Integer
Dim T as String
Do
    TCMDC += 1
    T = Command(TCMDC)
    If T = "" Then Exit Do
    If Trim(T) = "" Then Continue Do
    DC += 1
    Redim Preserve DD(DC) as String
    DD(DC) = Trim(T)
Loop

Print "read devlist..."
XIDUSD_Controler_TypesC = 0
Dim TAttr as Integer
T = Dir("controler/*", -1, @TAttr)
Do Until T = ""
    If (T <> ".") and (T <> "..") Then
        If (LCase(Right(T, 4)) = ".xid") and ((TAttr and &H10) = 0) and (Len(T) = 12) Then
            XIDUSD_Controler_TypesC += 1
            Redim Preserve XIDUSD_Controler_TypesD(XIDUSD_Controler_TypesC) as String
            XIDUSD_Controler_TypesD(XIDUSD_Controler_TypesC) = UCase(Left(T, 8))
            Print "DEV:>" & XIDUSD_Controler_TypesD(XIDUSD_Controler_TypesC) & "<"
        End If
    End If
    T = Dir("", -1, @TAttr)
Loop

'XIDUSD_UInput_Init()
'sleep 2000, 1
'XIDUSD_UInput_Term()
'end 0

Print "init usb..."
usb_init()

Print "ready!"
Dim TBPtr as usb_bus Ptr
Dim TDPtr as usb_device Ptr
Dim TDPtrF as usb_device Ptr
Dim TTotCheck as Double
Dim X as UInteger
Dim TBufferC as Integer = 256
Dim TBufferR as Integer
Dim TBufferD as String = String(TBufferC, 0)
Dim TCPtr as XIDUSD_Controler_Type Ptr
Dim TCPtrN as XIDUSD_Controler_Type Ptr
Dim TTXT as Double = Timer() + 1
Dim TBufferTX as String
Do Until InKey() = Chr(27)
    If TTotCheck < Timer() Then
        usb_find_busses()
        usb_find_devices()
        TBPtr = usb_get_busses()
        Do Until TBPtr = 0
            TDPtr = TBPtr->devices
            Do Until TDPtr = 0
                With TDPtr->descriptor
                    For X as UInteger = 1 to G_ControlerC
                        T = UCase(Hex(.idVendor, 4)) & UCase(Hex(.idProduct, 4))
                        For X = 1 to XIDUSD_Controler_TypesC
                            If XIDUSD_Controler_TypesD(X) = T Then
                                If XIDUSD_DevCheckExist(TDPtr) = -1 Then XIDUSD_DevAdd(TDPtr)
                            End If
                        Next
                    Next
                End With
                TDPtr = TDPtr->next
            Loop
            TBPtr = TBPtr->next
        Loop
        TTotCheck = Timer() + 3
    End If

    'If XIDUSD_Controler_F <> 0 Then
        'TBufferR = usb_bulk_read(XIDUSD_Controler_F->V_DeviceFN, EP_IN, @TBufferD[0], TBufferC, 5000)
        'Print TBufferR
    'End If
    TCPtr = XIDUSD_Controler_F
    Do Until TCPtr = 0
        TBufferR = usb_bulk_read(TCPtr->V_DeviceFN, TCPtr->V_EP_AdrR, @TBufferD[0], TBufferC, 5000)
        If TBufferR = 0 Then TCPtr = TCPtr->V_Next: Continue Do
        If TBufferR < 0 Then
            Print "-1!!!"
            With *TCPtr
                usb_release_interface(.V_DeviceFN, 0)
                usb_close(.V_DeviceFN)
                .V_MarkDel = 1
            End With
        Else
            'If TTXT < Timer() Then
                'TBufferTX = Chr(&H03, &H08, &H00, &H07, &H00, &H08, &H00, &H00)
                'TBufferTX = Chr(&H00, &H08, &H00, &H01, &H02, &H00, &H00, &H00)
                'Print usb_bulk_write(TCPtr->V_DeviceFN, TCPtr->V_EP_AdrW, @TBufferTX[0], Len(TBufferTX), 5000)

                '/* set the led mode (0x06 = 1 on) */
                'TBufferTX = Chr(&H01, &H03, &H06)
                'Print usb_bulk_write(TCPtr->V_DeviceFN, TCPtr->V_EP_AdrW, @TBufferTX[0], Len(TBufferTX), 5000)

                'TBufferTX = Chr(&H03, &H03, &H00)
                'Print usb_bulk_write(TCPtr->V_DeviceFN, EP_OUT, @TBufferTX[0], Len(TBufferTX), 5000)

                'TBufferTX = Chr(&H00, &H08, &H00)
                'Print usb_bulk_write(TCPtr->V_DeviceFN, EP_OUT, @TBufferTX[0], Len(TBufferTX), 5000)

                'TBufferTX = Chr(&H00, &H00, &H08, &H41, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00)
                'Print usb_bulk_write(TCPtr->V_DeviceFN, EP_OUT, @TBufferTX[0], Len(TBufferTX), 5000)
                'TBufferTX = Chr(&H00, &H01, &H0F, &HC0, &H00, &H00, &H00, &H00, &H00, &H00, &H00, &H00)
                'Print usb_bulk_write(TCPtr->V_DeviceFN, EP_OUT, @TBufferTX[0], Len(TBufferTX), 5000)
                'TTXT = Timer() + 1000
            'End If
            XIDUSD_DevReadData(TCPtr, TBufferD, TBufferR)
        End If
        TCPtr = TCPtr->V_Next
    Loop
    TCPtr = XIDUSD_Controler_F
    Do Until TCPtr = 0
        TCPtrN = TCPtr->V_Next
        If TCPtr->V_MarkDel = 1 Then XIDUSD_DevDel(TCPtr)
        TCPtr = TCPtrN
    Loop
    Sleep 10, 1
Loop




'Print "usb_close:"; usb_close(TDevFN)

'XIDUSD_UInput_Init()
'sleep 2000, 1
'XIDUSD_UInput_Term()
'end 0

End Sub



'##############################################################################################################################################################
Main()
end 0