1 /****************************************************************
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 ****************************************************************
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.
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 ****************************************************************/
41 #include <X11/XKBlib.h>
48 #include "wmcalc_err.h"
53 #include "backdrop.xpm" /* background graphic */
54 #include "calcbuttons.xpm" /* graphic of buttons */
55 #include "charmap.xpm" /* pixmap of characters */
58 /* Global Variables */
59 /* Ok, so I didn't get them all extracted. Should look into this
61 int N
= 1; /* Button number pressed to goto app # */
63 int Verbose
= 0; /* Debug flag */
64 int mmouse
= 1; /* flag to enable middle mouse (hold
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 */
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 /****************************************************************
82 ****************************************************************
84 This is the main Program control function for wmcalc. It
85 contains all the X11 windows function calls, as well as other
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
) {
98 XTextProperty app_name_atom
;
102 char Geometry_str
[64] = "64x64+0+0";
103 char Display_str
[64] = "";
104 int KeywithMask
= NO_BUTTON
;
106 XComposeStatus compose
;
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 */
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
);
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
);
137 case 'h': /* Show Help Message */
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
);
145 default: /* other, unknown, parameters */
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
,
180 if ( (win
= XCreateSimpleWindow(display
,
187 fg_pixel
, bg_pixel
) ) == 0 ) {
188 error_handler(ERR_X_CREATE_WINDOW
, NULL
);
191 if ( (iconwin
= XCreateSimpleWindow(display
,
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
,
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. */
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
))
244 error_handler(ERR_CREATE_GC
, NULL
);
247 /* XEvent Masks. We want both windows to process X events */
248 XSelectInput(display
, win
,
251 ButtonReleaseMask
| /* added ButtonReleaseMask *charkins*/
255 KeyPressMask
| /* Try this to get keyboard working */
256 StructureNotifyMask
|
258 XSelectInput(display
, iconwin
,
261 ButtonReleaseMask
| /* added ButtonReleaseMask *charkins*/
265 KeyPressMask
| /* Try this to get keyboard working */
266 StructureNotifyMask
|
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();
279 error_handler(err_code
, configfile
);
284 XNextEvent(display
, &report
);
285 switch (report
.type
) {
287 if (report
.xexpose
.count
!= 0) {
290 if ( Verbose
) printf("Event: Expose\n");
293 case ConfigureNotify
:
294 if ( Verbose
) printf("Event: ConfigureNotify\n");
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
);
312 case ButtonPress
: /* draw button pressed, don't launch *charkins*/
313 switch (report
.xbutton
.button
) {
315 N
= whichButton(report
.xbutton
.x
, report
.xbutton
.y
);
316 if ( (N
>= 0) && (N
<= NUM_BUTTONS
) ) {
317 button_pressed
= N
+ LMASK
;
321 printf("Button 1:x=%d y=%d N=%d\n",
322 report
.xbutton
.x
, report
.xbutton
.y
, N
+LMASK
);
326 N
= whichButton(report
.xbutton
.x
, report
.xbutton
.y
);
327 if ( (N
>= 0) && (N
<= NUM_BUTTONS
) ) {
328 button_pressed
= N
+ MMASK
;
332 printf("Button 2:x=%d y=%d N=%d\n",
333 report
.xbutton
.x
, report
.xbutton
.y
, N
+MMASK
);
337 N
= whichButton(report
.xbutton
.x
, report
.xbutton
.y
);
338 if ( (N
>= 0) && (N
<= NUM_BUTTONS
) ) {
339 button_pressed
= N
+ RMASK
;
343 printf("Button 3:x=%d y=%d N=%d\n",
344 report
.xbutton
.x
, report
.xbutton
.y
, N
+RMASK
);
348 case ButtonRelease
: /* If still over button, it was a real button press */
349 switch (report
.xbutton
.button
) {
351 N
= whichButton(report
.xbutton
.x
, report
.xbutton
.y
);
352 if ( (N
>= 0) && (N
<= NUM_BUTTONS
) && (N
== button_pressed
- LMASK
))
357 printf("Button 1:x=%d y=%d N=%d\n",
358 report
.xbutton
.x
, report
.xbutton
.y
, N
+LMASK
);
362 N
= whichButton(report
.xbutton
.x
, report
.xbutton
.y
);
363 if ( (N
>= 0) && (N
<= NUM_BUTTONS
) && (N
== button_pressed
- MMASK
))
364 ExecFunc( N
+ MMASK
);
368 printf("Button 2:x=%d y=%d N=%d\n",
369 report
.xbutton
.x
, report
.xbutton
.y
, N
+MMASK
);
373 N
= whichButton(report
.xbutton
.x
, report
.xbutton
.y
);
374 if ( (N
>= 0) && (N
<= NUM_BUTTONS
) && (N
== button_pressed
- RMASK
))
375 ExecFunc( N
+ RMASK
);
379 printf("Button 3:x=%d y=%d N=%d\n",
380 report
.xbutton
.x
, report
.xbutton
.y
, N
+RMASK
);
385 if ( Verbose
) printf("Requested Program Quit.\n");
386 XFreeGC(display
, gc
);
387 XDestroyWindow(display
,win
);
388 XDestroyWindow(display
,iconwin
);
389 XCloseDisplay(display
);
394 XSetInputFocus(display
, PointerRoot
, RevertToParent
, CurrentTime
);
395 if (Verbose
) printf("Focus Change\n");
400 } /***** End of main program ***********************************/
402 /****************************************************************
404 ****************************************************************
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
413 11/1/00 Function Header updated
414 ****************************************************************/
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);
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"); */
434 } /***** End of function redraw() ********************************/
436 /****************************************************************
437 * Function: whichButton
438 ****************************************************************
440 Return the button at the x,y coordinates. The button need not
441 be visible ( drawn ). Return -1 if no button match.
445 11/1/00 Function Header Updated
446 ****************************************************************/
447 int whichButton( int x
, int y
) {
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
) ) {
457 } /***** End of function whichButton() **************************/
459 /****************************************************************
461 ****************************************************************
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.
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
) {
475 int func
= NO_BUTTON
;
477 if (Verbose
) printf("KeySym 0x%x received, decoding...\n", (int) keysym
);
603 if (Verbose
) printf("Unknown Keysym, ignoring.\n");
608 } /***** End of function whichKey() ****************************/
610 /****************************************************************
611 * Function: getPixmaps
612 ****************************************************************
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
626 11/1/00 Function Header Updated
627 ****************************************************************/
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");
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 ****************************************************************
667 This function is a hold-over from previous programs (wmcp).
668 The use of this function is not well understood.
672 11/1/00 Function header updated.
673 ****************************************************************/
674 int flush_expose(Window w
) {
678 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
)) i
++;
680 } /***** End of function flush_expose() *************************/
682 /****************************************************************
683 * Function: defineButtonRegion
684 ****************************************************************
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
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 ****************************************************************
747 This function displays individual characters to the "display".
748 This function should only be called from displaystr().
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
) {
760 dispchar
= getboundaries(ch
); /* Get the region of the charmap
761 containing the character to
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
== '#') {
777 XCopyArea(display
, charmap
.pixmap
, win
, gc
,
778 dispchar
.x
, dispchar
.y
,
779 dispchar
.i
-dispchar
.x
, dispchar
.j
-dispchar
.y
,
781 XCopyArea(display
, charmap
.pixmap
, iconwin
, gc
,
782 dispchar
.x
, dispchar
.y
,
783 dispchar
.i
-dispchar
.x
, dispchar
.j
-dispchar
.y
,
786 } /***** End of Function displaychar() **************************/
788 /****************************************************************
789 * Function: displaystr
790 ****************************************************************
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
[];
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 */
819 displaychar('#', i
); /* Constant IS stored here */
823 } /***** End of function displaystr() ***************************/
825 /****************************************************************
826 * Function: show_usage
827 ****************************************************************
829 This function prints a brief usage message to stdout,
834 11/01/00 Function header updated
835 ****************************************************************/
836 void show_usage(void) {
839 printf(" %s %s\n",app_name
, PACKAGE_VERSION
);
841 printf("usage: %s [-g geometry] [-d display] [-f <filename>] [-v] [-h] \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");
851 } /***** End of function show_usage() ***************************/
853 /****************************************************************
854 * Function: error_handler
855 ****************************************************************
857 This function will handle all fatal error conditions that exist.
858 Error condition codes are kept in wmcalc_err.h
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!! */
874 fprintf(stderr
, "Error Code %d: ", err_code
);
876 case ERR_FILE_NOT_FOUND
:
877 fprintf(stderr
, "Could not open file %s\n",configfile
);
879 case ERR_TMP_FILE_FAILED
:
880 fprintf(stderr
, "Could not open temporary file %s\n", tempfile
);
882 case ERR_X_CREATE_WINDOW
:
883 fprintf(stderr
, "Could not create simple window\n");
885 case ERR_X_CREATE_BITMAP
:
886 fprintf(stderr
, "Could not create bitmap from data\n");
888 case ERR_SETUP_WINDOW_NAME
:
889 fprintf(stderr
, "Could not setup window name %s\n", err_string
);
892 fprintf(stderr
, "XCreateGC\n");
895 fprintf(stderr
, "Could not open display on %s\n", err_string
);
897 case ERR_CREATE_PIXMAP
:
898 fprintf(stderr
, "Can't Create %s Pixmap\n", err_string
);
903 fprintf(stderr
, "Unknown Error\n");
908 } /***** End of Function error_handler **************************/