~ubuntu-branches/debian/experimental/mednafen/experimental

« back to all changes in this revision

Viewing changes to src/psx/input/mouse.cpp

  • Committer: Package Import Robot
  • Author(s): Stephen Kitt
  • Date: 2012-01-31 07:21:35 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20120131072135-es3dj12y00xcnrsk
Tags: 0.9.19-1
* New upstream WIP version.
* Update copyright information.
* Refresh use-system-tremor.patch and remove psx-big-endian-only.patch.
* Add spelling-fixes.patch based on Lintian's recommendations.
* Build-depend on debhelper 9 or later and remove corresponding Lintian
  override.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "../psx.h"
 
2
#include "../frontio.h"
 
3
#include "mouse.h"
 
4
 
 
5
namespace MDFN_IEN_PSX
 
6
{
 
7
 
 
8
class InputDevice_Mouse : public InputDevice
 
9
{
 
10
 public:
 
11
 
 
12
 InputDevice_Mouse();
 
13
 virtual ~InputDevice_Mouse();
 
14
 
 
15
 virtual void Power(void);
 
16
 virtual void UpdateInput(const void *data);
 
17
 
 
18
 virtual void Update(const pscpu_timestamp_t timestamp);
 
19
 virtual void ResetTS(void);
 
20
 
 
21
 //
 
22
 //
 
23
 //
 
24
 virtual void SetDTR(bool new_dtr);
 
25
 virtual bool Clock(bool TxD, int32 &dsr_pulse_delay);
 
26
 
 
27
 private:
 
28
 
 
29
 int32 lastts;
 
30
 int32 clear_timeout;
 
31
 
 
32
 bool dtr;
 
33
 
 
34
 uint8 button;
 
35
 uint8 button_post_mask;
 
36
 int32 accum_xdelta;
 
37
 int32 accum_ydelta;
 
38
 
 
39
 int32 command_phase;
 
40
 uint32 bitpos;
 
41
 uint8 receive_buffer;
 
42
 
 
43
 uint8 command;
 
44
 
 
45
 uint8 transmit_buffer[5];
 
46
 uint32 transmit_pos;
 
47
 uint32 transmit_count;
 
48
};
 
49
 
 
50
InputDevice_Mouse::InputDevice_Mouse()
 
51
{
 
52
 Power();
 
53
}
 
54
 
 
55
InputDevice_Mouse::~InputDevice_Mouse()
 
56
{
 
57
 
 
58
}
 
59
 
 
60
void InputDevice_Mouse::Update(const pscpu_timestamp_t timestamp)
 
61
{
 
62
 int32 cycles = timestamp - lastts;
 
63
 
 
64
 clear_timeout += cycles;
 
65
 if(clear_timeout >= (33868800 / 4))
 
66
 {
 
67
  //puts("Mouse timeout\n");
 
68
  clear_timeout = 0;
 
69
  accum_xdelta = 0;
 
70
  accum_ydelta = 0;
 
71
  button &= button_post_mask;
 
72
 }
 
73
 
 
74
 lastts = timestamp;
 
75
}
 
76
 
 
77
void InputDevice_Mouse::ResetTS(void)
 
78
{
 
79
 lastts = 0;
 
80
}
 
81
 
 
82
void InputDevice_Mouse::Power(void)
 
83
{
 
84
 lastts = 0;
 
85
 clear_timeout = 0;
 
86
 
 
87
 dtr = 0;
 
88
 
 
89
 button = 0;
 
90
 button_post_mask = 0;
 
91
 accum_xdelta = 0;
 
92
 accum_ydelta = 0;
 
93
 
 
94
 command_phase = 0;
 
95
 
 
96
 bitpos = 0;
 
97
 
 
98
 receive_buffer = 0;
 
99
 
 
100
 command = 0;
 
101
 
 
102
 memset(transmit_buffer, 0, sizeof(transmit_buffer));
 
103
 
 
104
 transmit_pos = 0;
 
105
 transmit_count = 0;
 
106
}
 
107
 
 
108
void InputDevice_Mouse::UpdateInput(const void *data)
 
109
{
 
110
 accum_xdelta += (int32)MDFN_de32lsb((uint8*)data + 0);
 
111
 accum_ydelta += (int32)MDFN_de32lsb((uint8*)data + 4);
 
112
 
 
113
 if(accum_xdelta > 30 * 127) accum_xdelta = 30 * 127;
 
114
 if(accum_xdelta < 30 * -128) accum_xdelta = 30 * -128;
 
115
 
 
116
 if(accum_ydelta > 30 * 127) accum_ydelta = 30 * 127;
 
117
 if(accum_ydelta < 30 * -128) accum_ydelta = 30 * -128;
 
118
 
 
119
 button |= *((uint8 *)data + 8);
 
120
 button_post_mask = *((uint8 *)data + 8);
 
121
 
 
122
 //if(button)
 
123
 // MDFN_DispMessage("Button\n");
 
124
 //printf("%d %d\n", accum_xdelta, accum_ydelta);
 
125
}
 
126
 
 
127
 
 
128
void InputDevice_Mouse::SetDTR(bool new_dtr)
 
129
{
 
130
 if(!dtr && new_dtr)
 
131
 {
 
132
  command_phase = 0;
 
133
  bitpos = 0;
 
134
  transmit_pos = 0;
 
135
  transmit_count = 0;
 
136
 }
 
137
 else if(dtr && !new_dtr)
 
138
 {
 
139
  //if(bitpos || transmit_count)
 
140
  // printf("[PAD] Abort communication!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
 
141
 }
 
142
 
 
143
 dtr = new_dtr;
 
144
}
 
145
 
 
146
bool InputDevice_Mouse::Clock(bool TxD, int32 &dsr_pulse_delay)
 
147
{
 
148
 bool ret = 1;
 
149
 
 
150
 dsr_pulse_delay = 0;
 
151
 
 
152
 if(!dtr)
 
153
  return(1);
 
154
 
 
155
 if(transmit_count)
 
156
  ret = (transmit_buffer[transmit_pos] >> bitpos) & 1;
 
157
 
 
158
 receive_buffer &= ~(1 << bitpos);
 
159
 receive_buffer |= TxD << bitpos;
 
160
 bitpos = (bitpos + 1) & 0x7;
 
161
 
 
162
 if(!bitpos)
 
163
 {
 
164
  //printf("[PAD] Receive: %02x -- command_phase=%d\n", receive_buffer, command_phase);
 
165
 
 
166
  if(transmit_count)
 
167
  {
 
168
   transmit_pos++;
 
169
   transmit_count--;
 
170
  }
 
171
 
 
172
 
 
173
  switch(command_phase)
 
174
  {
 
175
   case 0:
 
176
          if(receive_buffer != 0x01)
 
177
            command_phase = -1;
 
178
          else
 
179
          {
 
180
           transmit_buffer[0] = 0x12;
 
181
           transmit_pos = 0;
 
182
           transmit_count = 1;
 
183
           command_phase++;
 
184
          }
 
185
          break;
 
186
 
 
187
   case 1:
 
188
        command = receive_buffer;
 
189
        command_phase++;
 
190
 
 
191
        transmit_buffer[0] = 0x5A;
 
192
 
 
193
        if(command == 0x42)
 
194
        {
 
195
         int32 xdelta = accum_xdelta;
 
196
         int32 ydelta = accum_ydelta;
 
197
 
 
198
         if(xdelta < -128) xdelta = -128;
 
199
         if(xdelta > 127) xdelta = 127;
 
200
 
 
201
         if(ydelta < -128) ydelta = -128;
 
202
         if(ydelta > 127) ydelta = 127;
 
203
 
 
204
         transmit_buffer[1] = 0xFF;
 
205
         transmit_buffer[2] = 0xFC ^ (button << 2);
 
206
         transmit_buffer[3] = xdelta;
 
207
         transmit_buffer[4] = ydelta;
 
208
 
 
209
         accum_xdelta -= xdelta;
 
210
         accum_ydelta -= ydelta;
 
211
 
 
212
         button &= button_post_mask;
 
213
 
 
214
         transmit_pos = 0;
 
215
         transmit_count = 5;
 
216
 
 
217
         clear_timeout = 0;
 
218
        }
 
219
        else
 
220
        {
 
221
         command_phase = -1;
 
222
         transmit_pos = 0;
 
223
         transmit_count = 0;
 
224
        }
 
225
        break;
 
226
 
 
227
  }
 
228
 }
 
229
 
 
230
 if(!bitpos && transmit_count)
 
231
  dsr_pulse_delay = 0x40; //0x100;
 
232
 
 
233
 return(ret);
 
234
}
 
235
 
 
236
InputDevice *Device_Mouse_Create(void)
 
237
{
 
238
 return new InputDevice_Mouse();
 
239
}
 
240
 
 
241
 
 
242
InputDeviceInputInfoStruct Device_Mouse_IDII[4] =
 
243
{
 
244
 { "x_axis", "X Axis", -1, IDIT_X_AXIS_REL },
 
245
 { "y_axis", "Y Axis", -1, IDIT_Y_AXIS_REL },
 
246
 { "right", "Right Button", 1, IDIT_BUTTON, NULL },
 
247
 { "left", "Left Button", 0, IDIT_BUTTON, NULL },
 
248
};
 
249
 
 
250
 
 
251
 
 
252
}