Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions dxinput/utils/button_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "button_map.h"
#include "type.h"
#include "x11_mutex.h"

static int get_button_number(Display* disp, const char* name);
static const XDeviceInfo* find_device_by_name(const XDeviceInfo* devs,
Expand All @@ -20,8 +21,6 @@ static int get_device_button_number(const XDeviceInfo* dev);
static unsigned char* do_get_button_map(Display* disp,
unsigned long xid, int nbuttons);

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

unsigned char*
get_button_map(unsigned long xid, const char* name, int* nbuttons)
{
Expand All @@ -37,14 +36,14 @@ get_button_map(unsigned long xid, const char* name, int* nbuttons)
return NULL;
}

pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

Display* disp = XOpenDisplay(NULL);
if (!disp) {
fprintf(stderr, "[get_button_map] open display failed for %lu %s\n",
xid, name);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

Expand All @@ -53,15 +52,15 @@ get_button_map(unsigned long xid, const char* name, int* nbuttons)
XCloseDisplay(disp);
fprintf(stderr, "[get_button_map] get button number failed for %lu %s\n",
xid, name);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

*nbuttons = num_btn;
unsigned char* map = do_get_button_map(disp, xid, num_btn);
XCloseDisplay(disp);

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

return map;
}
Expand All @@ -75,14 +74,14 @@ set_button_map(unsigned long xid, const char* name,
return -1;
}

pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

Display* disp = XOpenDisplay(0);
if (!disp) {
fprintf(stderr, "[set_button_map] open display failed: %lu %s\n",
xid, name);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

Expand All @@ -91,7 +90,7 @@ set_button_map(unsigned long xid, const char* name,
XCloseDisplay(disp);
fprintf(stderr, "[set_button_map] open device failed for %lu %s\n",
xid, name);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

Expand All @@ -101,7 +100,7 @@ set_button_map(unsigned long xid, const char* name,
XCloseDevice(disp, dev);
XCloseDisplay(disp);

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

// TODO: if ret == MappingBusy, try again
if (ret != MappingSuccess) {
Expand Down
9 changes: 4 additions & 5 deletions dxinput/utils/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include "type.h"

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#include "x11_mutex.h"

int
set_keyboard_repeat(int repeated, unsigned int delay, unsigned int interval)
{
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

Display *disp = XOpenDisplay(0);
if (!disp) {
fprintf(stderr, "Open display failed\n");
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

Expand All @@ -49,7 +48,7 @@ set_keyboard_repeat(int repeated, unsigned int delay, unsigned int interval)
XSync(disp, False);
XCloseDisplay(disp);

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

return ret;
}
14 changes: 7 additions & 7 deletions dxinput/utils/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@

#include "list.h"
#include "type.h"
#include "x11_mutex.h"

static int append_device(DeviceInfo** devs, XIDeviceInfo* xinfo, int idx);
static void free_device_info(DeviceInfo* dev);
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

DeviceInfo*
list_device(int* num)
{
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

if (!num) {
fprintf(stderr, "list_device failed, !num\n");
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

Display* disp = XOpenDisplay(0);
if (!disp) {
fprintf(stderr, "Open display failed\n");
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

Expand All @@ -42,7 +42,7 @@ list_device(int* num)
XCloseDisplay(disp);
if (!xinfos) {
fprintf(stderr, "List xinput device failed\n");
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

Expand All @@ -66,7 +66,7 @@ list_device(int* num)
XIFreeDeviceInfo(xinfos);
*num = j;

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

return devs;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ append_device(DeviceInfo** devs, XIDeviceInfo* xinfo, int idx)
tmp[idx].name = name;
tmp[idx].id = xinfo->deviceid;
tmp[idx].enabled = xinfo->enabled;
tmp[idx].ty = query_device_type(xinfo->deviceid);
tmp[idx].ty = query_device_type_unlocked(xinfo->deviceid);

return 0;
}
Expand Down
29 changes: 14 additions & 15 deletions dxinput/utils/property.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

#include "property.h"
#include "type.h"
#include "x11_mutex.h"

#define MAX_BUF_LEN 1000

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

/**
* The return data type if 'char' must be convert to 'int8_t*'
* if 'int' must be convert to 'int32_t*'
Expand All @@ -33,21 +32,21 @@ get_prop(int id, const char* prop, int* nitems)
return NULL;
}

pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

Display* disp = XOpenDisplay(0);
if (!disp) {
fprintf(stderr, "[get_prop] Open display failed for %d\n", id);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

Atom prop_id = XInternAtom(disp, prop, True);
if (prop_id == None) {
XCloseDisplay(disp);
fprintf(stderr, "[get_prop] Intern atom %s failed\n", prop);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

Expand All @@ -61,14 +60,14 @@ get_prop(int id, const char* prop, int* nitems)
if (ret != Success) {
XCloseDisplay(disp);
fprintf(stderr, "[get_prop] Get %s data failed for %d\n", prop, id);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return NULL;
}

*nitems = (int)num_items;
XCloseDisplay(disp);

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

return data;
}
Expand All @@ -83,25 +82,25 @@ set_prop_int(int id, const char* prop, unsigned char* data, int nitems, int bit)
int
set_prop_float(int id, const char* prop, unsigned char* data, int nitems)
{
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

Display* disp = XOpenDisplay(NULL);
if (!disp) {
fprintf(stderr, "[set_prop_float] open display failed\n");
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

Atom type = XInternAtom(disp, "FLOAT", False);
XCloseDisplay(disp);
if (type == None) {
fprintf(stderr, "[set_prop_float] Intern 'FLOAT' atom failed\n");
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

// Format must be 32
int ret = set_prop(id, prop, data, nitems, type, 32);
Expand All @@ -123,21 +122,21 @@ set_prop(int id, const char* prop, unsigned char* data, int nitems,
return -1;
}

pthread_mutex_lock(&mutex);
pthread_mutex_lock(&x11_global_mutex);
setErrorHandler();

Display* disp = XOpenDisplay(0);
if (!disp) {
fprintf(stderr, "[set_prop] Open display failed for %d\n", id);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

Atom prop_id = XInternAtom(disp, prop, True);
if (prop_id == None) {
XCloseDisplay(disp);
fprintf(stderr, "[set_prop] Intern atom %s failed\n", prop);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);
return -1;
}

Expand All @@ -146,7 +145,7 @@ set_prop(int id, const char* prop, unsigned char* data, int nitems,
/* XFree(&prop_id); */
XCloseDisplay(disp);

pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&x11_global_mutex);

return 0;
}
Loading