Skip to content

Commit

Permalink
get and set display name for XOpenDisplay()
Browse files Browse the repository at this point in the history
  • Loading branch information
jub3i authored and illegalprime committed Jun 1, 2016
1 parent 483b46d commit cdbf8df
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <nan.h>
#include <v8.h>
#include <vector>
#include "string.h"
#include "mouse.h"
#include "deadbeef_rand.h"
#include "keypress.h"
Expand All @@ -10,6 +11,7 @@
#include "MMBitmap.h"
#include "snprintf.h"
#include "microsleep.h"
#include "xdisplay.h"

using namespace v8;

Expand Down Expand Up @@ -678,6 +680,26 @@ NAN_METHOD(getScreenSize)
info.GetReturnValue().Set(obj);
}

NAN_METHOD(getXDisplayName)
{
NanScope();
NanReturnValue(NanNew<String>(getXDisplay()));
}

NAN_METHOD(setXDisplayName)
{
NanScope();

//Convert arg to c-string
//NOTE: surely better way to go from v8::String to char* ?
std::string name =
std::string(*v8::String::Utf8Value(args[0]->ToString()));
char *display_name = strdup(name.c_str());

setXDisplay(display_name);
NanReturnUndefined();
}

NAN_METHOD(captureScreen)
{
size_t x;
Expand Down Expand Up @@ -850,6 +872,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)
26 changes: 25 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 *display_name = ":0.0";
static int display_name_changed = 0;

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

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

/* 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,13 @@ void XCloseMainDisplay(void)
mainDisplay = NULL;
}
}

char *getXDisplay(void)
{
return display_name;
}

void setXDisplay(char *name) {
display_name = name;
display_name_changed = 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 cdbf8df

Please sign in to comment.