| Last Modified On : | April 22, 2009 5:13 PM PDT |
Rate |
|
gboolean isPhysicalKeyboardPresent(MIDPlatformSvc *obj, gboolean *keyboardPresent, GError **error)
{
DBusGConnection *sysBus;
DBusGProxy *proxy;
char **kbdList = NULL,**mouseList = NULL;
char **kbd_ptr,**mouse_ptr,**mousename;
int i = 0,len = 0;
// Initialize the System DBus connection
sysBus = obj->systemBus;
// Connect a proxy to the Hal Manager interface
proxy = dbus_g_proxy_new_for_name(sysBus,"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager","org.freedesktop.Hal.Manager" );
dbus_g_proxy_call(proxy, "FindDeviceByCapability", error, //Find Keyboard via HAL
G_TYPE_STRING, "input.keyboard", G_TYPE_INVALID, // param #1
G_TYPE_STRV, &kbdList, G_TYPE_INVALID); // ret val
if (kbdList)
len = g_strv_length(kbdList);
if (len == 0) { // no keyboard found
*keyboardPresent = FALSE; // Function Fail
g_set_error (error, // set an error
INPUT_DEVICE_NP, // error domain
1, // error code
"No Keyboard devices were found.\n"); // error message
return TRUE;
}
gboolean haskbd = FALSE,hasmouse;
for (kbd_ptr = kbdList; *kbd_ptr; kbd_ptr++,i++) // iterate through the list of
{ // keyboard devices, checking each udi to for mouse capability
hasmouse = FALSE;
DBusGProxy *kbdProxy = dbus_g_proxy_new_for_name(sysBus, //Open proxy for
"org.freedesktop.Hal",*kbd_ptr,"org.freedesktop.Hal.Device" );//Keyboard UDI
dbus_g_proxy_call(kbdProxy, "GetPropertyStringList", error, // Checking caps
G_TYPE_STRING, "info.capabilities", G_TYPE_INVALID,
G_TYPE_STRV, &mouseList, G_TYPE_INVALID);
len = g_strv_length(mouseList);
for (mouse_ptr = mouseList;*mouse_ptr && !hasmouse;mouse_ptr++)
{
if (!strcmp(*mouse_ptr,"input.mouse"))
hasmouse = TRUE;
}
if (!hasmouse)
{
haskbd = TRUE;
adjustinputdevicenames(obj,*kbd_ptr,ADDIT,KEYBOARD);
}
if (len)
g_strfreev(mouseList);
g_object_unref (kbdProxy);
}
g_strfreev(kbdList);
g_object_unref (proxy);
*keyboardPresent = haskbd;
return TRUE;
}
gboolean isPhysicalPointerPresent(MIDPlatformSvc *obj, gboolean *pointerPresent, GError **error)
{
DBusGConnection *sysBus;
DBusGProxy *proxy;
char **kbdList = NULL,**mouseList = NULL;
char **mouse_ptr,**kbd_ptr,*mouseproduct;
int i = 0,len = 0;
// Initialize the System DBus connection
sysBus = obj->systemBus;
// Connect a proxy to the Hal Manager interface
proxy = dbus_g_proxy_new_for_name(sysBus,"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager","org.freedesktop.Hal.Manager" );
dbus_g_proxy_call(proxy, "FindDeviceByCapability", error, //Find Mouse via HAL
G_TYPE_STRING, "input.mouse", G_TYPE_INVALID, // param #1
G_TYPE_STRV, &mouseList, G_TYPE_INVALID); // ret val
if (mouseList)
len = g_strv_length(mouseList);
if (len == 0) { // no keyboard found
*pointerPresent = FALSE; // Function fails
g_set_error (error, // set an error
INPUT_DEVICE_NP, // error domain
1, // error code
"No Mouse devices were found.\n"); // error message
return TRUE;
}
gboolean hasmouse = FALSE,haskeyboard; // No solely mouse capability is assumed
for (i=0,mouse_ptr = mouseList; *mouse_ptr && !hasmouse; mouse_ptr++,i++)
// iterate through the list of mouse devices, checking each udi for keyboard capability
{
haskeyboard = FALSE;
DBusGProxy *mouseProxy = dbus_g_proxy_new_for_name(sysBus,"org.freedesktop.Hal",
*mouse_ptr,"org.freedesktop.Hal.Device" ); //Open Proxy for Mouse UDI
dbus_g_proxy_call(mouseProxy, "GetPropertyString", error, // Checking caps.
G_TYPE_STRING, "info.product", G_TYPE_INVALID,
G_TYPE_STRING, &mouseproduct, G_TYPE_INVALID);
if (strcmp(mouseproduct,"Macintosh mouse button emulation")) // if mouse
{ // device is not Macintosh mouse button emulation continue
dbus_g_proxy_call(mouseProxy, "GetPropertyStringList", error,
G_TYPE_STRING, "info.capabilities", G_TYPE_INVALID,
G_TYPE_STRV, &kbdList, G_TYPE_INVALID);
len = g_strv_length(kbdList);
for (kbd_ptr = kbdList; *kbd_ptr && !haskeyboard; kbd_ptr++) //check to
{ // see if device has any "Keyboard" capability as well; if so,
// device is not solely a mouse device
if (!strncmp(*kbd_ptr,"input.key",9)) //check for input.key*,
// if any exist we have keyboard capability, not just mouse
haskeyboard = TRUE;
}
if (!haskeyboard) //set mouse capability with respect to keyboard:
// if no keyboard we have mouse; if keyboard we
{ // don't have mouse only
hasmouse = TRUE;
adjustinputdevicenames(obj,*mouse_ptr,ADDIT,MOUSE);
}
if (len)
g_strfreev(kbdList); //free elements as appropriate
}
free(mouseproduct);
g_object_unref (mouseProxy); //free proxy
}
g_strfreev (mouseList);
g_object_unref (proxy);
*pointerPresent = hasmouse;
return TRUE;
}
#define ADDIT 1
#define REMOVEIT 2
#define CLEANUP 3
#define KEYBOARD 4
#define MOUSE 5
#define NOACTION 6
#define INITIALIZE 7
#define TBLSIZE 20
typedef struct
{ char *name;
int active;
int role;
} DEVICE_INFO;
static void adjustinputdevicenames(MIDPlatformSvc *obj,char *name,int action,int role)
{
static DEVICE_INFO inpdevs[TBLSIZE]; // static variables will keep state
int i;
static int devcount = 0,mousecount = 0, keyboardcount = 0;
static gboolean firstpass = TRUE;
gboolean present = FALSE;
if (action == ADDIT) // operation is to add UDI to tracking mechanism
{
gboolean adjcount = FALSE;
for (i = 0; i < devcount && !present; i++)
if (!strcmp(name,inpdevs[i].name) && inpdevs[i].role == role)
{ // check to see if UDI name and role already exist and is yes
adjcount = !inpdevs[i].active; // determine previous state
inpdevs[i].active = TRUE; // set as active
present = TRUE; // set as present
}
if (!present) // UDI is not present so it is added
{
inpdevs[devcount].name = strdup(name); // keep UDI name
inpdevs[devcount].active = TRUE; // set as active
inpdevs[devcount].role = role; // store its role
devcount++; // increase device count
adjcount = TRUE; // indicate adjustment necessary
}
if (adjcount)
{ //if adjustments are necessary, make them based on role of UDI
if (role == KEYBOARD)
{ // if keyboard has not been present and now is then
if (!firstpass && !keyboardcount) // alert users of change
keyboardPresentChanged(obj,TRUE,NULL);
keyboardcount++; // adjust keyboard count
}
else if (role == MOUSE)
{ // if mouse has not been present and now is then
if (!firstpass && !mousecount) // alert users of change
pointerPresentChanged(obj,TRUE,NULL);
mousecount++; // adjust mouse count
}
firstpass = FALSE; // set controlling variable to FALSE
}
}
else if (action == REMOVEIT) // operation is to remove UDI as active
{
for (i = 0; i < devcount; i++) // for each UDI that is tracked
{ // if UDI is being queried and is active
if (!strcmp(name,inpdevs[i].name) && inpdevs[i].active)
{
inpdevs[i].active = FALSE; // set as inactive
if (inpdevs[i].role == KEYBOARD)
{ // if role is keyboard decrement refence count
keyboardcount--; // and if now no keyboards are
if (!firstpass && keyboardcount < 1) // present
keyboardPresentChanged(obj,FALSE,NULL);
} // alert users of change
else if (inpdevs[i].role == MOUSE)
{ // if role is mouse, decrement reference count
mousecount--; // and is now no mouse is present
if (!firstpass && mousecount < 1) // alert users
pointerPresentChanged(obj,FALSE,NULL);
} //of change
}
}
firstpass = FALSE;
}
else if (action == CLEANUP) // operation is to cleanup information
{
for (i = 0; i < devcount; i++)
{
free(inpdevs[i].name); // release memory allocated for name
inpdevs[i].name = NULL;
inpdevs[i].active = FALSE; // set active and role to zero state
inpdevs[i].role = 0;
}
devcount = 0; // set counters all to zero
keyboardcount = 0;
mousecount = 0;
firstpass = TRUE; // set to initial state
}
else if (action == INITIALIZE && firstpass) // operation is to initialize
{
for (i = 0; i < TBLSIZE; i++)
{
inpdevs[i].name = NULL; // set a known state for all
inpdevs[i].active = FALSE; // elements
inpdevs[i].role = 0;
}
}
}
void beginInputEventThread(MIDPlatformSvc *obj)
{
DBusGConnection *sysBus = obj->systemBus; // Initialize the System DBus connection
DBusGProxy *proxy;
gboolean kpresent,mpresent;
GError *error = NULL;
adjustinputdevicenames(obj,NULL,INITIALIZE,NOACTION); //initialize keyboard/mouse data
isPhysicalPointerPresent(obj,&mpresent,&error); //Make first check of mouse
isPhysicalKeyboardPresent(obj,&kpresent,&error); //Make first check of keyboards
// Connect a proxy to the Hal Manager interface
proxy = dbus_g_proxy_new_for_name(sysBus, // Retrieve proxy to HAL manager
"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager" );
dbus_g_proxy_add_signal(proxy,"DeviceAdded",G_TYPE_STRING,G_TYPE_INVALID);
dbus_g_proxy_connect_signal(proxy,"DeviceAdded",G_CALLBACK(checkAddedDevice),
(void *)obj,NULL); // add and connect to DeviceAdded signal
// with callbackfunction checkAddedDevice()
dbus_g_proxy_add_signal(proxy,"DeviceRemoved",G_TYPE_STRING,G_TYPE_INVALID);
dbus_g_proxy_connect_signal(proxy,"DeviceRemoved",G_CALLBACK(checkRemovedDevice),
(void *)obj,NULL); // add and connect to DeviceRemoved signal
return; // with callback function checkRemovedDevice
}
void checkRemovedDevice(DBusGProxy *proxy, gchar *device_udi, gpointer userData)
{
adjustinputdevicenames((MIDPlatformSvc *)userData,device_udi,REMOVEIT,NOACTION);
return;
}
void checkAddedDevice(DBusGProxy *proxy, gchar *device_udi, gpointer userData)
{
examineUDI(proxy,device_udi,(MIDPlatformSvc *)userData);
return;
}
static void examineUDI (DBusGProxy *proxy, gchar *device_udi,MIDPlatformSvc *obj)
{
char **devicecaps = NULL,**list1 = NULL,**list2 = NULL;
GError **error = NULL;
int len = -1;
DBusGProxy *capProxy = dbus_g_proxy_new_for_name(obj->systemBus,
"org.freedesktop.Hal",device_udi,
"org.freedesktop.Hal.Device"); //Open Proxy for supplied UDI
dbus_g_proxy_call(capProxy,"GetPropertyStringList", // Checking for device capability
error,G_TYPE_STRING, "info.capabilities", G_TYPE_INVALID,
G_TYPE_STRV, &devicecaps, G_TYPE_INVALID);
if (devicecaps)
{
len = g_strv_length(devicecaps);
for (list1 = devicecaps;*list1;list1++)
{
if (!strncmp(*list1,"input.key",9)) // Is any key type capability present
{
gboolean haskbd = FALSE;
list2 = list1; // check remaining through second pointer
for (;*list2 && !haskbd;list2++) // check for keyboard
if (!strcmp(*list2,"input.keyboard"))
haskbd = TRUE;
if (haskbd) // if there is a keyboard make sure mouse is missing
{
gboolean hasmouse = FALSE;
list1++;//begin mouse search one beyond where key was found
if (list1)
for (;*list1 && !hasmouse;list1++)
if (!strcmp(*list1,"input.mouse"))
hasmouse = TRUE;
if (!hasmouse) // if no mouse present add UDI to kbd list
adjustinputdevicenames(obj,device_udi,ADDIT,
KEYBOARD);
}
break; //end for loop search
}
else if (!strcmp(*list1,"input.mouse")) // is mouse the capability
{
gboolean haskey = FALSE;
list1++;
if (list1) // make sure not “key” capability is present
for (;*list1 && !haskey;list1++)
if (!strncmp(*list1,"input.key",9))
haskey = TRUE;
if (!haskey) // if no key, then add UDI to mouse list
adjustinputdevicenames(obj,device_udi,ADDIT,MOUSE);
break; //end 'for' loop search
}
}
}
g_object_unref (capProxy);
}
gboolean keyboardPresentChanged(MIDPlatformSvc*obj, gboolean isPresent, GError *error)
{
if (error == NULL) //GQuark stuff #define SIG_PHYSICAL_KEYBOARD_PRESENT_ID
{ //"physical_keyboard_present_changed": whether the physical keyboard is present
g_signal_emit(obj,g_inputSignals[SIG_PHYSICAL_KEYBOARD_PRESENT_ID].signalId,
0,isPresent);
}
return TRUE;
}
gboolean pointerPresentChanged(MIDPlatformSvc*obj, gboolean isPresent, GError *error)
{
if (error == NULL) //GQuark stuff #define SIG_PHYSICAL_POINTER_PRESENT_ID
{ //"physical_mouse_present_changed": whether the physical mouse is present
g_signal_emit(obj,g_inputSignals[SIG_PHYSICAL_POINTER_PRESENT_ID].signalId,
0,isPresent);
}
return TRUE;
}
void finalizeInputObject(MIDPlatformSvc *obj)
{
DBusGConnection *sysBus = obj->systemBus; // Initialize the System DBus connection
DBusGProxy *proxy;
gboolean present;
GError *error = NULL;
adjustinputdevicenames(obj,NULL,CLEANUP,NOACTION); //cleanup the tracking table
// Connect a proxy to the Hal Manager interface
proxy = dbus_g_proxy_new_for_name(sysBus,"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager","org.freedesktop.Hal.Manager" );
dbus_g_proxy_disconnect_signal(proxy,"DeviceAdded",G_CALLBACK(checkAddedDevice),
(void *)obj); // disconnect from DeviceAdded signal
dbus_g_proxy_disconnect_signal(proxy,"DeviceRemoved",G_CALLBACK(checkRemovedDevice),
(void *)obj); // disconnect from DeviceRemoved signal
return;
}
gboolean isTouchScreen(MIDPlatformSvc *obj, gboolean *isTouchScreen, GError **error)
{
*isTouchScreen = TRUE;
return TRUE;
}
gboolean isOnScreenKeyboardVisible(MIDPlatformSvc *obj, gboolean *visible, GError **error)
{
*visible = FALSE;
return TRUE;
}
gboolean onScreenKeyboardChanged(MIDPlatformSvc* obj)
{
gboolean visible;
GError* error = NULL;
dbg("Simulating a OnScreenKeyboardChanged Event.");
isOnScreenKeyboardVisible(obj, &visible, &error);
if (error == NULL)
{
g_signal_emit ( obj,
g_inputSignals[SIG_ON_SCREEN_KEYBOARD_CHANGED_ID].signalId ,
0, //GQuark stuff
//the parameters; in this case, whether or not the
//keyboard is visible
);
}//GQuark stuff #define SIG_PHYSICAL_KEYBOARD_PRESENT_ID "on_screen_keyboard_changed"
return TRUE;
}
