File: winmmhelp.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-6
  • links: PTS
  • area: main
  • in suites: buster
  • size: 35,916 kB
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (204 lines) | stat: -rw-r--r-- 8,042 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#if !JAVA
/*
 * winmmhelp.cs
 * Copyright © 2009-2011 kbinani
 *
 * This file is part of org.kbinani.cadencii.
 *
 * org.kbinani.cadencii is free software; you can redistribute it and/or
 * modify it under the terms of the GPLv3 License.
 *
 * org.kbinani.cadencii 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.
 */
using System;
using System.Runtime.InteropServices;

using org.kbinani;

namespace org.kbinani.cadencii {
    using boolean = Boolean;

    public unsafe static class winmmhelp {
        static uint s_num_joydev = 0;
        static boolean[] s_joy_attatched;
        static boolean s_initialized = false;
        static int[] s_joy_available;
        static JOYCAPSW[] s_joycaps;
        static readonly uint[] _BTN = new uint[32] { 0x0001,
                               0x0002,
                               0x0004,
                               0x0008,
                               0x00000010,
                               0x00000020,
                               0x00000040,
                               0x00000080,
                               0x00000100,
                               0x00000200,
                               0x00000400,
                               0x00000800,
                               0x00001000,
                               0x00002000,
                               0x00004000,
                               0x00008000,
                               0x00010000,
                               0x00020000,
                               0x00040000,
                               0x00080000,
                               0x00100000,
                               0x00200000,
                               0x00400000,
                               0x00800000,
                               0x01000000,
                               0x02000000,
                               0x04000000,
                               0x08000000,
                               0x10000000,
                               0x20000000,
                               0x40000000,
                               0x80000000 };

        public static int JoyInit() {
            if ( s_initialized ) {
                JoyReset();
            }
            s_initialized = true;
            int num_joydev = (int)win32.joyGetNumDevs();
#if DEBUG
            org.kbinani.debug.push_log( "winmmhelp.JoyInit" );
            org.kbinani.debug.push_log( "    num_joydev=" + num_joydev );
#endif
            if ( num_joydev <= 0 ) {
                num_joydev = 0;
                return num_joydev;
            }
            s_joy_attatched = new boolean[num_joydev];
            s_joycaps = new JOYCAPSW[num_joydev];
            int count = 0;
            for ( int k = 0; k < num_joydev; k++ ) {
                JOYINFO ji = new JOYINFO();
                if ( win32.joyGetPos( (uint)k, ref ji ) == win32.JOYERR_NOERROR ) {
                    s_joy_attatched[k] = true;
                    JOYCAPSW jc = new JOYCAPSW();
                    win32.joyGetDevCapsW( (uint)k, ref jc, (uint)Marshal.SizeOf( jc ) );
                    s_joycaps[k] = jc;
                    count++;
                } else {
                    s_joy_attatched[k] = false;
                }
            }
            if ( count > 0 ) {
                s_joy_available = new int[count];
                int c = -1;
                for ( int i = 0; i < num_joydev; i++ ) {
                    if ( s_joy_attatched[i] ) {
                        c++;
                        if ( c >= count ) {
                            break; //ここに来るのはエラー
                        }
                        s_joy_available[c] = i;
                    }
                }
            }
            s_num_joydev = (uint)count;
            return (int)s_num_joydev;
        }

        public static boolean IsJoyAttatched( int index ) {
            if ( !s_initialized ) {
                JoyInit();
            }
            if ( s_num_joydev == 0 || index < 0 || (int)s_num_joydev <= index ) {
                return false;
            }
            return s_joy_attatched[index];
        }

        public static boolean JoyGetStatus( int index_, out byte[] buttons, out int pov ) {
            if ( !s_initialized ) {
                pov = -1;
                buttons = new byte[0];
                return false;
            }
            if ( s_num_joydev == 0 || index_ < 0 || (int)s_num_joydev <= index_ ) {
                pov = -1;
                buttons = new byte[0];
                return false;
            }
            int index = s_joy_available[index_];
            int len = (int)s_joycaps[index].wNumButtons;
            buttons = new byte[len];
            pov = -1;
            JOYINFOEX ji_ex = new JOYINFOEX();
            JOYCAPSW jcs = s_joycaps[index];
            ji_ex.dwSize = (ushort)Marshal.SizeOf( ji_ex );
            if ( (jcs.wCaps & win32.JOYCAPS_HASPOV) == win32.JOYCAPS_HASPOV ) {
                ji_ex.dwFlags = win32.JOY_RETURNPOV | win32.JOY_RETURNBUTTONS;
            } else {
                ji_ex.dwFlags = win32.JOY_RETURNBUTTONS | win32.JOY_RETURNX | win32.JOY_RETURNY;
            }

            if ( s_joy_attatched[index] ) {
                uint ret_getpos = win32.joyGetPosEx( (uint)index, ref ji_ex );
                if ( ret_getpos == win32.JOYERR_NOERROR ) {
                    if ( (jcs.wCaps & win32.JOYCAPS_HASPOV) == win32.JOYCAPS_HASPOV ) {
                        pov = (int)ji_ex.dwPOV;
                        if ( (0xffff & ji_ex.dwPOV) == 0xffff ) {
                            pov = -1;
                        }
                    } else {
                        int flag = 0;
                        if ( ji_ex.dwXpos < jcs.wXmin + (jcs.wXmax - jcs.wXmin) / 3 ) flag = flag | 1;
                        if ( ji_ex.dwYpos < jcs.wYmin + (jcs.wYmax - jcs.wYmin) / 3 ) flag = flag | 2;
                        if ( ji_ex.dwXpos > jcs.wXmax - (jcs.wXmax - jcs.wXmin) / 3 ) flag = flag | 4;
                        if ( ji_ex.dwYpos > jcs.wYmax - (jcs.wYmax - jcs.wYmin) / 3 ) flag = flag | 8;
                        if ( flag == 1 ) pov = 27000;//左
                        if ( flag == 2 ) pov = 0;//上
                        if ( flag == 4 ) pov = 9000;//右
                        if ( flag == 8 ) pov = 18000;//下
                        if ( flag == 3 ) pov = 31500;//左上
                        if ( flag == 6 ) pov = 4500;//右上
                        if ( flag == 12 ) pov = 13500;//右下
                        if ( flag == 9 ) pov = 22500;//左下
                    }
                    for ( int i = 0; i < len && i < jcs.wNumButtons; i++ ) {
                        buttons[i] = (((uint)ji_ex.dwButtons & _BTN[i]) != 0x0) ? (byte)0x80 : (byte)0x00;
                    }
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        public static int JoyGetNumButtons( int index ) {
            if ( !s_initialized ) {
                JoyInit();
            }
            if ( s_num_joydev == 0 || index < 0 || (int)s_num_joydev <= index ) {
                return 0;
            }
            return (int)s_joycaps[s_joy_available[index]].wNumButtons;
        }

        public static void JoyReset() {
            if ( s_initialized ) {
                s_initialized = false;
                s_num_joydev = 0;
            }
        }

        public static int JoyGetNumJoyDev() {
            if ( !s_initialized ) {
                return JoyInit();
            } else {
                return (int)s_num_joydev;
            }
        }
    }

}
#endif