Subject:
Re: [nanogui] RE: Newbie question--passing events between programs
From:
Alex Holden ####@####.####
Date:
29 Jan 2002 16:36:05 -0000
Message-Id: <3C56CEA0.6020300@linuxhacker.org>
Henry Chea wrote:
> Maybe point me to examples, docs, or perhaps edit my source code below to
> show how to solve the problem?
One way is to use a pipe to send events to the slave.
Something along these lines (completely untested):
//control.c
#include <unistd.h>
#include <stdio.h>
#define MWINCLUDECOLORS
#include "microwin/nano-X.h"
GR_WINDOW_ID control_wid;
GR_GC_ID gc;
int control_fd;
int pipefds[2];
void event_handler (GR_EVENT *event);
int main (void)
{
pid_t pid;
if(pipe(pipefds) < 0) {
fprintf(stderr, "pipe() failed\n");
exit(1);
}
if((pid = fork()) < 0) {
fprintf(stderr, "fork() failed\n");
exit(1);
} else if(pid > 0) {
close(pipefds[0]); /* parent closes the output side */
} else {
close(pipefds[1]); /* child closes the input side */
/* Set stdin to the output of the pipe: */
if(dup2(pipefds[0], STDIN_FILENO) != STDIN_FILENO) {
fprintf(stderr, "dup2() failed\n");
exit(1);
}
close(pipefds[0]);
if(execl("/usr/local/bin/SLAVE", "SLAVE", NULL) < 0) {
fprintf(stderr, "execl() failed\n");
exit(1);
}
}
if ((control_fd = GrOpen()) < 0)
{
fprintf (stderr, "GrOpen failed");
exit (1);
}
gc = GrNewGC();
GrSetGCUseBackground (gc, GR_FALSE);
GrSetGCForeground (gc, BLUE);
control_wid = GrNewWindowEx (GR_WM_PROPS_NOMOVE |
GR_WM_PROPS_BORDER |
GR_WM_PROPS_NOAUTOMOVE |
GR_WM_PROPS_NOAUTORESIZE,
"Control Window",
GR_ROOT_WINDOW_ID,
0, 0, 150, 478, YELLOW);
GrSelectEvents (control_wid, GR_EVENT_MASK_EXPOSURE |
GR_EVENT_MASK_BUTTON_DOWN);
GrMapWindow (control_wid);
GrMainLoop (event_handler);
}
void event_handler (GR_EVENT *event)
{
switch (event->type)
{
case GR_EVENT_TYPE_EXPOSURE:
GrText (control_wid, gc, 20, 20,
"Click me", -1, GR_TFASCII);
break;
case GR_EVENT_TYPE_BUTTON_DOWN:
GrClearWindow (control_wid , GR_TRUE);
if(write(pipe_fds[1], "1", 1) != 1) {
fprintf(stderr, "Couldn't write message to child\n");
exit(1);
}
break;
}
}
******************************
//SLAVE.C
#include <stdio.h>
#define MWINCLUDECOLORS
#include "microwin/nano-X.h"
GR_WINDOW_ID slave_wid;
GR_GC_ID gc;
void event_handler (GR_EVENT *event);
int slave_fd;
int main (void)
{
if ((slave_fd = GrOpen()) < 0)
{
fprintf (stderr, "GrOpen failed");
exit (1);
}
gc = GrNewGC();
GrSetGCUseBackground (gc, GR_FALSE);
GrSetGCForeground (gc, RED);
slave_wid = GrNewWindowEx (GR_WM_PROPS_APPFRAME |
GR_WM_PROPS_CAPTION |
GR_WM_PROPS_CLOSEBOX,
"slave Window",
GR_ROOT_WINDOW_ID,
150, 150, 200, 100, WHITE);
GrRegisterInput(STDIN_FILENO);
GrSelectEvents (slave_wid, GR_EVENT_MASK_CLOSE_REQ|
GR_EVENT_TYPE_FDINPUT);
GrMapWindow (slave_wid);
GrMainLoop (event_handler);
}
void event_handler (GR_EVENT *event)
{
char in;
switch (event->type)
{
case GR_EVENT_TYPE_CLOSE_REQ:
GrClose();
exit (0);
case GR_EVENT_TYPE_FDINPUT:
if(read(STDIN_FILENO, &in, 1) != 1) {
fprintf(stderr, "couldn't read event from parent\n");
exit(1);
}
if(in == '1') {
GrText (slave_wid, gc, 50, 50,
"Button 1 pressed", -1, GR_TFASCII);
} else if(in == '2') {
GrText (slave_wid, gc, 50, 50,
"Button 2 pressed", -1, GR_TFASCII);
} else {
GrText (slave_wid, gc, 50, 50,
"Unknown button pressed", -1, GR_TFASCII);
}
break;
}
}
--
------------ Alex Holden - http://www.linuxhacker.org ------------
If it doesn't work, you're not hitting it with a big enough hammer