Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get and set display name for XOpenDisplay() #34

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#include <nan.h>
#include <v8.h>
#include <vector>
#include "string.h"
#include "mouse.h"
#include "deadbeef_rand.h"
#include "keypress.h"
#include "screen.h"
#include "screengrab.h"
#include "MMBitmap.h"
#include "xdisplay.h"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will probably cause errors on other OSes, right?


using namespace v8;

Expand Down Expand Up @@ -472,6 +474,26 @@ NAN_METHOD(getScreenSize)
NanReturnValue(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 =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels pretty dirty, you know better way to accomplish?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work!

NanUtf8String name(arg[0]);
char *display_name = *name;

std::string(*v8::String::Utf8Value(args[0]->ToString()));
char *display_name = strdup(name.c_str());

setXDisplay(display_name);
NanReturnUndefined();
}

void init(Handle<Object> target)
{

Expand Down Expand Up @@ -505,6 +527,12 @@ void init(Handle<Object> target)
target->Set(NanNew<String>("getScreenSize"),
NanNew<FunctionTemplate>(getScreenSize)->GetFunction());

target->Set(NanNew<String>("getXDisplayName"),
NanNew<FunctionTemplate>(getXDisplayName)->GetFunction());

target->Set(NanNew<String>("setXDisplayName"),
NanNew<FunctionTemplate>(setXDisplayName)->GetFunction());

}

NODE_MODULE(robotjs, init)
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 */