Skip to content

Commit

Permalink
Merge pull request #206 from illegalprime/x11-display-spec
Browse files Browse the repository at this point in the history
add a way to specify the X11 display in Linux.
  • Loading branch information
octalmage committed Jun 19, 2016
2 parents a4d8295 + 3f3dc7b commit 772764b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "MMBitmap.h"
#include "snprintf.h"
#include "microsleep.h"
#if defined(USE_X11)
#include "xdisplay.h"
#endif

using namespace v8;

Expand Down Expand Up @@ -690,6 +693,27 @@ NAN_METHOD(getScreenSize)
info.GetReturnValue().Set(obj);
}

NAN_METHOD(getXDisplayName)
{
#if defined(USE_X11)
const char* display = getXDisplay();
info.GetReturnValue().Set(Nan::New<String>(display).ToLocalChecked());
#else
Nan::ThrowError("getXDisplayName is only supported on Linux");
#endif
}

NAN_METHOD(setXDisplayName)
{
#if defined(USE_X11)
Nan::Utf8String string(info[0]);
setXDisplay(*string);
info.GetReturnValue().Set(Nan::New(1));
#else
Nan::ThrowError("setXDisplayName is only supported on Linux");
#endif
}

NAN_METHOD(captureScreen)
{
size_t x;
Expand Down Expand Up @@ -862,6 +886,12 @@ NAN_MODULE_INIT(InitAll)

Nan::Set(target, Nan::New("getColor").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(getColor)).ToLocalChecked());

Nan::Set(target, Nan::New("getXDisplayName").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(getXDisplayName)).ToLocalChecked());

Nan::Set(target, Nan::New("setXDisplayName").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(setXDisplayName)).ToLocalChecked());
}

NODE_MODULE(robotjs, InitAll)
27 changes: 26 additions & 1 deletion src/xdisplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

static Display *mainDisplay = NULL;
static int registered = 0;
static char *displayName = ":0.0";
static int hasDisplayNameChanged = 0;

Display *XGetMainDisplay(void)
{
/* Close the display if displayName has changed */
if (hasDisplayNameChanged) {
XCloseMainDisplay();
hasDisplayNameChanged = 0;
}

if (mainDisplay == NULL) {
mainDisplay = XOpenDisplay(NULL);
/* First try the user set displayName */
mainDisplay = XOpenDisplay(displayName);

/* Then try using environment variable DISPLAY */
if (mainDisplay == NULL) {
mainDisplay = XOpenDisplay(NULL);
}

if (mainDisplay == NULL) {
fputs("Could not open main display\n", stderr);
Expand All @@ -28,3 +42,14 @@ void XCloseMainDisplay(void)
mainDisplay = NULL;
}
}

char *getXDisplay(void)
{
return displayName;
}

void setXDisplay(char *name)
{
displayName = strdup(name);
hasDisplayNameChanged = 1;
}
12 changes: 12 additions & 0 deletions src/xdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ Display *XGetMainDisplay(void);
/* Closes the main display if it is open, or does nothing if not. */
void XCloseMainDisplay(void);

#ifdef __cplusplus
extern "C"
{
#endif

char *getXDisplay(void);
void setXDisplay(char *name);

#ifdef __cplusplus
}
#endif

#endif /* XDISPLAY_H */

0 comments on commit 772764b

Please sign in to comment.