Skip to content

Commit

Permalink
nxcomp: Duplicate NXTransDialog code as NXTransCallbacksDispatcher.
Browse files Browse the repository at this point in the history
  - As a starting point for newly implementing a callbacks dispatcher
    scenario that allows capturing various session events and execute
    external tools (like dialog boxes, pulldown menus, etc.).
  - The callbacks dispatch approach will finally deprecated the NX_CLIENT
    env var related code path in later releases of nxcomp/nxagent.

 For now, the callbacks dispatcher code path is taken if the env var
 NX_CALLBACKS_DISPATCHER is set, ideally to the applications that handles
 the dispatching of callbacks from nxagent.
  • Loading branch information
sunweaver committed Mar 8, 2017
1 parent 4b67d31 commit 3d4d9ed
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 2 deletions.
11 changes: 9 additions & 2 deletions nx-X11/programs/Xserver/hw/nxagent/Dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ void nxagentLaunchDialog(DialogType dialogType)
int local;
const char *window = NULL;

char *cbDispatcherCmd;

switch (dialogType)
{
case DIALOG_KILL_SESSION:
Expand Down Expand Up @@ -319,9 +321,14 @@ void nxagentLaunchDialog(DialogType dialogType)

sigprocmask(SIG_BLOCK, &set, &oldSet);

*pid = NXTransDialog(nxagentDialogName, message, window,
type, local, dialogDisplay);
cbDispatcherCmd = getenv("NX_CALLBACKS_DISPATCHER");

if (cbDispatcherCmd == NULL)
*pid = NXTransDialog(nxagentDialogName, message, window,
type, local, dialogDisplay);
else
*pid = NXTransCallbacksDispatcher(nxagentDialogName, message, window,
type, local, dialogDisplay);
#ifdef TEST
fprintf(stderr, "nxagentLaunchDialog: Launched dialog %s with pid [%d] on display %s.\n",
DECODE_DIALOG_TYPE(dialogType), *pid, dialogDisplay);
Expand Down
219 changes: 219 additions & 0 deletions nxcomp/Children.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,225 @@ static int NXTransKeeperHandler(int signal);
static void NXTransKeeperCheck();


int NXTransCallbacksDispatcher(const char *caption, const char *message,
const char *window, const char *type, int local,
const char* display)
{
//
// Be sure log file is valid.
//

if (logofs == NULL)
{
logofs = &cerr;
}

int pid;

#ifdef TEST
*logofs << "NXTransCallbacksDispatcher: Going to fork with NX pid '"
<< getpid() << "'.\n" << logofs_flush;
#endif

pid = Fork();

if (pid != 0)
{
if (pid < 0)
{
#ifdef TEST
*logofs << "NXTransCallbacksDispatcher: WARNING! Function fork failed. "
<< "Error is " << EGET() << " '" << ESTR()
<< "'.\n" << logofs_flush;
#endif

cerr << "Warning" << ": Function fork failed. "
<< "Error is " << EGET() << " '" << ESTR()
<< "'.\n";
}
#ifdef TEST
else
{
*logofs << "NXTransCallbacksDispatcher: Created NX dialog process "
<< "with pid '" << pid << "'.\n"
<< logofs_flush;
}
#endif

return pid;
}

#ifdef TEST
*logofs << "NXTransCallbacksDispatcher: Executing child with pid '"
<< getpid() << "' and parent '" << getppid()
<< "'.\n" << logofs_flush;
#endif

SystemCleanup("NXTransCallbacksDispatcher");

//
// Copy the client command before
// freeing up the control class.
//

char command[DEFAULT_STRING_LIMIT];

if (control != NULL)
{
strcpy(command, control -> ClientPath);
}
else
{
char *path = GetClientPath();

strcpy(command, path);

delete [] path;
}

//
// Get rid of the unused resources.
//

MemoryCleanup("NXTransCallbacksDispatcher");

#ifdef TEST
*logofs << "NXTransCallbacksDispatcher: Running external NX dialog with caption '"
<< caption << "' message '" << message << "' type '"
<< type << "' local '" << local << "' display '"
<< display << "'.\n"
<< logofs_flush;
#endif

int pulldown = (strcmp(type, "pulldown") == 0);

char parent[DEFAULT_STRING_LIMIT];

snprintf(parent, DEFAULT_STRING_LIMIT, "%d", getppid());

parent[DEFAULT_STRING_LIMIT - 1] = '\0';

UnsetEnv("LD_LIBRARY_PATH");

for (int i = 0; i < 2; i++)
{
if (local != 0)
{
if (pulldown)
{
execlp(command, command, "--dialog", type, "--caption", caption,
"--window", window, "--local", "--parent", parent,
"--display", display, NULL);
}
else
{
execlp(command, command, "--dialog", type, "--caption", caption,
"--message", message, "--local", "--parent", parent,
"--display", display, NULL);
}
}
else
{
if (pulldown)
{
execlp(command, command, "--dialog", type, "--caption", caption,
"--window", window, "--parent", parent,
"--display", display, NULL);
}
else
{
execlp(command, command, "--dialog", type, "--caption", caption,
"--message", message, "--parent", parent,
"--display", display, NULL);
}
}

#ifdef WARNING
*logofs << "NXTransCallbacksDispatcher: WARNING! Couldn't start '"
<< command << "'. " << "Error is " << EGET()
<< " '" << ESTR() << "'.\n" << logofs_flush;
#endif

cerr << "Warning" << ": Couldn't start '" << command
<< "'. Error is " << EGET() << " '" << ESTR()
<< "'.\n";

//
// Retry by looking for the default name
// in the default NX path.
//

if (i == 0)
{

strcpy(command, "nxclient");

char newPath[DEFAULT_STRING_LIMIT];

strcpy(newPath, "/usr/NX/bin:/opt/NX/bin:/usr/local/NX/bin:");

#ifdef __APPLE__

strcat(newPath, "/Applications/NX Client for OSX.app/Contents/MacOS:");

#endif

#ifdef __CYGWIN32__

strcat(newPath, ".:");

#endif

int newLength = strlen(newPath);

char *oldPath = getenv("PATH");

strncpy(newPath + newLength, oldPath, DEFAULT_STRING_LIMIT - newLength - 1);

newPath[DEFAULT_STRING_LIMIT - 1] = '\0';

#ifdef WARNING
*logofs << "NXTransCallbacksDispatcher: WARNING! Trying with path '"
<< newPath << "'.\n" << logofs_flush;
#endif

cerr << "Warning" << ": Trying with path '" << newPath
<< "'.\n";

//
// Solaris doesn't seem to have
// function setenv().
//

#ifdef __sun

char newEnv[DEFAULT_STRING_LIMIT + 5];

sprintf(newEnv,"PATH=%s", newPath);

putenv(newEnv);

#else

setenv("PATH", newPath, 1);

#endif

}
}

//
// Hopefully useless.
//

exit(0);
}

//
// Start a nxclient process in dialog mode.
//


//
// Start a nxclient process in dialog mode.
//
Expand Down
4 changes: 4 additions & 0 deletions nxcomp/NX.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ extern int NXTransProxy(int fd, int mode, const char *display);

extern int NXTransClient(const char *display);

extern int NXTransCallbacksDispatcher(const char *caption, const char *message,
const char *window, const char *type, int local,
const char *display);

extern int NXTransDialog(const char *caption, const char *message,
const char *window, const char *type, int local,
const char *display);
Expand Down

0 comments on commit 3d4d9ed

Please sign in to comment.