wmail: fixed possible NULL-pointer dereference in config-parser.
[dockapps.git] / wmcalc / wmcalc.c
blobbc6e428b65284f68910535649ccbf3214ff62b09
1 /****************************************************************
2 * File: wmcalc.c
3 * Version: 0.3
4 * Date: January 17, 2001
5 * Author: Edward H. Flora <ehflora@access1.net>
7 * This file is a part of the wmcalc application. As such, this
8 * file is licensed under the GNU General Public License, version 2.
9 * A copy of this license may be found in the file COPYING that should
10 * have been distributed with this file. If not, please refer to
11 * http://www.gnu.org/copyleft/gpl.html for details.
13 ****************************************************************
14 Description:
15 This file contains the main program code for the wmcalc
16 application. Wmcalc is a dockapp designed for the WindowMaker or
17 Afterstep window managers (although it should run well in most
18 others.) wmcalc is a four-function (and more) calculator that
19 has a small enough footprint that you may leave it open on your
20 desktop at all times, for convenient use.
23 Change History:
24 Date Modification
25 01/17/01 Updated to use XLookupString
26 12/10/00 Revised includes, extracting X libs to wmcalc_x.h
27 11/09/00 Added "locked" memory capabilities
28 11/08/00 Added Code to Support Keyboard / focus
29 10/29/00 Implemented memory use, configuration files, and a
30 quickstart button for a larger calculator. Also
31 abstracted some of the macros, global vars, function
32 prototypes, etc out to independent header files, to
33 eliminate some dependency issues between source files.
34 02/10/00 Added keyboard event code, without success
35 12/21/99 Original product release, version 0.1
36 11/26/99 Original file creation
38 ****************************************************************/
40 #include "wmcalc_x.h"
41 #include <X11/XKBlib.h>
42 #include <stdio.h>
43 #include <math.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include "wmcalc_c.h"
48 #include "wmcalc_err.h"
49 #include "wmcalc_t.h"
50 #include "wmcalc_g.h"
51 #include "wmcalc_f.h"
53 #include "backdrop.xpm" /* background graphic */
54 #include "calcbuttons.xpm" /* graphic of buttons */
55 #include "charmap.xpm" /* pixmap of characters */
56 #include "mask.xbm"
58 /* Global Variables */
59 /* Ok, so I didn't get them all extracted. Should look into this
60 further */
61 int N = 1; /* Button number pressed to goto app # */
62 int border = 0;
63 int Verbose = 0; /* Debug flag */
64 int mmouse = 1; /* flag to enable middle mouse (hold
65 over from wmbutton */
66 int button_pressed = -1; /* button to be drawn pressed */
67 char PlusMinusFlag = '+'; /* flag for sign of number in display */
68 char OpFlag = ' '; /* Operation requested */
69 int ExpFlag = 0; /* Flag if in scientific notation */
70 int DecFlag = 0; /* Flag if a decimal is in display */
71 int ImgFlag = 0; /* Flag if a number is imaginary */
72 int StrCnt = 0;
73 double RegisterA = 0.0; /* Main working register, displayed */
74 double RegisterB = 0.0; /* Second register to add to, etc */
75 char DispString[DISPSIZE+1]; /* Pointer to string of display */
76 ButtonArea button_region[NUM_BUTTONS]; /* Array of buttons */
78 char *app_name = "wmcalc"; /* Name of app, for window management */
80 /****************************************************************
81 * Function: main
82 ****************************************************************
83 Description:
84 This is the main Program control function for wmcalc. It
85 contains all the X11 windows function calls, as well as other
86 general operations.
88 Change History:
89 Date Modification
90 01/17/01 Updated to use XLookupString to get KeySym
91 11/09/00 Added Events for focus and keyboard work.
92 11/01/00 File Header Added
93 21/09/01 Added global configfile by Gordon Fraser
94 ****************************************************************/
95 int main( int argc, char **argv ) {
96 XEvent report;
97 XGCValues xgcValues;
98 XTextProperty app_name_atom;
99 int err_code = OKAY;
100 int dummy = 0;
101 int i;
102 char Geometry_str[64] = "64x64+0+0";
103 char Display_str[64] = "";
104 int KeywithMask = NO_BUTTON;
105 KeySym ksym;
106 XComposeStatus compose;
107 char buffer[20];
108 int bufsize = 20;
111 strcpy(configfile, getenv("HOME")); /* Added to wmbutton by Casey Harkin, 3/6/99 */
112 strcat(configfile, CONFFILENAME); /* Fixed Bug - didn't look in home directory */
113 /* but startup directory */
114 strcat(tempfile, CONFTEMPFILE); /* Setup name for temp file */
116 /* Clear the Calculator Display */
117 for(i=0; i<DISPSIZE; i++) DispString[i] = ' ';
118 DispString[DISPSIZE] = '\0';
120 /* Parse Command Line Arguments */
121 for ( i=1; i < argc; i++ ) {
122 if ( *argv[i] == '-' ) {
123 switch ( *(argv[i]+1) ) {
124 case 'v': /* Turn on Verbose (debugging) Mode */
125 Verbose = 1;
126 break;
127 case 'g': /* Set Geometry Options */
128 if ( ++i >= argc ) show_usage();
129 sscanf(argv[i], "%s", Geometry_str);
130 if ( Verbose ) printf("Geometry is: %s\n", Geometry_str);
131 break;
132 case 'd': /* Set display */
133 if ( ++i >= argc ) show_usage();
134 sscanf(argv[i], "%s", Display_str);
135 if ( Verbose ) printf("Display is: %s\n", Display_str);
136 break;
137 case 'h': /* Show Help Message */
138 show_usage();
139 break;
140 case 'f': /* use config file <filename> */
141 if ( ++i >= argc ) show_usage();
142 sscanf(argv[i], "%s", configfile);
143 if ( Verbose ) printf("Using Config File: %s\n", configfile);
144 break;
145 default: /* other, unknown, parameters */
146 show_usage();
147 break;
150 } /* End of loop to process command line options */
152 /* Open display on requested X server */
153 if ( (display = XOpenDisplay(Display_str)) == NULL ) {
154 error_handler(ERR_X_DISPLAY, Display_str);
157 screen = DefaultScreen(display);
158 rootwin = RootWindow(display,screen);
159 depth = DefaultDepth(display, screen);
161 bg_pixel = WhitePixel(display, screen );
162 fg_pixel = BlackPixel(display, screen );
164 xsizehints.flags = USSize | USPosition;
165 xsizehints.width = APP_WIDTH;
166 xsizehints.height = APP_HEIGHT;
168 /* Parse Geometry string and fill in sizehints fields */
169 XWMGeometry(display, screen,
170 Geometry_str,
171 NULL,
172 border,
173 &xsizehints,
174 &xsizehints.x,
175 &xsizehints.y,
176 &xsizehints.width,
177 &xsizehints.height,
178 &dummy);
180 if ( (win = XCreateSimpleWindow(display,
181 rootwin,
182 xsizehints.x,
183 xsizehints.y,
184 xsizehints.width,
185 xsizehints.height,
186 border,
187 fg_pixel, bg_pixel) ) == 0 ) {
188 error_handler(ERR_X_CREATE_WINDOW, NULL);
191 if ( (iconwin = XCreateSimpleWindow(display,
192 win,
193 xsizehints.x,
194 xsizehints.y,
195 xsizehints.width,
196 xsizehints.height,
197 border,
198 fg_pixel, bg_pixel) ) == 0 ) {
199 error_handler(ERR_X_CREATE_WINDOW, NULL);
202 /* Set up shaped windows */
203 /*Gives the appicon a border so you can grab and move it. */
205 if ( ( pixmask = XCreateBitmapFromData(display,
206 win,
207 (char *)mask_bits,
208 mask_width,
209 mask_height) ) == 0 ) {
210 error_handler(ERR_X_CREATE_BITMAP, NULL);
213 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet );
214 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
216 /* Convert in pixmaps from .xpm includes. */
217 getPixmaps();
219 /* Interclient Communication stuff */
220 /* Appicons don't work with out this stuff */
221 xwmhints = XAllocWMHints();
222 xwmhints->flags = WindowGroupHint | IconWindowHint | StateHint;
223 xwmhints->icon_window = iconwin;
224 xwmhints->window_group = win;
225 xwmhints->initial_state = WithdrawnState;
226 XSetWMHints( display, win, xwmhints );
228 xclasshint.res_name = app_name;
229 xclasshint.res_class = app_name;
230 XSetClassHint( display, win, &xclasshint );
232 XSetWMNormalHints( display, win, &xsizehints );
234 /* Tell window manager what the title bar name is. We never see */
235 /* this anyways in the WithdrawnState */
236 if ( XStringListToTextProperty(&app_name, 1, &app_name_atom) == 0 ) {
237 error_handler(ERR_SETUP_WINDOW_NAME, app_name);
239 XSetWMName( display, win, &app_name_atom );
241 /* Create Graphic Context */
242 if (( gc = XCreateGC(display, win,(GCForeground | GCBackground), &xgcValues))
243 == NULL ) {
244 error_handler(ERR_CREATE_GC, NULL);
247 /* XEvent Masks. We want both windows to process X events */
248 XSelectInput(display, win,
249 ExposureMask |
250 ButtonPressMask |
251 ButtonReleaseMask | /* added ButtonReleaseMask *charkins*/
252 PointerMotionMask |
253 FocusChangeMask |
254 LeaveWindowMask |
255 KeyPressMask | /* Try this to get keyboard working */
256 StructureNotifyMask |
257 EnterWindowMask );
258 XSelectInput(display, iconwin,
259 ExposureMask |
260 ButtonPressMask |
261 ButtonReleaseMask | /* added ButtonReleaseMask *charkins*/
262 PointerMotionMask |
263 FocusChangeMask |
264 LeaveWindowMask |
265 KeyPressMask | /* Try this to get keyboard working */
266 StructureNotifyMask |
267 EnterWindowMask );
269 /* Store the 'state' of the application for restarting */
270 XSetCommand( display, win, argv, argc );
272 /* Window won't ever show up until it is mapped.. then drawn after a */
273 /* ConfigureNotify */
274 XMapWindow( display, win );
276 /* Read Configuration File */
277 err_code = read_config();
278 if (err_code) {
279 error_handler(err_code, configfile);
282 /* X Event Loop */
283 while (1) {
284 XNextEvent(display, &report );
285 switch (report.type) {
286 case Expose:
287 if (report.xexpose.count != 0) {
288 break;
290 if ( Verbose ) printf("Event: Expose\n");
291 redraw();
292 break;
293 case ConfigureNotify:
294 if ( Verbose ) printf("Event: ConfigureNotify\n");
295 /* redraw(); */
296 break;
298 case KeyPress:
299 if (Verbose) printf("Event: Key state: 0x%x Key: 0x%x\n",
300 report.xkey.state, report.xkey.keycode);
302 /* ksym = XLookupKeysym(&(report.xkey), report.xkey.state); */
303 /* KeywithMask - this allows Left, middle, and right button functions
304 to be implemented via keyboard */
305 XLookupString(&(report.xkey), buffer, bufsize, &ksym, &compose);
306 if (Verbose) printf("Keysym is: 0x%x\n", (int) ksym);
307 KeywithMask = whichKey(ksym);
308 ExecFunc( KeywithMask );
309 redraw();
310 break;
312 case ButtonPress: /* draw button pressed, don't launch *charkins*/
313 switch (report.xbutton.button) {
314 case Button1:
315 N = whichButton(report.xbutton.x, report.xbutton.y );
316 if ( (N >= 0) && (N <= NUM_BUTTONS) ) {
317 button_pressed = N + LMASK;
318 /* redraw(); */
320 if ( Verbose )
321 printf("Button 1:x=%d y=%d N=%d\n",
322 report.xbutton.x, report.xbutton.y, N+LMASK);
323 break;
324 case Button2:
325 if (mmouse) {
326 N = whichButton(report.xbutton.x, report.xbutton.y );
327 if ( (N >= 0) && (N <= NUM_BUTTONS) ) {
328 button_pressed = N + MMASK;
329 /* redraw(); */
331 if ( Verbose )
332 printf("Button 2:x=%d y=%d N=%d\n",
333 report.xbutton.x, report.xbutton.y, N+MMASK);
335 break;
336 case Button3:
337 N = whichButton(report.xbutton.x, report.xbutton.y );
338 if ( (N >= 0) && (N <= NUM_BUTTONS) ) {
339 button_pressed = N + RMASK;
340 /* redraw(); */
342 if ( Verbose )
343 printf("Button 3:x=%d y=%d N=%d\n",
344 report.xbutton.x, report.xbutton.y, N+RMASK);
345 break;
347 break;
348 case ButtonRelease: /* If still over button, it was a real button press */
349 switch (report.xbutton.button) {
350 case Button1:
351 N = whichButton(report.xbutton.x, report.xbutton.y );
352 if ( (N >= 0) && (N <= NUM_BUTTONS) && (N == button_pressed - LMASK))
353 ExecFunc(N + LMASK);
354 button_pressed=-1;
355 redraw();
356 if ( Verbose )
357 printf("Button 1:x=%d y=%d N=%d\n",
358 report.xbutton.x, report.xbutton.y, N+LMASK);
359 break;
360 case Button2:
361 if (mmouse) {
362 N = whichButton(report.xbutton.x, report.xbutton.y );
363 if ( (N >= 0) && (N <= NUM_BUTTONS) && (N == button_pressed - MMASK))
364 ExecFunc( N + MMASK);
365 button_pressed=-1;
366 redraw();
367 if ( Verbose )
368 printf("Button 2:x=%d y=%d N=%d\n",
369 report.xbutton.x, report.xbutton.y, N+MMASK);
371 break;
372 case Button3:
373 N = whichButton(report.xbutton.x, report.xbutton.y );
374 if ( (N >= 0) && (N <= NUM_BUTTONS) && (N == button_pressed - RMASK))
375 ExecFunc( N + RMASK);
376 button_pressed=-1;
377 redraw();
378 if ( Verbose )
379 printf("Button 3:x=%d y=%d N=%d\n",
380 report.xbutton.x, report.xbutton.y, N+RMASK);
381 break;
383 break;
384 case DestroyNotify:
385 if ( Verbose ) printf("Requested Program Quit.\n");
386 XFreeGC(display, gc);
387 XDestroyWindow(display,win);
388 XDestroyWindow(display,iconwin);
389 XCloseDisplay(display);
390 exit(OKAY);
391 break;
392 case EnterNotify:
393 case LeaveNotify:
394 XSetInputFocus(display, PointerRoot, RevertToParent, CurrentTime);
395 if (Verbose) printf("Focus Change\n");
396 break;
399 return (OKAY);
400 } /***** End of main program ***********************************/
402 /****************************************************************
403 * Function: redraw
404 ****************************************************************
405 Description:
406 This function maintains the appearance of the application
407 by copying the "visible" pixmap to the two windows (the withdrawn
408 main window, and the icon window which is the main windows's icon
409 image).
411 Change History:
412 Date Modification
413 11/1/00 Function Header updated
414 ****************************************************************/
415 void redraw() {
417 XCopyArea(display, template.pixmap, visible.pixmap, gc, 0, 0,
418 template.attributes.width, template.attributes.height, 0, 0 );
420 defineButtonRegions();
422 /* Copy button to icon */
423 XCopyArea(display, buttons.pixmap, visible.pixmap, gc,
424 1, 1, 53, 40, 6, 20);
426 flush_expose( win );
427 XCopyArea(display, visible.pixmap, win, gc, 0, 0,
428 visible.attributes.width, visible.attributes.height, 0, 0 );
429 flush_expose( iconwin );
430 XCopyArea(display, visible.pixmap, iconwin, gc, 0, 0,
431 visible.attributes.width, visible.attributes.height, 0, 0 );
432 /* if ( Verbose ) printf("In Redraw()\n"); */
433 displaystr();
434 } /***** End of function redraw() ********************************/
436 /****************************************************************
437 * Function: whichButton
438 ****************************************************************
439 Description:
440 Return the button at the x,y coordinates. The button need not
441 be visible ( drawn ). Return -1 if no button match.
443 Change History:
444 Date Modification
445 11/1/00 Function Header Updated
446 ****************************************************************/
447 int whichButton( int x, int y ) {
448 int index;
450 for ( index=0; index < NUM_BUTTONS; index++ ) {
451 if ( (x >= button_region[index].x) && (x <= button_region[index].i) &&
452 (y >= button_region[index].y) && (y <= button_region[index].j) ) {
453 return(index);
456 return(NO_BUTTON);
457 } /***** End of function whichButton() **************************/
459 /****************************************************************
460 * Function: whichKey
461 ****************************************************************
462 Description:
463 This function decodes the keycode passed in and converts this to
464 a function number to execute. This is not simply the Button number,
465 but also contains the LMASK, MMASK, or RMASK to pass into ExecFunc to
466 handle expanded functions.
468 Change History:
469 Date Modification
470 01/17/01 Updated to take a KeySym, rather than a KeyCode
471 11/09/00 Original Function creation
472 ****************************************************************/
473 int whichKey (KeySym keysym) {
474 extern int Verbose;
475 int func = NO_BUTTON;
477 if (Verbose) printf("KeySym 0x%x received, decoding...\n", (int) keysym);
479 switch(keysym) {
480 case XK_Escape:
481 case XK_space:
482 case XK_KP_Space:
483 func = LMASK + 0;
484 break;
485 case XK_Delete:
486 func = MMASK + 0;
487 break;
488 case XK_BackSpace:
489 func = RMASK + 0;
490 break;
491 case XK_Return:
492 case XK_KP_Equal:
493 case XK_KP_Enter:
494 case XK_equal:
495 func = LMASK + 19;
496 break;
497 case XK_KP_0:
498 case XK_0:
499 func = LMASK + 17;
500 break;
501 case XK_KP_1:
502 case XK_1:
503 func = LMASK + 12;
504 break;
505 case XK_KP_2:
506 case XK_2:
507 func = LMASK + 13;
508 break;
509 case XK_KP_3:
510 case XK_3:
511 func = LMASK + 14;
512 break;
513 case XK_KP_4:
514 case XK_4:
515 func = LMASK + 7;
516 break;
517 case XK_KP_5:
518 case XK_5:
519 func = LMASK + 8;
520 break;
521 case XK_KP_6:
522 case XK_6:
523 func = LMASK + 9;
524 break;
525 case XK_KP_7:
526 case XK_7:
527 func = LMASK + 2;
528 break;
529 case XK_KP_8:
530 case XK_8:
531 func = LMASK + 3;
532 break;
533 case XK_KP_9:
534 case XK_9:
535 func = LMASK + 4;
536 break;
537 case XK_KP_Decimal:
538 case XK_period:
539 func = LMASK + 18;
540 break;
541 case XK_KP_Divide:
542 case XK_slash:
543 func = LMASK + 5;
544 break;
545 case XK_KP_Subtract:
546 case XK_minus:
547 func = LMASK + 15;
548 break;
549 case XK_KP_Multiply:
550 case XK_asterisk:
551 func = LMASK + 10;
552 break;
553 case XK_KP_Add:
554 case XK_plus:
555 func = LMASK + 20;
556 break;
557 case XK_R:
558 case XK_r:
559 func = LMASK + 1;
560 break;
561 case XK_asciicircum:
562 func = LMASK + 6;
563 break;
564 case XK_C:
565 case XK_c:
566 func = LMASK + 11;
567 break;
568 case XK_N:
569 case XK_n:
570 func = LMASK + 16;
571 break;
572 case XK_F1:
573 func = MMASK + 12;
574 break;
575 case XK_F2:
576 func = MMASK + 13;
577 break;
578 case XK_F3:
579 func = MMASK + 14;
580 break;
581 case XK_F4:
582 func = MMASK + 7;
583 break;
584 case XK_F5:
585 func = MMASK + 8;
586 break;
587 case XK_F6:
588 func = MMASK + 9;
589 break;
590 case XK_F7:
591 func = MMASK + 2;
592 break;
593 case XK_F8:
594 func = MMASK + 3;
595 break;
596 case XK_F9:
597 func = MMASK + 4;
598 break;
599 case XK_F10:
600 func = MMASK + 17;
601 break;
602 default:
603 if (Verbose) printf("Unknown Keysym, ignoring.\n");
604 func = NO_BUTTON;
605 break;
607 return(func);
608 } /***** End of function whichKey() ****************************/
610 /****************************************************************
611 * Function: getPixmaps
612 ****************************************************************
613 Description:
614 Load XPM data into X Pixmaps.
616 * Pixmap 'template' contains the untouched window backdrop image.
617 * Pixmap 'visible' is the template pixmap with buttons drawn on it.
618 -- what is seen by the user.
619 * Pixmap 'buttons' holds the images for individual buttons that are
620 later copied onto Pixmap visible.
621 * Pixmap 'charmap' holds the character map for the characters in
622 the display.
624 Change History:
625 Date Modification
626 11/1/00 Function Header Updated
627 ****************************************************************/
628 void getPixmaps() {
629 template.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
630 visible.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
631 buttons.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
632 charmap.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
634 /* Template Pixmap. Never Drawn To. */
635 if ( XpmCreatePixmapFromData( display, rootwin, backdrop_xpm,
636 &template.pixmap, &template.mask,
637 &template.attributes) != XpmSuccess ) {
638 error_handler(ERR_CREATE_PIXMAP, "template");
641 /* Visible Pixmap. Copied from template Pixmap and then drawn to. */
642 if ( XpmCreatePixmapFromData( display, rootwin, backdrop_xpm,
643 &visible.pixmap, &visible.mask,
644 &visible.attributes) != XpmSuccess ) {
645 error_handler(ERR_CREATE_PIXMAP, "visible");
648 /* Button Pixmap. */
649 if ( XpmCreatePixmapFromData( display, rootwin, calcbuttons_xpm,
650 &buttons.pixmap, &buttons.mask,
651 &buttons.attributes) != XpmSuccess ) {
652 error_handler(ERR_CREATE_PIXMAP, "buttons");
655 /* Character Map Pixmap. */
656 if ( XpmCreatePixmapFromData( display, rootwin, charmap_xpm,
657 &charmap.pixmap, &charmap.mask,
658 &charmap.attributes) != XpmSuccess ) {
659 error_handler(ERR_CREATE_PIXMAP, "charmap");
661 } /***** End of function getPixmaps() *****************************/
663 /****************************************************************
664 * Function: flush_expose
665 ****************************************************************
666 Description:
667 This function is a hold-over from previous programs (wmcp).
668 The use of this function is not well understood.
670 Change History:
671 Date Modification
672 11/1/00 Function header updated.
673 ****************************************************************/
674 int flush_expose(Window w) {
675 XEvent dummy;
676 int i=0;
678 while (XCheckTypedWindowEvent(display, w, Expose, &dummy)) i++;
679 return(i);
680 } /***** End of function flush_expose() *************************/
682 /****************************************************************
683 * Function: defineButtonRegion
684 ****************************************************************
685 Description:
686 This function defines the start and end x and y coordinates for
687 the various buttons used in wmcalc.
689 There should be a better way to do this, as right now, changing
690 the pixmap calcbuttons.xpm may require the modification of these
691 magic numbers.
693 Change History:
694 Date Modification
695 11/1/00 Function header updated
696 ****************************************************************/
697 void defineButtonRegions(void) {
698 int ndx = 0; /* button index */
700 button_region[0].x = 1; /* Define display region button */
701 button_region[0].i = 62;
702 button_region[0].y = 6;
703 button_region[0].j = 17;
705 for (ndx = 1; ndx <= 5; ndx++) { /* Define y coord's for top row */
706 button_region[ndx].y = 20;
707 button_region[ndx].j = 29;
709 for (ndx = 6; ndx <= 10; ndx++) { /* Define y coord's for 2nd row */
710 button_region[ndx].y = 30;
711 button_region[ndx].j = 39;
713 for (ndx = 11; ndx <= 15; ndx++) { /* Define y coord's for 3rd row */
714 button_region[ndx].y = 40;
715 button_region[ndx].j = 49;
717 for (ndx = 16; ndx <= 20; ndx++) { /* Define y coord's for bottom row */
718 button_region[ndx].y = 50;
719 button_region[ndx].j = 59;
721 for (ndx = 1; ndx <= 16; ndx+=5) { /* Define x coord's for Left column */
722 button_region[ndx].x = 5;
723 button_region[ndx].i = 16;
725 for (ndx = 2; ndx <= 17; ndx+=5) { /* Define x coord's for 2nd Left column */
726 button_region[ndx].x = 17;
727 button_region[ndx].i = 26;
729 for (ndx = 3; ndx <= 18; ndx+=5) { /* Define x coord's for middle column */
730 button_region[ndx].x = 27;
731 button_region[ndx].i = 36;
733 for (ndx = 4; ndx <= 19; ndx+=5) { /* Define x coord's for 2nd right column */
734 button_region[ndx].x = 37;
735 button_region[ndx].i = 46;
737 for (ndx = 5; ndx <= 20; ndx+=5) { /* Define x coord's for 2nd right column */
738 button_region[ndx].x = 47;
739 button_region[ndx].i = 57;
741 } /***** End of function defineButtonRgions() *************************/
743 /****************************************************************
744 * Function: displaychar
745 ****************************************************************
746 Description:
747 This function displays individual characters to the "display".
748 This function should only be called from displaystr().
750 Change History:
751 Date Modification
752 11/09/00 Added "Locked" character and capabilities
753 11/01/00 Function header updated
754 10/30/00 Updated to include the memory indicators as well.
755 ****************************************************************/
756 void displaychar(char ch, int location) {
757 ButtonArea dispchar;
758 int locatx, locaty;
760 dispchar = getboundaries(ch); /* Get the region of the charmap
761 containing the character to
762 display */
764 locaty = 6;
765 locatx = 2 + location * 6;
767 /* If the character is a memory display character, use the memory
768 location display region. Valid Characters are:
769 '_' - No data in Memory Location
770 '=' - Value in Memory Location, Not Locked
771 '#' - Constant in Memory Location, Locked
773 if ((ch == '=') || (ch == '_') || ch == '#') {
774 locaty = 15;
777 XCopyArea(display, charmap.pixmap, win, gc,
778 dispchar.x, dispchar.y,
779 dispchar.i-dispchar.x, dispchar.j-dispchar.y,
780 locatx, locaty);
781 XCopyArea(display, charmap.pixmap, iconwin, gc,
782 dispchar.x, dispchar.y,
783 dispchar.i-dispchar.x, dispchar.j-dispchar.y,
784 locatx, locaty);
786 } /***** End of Function displaychar() **************************/
788 /****************************************************************
789 * Function: displaystr
790 ****************************************************************
791 Description:
793 Change History:
794 Date Modification
795 11/09/00 Added Capabilities for "Locked" memories
796 11/01/00 Function header updated
797 10/30/00 Added memory location indicators
798 ****************************************************************/
799 void displaystr(void) {
800 extern char DispString[];
801 extern int MemLock[];
802 extern double MemArray[];
803 extern int Verbose;
804 int i;
806 if (Verbose) printf("Displaystr %s\n", DispString);
808 /* Update the alphanumeric display */
809 for (i = 0; i < DISPSIZE; i++)
810 displaychar(DispString[i], i);
812 /* Update the memory location indicators */
813 for (i = 0; i < NUM_MEM_CELLS; i++) {
814 if (MemArray[i] == 0.0)
815 displaychar('_', i); /* Value NOT stored here */
816 else if (MemLock[i] == 0)
817 displaychar('=', i); /* Value IS stored here */
818 else
819 displaychar('#', i); /* Constant IS stored here */
823 } /***** End of function displaystr() ***************************/
825 /****************************************************************
826 * Function: show_usage
827 ****************************************************************
828 Description:
829 This function prints a brief usage message to stdout,
830 and exits.
832 Change History:
833 Date Modification
834 11/01/00 Function header updated
835 ****************************************************************/
836 void show_usage(void) {
838 printf("\n");
839 printf(" %s %s\n",app_name, PACKAGE_VERSION);
840 printf("\n");
841 printf("usage: %s [-g geometry] [-d display] [-f <filename>] [-v] [-h] \n",
842 app_name);
843 printf("\n");
844 printf("-g <geometry> Window Geometry - ie: 64x64+10+10\n");
845 printf("-d <display> Display - ie: 127.0.0.1:0.0\n");
846 printf("-f <filename> Name of Config file - ie: /home/user/.wmcalc\n");
847 printf("-v Verbose Mode. \n");
848 printf("-h Help. This message.\n");
849 printf("\n");
850 exit(OKAY);
851 } /***** End of function show_usage() ***************************/
853 /****************************************************************
854 * Function: error_handler
855 ****************************************************************
856 Description:
857 This function will handle all fatal error conditions that exist.
858 Error condition codes are kept in wmcalc_err.h
860 Change History:
861 Date Modification
862 11/1/00 Function created
863 ****************************************************************/
864 void error_handler (int err_code, char *err_string) {
865 extern char tempfile[];
867 if (err_code == OKAY) {
868 /* This case should never happen.
869 If it does, somebody screwed up (probably me),
870 but don't kill the program!! */
871 return;
873 else {
874 fprintf(stderr, "Error Code %d: ", err_code);
875 switch (err_code) {
876 case ERR_FILE_NOT_FOUND:
877 fprintf(stderr, "Could not open file %s\n",configfile);
878 break;
879 case ERR_TMP_FILE_FAILED:
880 fprintf(stderr, "Could not open temporary file %s\n", tempfile);
881 break;
882 case ERR_X_CREATE_WINDOW:
883 fprintf(stderr, "Could not create simple window\n");
884 break;
885 case ERR_X_CREATE_BITMAP:
886 fprintf(stderr, "Could not create bitmap from data\n");
887 break;
888 case ERR_SETUP_WINDOW_NAME:
889 fprintf(stderr, "Could not setup window name %s\n", err_string);
890 break;
891 case ERR_CREATE_GC:
892 fprintf(stderr, "XCreateGC\n");
893 break;
894 case ERR_X_DISPLAY:
895 fprintf(stderr, "Could not open display on %s\n", err_string);
896 break;
897 case ERR_CREATE_PIXMAP:
898 fprintf(stderr, "Can't Create %s Pixmap\n", err_string);
899 break;
900 /* case : */
901 /* break; */
902 default:
903 fprintf(stderr, "Unknown Error\n");
904 break;
907 exit(err_code);
908 } /***** End of Function error_handler **************************/