summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_fb/ecore_fb_ps2.c
blob: 7c8dc62ec2f6787f59dc886e53b1da2aeadc7165 (plain)
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
typedef struct _Ecore_Fb_Ps2_Event Ecore_Fb_Ps2_Event;
struct _Ecore_Fb_Ps2_Event
{
   unsigned char button;
   unsigned char x;
   unsigned char y;
   unsigned char z;
};

static int _ecore_fb_ps2_event_byte_count = 0;
static Ecore_Fb_Ps2_Event _ecore_fb_ps2_event;
static int _ecore_fb_ps2_fd = 0;
static Eina_Bool _ecore_fb_ps2_fd_handler(void *data, Ecore_Fd_Handler *fd_handler);

int
ecore_fb_ps2_init(void)
{
   _ecore_fb_ps2_fd = open("/dev/psaux", O_RDWR);
   if (_ecore_fb_ps2_fd >= 0)
     {
	prev_flags = fcntl(_ecore_fb_ps2_fd, F_GETFL);
 	fcntl(_ecore_fb_ps2_fd, F_SETFL, prev_flags | O_NONBLOCK);
	_ecore_fb_ts_fd_handler_handle = ecore_main_fd_handler_add(_ecore_fb_ps2_fd, 
									ECORE_FD_READ,
									_ecore_fb_ps2_fd_handler, NULL,	NULL, NULL);
	if (!_ecore_fb_ts_fd_handler_handle)
	 {
	   close(_ecore_fb_ps2_fd);
	   return 0;
         }
	return 1;
     }
  return 0;   
}

void
ecore_fb_ps2_shutdown(void)
{
   if (_ecore_fb_ps2_fd > 0) close(_ecore_fb_ps2_fd);
   _ecore_fb_ps2_fd = 0;
}

static int
_ecore_fb_ps2_fd_handler(void *data __UNUSED__, Ecore_Fd_Handler *fd_handler __UNUSED__)
{
   static int prev_x = 0, prev_y = 0, prev_button = 0;
   static double last_time = 0;
   static double last_last_time = 0;
   int v = 0;

   do
     {
	int x, y, button, i;
	int num;
	char *ptr;
	double t;
	int did_triple = 0;
	
	ptr = (char *)&(_ecore_fb_ps2_event);
	ptr += _ecore_fb_ps2_event_byte_count;
	num = sizeof(Ecore_Fb_Ps2_Event) - _ecore_fb_ps2_event_byte_count;
	v = read(_ecore_fb_ps2_fd, ptr, num);
	if (v < 0) return 1;
	_ecore_fb_ps2_event_byte_count += v;
	if (v < num) return 1;
	t = ecore_time_get();
	_ecore_fb_ps2_event_byte_count = 0;
	if (_ecore_fb_ps2_event.button & 0x10)
	  x = prev_x + (0xffffff00 | _ecore_fb_ps2_event.x);
	else
	  x = prev_x + _ecore_fb_ps2_event.x;
	if (_ecore_fb_ps2_event.button & 0x20)
	  y = prev_y - (0xffffff00 | _ecore_fb_ps2_event.y);
	else
	  y = prev_y - _ecore_fb_ps2_event.y;
	button = _ecore_fb_ps2_event.button & 0x7;
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	if (x >= _ecore_fb_console_w) x = _ecore_fb_console_w - 1;
	if (y >= _ecore_fb_console_h) y = _ecore_fb_console_h - 1;
	/* add event to queue */
	/* always add a move event */
	if (1)
	  {
	     /* MOVE: mouse is down and was */
	     Ecore_Fb_Event_Mouse_Move *e;
	     
	     e = calloc(1, sizeof(Ecore_Fb_Event_Mouse_Move));
	     if (!e) goto retry;
	     e->x = x;
	     e->y = y;
	     ecore_event_add(ECORE_FB_EVENT_MOUSE_MOVE, e, NULL, NULL);
	  }
	for (i = 1; i <= 3; i++)
	  {
	     int mask;
	     
	     mask = 1 << (i - 1);
	     if (((button & mask)) && (!(prev_button & mask)))
	       {
		  /* DOWN: mouse is down, but was not now */
		  Ecore_Fb_Event_Mouse_Button_Down *e;
		  
		  e = calloc(1, sizeof(Ecore_Fb_Event_Mouse_Button_Down));
		  if (!e) goto retry;
		  e->x = x;
		  e->y = y;
		  e->button = 1;
		  if ((t - last_time) <= _ecore_fb_double_click_time)
		    e->double_click = 1;
		  if ((t - last_last_time) <= (2 * _ecore_fb_double_click_time))
		    {
		       did_triple = 1;
		       e->triple_click = 1;
		    }
		  ecore_event_add(ECORE_FB_EVENT_MOUSE_BUTTON_DOWN, e, NULL, NULL);
	       }
	     else if ((!(button & mask)) && ((prev_button & mask)))
	       {
		  /* UP: mouse was down, but is not now */
		  Ecore_Fb_Event_Mouse_Button_Up *e;
		  
		  e = calloc(1, sizeof(Ecore_Fb_Event_Mouse_Button_Up));
		  if (!e) goto retry;
		  e->x = x;
		  e->y = y;
		  e->button = 1;
		  ecore_event_add(ECORE_FB_EVENT_MOUSE_BUTTON_UP, e, NULL, NULL);
	       }
	  }
	if (did_triple)
	  {
	     last_time = 0;
	     last_last_time = 0;
	  }
	else
	  {
	     last_last_time = last_time;
	     last_time = t;
	  }
	retry:	     
	prev_x = x;
	prev_y = y;
	prev_button = button;
     }
   while (v > 0);
   return 1;
}
/**
 * @defgroup Ecore_FB_Click_Group Framebuffer Double Click Functions
 *
 * Functions that deal with the double click time of the framebuffer.
 */

/**
 * Sets the timeout for a double and triple clicks to be flagged.
 * 
 * This sets the time between clicks before the double_click flag is
 * set in a button down event. If 3 clicks occur within double this
 * time, the triple_click flag is also set.
 *
 * @param   t The time in seconds
 * @ingroup Ecore_FB_Click_Group
 */
EAPI void
ecore_fb_double_click_time_set(double t)
{
   if (t < 0.0) t = 0.0;
   _ecore_fb_double_click_time = t;
}

/**
 * Retrieves the double and triple click flag timeout.
 *
 * See @ref ecore_x_double_click_time_set for more information.
 *
 * @return  The timeout for double clicks in seconds.
 * @ingroup Ecore_FB_Click_Group
 */
EAPI double
ecore_fb_double_click_time_get(void)
{
   return _ecore_fb_double_click_time;
}