Link to home
Start Free TrialLog in
Avatar of jimdgar
jimdgar

asked on

C/Motif on unix: how to pause app while waiting for user response?

I'm trying to implement a reusable function which asks the user for a response, then performs tasks based on the response.  The app needs to pause and wait until the response is received, but I can't figure out how to do this without locking up the entire app.  In the following example, the Response at line 48 always comes back blank because the run_b routine keeps processing instead of waiting for user_response to complete.  Uncommenting the lines at 46.47 causes the entire app to lockup.  How can I do this?

#define  true            1
#define  false           0

#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/PushBG.h>
#include <Xm/MessageB.h>

Widget toplevel;
void question_cb (Widget parent, XtPointer client_data, XtPointer call_data);
void run_cb (Widget widget, XtPointer client_data, XtPointer call_data);
void user_response (char *question);
char Response;
int response_valid;

main (int argc, char *argv[])
{
  XtAppContext app;
  Widget main_window,run_button;

  XtSetLanguageProc (NULL, NULL, NULL);
  toplevel = XtVaAppInitialize (&app, "App", NULL, 0, &argc, argv, NULL,
                        XmNtitle, "Program Do Something Or Other",
                        XmNwidth,     250,
                        XmNheight,    150,
                        NULL);
  main_window = XtVaCreateManagedWidget ("main_window",
                               xmMainWindowWidgetClass, toplevel,
                               NULL);

  run_button = XtVaCreateManagedWidget ("Run", xmPushButtonGadgetClass, main_window,
                              XmNtopAttachment, main_window,
                              NULL);
  XtAddCallback (run_button, XmNactivateCallback, run_cb, NULL);

  XtRealizeWidget (toplevel);

  XtAppMainLoop (app);
}


void run_cb (Widget widget, XtPointer client_data, XtPointer call_data)
{
  /* process a lot of stuff */
 
  user_response ("Do you want to whatever?");
/*  while (!response_valid) {
  }*/
  printf ("Response = '%c'\n",Response);
  if (Response == 'Y') {
    /* do some things */
  } else {
    /* do other things */
  }

  /* continue processing */
}

void user_response (char *question)
{
  Widget dialog;

  response_valid = false;
  dialog = XmCreateQuestionDialog (toplevel, "notice", NULL, 0);
  XtVaSetValues (dialog,
             XmNdialogTitle, XmStringCreateLocalized ("Response"),
             XmNmessageString, XmStringCreateLocalized (question),
             XmNokLabelString, XmStringCreateLocalized ("Yes"),
             XmNcancelLabelString, XmStringCreateLocalized ("No"),
             NULL);
  XtAddCallback (dialog, XmNokCallback, question_cb, NULL);
  XtAddCallback (dialog, XmNcancelCallback, question_cb, NULL);
  XtUnmanageChild (XmMessageBoxGetChild (dialog, XmDIALOG_HELP_BUTTON));
  XtManageChild (dialog);
  XtPopup (XtParent (dialog), XtGrabNone);
}


void question_cb (Widget parent, XtPointer client_data, XtPointer call_data)
{
  XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) call_data;

  switch (cbs->reason) {
  case XmCR_OK :
    Response = 'Y';
    break;
  case XmCR_CANCEL :
    Response = 'N';
    XtPopdown (XtParent (parent));
    break;
  case XmCR_ACTIVATE :
    break;
  case XmCR_HELP :
    break;
  }
  response_valid = true;
}


Avatar of sunnycoder
sunnycoder
Flag of India image

Not into Motif so no code from my side but there are a couple of possibilities

1. Fork a new process/thread ... let one process/thread block for user while other does the job

2. Use semaphores or conditional variables ... Suppose func A sets something that func B uses and you dont want to block the rest of the app,
Iitialize a semaphore S to 0

func A ()
{
        acquire S if value is 0
        do the processing
        set S to 1 and continue
}

func B ()
{
       acquire S if value is 1
       do the processing
       set S to 0
       continue
}

this will ensure alternation of turns ... You can also use conditional variables if you have more threads in the fray.
>>  user_response ("Do you want to whatever?");

This line only creates the UI part and sets the callbacks.

You are trying to use the wrong  programming model. X/Motif is an event driven system. Therefore the action takes place in an event handler. The event handler for your response is "question_cb() " and that's where youi have to deal with the response. If you are planning to create a kind of "GUI library" you have to use the callback function as input paramater for "user_response()"

There is no reason for you to wait for the reply, since X is already taking care of that wait. You provided X with a way to handle the reply by installing the event handler "question_cb() ". This function is only called when (!) the user presses one of the buttons.

=====
Werner
Avatar of jimdgar
jimdgar

ASKER

Conceptually I understand what you're saying but I still can't see how to implement this.  The legacy C code I have is "flow-driven" and depends on getting inputs from the user to make decisions.  These inputs cannot be predetermined, they must happen in realtime.  In a command-line mode it's easy since the entire app simply waits until the user enters a response to an xterm.  Now I want to get the same response from a popup text entry window.

Anyone know of any guidelines on how to do this?  I cannot find similar cases in my Motif manuals or online.
ASKER CERTIFIED SOLUTION
Avatar of griessh
griessh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial