Skip to content

Commit cdbf8df

Browse files
jub3iillegalprime
authored andcommitted
get and set display name for XOpenDisplay()
1 parent 483b46d commit cdbf8df

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/robotjs.cc

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <nan.h>
33
#include <v8.h>
44
#include <vector>
5+
#include "string.h"
56
#include "mouse.h"
67
#include "deadbeef_rand.h"
78
#include "keypress.h"
@@ -10,6 +11,7 @@
1011
#include "MMBitmap.h"
1112
#include "snprintf.h"
1213
#include "microsleep.h"
14+
#include "xdisplay.h"
1315

1416
using namespace v8;
1517

@@ -678,6 +680,26 @@ NAN_METHOD(getScreenSize)
678680
info.GetReturnValue().Set(obj);
679681
}
680682

683+
NAN_METHOD(getXDisplayName)
684+
{
685+
NanScope();
686+
NanReturnValue(NanNew<String>(getXDisplay()));
687+
}
688+
689+
NAN_METHOD(setXDisplayName)
690+
{
691+
NanScope();
692+
693+
//Convert arg to c-string
694+
//NOTE: surely better way to go from v8::String to char* ?
695+
std::string name =
696+
std::string(*v8::String::Utf8Value(args[0]->ToString()));
697+
char *display_name = strdup(name.c_str());
698+
699+
setXDisplay(display_name);
700+
NanReturnUndefined();
701+
}
702+
681703
NAN_METHOD(captureScreen)
682704
{
683705
size_t x;
@@ -850,6 +872,12 @@ NAN_MODULE_INIT(InitAll)
850872

851873
Nan::Set(target, Nan::New("getColor").ToLocalChecked(),
852874
Nan::GetFunction(Nan::New<FunctionTemplate>(getColor)).ToLocalChecked());
875+
876+
Nan::Set(target, Nan::New("getXDisplayName").ToLocalChecked(),
877+
Nan::GetFunction(Nan::New<FunctionTemplate>(getXDisplayName)).ToLocalChecked());
878+
879+
Nan::Set(target, Nan::New("setXDisplayName").ToLocalChecked(),
880+
Nan::GetFunction(Nan::New<FunctionTemplate>(setXDisplayName)).ToLocalChecked());
853881
}
854882

855883
NODE_MODULE(robotjs, InitAll)

src/xdisplay.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
44

55
static Display *mainDisplay = NULL;
66
static int registered = 0;
7+
static char *display_name = ":0.0";
8+
static int display_name_changed = 0;
79

810
Display *XGetMainDisplay(void)
911
{
12+
/* Close the display if display_name has changed */
13+
if (display_name_changed) {
14+
XCloseMainDisplay();
15+
}
16+
display_name_changed = 0;
17+
1018
if (mainDisplay == NULL) {
11-
mainDisplay = XOpenDisplay(NULL);
19+
/* First try the user set display_name */
20+
mainDisplay = XOpenDisplay(display_name);
21+
22+
/* Then try using environment variable DISPLAY */
23+
if (mainDisplay == NULL) {
24+
mainDisplay = XOpenDisplay(NULL);
25+
}
1226

1327
if (mainDisplay == NULL) {
1428
fputs("Could not open main display\n", stderr);
@@ -28,3 +42,13 @@ void XCloseMainDisplay(void)
2842
mainDisplay = NULL;
2943
}
3044
}
45+
46+
char *getXDisplay(void)
47+
{
48+
return display_name;
49+
}
50+
51+
void setXDisplay(char *name) {
52+
display_name = name;
53+
display_name_changed = 1;
54+
}

src/xdisplay.h

+12
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ Display *XGetMainDisplay(void);
1414
/* Closes the main display if it is open, or does nothing if not. */
1515
void XCloseMainDisplay(void);
1616

17+
#ifdef __cplusplus
18+
extern "C"
19+
{
20+
#endif
21+
22+
char *getXDisplay(void);
23+
void setXDisplay(char *name);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
1729
#endif /* XDISPLAY_H */

0 commit comments

Comments
 (0)