2013-04-26 02:58:22 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* X11 Input
|
|
|
|
*
|
|
|
|
* Copyright 2013 Corey Clayton <can.of.tuna@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-04-26 06:04:35 +04:00
|
|
|
#include <math.h>
|
2013-04-26 02:58:22 +04:00
|
|
|
|
|
|
|
#include "xf_input.h"
|
|
|
|
|
2013-04-26 06:04:35 +04:00
|
|
|
#define MAX_CONTACTS 2
|
|
|
|
|
|
|
|
typedef struct touch_contact
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
int count;
|
|
|
|
double pos_x;
|
|
|
|
double pos_y;
|
|
|
|
double last_x;
|
|
|
|
double last_y;
|
|
|
|
|
|
|
|
} touchContact;
|
|
|
|
|
|
|
|
touchContact contacts[MAX_CONTACTS];
|
|
|
|
|
|
|
|
int active_contacts;
|
|
|
|
|
2013-04-27 00:23:52 +04:00
|
|
|
XIDeviceEvent lastEvent;
|
|
|
|
|
2013-04-26 03:52:17 +04:00
|
|
|
int xinput_opcode; //TODO: use this instead of xfi
|
|
|
|
|
2013-04-26 03:38:13 +04:00
|
|
|
void xf_input_init(xfInfo* xfi)
|
2013-04-26 02:58:22 +04:00
|
|
|
{
|
|
|
|
int opcode, event, error;
|
|
|
|
int major = 2, minor = 2;
|
|
|
|
|
|
|
|
|
|
|
|
XIEventMask eventmask;
|
|
|
|
unsigned char mask[(XI_LASTEVENT + 7)/8];
|
|
|
|
|
2013-04-26 06:04:35 +04:00
|
|
|
active_contacts = 0;
|
|
|
|
ZeroMemory(contacts, sizeof(touchContact) * MAX_CONTACTS);
|
|
|
|
|
2013-04-26 02:58:22 +04:00
|
|
|
ZeroMemory(mask, sizeof(mask));
|
|
|
|
|
|
|
|
|
|
|
|
if (XQueryExtension(xfi->display, "XInputExtension", &opcode, &event, &error))
|
|
|
|
{
|
|
|
|
|
|
|
|
xfi->XInputOpcode = opcode;
|
|
|
|
|
|
|
|
XIQueryVersion(xfi->display, &major, &minor);
|
|
|
|
if (!(major * 1000 + minor < 2002))
|
|
|
|
{
|
|
|
|
|
2013-04-26 03:52:17 +04:00
|
|
|
eventmask.deviceid = XIAllDevices;
|
|
|
|
eventmask.mask_len = sizeof(mask);
|
2013-04-26 02:58:22 +04:00
|
|
|
eventmask.mask = mask;
|
2013-04-26 03:52:17 +04:00
|
|
|
|
2013-04-26 02:58:22 +04:00
|
|
|
XISetMask(mask, XI_TouchBegin);
|
|
|
|
XISetMask(mask, XI_TouchUpdate);
|
|
|
|
XISetMask(mask, XI_TouchEnd);
|
|
|
|
|
|
|
|
error = XISelectEvents(xfi->display, xfi->window->handle, &eventmask, 1);
|
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case BadValue:
|
|
|
|
printf("\tBadValue\n");
|
|
|
|
break;
|
|
|
|
case BadWindow:
|
|
|
|
printf("\tBadWindow\n");
|
|
|
|
break;
|
|
|
|
|
2013-04-26 03:38:13 +04:00
|
|
|
case 0: //no error
|
|
|
|
break;
|
|
|
|
|
2013-04-26 02:58:22 +04:00
|
|
|
default:
|
|
|
|
printf("XISelectEvents() returned %d\n", error);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Server does not support XI 2.2\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("X Input extension not available!!!\n");
|
|
|
|
}
|
|
|
|
}
|
2013-04-26 03:38:13 +04:00
|
|
|
|
|
|
|
void xf_input_handle_event(xfInfo* xfi, XEvent* event)
|
|
|
|
{
|
|
|
|
//handle touch events
|
|
|
|
XGenericEventCookie* cookie = &event->xcookie;
|
|
|
|
|
|
|
|
XGetEventData(xfi->display, cookie);
|
|
|
|
|
|
|
|
if ( (cookie->type == GenericEvent) &&
|
|
|
|
(cookie->extension == xfi->XInputOpcode) )
|
|
|
|
{
|
2013-04-27 00:23:52 +04:00
|
|
|
|
|
|
|
|
2013-04-26 03:38:13 +04:00
|
|
|
switch(cookie->evtype)
|
|
|
|
{
|
|
|
|
case XI_TouchBegin:
|
2013-04-26 06:04:35 +04:00
|
|
|
xf_input_touch_begin(xfi, cookie->data);
|
|
|
|
break;
|
|
|
|
|
2013-04-26 03:38:13 +04:00
|
|
|
case XI_TouchUpdate:
|
2013-04-26 06:04:35 +04:00
|
|
|
xf_input_touch_update(xfi, cookie->data);
|
|
|
|
break;
|
|
|
|
|
2013-04-26 03:38:13 +04:00
|
|
|
case XI_TouchEnd:
|
2013-04-26 06:04:35 +04:00
|
|
|
xf_input_touch_end(xfi, cookie->data);
|
2013-04-26 03:38:13 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("unhandled xi type= %d\n", cookie->evtype);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XFreeEventData(xfi->display,cookie);
|
|
|
|
}
|
2013-04-26 06:04:35 +04:00
|
|
|
|
|
|
|
void xf_input_touch_begin(xfInfo* xfi, XIDeviceEvent* event)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
//find an empty slot and save it
|
|
|
|
for(i=0; i<MAX_CONTACTS; i++)
|
|
|
|
{
|
|
|
|
if(contacts[i].id == 0)
|
|
|
|
{
|
|
|
|
contacts[i].id = event->detail;
|
|
|
|
contacts[i].count = 1;
|
|
|
|
contacts[i].pos_x = event->event_x;
|
|
|
|
contacts[i].pos_y = event->event_y;
|
|
|
|
|
|
|
|
active_contacts++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
printf("\tTouchBegin (%d) [%.2f,%.2f]\n",
|
|
|
|
event->detail,
|
|
|
|
event->event_x,
|
|
|
|
event->event_y);
|
|
|
|
|
2013-04-27 00:23:52 +04:00
|
|
|
printf("state: a=%d time=%lu", active_contacts, event->time);
|
2013-04-26 23:27:08 +04:00
|
|
|
printf("c0=[%d, %d, %.2f, %.2f, %.2f, %.2f]", contacts[0].id, contacts[0].count,contacts[0].pos_x, contacts[0].pos_y, contacts[0].last_x, contacts[0].last_y );
|
|
|
|
printf("c0=[%d, %d, %.2f, %.2f, %.2f, %.2f]", contacts[1].id, contacts[1].count,contacts[1].pos_x, contacts[1].pos_y, contacts[1].last_x, contacts[1].last_y );
|
|
|
|
|
|
|
|
|
2013-04-26 06:04:35 +04:00
|
|
|
}
|
|
|
|
void xf_input_touch_update(xfInfo* xfi, XIDeviceEvent* event)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0; i<MAX_CONTACTS; i++)
|
|
|
|
{
|
|
|
|
if(contacts[i].id == event->detail)
|
|
|
|
{
|
|
|
|
contacts[i].count++;
|
|
|
|
contacts[i].last_x = contacts[i].pos_x;
|
|
|
|
contacts[i].last_y = contacts[i].pos_y;
|
|
|
|
contacts[i].pos_x = event->event_x;
|
|
|
|
contacts[i].pos_y = event->event_y;
|
|
|
|
|
|
|
|
//detect pinch-zoom
|
|
|
|
if(active_contacts == 2)
|
|
|
|
{
|
2013-04-27 00:23:52 +04:00
|
|
|
int j = 0; printf("event: %p\n", event);
|
|
|
|
|
2013-04-26 06:04:35 +04:00
|
|
|
|
|
|
|
//find the other nonzero contact
|
|
|
|
for(j=0; j<MAX_CONTACTS; j++)
|
|
|
|
{
|
|
|
|
if( (contacts[j].id != 0) && (contacts[j].id != contacts[i].id) )
|
|
|
|
{
|
|
|
|
double delta;
|
|
|
|
//now compute the delta
|
|
|
|
delta = sqrt(pow(contacts[j].pos_x - contacts[i].last_x, 2.0) +
|
|
|
|
pow(contacts[j].pos_y - contacts[i].last_y, 2.0));
|
|
|
|
|
|
|
|
printf("delta = %.2f\n", delta);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("\tTouchUpdate (%d) [%.2f,%.2f]\n",
|
|
|
|
event->detail,
|
|
|
|
event->event_x,
|
|
|
|
event->event_y);
|
2013-04-27 00:23:52 +04:00
|
|
|
|
|
|
|
printf("state: a=%d time=%lu", active_contacts, event->time);
|
|
|
|
printf("c0=[%d, %d, %.2f, %.2f, %.2f, %.2f]", contacts[0].id, contacts[0].count,contacts[0].pos_x, contacts[0].pos_y, contacts[0].last_x, contacts[0].last_y );
|
|
|
|
printf("c0=[%d, %d, %.2f, %.2f, %.2f, %.2f]", contacts[1].id, contacts[1].count,contacts[1].pos_x, contacts[1].pos_y, contacts[1].last_x, contacts[1].last_y );
|
2013-04-26 06:04:35 +04:00
|
|
|
}
|
|
|
|
void xf_input_touch_end(xfInfo* xfi, XIDeviceEvent* event)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
//find our and delete the point
|
|
|
|
for(i=0; i<MAX_CONTACTS; i++)
|
|
|
|
{
|
|
|
|
if(contacts[i].id == event->detail)
|
|
|
|
{
|
|
|
|
contacts[i].id = 0;
|
|
|
|
contacts[i].count = 0;
|
|
|
|
//contacts[i].pos_x = (int)event->event_x;
|
|
|
|
//contacts[i].pos_y = (int)event->event_y;
|
|
|
|
|
|
|
|
active_contacts--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("\tTouchEnd (%d) [%.2f,%.2f]\n",
|
|
|
|
event->detail,
|
|
|
|
event->event_x,
|
|
|
|
event->event_y);
|
2013-04-27 00:23:52 +04:00
|
|
|
|
|
|
|
printf("state: a=%d time=%lu", active_contacts, event->time);
|
|
|
|
printf("c0=[%d, %d, %.2f, %.2f, %.2f, %.2f]", contacts[0].id, contacts[0].count,contacts[0].pos_x, contacts[0].pos_y, contacts[0].last_x, contacts[0].last_y );
|
|
|
|
printf("c0=[%d, %d, %.2f, %.2f, %.2f, %.2f]", contacts[1].id, contacts[1].count,contacts[1].pos_x, contacts[1].pos_y, contacts[1].last_x, contacts[1].last_y );
|
2013-04-26 06:04:35 +04:00
|
|
|
}
|
|
|
|
|