| /*
*
* Trasys Benchmark using XUI and XLib
*
*
* D. Dierick februari 1989
*/
#include <stdio.h> /* For printf and so on. */
#include <string.h>
#include <decw$include/DECwDwtApplProg.h> /* DECwindows Toolkit */
/*
* These numbers are matched with corresponding numbers in the
* UIL module.
*/
#define k_menu_bar 1
#define k_file_pdme 2
#define k_file_menu 3
#define k_image_pdme 4
#define k_image_menu 5
#define k_utilities_menu 7
#define k_utilities_pdme 6
#define k_nyi 99
#define k_image_1_drawing 1
#define k_image_2_drawing 2
#define k_image_3_drawing 3
/* widgets id from DRM hierarchy */
#define k_mail_button 4
#define k_card_button 5
#define k_show_1_button 12
#define k_show_2_button 13
#define k_popup_menu 11
#define k_adb 14
#define k_pdb 15
#define k_id 16
#define k_description 17
#define k_status 18
#define k_x 19
#define k_y 20
#define k_dismiss 21
#define Success 1
#define RED 0
#define BLUE 1
#define GREEN 2
#define FIREBRICK 3
#define BLACK 4
#define BORDER 3
/*
* My type_definitions.
*/
typedef unsigned long Color ;
typedef struct
{ int valve_id;
int xo;
int yo;
int vxo;
int vyo;
int w;
int h;
char *description;
int status;
} valve;
typedef struct
{ int text_id;
int xo;
int yo;
int w;
int h;
int status;
char *text;
} simple_text;
/*
* Global data
*/
static int interval; /* event time interval */
static int is_image; /* switch indicating image drawn */
static int selected_valve; /* current valve from popup menu */
static valve valves[100];
static simple_text labels[100];
static Display *display_id; /* The display_id where the application */
/* is running on. */
static Widget toplevel_widget, /* Root widget ID of our application. */
mmi_main_window, /* Root widget ID of main DRM fetch */
popup_menu_widget,
pdb_widget, /* popup box with valve info */
id_widget,
description_widget,
status_widget,
x_widget,
y_widget,
dismiss_widget,
adb_widget, /* attached box containing draw wind*/
show_1_widget,
show_2_widget,
draw_widget;
static Window draw_window; /* Window ID of the Draw_window */
static Pixmap pixmap_id;
Screen *screen_id ;
static GC gc_id,
gc_clear;
static DRMHierarchy s_DRMHierarchy; /* DRM database hierarchy ID */
static DRMType *dummy_class; /* and class variable. */
static DRMType *dummy_class2; /* and class variable. */
static char *mmi_filename_vec[] = /* DRM heirachy file list. */
{ "2nd.uid" }; /* There is only one UID file */
static int mmi_filename_num =
(sizeof mmi_filename_vec / sizeof mmi_filename_vec [0]);
static int window_width, /* Width & Height of the draw_window */
window_height;
XColor exact_color,screen_color;
XColor *colors[] = { "red","blue","green","firebrick","black"};
XColor color[4];
int screen;
/*
* Function declarations
*/
XtTimerCallbackProc timer_proc();
static int give_random();
static char *itoa();
static void set_something();
static void s_error();
static void create_pdb();
static void create_proc();
static void quit_proc();
static void activate_proc();
static void create_display();
static void clear_pixmap();
static void copy_pixmap_to_display();
static void draw_image_1();
static void draw_text();
static void init_image_1();
static void menu_flip_color();
static void flip_color();
static void expose_display();
static void popup_menu();
static void display_dialog();
static void dismiss_box();
/* Make pointers for the callbacks comming from DECToolkit (DRM). */
/* The names and addresses of things that DRM has to bind.
* The names do not have to be in alphabetical order. */
static DRMRegisterArg reglist[] = {
{"create_proc", (caddr_t) create_proc},
{"dismiss_box", (caddr_t) dismiss_box},
{"activate_proc", (caddr_t) activate_proc},
{"menu_flip_color", (caddr_t) menu_flip_color},
{"flip_color", (caddr_t) flip_color},
{"quit_proc", (caddr_t) quit_proc},
{"create_display", (caddr_t) create_display},
{"expose_display", (caddr_t) expose_display},
{"popup_menu", (caddr_t) popup_menu},
{"display_dialog", (caddr_t) display_dialog}
};
/* this will contain the action records for the translation of mouse
button events in the graphics window */
static XtActionsRec trasys_action_table [] =
{
{"flip_color", (caddr_t)flip_color },
{"popup_menu", (caddr_t)popup_menu }
};
static DRMResourceContextPtr context_id;
/* register list for DRM */
static int reglist_num = (sizeof reglist / sizeof reglist [0]);
/*
* The main routine does all the one-time setup and
* then calls XtMainLoop.
*/
unsigned int main(argc, argv)
unsigned int argc; /* Command line argument count. */
char *argv[]; /* Pointers to command line args. */
{
/* first initialize image 1 data */
init_image_1();
DwtInitializeDRM(); /* Initialize DRM before initializing
/* the X Toolkit. */
/* Initialize the X Toolkit. We get back a top level shell widget. */
toplevel_widget = XtInitialize(
"TRASYS BENCHMARK (don't worry, be happy)", /* Main window name. */
"mmi", /* Root class name.
This name is also be used
for the <application>.dat file !!!
so it is in fact <class_name>.dat !*/
NULL, /* No option list. */
0, /* Number of options. */
&argc, /* Address of argc */
argv); /* argv */
/* Open the UID files in the hierarchy*/
if (DwtOpenHierarchy(mmi_filename_num, /* Number of files. */
mmi_filename_vec, /* Array of file names. */
NULL, /* Default OS extenstion. */
&s_DRMHierarchy) /* Pointer to returned DRM ID */
!= DRMSuccess )
/*then*/ s_error("Can't open UID !!!");
if (DwtDrmGetResourceContext (0,0,4 ,&context_id) != DRMSuccess)
s_error("Can't get resource context \n");
if (DwtDrmHGetIndexedLiteral (s_DRMHierarchy,"interval",context_id)
!= DRMSuccess) s_error("Can't get indexed literal\n");
interval = *(context_id->data_buffer);
interval = 999; /* override uil value for test reason */
/* Register the items DRM needs to bind for us. */
DwtRegisterDRMNames(reglist, reglist_num);
/* Go get the application. (from the UID-file (DRManager)) */
if (DwtFetchWidget(s_DRMHierarchy, "S_MAIN_WINDOW", toplevel_widget,
&mmi_main_window, &dummy_class) != DRMSuccess)
s_error("Can't fetch main window !!!\n");
/* because we use translations to get non_standard_toolkit events
(button_events in draw_window) we have to add the actions here */
/* make our action table known to the toolkit */
XtAddActions (trasys_action_table, 2);
/* Manage and realize everything.
* The interface comes up on the display now. */
XtManageChild(mmi_main_window);
XtRealizeWidget(toplevel_widget);
/* initialize graphical context, create pixmaps */
init_application();
/* fetch some popup widgets */
create_pdb();
if (DwtFetchWidget(s_DRMHierarchy, "mpopup",adb_widget,
&popup_menu_widget,&dummy_class) != DRMSuccess)
s_error("Can't get valve popup menu !!!\n");
XtManageChild(show_1_widget);
XtManageChild(show_2_widget);
/* start the timer clock */
XtAddTimeOut (interval, timer_proc,0);
/* Sit around forever waiting to process X-events. We never leave
* XtMainLoop. From here on, we only execute our callback routines. */
XtMainLoop();
/* Normally you can NEVER get here !!! */
s_error("You passed the XtMainLoop call ???");
}
/*
* All errors are fatal.
*/
static void s_error(problem_string)
char *problem_string;
{
exit(0);
}
/*
* The user pushed the quit button, so the application exits.
*/
static void quit_proc(w, tag, reason)
Widget w;
int *tag;
unsigned long *reason;
{ if (tag != NULL)
exit(1);
}
static void create_proc(w, tag, reason)
Widget w;
int *tag;
unsigned long *reason;
{ int our_id = *tag;
switch (our_id)
/* just to get the widget number of the popup menu to manage it */
{
case k_show_1_button:
show_1_widget = w;
break;
case k_show_2_button:
show_2_widget = w;
break;
case k_adb:
adb_widget = w;
break;
case k_pdb:
pdb_widget = w;
break;
case k_id:
id_widget = w;
break;
case k_description:
description_widget = w;
break;
case k_status:
status_widget = w;
break;
case k_x:
x_widget = w;
break;
case k_y:
y_widget = w;
break;
case k_dismiss:
dismiss_widget = w;
break;
default :
break;
}
}
static void activate_proc(w, tag, reason)
Widget w;
int *tag;
unsigned long *reason;
{ int our_id = *tag;
switch (our_id)
{
case k_image_1_drawing:
draw_image_1();
break;
case k_image_2_drawing:
draw_image_1();
break;
default :
break;
}
}
static int init_application()
{ unsigned int screen_depth ;
XtGCMask gc_mask;
XGCValues gc_values;
Color black,
white ;
Window dummy_root;
int dummy_x,dummy_y;
unsigned int border_width;
/* the widget_id is found during the create_display_callback,
which is executed during the fetch */
/* convert the draw_window_widget_id into a draw_window_window_id */
draw_window = XtWindow(draw_widget) ;
/* find now the display_id, we need this when we are going to draw. */
/* display_id is the static variable where we are going to put it in */
display_id = XtDisplay(toplevel_widget);
XGetGeometry(display_id, draw_window,
&dummy_root,
&dummy_x, &dummy_y,
&window_width, &window_height,
&border_width,
&screen_depth) ;
pixmap_id = XCreatePixmap(display_id,
XDefaultRootWindow(display_id),
window_width,window_height,
screen_depth);
/* now init (clear) the pixmap */
/* Set the values in the GCValues structure and set masks for the GC
Then Create the "GC_clear" and get the GC identifier return value */
screen_id = XDefaultScreenOfDisplay (display_id) ;
black = XBlackPixel(display_id,*screen_id);
white = XWhitePixel(display_id,*screen_id);
gc_mask = GCFunction | GCForeground | GCBackground | GCPlaneMask;
gc_values.plane_mask = AllPlanes;
gc_values.function = GXcopy;
gc_values.foreground = white;
gc_values.background = black;
/* create a GC_clear to clear the pixmap */
gc_clear = XCreateGC(display_id, pixmap_id, gc_mask, &gc_values);
/* now clear the pixmap */
clear_pixmap();
/* create a GC for all the drawing stuff */
gc_id = XCreateGC(display_id, pixmap_id, 0, NULL);
color[RED].pixel = define_color(RED);
color[BLUE].pixel = define_color(BLUE);
color[GREEN].pixel = define_color(GREEN);
color[FIREBRICK].pixel = define_color(FIREBRICK);
color[BLACK].pixel = define_color(BLACK);
}
static void create_display(w, tag, reason)
Widget w;
int *tag;
unsigned long *reason;
{
/* draw_widget is the static_variable where we put
the draw_window_widget_i */
draw_widget = w;
}
/*
* Clear_pixmap routine.
*/
static void clear_pixmap()
{
/* now clear the pixmap by drawing a filled_rectangle */
XFillRectangle(display_id, pixmap_id, gc_clear, 0,
0, window_width, window_height);
}
static void draw_valve(id)
int id;
{ XPoint pts[4];
pts[0].x = valves[id].xo+84;
pts[0].y = valves[id].yo-5;
pts[1].x = valves[id].xo+114;
pts[1].y = valves[id].yo+5;
pts[2].x = valves[id].xo+114;
pts[2].y = valves[id].yo-5;
pts[3].x = valves[id].xo+84;
pts[3].y = valves[id].yo+5;
XSetForeground(display_id,gc_id,color[BLACK].pixel);
XDrawLine (display_id,draw_window,gc_id,valves[id].xo,valves[id].yo,
valves[id].xo+84,valves[id].yo);
XDrawLine (display_id,pixmap_id,gc_id,valves[id].xo,valves[id].yo,
valves[id].xo+84,valves[id].yo);
XDrawLine (display_id,draw_window,gc_id,valves[id].xo+114,valves[id].yo,
valves[id].xo+200,valves[id].yo);
XDrawLine (display_id,pixmap_id,gc_id,valves[id].xo+114,valves[id].yo,
valves[id].xo+200,valves[id].yo);
if (valves[id].status ==1 )
XSetForeground(display_id,gc_id,color[GREEN].pixel);
else
XSetForeground(display_id,gc_id,color[RED].pixel);
XFillPolygon(display_id,draw_window,gc_id,pts,4,Complex,
CoordModeOrigin);
XFillPolygon(display_id,pixmap_id,gc_id,pts,4,Complex,
CoordModeOrigin);
}
static void draw_image_1()
{ int count;
int i;
is_image = 1;
clear_pixmap();
XClearWindow(display_id,draw_window);
for (i = 0; i<= 99 ; i++)
draw_valve(i,0); /* only draw to pixmap*/
for ( i = 0 ; i <= 99 ; i++)
draw_text(i);
}
/*
* Copy the pixmap to the display routine.
*/
static void copy_pixmap_to_display()
{
XCopyArea(display_id, pixmap_id, draw_window, gc_id, 0, 0 ,
window_width, window_height, 0, 0 ) ;
}
/*
* Routine to draw simple graphics text in a stupid box
*/
static void draw_text(id)
int id;
{
XtGCMask gc_mask;
XGCValues gc_values;
XSetForeground(display_id,gc_id,color[BLACK].pixel);
XDrawRectangle(display_id,draw_window,gc_id,labels[id].xo,labels[id].yo,
labels[id].w,labels[id].h);
XDrawRectangle(display_id,pixmap_id,gc_id,labels[id].xo,labels[id].yo,
labels[id].w,labels[id].h);
if (labels[id].status == 1)
XSetForeground(display_id,gc_id,color[BLUE].pixel);
else
XSetForeground(display_id,gc_id,color[RED].pixel);
XDrawString(display_id,draw_window,gc_id,labels[id].xo+5,labels[id].yo+16,
labels[id].text,strlen(labels[id].text));
XDrawString(display_id,pixmap_id,gc_id,labels[id].xo+5,labels[id].yo+16,
labels[id].text,strlen(labels[id].text));
}
/*
* Routine to allocate a named color
*/
static int define_color(n)
int n;
{
if ((XDefaultVisualOfScreen(screen_id)->class == PseudoColor
|| (XDefaultVisualOfScreen(screen_id))->class == DirectColor))
if (XAllocNamedColor(display_id,DefaultColormapOfScreen(screen_id),
colors[n],&screen_color,&exact_color))
return screen_color.pixel;
else printf("Color not allocated\n");
}
static void init_image_1()
{ int count,y;
y = 10;
for (count=0;count<=49;count+= 1,y+=16)
{ valves[count].xo = 26;
valves[count].yo = y;
valves[count].status = 1;
valves[count].valve_id = count;
valves[count].vxo = 110;
valves[count].vyo = y - 5;
valves[count].description = " Distrigaz Valve Range A ";
valves[count].w = 30;
valves[count].h = 10;
}
y= 10;
for (count=50;count<=99;count+= 1,y+=16)
{ valves[count].xo = 276;
valves[count].yo = y;
valves[count].status = 1;
valves[count].valve_id = count;
valves[count].vxo = 360;
valves[count].vyo = y - 5;
valves[count].description = " Distrigaz Valve Range B ";
valves[count].w = 30;
valves[count].h = 10;
}
y = 10;
for (count=0;count <= 24; count +=1,y+=32)
{ labels[count].xo = 510;
labels[count].yo = y;
labels[count].text = " STATE IS NORMAL ";
labels[count].status = 1;
labels[count].text_id = count;
labels[count].w = 100;
labels[count].h = 20;
}
y = 10;
for (count=25;count <= 49; count +=1,y+=32)
{ labels[count].xo = 620;
labels[count].yo = y;
labels[count].text = " STATE IS NORMAL ";
labels[count].status = 1;
labels[count].text_id = count;
labels[count].w = 100;
labels[count].h = 20;
}
y = 10;
for (count=50;count <= 74; count +=1,y+=32)
{ labels[count].xo = 730;
labels[count].yo = y;
labels[count].text = " STATE IS NORMAL ";
labels[count].status = 1;
labels[count].text_id = count;
labels[count].w = 100;
labels[count].h = 20;
}
y = 10;
for (count=75;count <= 99; count +=1,y+=32)
{ labels[count].xo = 840;
labels[count].yo = y;
labels[count].text = " STATE IS NORMAL ";
labels[count].status = 1;
labels[count].text_id = count;
labels[count].w = 100;
labels[count].h = 20;
}
}
/* routine to flip color of a valve */
static void flip_color(w,event,params,num_params)
Widget w;
XButtonPressedEvent *event;
char **params;
int num_params;
{ int i;
for (i=0; i <= 99 ;i ++)
{ if (event->x <= (valves[i].vxo+valves[i].w) && event->x >=
valves[i].vxo && event->y <= (valves[i].vyo+valves[i].h
+ BORDER)
&& event->y >= (valves[i].vyo+BORDER))
{ if (valves[i].status)
valves[i].status = 0;
else valves[i].status= 1;
draw_valve(i); /* draw to pixmap and display */
return;
}
};
}
static void menu_flip_color(w,event,params,num_params)
Widget w;
XButtonPressedEvent *event;
char **params;
int num_params;
{
if (valves[selected_valve].status)
valves[selected_valve].status = 0;
else valves[selected_valve].status= 1;
draw_valve(selected_valve); /* draw to pixmap and display */
}
/* routine to handle exposure events in graphical window */
static void expose_display(w,tag,cbdata)
Widget w;
int *tag;
DwtAnyCallbackStruct *cbdata;
{
XExposeEvent *ee;
ee = (XExposeEvent *)cbdata->event;
XCopyArea(display_id,pixmap_id,draw_window,gc_id,ee->x,ee->y,
ee->width,ee->height,ee->x,ee->y);
}
/* routine to manage the popup menu and continu eventually with the
pop dialog box*/
static void popup_menu(w,event,params,num_params)
Widget w;
XButtonPressedEvent *event;
char **params;
int num_params;
{ int i;
for (i=0; i <= 99 ;i ++)
{ if (event->x <= (valves[i].vxo+valves[i].w) && event->x >=
valves[i].vxo && event->y <= (valves[i].vyo+valves[i].h
+ BORDER)
&& event->y >= (valves[i].vyo+BORDER))
{ event->y += BORDER;
DwtMenuPosition(popup_menu_widget,event);
selected_valve = i;
XtManageChild(popup_menu_widget);
}
};
return;
}
/* routine to popup a modal dialog box to show some bloody stupid
info about a valve. In fact it is a simple frame with not much
info, add info as you want */
static void display_dialog()
{
set_something(id_widget,DwtNlabel,DwtLatin1String(itoa(valves[selected_valve].valve_id)));
set_something(y_widget,DwtNlabel,DwtLatin1String(itoa(valves[selected_valve].yo)));
set_something(x_widget,DwtNlabel,DwtLatin1String(itoa(valves[selected_valve].xo)));
set_something(status_widget,DwtNlabel,DwtLatin1String(itoa(valves[selected_valve].status)));
set_something(description_widget,DwtNlabel,DwtLatin1String(valves[selected_valve].descriptio
XtManageChild(pdb_widget);
}
/* routine to dismiss the modal dialog box */
static void dismiss_box()
{
XtUnmanageChild(pdb_widget);
}
/* This routine will create the popup dialog box with valve info */
static void create_pdb()
{
if (DwtFetchWidget(s_DRMHierarchy, "dpopup",adb_widget ,
&pdb_widget, &dummy_class) != DRMSuccess)
s_error("can't fetch valve info box widget");
}
static void set_something(w, resource, value)
Widget w;
char *resource, *value;
{
Arg al[1];
XtSetArg(al[0], resource, value);
XtSetValues(w, al, 1);
}
/*
* Function ITOA
* Converts its int argument to an ASCII string,
* and returns a pointer to a string descriptor for it.
*/
static char *itoa (i)
int i;
{
static char string [12];
sprintf (string, "%d", i);
return string;
}
/* this routine will periodically update a text field on the screen */
XtTimerCallbackProc timer_proc()
{ unsigned int i;
if (is_image)
{ i = give_random(); /* take a random number form 0 - 99 */
XSetFunction(display_id,gc_id,GXxor);
if (labels[i].status == 1)
XSetForeground(display_id,gc_id,color[RED].pixel);
else
XSetForeground(display_id,gc_id,color[BLUE].pixel);
XDrawString(display_id,draw_window,gc_id,labels[i].xo+5,labels[i].yo+16,
labels[i].text,strlen(labels[i].text));
XDrawString(display_id,pixmap_id,gc_id,labels[i].xo+5,labels[i].yo+16,
labels[i].text,strlen(labels[i].text));
XSetFunction(display_id,gc_id,GXcopy);
if (labels[i].status == 1)
{ labels[i].status = 0;
labels[i].text = " STATE IS ALARM ";
}
else
{ labels[i].status = 1;
labels[i].text = " STATE IS NORMAL ";
}
draw_text(i);
}
XtAddTimeOut(interval ,timer_proc,0);
}
/* random function */
static int give_random()
{
static unsigned int i= 10;
unsigned int r;
MTH$RANDOM(&i);
r = i>>25;
if (r >= 100 ) return (r - 28);
else return (r);
}
|