diff options
author | B. Watson <urchlay@slackware.uk> | 2024-01-10 12:00:52 -0500 |
---|---|---|
committer | B. Watson <urchlay@slackware.uk> | 2024-01-10 12:00:52 -0500 |
commit | 4b982efaa978ef0bb9f637b06e112cada5ed64e7 (patch) | |
tree | 6b5cb8778e9b0aae4c245af3fd8f4e11e6230ab6 | |
parent | f833c4fbabb8a46692f4ec4d32f738dfd9974ce8 (diff) | |
download | hcalc-4b982efaa978ef0bb9f637b06e112cada5ed64e7.tar.gz |
Reindent all code.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | config.c | 28 | ||||
-rw-r--r-- | draw.c | 185 | ||||
-rw-r--r-- | hcalc.c | 83 | ||||
-rw-r--r-- | images.c | 37 | ||||
-rw-r--r-- | input.c | 977 | ||||
-rw-r--r-- | setupx.c | 284 |
7 files changed, 767 insertions, 828 deletions
@@ -1,2 +1,3 @@ *.o hcalc +tags @@ -38,7 +38,7 @@ struct hcalc_config { }; char *home_dir = NULL; -char config_path[MAX_HOME]; /* ludicrous size */ +char config_path[MAX_HOME]; /* ludicrous size */ size_t filesize(FILE *f) { struct stat st; @@ -52,8 +52,9 @@ size_t filesize(FILE *f) { } void set_home_dir(void) { - home_dir = getenv("HOME"); /* could be fancier (getpwent()) */ - if(!home_dir) return; + home_dir = getenv("HOME"); /* could be fancier (getpwent()) */ + if(!home_dir) + return; if(strlen(home_dir) > (MAX_HOME - 100)) home_dir = NULL; } @@ -71,19 +72,23 @@ void load_config(void) { /* printf("load_config() called\n"); */ set_home_dir(); if(!home_dir) { - fprintf(stderr, "HOME not set or too long, not loading/saving config file.\n"); + fprintf(stderr, + "HOME not set or too long, not loading/saving config file.\n"); return; } set_config_path(); f = fopen(config_path, "rb"); if(!f) { - if(errno != ENOENT) perror(config_path); + if(errno != ENOENT) + perror(config_path); return; } if(filesize(f) != sizeof(conf)) { - fprintf(stderr, "%s: wrong size (should be %ld bytes), ignoring.\n", config_path, sizeof(conf)); + fprintf(stderr, + "%s: wrong size (should be %ld bytes), ignoring.\n", + config_path, sizeof(conf)); fclose(f); return; } @@ -98,12 +103,14 @@ void load_config(void) { } if(memcmp(conf.signature, SIGNATURE, SIGLEN) != 0) { - fprintf(stderr, "%s: Bad config file signature, ignoring\n", config_path); + fprintf(stderr, "%s: Bad config file signature, ignoring\n", + config_path); return; } if(conf.doublesize != sizeof(double) || conf.trailer != TRAILER) { - fprintf(stderr, "%s: Bad config file architecture, ignoring\n", config_path); + fprintf(stderr, "%s: Bad config file architecture, ignoring\n", + config_path); return; } @@ -123,10 +130,11 @@ void save_config(void) { /* printf("save_config() called\n"); */ set_home_dir(); - if(!home_dir) return; + if(!home_dir) + return; set_config_path(); - unlink(config_path); /* avoid symlink weirdness */ + unlink(config_path); /* avoid symlink weirdness */ f = fopen(config_path, "wb"); if(!f) { @@ -33,118 +33,101 @@ int scale_factor; #define BITS_TOP_0 (BASE_BITS_TOP_0 * scale_factor) #define BITS_BOT_0 (BASE_BITS_BOT_0 * scale_factor) -void -redraw_chars() -{ - int i; - for (i=0; i<15; i++) - { - XCopyArea(display, chars, window, gc, - shown_offsets[i], 0, CHAR_WIDTH, CHAR_HEIGHT, - CHARS_LEFT+BITS_TOP_1*i, CHARS_TOP); - } +void redraw_chars() { + int i; + for(i = 0; i < 15; i++) { + XCopyArea(display, chars, window, gc, + shown_offsets[i], 0, CHAR_WIDTH, CHAR_HEIGHT, + CHARS_LEFT + BITS_TOP_1 * i, CHARS_TOP); + } } -void -redraw() -{ - XCopyArea(display, face, window, gc, 0, 0, widths[winsize], heights[winsize], 0, 0); +void redraw() { + XCopyArea(display, face, window, gc, 0, 0, widths[winsize], + heights[winsize], 0, 0); - if (show_bits) - { - int i, x; - XSetForeground(display, gc, bit_off); - for (i=0; i<32; i++) - { - x = BITS_LEFT_X - (BITS_TOP_1/3)*i - (BITS_TOP_1)/2*(i/4); - if (!(shown_bitmask & (1<<i))) - XDrawLine(display, window, gc, x, BITS_TOP_0, x, BITS_BOT_0); - } - XSetForeground(display, gc, bit_on); - for (i=0; i<32; i++) - { - x = BITS_LEFT_X - scale_factor*2*i - scale_factor*3*(i/4); - if (shown_bitmask & (1<<i)) - XDrawLine(display, window, gc, x, BITS_TOP_1, x, BITS_BOT_1); - } - } - else - redraw_chars(); + if(show_bits) { + int i, x; + XSetForeground(display, gc, bit_off); + for(i = 0; i < 32; i++) { + x = BITS_LEFT_X - (BITS_TOP_1 / 3) * i - + (BITS_TOP_1) / 2 * (i / 4); + if(!(shown_bitmask & (1 << i))) + XDrawLine(display, window, gc, x, BITS_TOP_0, x, + BITS_BOT_0); + } + XSetForeground(display, gc, bit_on); + for(i = 0; i < 32; i++) { + x = BITS_LEFT_X - scale_factor * 2 * i - + scale_factor * 3 * (i / 4); + if(shown_bitmask & (1 << i)) + XDrawLine(display, window, gc, x, BITS_TOP_1, x, + BITS_BOT_1); + } + } else + redraw_chars(); } -void -set_bits(int b) -{ - shown_bitmask = b; - show_bits = 1; - redraw(); +void set_bits(int b) { + shown_bitmask = b; + show_bits = 1; + redraw(); } -void -set_string(char *s) -{ - char tmp[16]; - int i; - sprintf(tmp, "%15.15s", s); - for (i=0; i<15; i++) - shown_offsets[i] = char_to_x[(int)tmp[i]]; - if (show_bits == 0) - redraw_chars(); - else - { - show_bits = 0; - redraw(); - } +void set_string(char *s) { + char tmp[16]; + int i; + sprintf(tmp, "%15.15s", s); + for(i = 0; i < 15; i++) + shown_offsets[i] = char_to_x[(int)tmp[i]]; + if(show_bits == 0) + redraw_chars(); + else { + show_bits = 0; + redraw(); + } } -void -send_current_display() -{ - char tmp[40], *tp=tmp; - int i; - XEvent e; +void send_current_display() { + char tmp[40], *tp = tmp; + int i; + XEvent e; - if (show_bits) - { - for (i=31; i>0; i--) - if (shown_bitmask & (1<<i)) - break; - for (; i>=0; i--) - { - if (shown_bitmask & (1<<i)) - *tp++ = '1'; - else - *tp++ = '0'; - } - } - else - { - for (i=0; i<15; i++) - { - char c = charmap[shown_offsets[i]/BITS_TOP_1]; - if (c != ' ' && c != ',') - *tp++ = c; - } - } + if(show_bits) { + for(i = 31; i > 0; i--) + if(shown_bitmask & (1 << i)) + break; + for(; i >= 0; i--) { + if(shown_bitmask & (1 << i)) + *tp++ = '1'; + else + *tp++ = '0'; + } + } else { + for(i = 0; i < 15; i++) { + char c = charmap[shown_offsets[i] / BITS_TOP_1]; + if(c != ' ' && c != ',') + *tp++ = c; + } + } - *tp = 0; + *tp = 0; - e.type = SelectionNotify; - e.xselection.display = display; - e.xselection.requestor = event.xselectionrequest.requestor; - e.xselection.selection = event.xselectionrequest.selection; - e.xselection.target = event.xselectionrequest.target; - e.xselection.property = event.xselectionrequest.property; - e.xselection.time = event.xselectionrequest.time; + e.type = SelectionNotify; + e.xselection.display = display; + e.xselection.requestor = event.xselectionrequest.requestor; + e.xselection.selection = event.xselectionrequest.selection; + e.xselection.target = event.xselectionrequest.target; + e.xselection.property = event.xselectionrequest.property; + e.xselection.time = event.xselectionrequest.time; - if (event.xselectionrequest.target != XA_STRING) - { - e.xselection.property = None; - } - else - { - XChangeProperty(display, e.xselection.requestor, e.xselection.property, - XA_STRING, 8, PropModeReplace, (unsigned char *)tmp, strlen(tmp)); - } - XSendEvent(display, e.xselection.requestor, False, 0, &e); + if(event.xselectionrequest.target != XA_STRING) { + e.xselection.property = None; + } else { + XChangeProperty(display, e.xselection.requestor, + e.xselection.property, XA_STRING, 8, + PropModeReplace, (unsigned char *)tmp, + strlen(tmp)); + } + XSendEvent(display, e.xselection.requestor, False, 0, &e); } @@ -17,52 +17,53 @@ void version(void) { } void usage(const char *self, const char *msg) { - if(msg) fprintf(stderr, "%s: %s\n", self, msg); - printf("%s %s\nUsage: %s [-default] [-small|-medium|-large] [-dec|-hex|-oct|-bin] [-quiet] [--version] [--help]\n", SELF, VERSION, self); + if(msg) + fprintf(stderr, "%s: %s\n", self, msg); + printf + ("%s %s\nUsage: %s [-default] [-small|-medium|-large] [-dec|-hex|-oct|-bin] [-quiet] [--version] [--help]\n", + SELF, VERSION, self); exit(msg != NULL); } -int -main(int argc, char **argv) -{ - self = argv[0]; +int main(int argc, char **argv) { + self = argv[0]; - setbuf(stdout, 0); + setbuf(stdout, 0); - if(argc >= 2 && strcmp(argv[1], "-default") == 0) { - /* don't load the config file */ - } else { - load_config(); - } - atexit(save_config); + if(argc >= 2 && strcmp(argv[1], "-default") == 0) { + /* don't load the config file */ + } else { + load_config(); + } + atexit(save_config); - while(argv++, --argc) { - if(strcmp(*argv, "-small") == 0) - winsize = 0; - else if(strcmp(*argv, "-medium") == 0) - winsize = 1; - else if(strcmp(*argv, "-large") == 0) - winsize = 2; - else if(strcmp(*argv, "-dec") == 0) - base = 10; - else if(strcmp(*argv, "-hex") == 0) - base = 16; - else if(strcmp(*argv, "-oct") == 0) - base = 8; - else if(strcmp(*argv, "-bin") == 0) - base = 2; - else if(strcmp(*argv, "-quiet") == 0) - quiet = 1; - else if(strcmp(*argv, "--help") == 0) - usage(self, NULL); - else if(strcmp(*argv, "--version") == 0) - version(); - else if(strcmp(*argv, "-default") == 0) - { /* do nothing */ } - else usage(self, "Invalid command-line option %s"); - } + while(argv++, --argc) { + if(strcmp(*argv, "-small") == 0) + winsize = 0; + else if(strcmp(*argv, "-medium") == 0) + winsize = 1; + else if(strcmp(*argv, "-large") == 0) + winsize = 2; + else if(strcmp(*argv, "-dec") == 0) + base = 10; + else if(strcmp(*argv, "-hex") == 0) + base = 16; + else if(strcmp(*argv, "-oct") == 0) + base = 8; + else if(strcmp(*argv, "-bin") == 0) + base = 2; + else if(strcmp(*argv, "-quiet") == 0) + quiet = 1; + else if(strcmp(*argv, "--help") == 0) + usage(self, NULL); + else if(strcmp(*argv, "--version") == 0) + version(); + else if(strcmp(*argv, "-default") == 0) { /* do nothing */ + } else + usage(self, "Invalid command-line option %s"); + } - setup_x(); - load_pixmaps(); - process_input(); + setup_x(); + load_pixmaps(); + process_input(); } @@ -38,23 +38,22 @@ Pixmap chars; char charmap[] = " 0123456789ABCDEF-x,.ro+"; int char_to_x[256]; -void -load_pixmaps(void) -{ - int i, c; - - XpmAttributes attr; - attr.valuemask = 0; - XpmCreatePixmapFromData(display, window, faces[winsize], &face, 0, &attr); - XpmCreatePixmapFromData(display, window, charss[winsize], &chars, 0, &attr); - - for (i=0; i<256; i++) - { - char_to_x[i] = 0; - for (c=0; charmap[c]; c++) - if (charmap[c] == i) { - char_to_x[i] = c * c_factors[winsize]; - /* printf("char_to_x[%d] ('%c') = %d\n", i, i, c*C_FACTOR); */ - } - } +void load_pixmaps(void) { + int i, c; + + XpmAttributes attr; + attr.valuemask = 0; + XpmCreatePixmapFromData(display, window, faces[winsize], &face, 0, + &attr); + XpmCreatePixmapFromData(display, window, charss[winsize], &chars, 0, + &attr); + + for(i = 0; i < 256; i++) { + char_to_x[i] = 0; + for(c = 0; charmap[c]; c++) + if(charmap[c] == i) { + char_to_x[i] = c * c_factors[winsize]; + /* printf("char_to_x[%d] ('%c') = %d\n", i, i, c*C_FACTOR); */ + } + } } @@ -22,13 +22,13 @@ int showing_err = 0; char input_buf[MAXIN]; int iptr; -double value, saved, stored=0; +double value, saved, stored = 0; void paste(void); void copy(void); int number_is_ok(double n) { - return (fpclassify(n) & (FP_ZERO | FP_NORMAL )); + return (fpclassify(n) & (FP_ZERO | FP_NORMAL)); } void bell(void) { @@ -36,532 +36,493 @@ void bell(void) { XBell(display, 0); } -void -convert_number() -{ - char *ip = input_buf; - double scale = 1; - int sign = +1; - - if (*ip == '-') - sign = -1; - value = 0; - while (*++ip) - { - if (*ip == '.') - break; - if (*ip >= '0' && *ip <= '9') - { - value *= base; - value += *ip-'0'; - } - if (*ip >= 'a' && *ip <= 'f') - { - value *= base; - value += *ip-'a'+10; - } - } - if (*ip) - while (*++ip) - { - if (*ip >= '0' && *ip <= '9') - { - scale *= base; - value += (*ip-'0')/scale; - } - if (*ip >= 'a' && *ip <= 'f') - { - scale *= base; - value += (*ip-'a'+10)/scale; - } - } - value *= sign; +void convert_number() { + char *ip = input_buf; + double scale = 1; + int sign = +1; + + if(*ip == '-') + sign = -1; + value = 0; + while(*++ip) { + if(*ip == '.') + break; + if(*ip >= '0' && *ip <= '9') { + value *= base; + value += *ip - '0'; + } + if(*ip >= 'a' && *ip <= 'f') { + value *= base; + value += *ip - 'a' + 10; + } + } + if(*ip) + while(*++ip) { + if(*ip >= '0' && *ip <= '9') { + scale *= base; + value += (*ip - '0') / scale; + } + if(*ip >= 'a' && *ip <= 'f') { + scale *= base; + value += (*ip - 'a' + 10) / scale; + } + } + value *= sign; } -void -show_value() -{ - char tmp[25], *tp; - char commas[40], *cp, *dp; - double v = value; - - if(!number_is_ok(value)) { - set_string("Err"); - bell(); - showing_err = 1; - saved = value = 0; /* so it doesn't get saved on exit! */ - return; - } - - if (base == 2) - { - int q = (long long)v & 0xffffffffL; - set_bits(q); - return; - } - - tmp[0] = ' '; - if (v < 0) - { - tmp[0] = '-'; - v = -v; - } - - if (base == 10) - { - sprintf(tmp+1, "%.14G", v); - if (strchr(tmp+1, 'E')) - sprintf(tmp+1, "%.9G", v); - if (tmp[14] == '.') - tmp[14] = 0; - } - else - { - static char tohex[] = "0123456789ABCDEF"; - long long ll = (long long)v; - char *revptr; - tp = tmp+1; - if (base == 16) - { - *tp++ = '0'; - *tp++ = 'x'; - } - else if (base == 8) - *tp++ = '0'; - - revptr = tp; - do { - *tp++ = tohex[ll%base]; - ll /= base; - } while (ll); - *tp-- = 0; - while (revptr < tp) { - char t = *revptr; - *revptr = *tp; - *tp = t; - tp--; - revptr++; - } - } - - cp = commas+40; - tp = tmp+strlen(tmp); - dp = strchr(tmp, '.'); - if (dp == 0) - dp = tp; - - *--cp = 0; - while (tp>=tmp) - { - *--cp = *tp--; - switch (base) - { - case 10: - if (isdigit(cp[0]) && isdigit(cp[1]) && isdigit(cp[2]) && tp<dp - && tp>=tmp && isdigit(*tp)) - *--cp = ','; - break; - case 16: - if (isxdigit(cp[0]) && isxdigit(cp[1]) - && isxdigit(cp[2]) && isxdigit(cp[3]) - && tp>=tmp && isxdigit(*tp)) - *--cp = ','; - break; - } - } - - if (strlen(cp) > 15) - set_string(tmp); - else - set_string(cp); +void show_value() { + char tmp[25], *tp; + char commas[40], *cp, *dp; + double v = value; + + if(!number_is_ok(value)) { + set_string("Err"); + bell(); + showing_err = 1; + saved = value = 0; /* so it doesn't get saved on exit! */ + return; + } + + if(base == 2) { + int q = (long long)v & 0xffffffffL; + set_bits(q); + return; + } + + tmp[0] = ' '; + if(v < 0) { + tmp[0] = '-'; + v = -v; + } + + if(base == 10) { + sprintf(tmp + 1, "%.14G", v); + if(strchr(tmp + 1, 'E')) + sprintf(tmp + 1, "%.9G", v); + if(tmp[14] == '.') + tmp[14] = 0; + } else { + static char tohex[] = "0123456789ABCDEF"; + long long ll = (long long)v; + char *revptr; + tp = tmp + 1; + if(base == 16) { + *tp++ = '0'; + *tp++ = 'x'; + } else if(base == 8) + *tp++ = '0'; + + revptr = tp; + do { + *tp++ = tohex[ll % base]; + ll /= base; + } while(ll); + *tp-- = 0; + while(revptr < tp) { + char t = *revptr; + *revptr = *tp; + *tp = t; + tp--; + revptr++; + } + } + + cp = commas + 40; + tp = tmp + strlen(tmp); + dp = strchr(tmp, '.'); + if(dp == 0) + dp = tp; + + *--cp = 0; + while(tp >= tmp) { + *--cp = *tp--; + switch (base) { + case 10: + if(isdigit(cp[0]) && isdigit(cp[1]) && isdigit(cp[2]) + && tp < dp && tp >= tmp && isdigit(*tp)) + *--cp = ','; + break; + case 16: + if(isxdigit(cp[0]) && isxdigit(cp[1]) + && isxdigit(cp[2]) && isxdigit(cp[3]) + && tp >= tmp && isxdigit(*tp)) + *--cp = ','; + break; + } + } + + if(strlen(cp) > 15) + set_string(tmp); + else + set_string(cp); } -void -end_number() -{ - if (!making_number) - return; - making_number = 0; - iptr = 0; - - switch (pending_op) - { - case '+': - value = saved + value; - break; - case '-': - value = saved - value; - break; - case '*': - value = saved * value; - break; - case '/': - value = saved / value; - break; - case '%': - value = (long long)saved % (long long)value; - break; - case '&': - value = (long long)saved & (long long)value; - break; - case '|': - value = (long long)saved | (long long)value; - break; - case '^': - value = (long long)saved ^ (long long)value; - break; - case 'S': /* Shift. Positive means <<, negative means >> */ - if (value < 0) - value = (long long)saved >> (long long)(-value); - else - value = (long long)saved << (long long)value; - break; - } - saved = value; - pending_op = 0; - show_value(); +void end_number() { + if(!making_number) + return; + making_number = 0; + iptr = 0; + + switch (pending_op) { + case '+': + value = saved + value; + break; + case '-': + value = saved - value; + break; + case '*': + value = saved * value; + break; + case '/': + value = saved / value; + break; + case '%': + value = (long long)saved % (long long)value; + break; + case '&': + value = (long long)saved & (long long)value; + break; + case '|': + value = (long long)saved | (long long)value; + break; + case '^': + value = (long long)saved ^ (long long)value; + break; + case 'S': /* Shift. Positive means <<, negative means >> */ + if(value < 0) + value = (long long)saved >> (long long)(-value); + else + value = (long long)saved << (long long)value; + break; + } + saved = value; + pending_op = 0; + show_value(); } -void -start_number() -{ - if (making_number) - return; +void start_number() { + if(making_number) + return; - iptr = 1; - input_buf[0] = ' '; - input_buf[1] = 0; - making_number = 1; + iptr = 1; + input_buf[0] = ' '; + input_buf[1] = 0; + making_number = 1; } -void -key(char c) -{ - int v = c; - /* printf("key_number 0x%x\n", v); */ - - if(showing_err) { - switch(c) { - /* list the keys that should be still active in error state */ - case 27: - case 'C': - case 'Q': - case 17: - break; - default: - bell(); - return; - } - } - - switch (c) - { - case 27: - case 'C': - showing_err = 0; - making_number = 0; - iptr = 0; - pending_op = 0; - value = saved = 0; - set_string(""); - show_value(); - break; - case 'u': - if (making_number) - { - making_number = 0; - set_string(""); - } - break; - - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - v = c - 39; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - v -= '0'; - if (v >= base || iptr == MAXIN-1) - bell(); - else - { - start_number(); - input_buf[iptr++] = c; - input_buf[iptr] = 0; - convert_number(); - show_value(); - } - break; - case '.': - if (strchr(input_buf, '.')) - { - bell(); - break; - } - case ',': - if (iptr == 1 || iptr == MAXIN-1) - bell(); - else - { - start_number(); - input_buf[iptr++] = c; - input_buf[iptr] = 0; - convert_number(); - show_value(); - } - break; - - case 8: - case 127: - if (iptr <= 1) - bell(); - else - { - input_buf[--iptr] = 0; - convert_number(); - show_value(); - } - break; - - case '_': /* The +/- key */ - case 'i': - if (making_number) - { - if (input_buf[0] == '-') - input_buf[0] = ' '; - else - input_buf[0] = '-'; - convert_number(); - show_value(); - } - else - { - value *= -1.0; - saved *= -1.0; - show_value(); - } - break; - - case 'D': - end_number(); - base = 10; - show_value(); - break; - case 'H': - end_number(); - base = 16; - show_value(); - break; - case 'O': - end_number(); - base = 8; - show_value(); - break; - case 'B': - end_number(); - base = 2; - show_value(); - break; - - case 'x': /* Allow 'x' on keyboard to be used to enter '*' */ - c = '*'; - case '+': - case '-': - case '*': - case '/': - case '%': - case '^': - case '&': - case '|': - case 'S': - case '=': - end_number(); - pending_op = c; - break; - - case 13: - case 10: - end_number(); - pending_op = '='; - break; - - case '~': /* Invert bits (one's complement) */ - end_number(); - value = ~ (long long)value; - saved = value; - show_value(); - break; - - case '@': /* Two's complement */ - end_number(); - { - unsigned int i = ((unsigned int)value) & 0xffffffff; - value = ~i + 1; - } - saved = value; - show_value(); - break; - - case '<': /* SHL by one bit only */ -/* Modified by Keith Meehl to fix bit shift bug*/ - end_number(); - value = (long long)value << 1; - saved = value; - pending_op = 0; - show_value(); - break; - - case '>': /* SHR by one bit only */ -/* Modified by Keith Meehl to fix bit shift bug*/ - end_number(); - value = (long long)value >> 1; - saved = value; - pending_op = 0; - show_value(); - break; - - case '[': /* STO */ - stored = value; - break; - case ']': /* RCL */ - value = stored; - show_value(); - making_number = 1; - iptr = 1; - input_buf[0] = ' '; - break; - case '}': /* SUM */ - stored += value; - break; - - case 'P': /* click on the display itself */ - break; - - case 3: /* ^C */ - copy(); - break; - - case 22: /* ^V */ - paste(); - break; - - case 'q': - quiet = !quiet; - break; - - case 'Q': - case 17: - exit(0); - - case 'z': - { - /* dirty hack method of changing window size: - re-execute ourselves with appropriate argument. - TODO: see if this leaks memory, fds, etc */ - extern char *self; - char *args[4]; - - args[0] = self; - - switch(winsize) { - case 0: - args[1] = "-medium"; - break; - case 1: - args[1] = "-large"; - break; - case 2: - args[1] = "-small"; +void key(char c) { + int v = c; + /* printf("key_number 0x%x\n", v); */ + + if(showing_err) { + switch (c) { + /* list the keys that should be still active in error state */ + case 27: + case 'C': + case 'Q': + case 17: break; + default: + bell(); + return; } - - args[2] = NULL; - - /* note that atexit() functions do NOT get called on execv() */ - save_config(); - - XCloseDisplay(display); - - execv(self, args); - } - } + } + + switch (c) { + case 27: + case 'C': + showing_err = 0; + making_number = 0; + iptr = 0; + pending_op = 0; + value = saved = 0; + set_string(""); + show_value(); + break; + case 'u': + if(making_number) { + making_number = 0; + set_string(""); + } + break; + + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + v = c - 39; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + v -= '0'; + if(v >= base || iptr == MAXIN - 1) + bell(); + else { + start_number(); + input_buf[iptr++] = c; + input_buf[iptr] = 0; + convert_number(); + show_value(); + } + break; + case '.': + if(strchr(input_buf, '.')) { + bell(); + break; + } + case ',': + if(iptr == 1 || iptr == MAXIN - 1) + bell(); + else { + start_number(); + input_buf[iptr++] = c; + input_buf[iptr] = 0; + convert_number(); + show_value(); + } + break; + + case 8: + case 127: + if(iptr <= 1) + bell(); + else { + input_buf[--iptr] = 0; + convert_number(); + show_value(); + } + break; + + case '_': /* The +/- key */ + case 'i': + if(making_number) { + if(input_buf[0] == '-') + input_buf[0] = ' '; + else + input_buf[0] = '-'; + convert_number(); + show_value(); + } else { + value *= -1.0; + saved *= -1.0; + show_value(); + } + break; + + case 'D': + end_number(); + base = 10; + show_value(); + break; + case 'H': + end_number(); + base = 16; + show_value(); + break; + case 'O': + end_number(); + base = 8; + show_value(); + break; + case 'B': + end_number(); + base = 2; + show_value(); + break; + + case 'x': /* Allow 'x' on keyboard to be used to enter '*' */ + c = '*'; + case '+': + case '-': + case '*': + case '/': + case '%': + case '^': + case '&': + case '|': + case 'S': + case '=': + end_number(); + pending_op = c; + break; + + case 13: + case 10: + end_number(); + pending_op = '='; + break; + + case '~': /* Invert bits (one's complement) */ + end_number(); + value = ~(long long)value; + saved = value; + show_value(); + break; + + case '@': /* Two's complement */ + end_number(); + { + unsigned int i = ((unsigned int)value) & 0xffffffff; + value = ~i + 1; + } + saved = value; + show_value(); + break; + + case '<': /* SHL by one bit only */ +/* Modified by Keith Meehl to fix bit shift bug*/ + end_number(); + value = (long long)value << 1; + saved = value; + pending_op = 0; + show_value(); + break; + + case '>': /* SHR by one bit only */ +/* Modified by Keith Meehl to fix bit shift bug*/ + end_number(); + value = (long long)value >> 1; + saved = value; + pending_op = 0; + show_value(); + break; + + case '[': /* STO */ + stored = value; + break; + case ']': /* RCL */ + value = stored; + show_value(); + making_number = 1; + iptr = 1; + input_buf[0] = ' '; + break; + case '}': /* SUM */ + stored += value; + break; + + case 'P': /* click on the display itself */ + break; + + case 3: /* ^C */ + copy(); + break; + + case 22: /* ^V */ + paste(); + break; + + case 'q': + quiet = !quiet; + break; + + case 'Q': + case 17: + exit(0); + + case 'z': + { + /* dirty hack method of changing window size: + re-execute ourselves with appropriate argument. + TODO: see if this leaks memory, fds, etc */ + extern char *self; + char *args[4]; + + args[0] = self; + + switch (winsize) { + case 0: + args[1] = "-medium"; + break; + case 1: + args[1] = "-large"; + break; + case 2: + args[1] = "-small"; + break; + } + + args[2] = NULL; + + /* note that atexit() functions do NOT get called on execv() */ + save_config(); + + XCloseDisplay(display); + + execv(self, args); + } + } } static char *bmap[] = { - "PPPP\033", /* mouse copy and paste, CLR */ - "DHOB\010", /* DEC, HEX, OCT, BIN, DEL */ - "[]}<>", /* STO, RCL, SUM, <<, >> */ - "Sdef/", /* SHF, D E F / */ - "~abc*", /* INV, */ - "|789-", /* OR, */ - "&456+", /* AND, */ - "^123=", /* XOR, */ - "u0._=" /* CE, 0, . , +/-, = */ + "PPPP\033", /* mouse copy and paste, CLR */ + "DHOB\010", /* DEC, HEX, OCT, BIN, DEL */ + "[]}<>", /* STO, RCL, SUM, <<, >> */ + "Sdef/", /* SHF, D E F / */ + "~abc*", /* INV, */ + "|789-", /* OR, */ + "&456+", /* AND, */ + "^123=", /* XOR, */ + "u0._=" /* CE, 0, . , +/-, = */ }; -void -copy(void) -{ - XSetSelectionOwner(display, XA_PRIMARY, window, event.xbutton.time); +void copy(void) { + XSetSelectionOwner(display, XA_PRIMARY, window, event.xbutton.time); } -void -paste(void) -{ - XConvertSelection(display, XA_PRIMARY, XA_STRING, paste_atom, window, - event.xbutton.time); +void paste(void) { + XConvertSelection(display, XA_PRIMARY, XA_STRING, paste_atom, window, + event.xbutton.time); } -void -complete_paste(unsigned char *s, int n) -{ - int i; - for (i=0; i<n; i++) - key(s[i]); - +void complete_paste(unsigned char *s, int n) { + int i; + for(i = 0; i < n; i++) + key(s[i]); + } -void -button(int b, int x, int y) -{ - char k; - - if(b == 2) { - paste(); - return; - } - - x = (x-2)/(24 * scale_factor); - if (x < 0) x = 0; - if (x > 4) x = 4; - y = (y-1)/(16 * scale_factor); - if (y < 0) y = 0; - if (y > 8) y = 8; - - k = bmap[y][x]; - - if (k == 27 && b == 3) - exit(0); - - if (k == 'P' && b == 1) - copy(); - else if (k == 'P' && b != 1) - paste(); - else if(k == '/' && b != 1) - key('%'); - else if(k == '~' && b != 1) - key('@'); - else - key(k); +void button(int b, int x, int y) { + char k; + + if(b == 2) { + paste(); + return; + } + + x = (x - 2) / (24 * scale_factor); + if(x < 0) + x = 0; + if(x > 4) + x = 4; + y = (y - 1) / (16 * scale_factor); + if(y < 0) + y = 0; + if(y > 8) + y = 8; + + k = bmap[y][x]; + + if(k == 27 && b == 3) + exit(0); + + if(k == 'P' && b == 1) + copy(); + else if(k == 'P' && b != 1) + paste(); + else if(k == '/' && b != 1) + key('%'); + else if(k == '~' && b != 1) + key('@'); + else + key(k); } @@ -10,162 +10,148 @@ int widths[3] = { 125, 250, 500 }; int heights[3] = { 147, 294, 588 }; + int winsize = 2; -Display *display=0; -int screen=0; -Visual *visual=0; -Colormap cmap=0; -Window window=0; -Window rootwin=0; -GC gc=0; -Atom wm_protocols_atom=0; -Atom delete_atom=0; -Atom paste_atom=0; +Display *display = 0; +int screen = 0; +Visual *visual = 0; +Colormap cmap = 0; +Window window = 0; +Window rootwin = 0; +GC gc = 0; +Atom wm_protocols_atom = 0; +Atom delete_atom = 0; +Atom paste_atom = 0; XEvent event; int bit_on, bit_off; -void -setup_x(void) -{ - char *name = "hcalc"; - - if(winsize == 2) - scale_factor = 4; - else - scale_factor = winsize + 1; - - XSizeHints size_hints; - XTextProperty xtp; - XSetWindowAttributes attributes; - XColor color; - - display = XOpenDisplay(0); - screen = XDefaultScreen(display); - cmap = XDefaultColormap(display, screen); - visual = XDefaultVisual(display, screen); - rootwin = XDefaultRootWindow(display); - gc = XCreateGC(display, rootwin, 0, 0); - - wm_protocols_atom = XInternAtom(display, "WM_PROTOCOLS", 0); - delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", 0); - paste_atom = XInternAtom(display, "PASTE_DATA", 0); - - size_hints.flags = PSize | PMinSize | PMaxSize; - size_hints.width = size_hints.min_width = size_hints.max_width = widths[winsize]; - size_hints.height = size_hints.min_height = size_hints.max_height = heights[winsize]; - size_hints.x = 0; - size_hints.y = 0; - - window = XCreateWindow(display, - rootwin, - size_hints.x, size_hints.y, - size_hints.width, size_hints.height, - 0, - CopyFromParent, /* depth */ - InputOutput, - CopyFromParent, /* visual */ - 0, 0); - - XSetWMNormalHints(display, window, &size_hints); - - XStringListToTextProperty(&name, 1, &xtp); - XSetWMName(display, window, &xtp); - XFree(xtp.value); - - XSetWMProtocols(display, window, &delete_atom, 1); - - attributes.event_mask = ExposureMask | ButtonPressMask | KeyPressMask; - XChangeWindowAttributes(display, window, CWEventMask, &attributes); - - XMapWindow(display, window); - XFlush(display); - - color.red = 0x6666; - color.green = 0xffff; - color.blue = 0x6666; - color.flags = DoRed | DoGreen | DoBlue; - XAllocColor(display, cmap, &color); - bit_on = color.pixel; - - color.red = 0x3333; - color.green = 0xcccc; - color.blue = 0x3333; - color.flags = DoRed | DoGreen | DoBlue; - XAllocColor(display, cmap, &color); - bit_off = color.pixel; -} +void setup_x(void) { + char *name = "hcalc"; -void -process_input() -{ - char c; - KeySym keysym; - XTextProperty tprop; - - show_value(); - - while (1) - { - XNextEvent(display, &event); - if (event.xany.window == window) - { - switch (event.type) - { - case Expose: - if (event.xexpose.count == 0) - redraw(); - break; - case KeyPress: - if (XLookupString(&event.xkey, &c, 1, &keysym, 0) == 1) - { - key(c); - } + if(winsize == 2) + scale_factor = 4; else - { - switch (keysym) - { - case XK_F1: - key('D'); - break; - case XK_F2: - key('H'); - break; - case XK_F3: - key('O'); - break; - case XK_F4: - key('B'); - break; - case XK_Insert: - if(event.xkey.state & ShiftMask) - paste(); - break; - } + scale_factor = winsize + 1; + + XSizeHints size_hints; + XTextProperty xtp; + XSetWindowAttributes attributes; + XColor color; + + display = XOpenDisplay(0); + screen = XDefaultScreen(display); + cmap = XDefaultColormap(display, screen); + visual = XDefaultVisual(display, screen); + rootwin = XDefaultRootWindow(display); + gc = XCreateGC(display, rootwin, 0, 0); + + wm_protocols_atom = XInternAtom(display, "WM_PROTOCOLS", 0); + delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", 0); + paste_atom = XInternAtom(display, "PASTE_DATA", 0); + + size_hints.flags = PSize | PMinSize | PMaxSize; + size_hints.width = size_hints.min_width = size_hints.max_width = + widths[winsize]; + size_hints.height = size_hints.min_height = size_hints.max_height = + heights[winsize]; + size_hints.x = 0; + size_hints.y = 0; + + window = XCreateWindow(display, rootwin, size_hints.x, size_hints.y, size_hints.width, size_hints.height, 0, CopyFromParent, /* depth */ + InputOutput, CopyFromParent, /* visual */ + 0, 0); + + XSetWMNormalHints(display, window, &size_hints); + + XStringListToTextProperty(&name, 1, &xtp); + XSetWMName(display, window, &xtp); + XFree(xtp.value); + + XSetWMProtocols(display, window, &delete_atom, 1); + + attributes.event_mask = ExposureMask | ButtonPressMask | KeyPressMask; + XChangeWindowAttributes(display, window, CWEventMask, &attributes); + + XMapWindow(display, window); + XFlush(display); + + color.red = 0x6666; + color.green = 0xffff; + color.blue = 0x6666; + color.flags = DoRed | DoGreen | DoBlue; + XAllocColor(display, cmap, &color); + bit_on = color.pixel; + + color.red = 0x3333; + color.green = 0xcccc; + color.blue = 0x3333; + color.flags = DoRed | DoGreen | DoBlue; + XAllocColor(display, cmap, &color); + bit_off = color.pixel; +} + +void process_input() { + char c; + KeySym keysym; + XTextProperty tprop; + + show_value(); + + while(1) { + XNextEvent(display, &event); + if(event.xany.window == window) { + switch (event.type) { + case Expose: + if(event.xexpose.count == 0) + redraw(); + break; + case KeyPress: + if(XLookupString(&event.xkey, &c, 1, &keysym, 0) == 1) { + key(c); + } else { + switch (keysym) { + case XK_F1: + key('D'); + break; + case XK_F2: + key('H'); + break; + case XK_F3: + key('O'); + break; + case XK_F4: + key('B'); + break; + case XK_Insert: + if(event.xkey.state & ShiftMask) + paste(); + break; + } + } + break; + case ButtonPress: + button(event.xbutton.button, event.xbutton.x, + event.xbutton.y); + break; + case SelectionRequest: + send_current_display(); + break; + case SelectionNotify: + if(event.xselection.property == paste_atom) { + XGetTextProperty(display, window, &tprop, + paste_atom); + complete_paste(tprop.value, tprop.nitems); + } + break; + } + if(event.type == ClientMessage + && event.xclient.data.l[0] == delete_atom) { + /* printf( "Shutting down now!!!\n"); */ + break; + } + } } - break; - case ButtonPress: - button(event.xbutton.button, event.xbutton.x, event.xbutton.y); - break; - case SelectionRequest: - send_current_display(); - break; - case SelectionNotify: - if (event.xselection.property == paste_atom) - { - XGetTextProperty(display, window, &tprop, paste_atom); - complete_paste(tprop.value, tprop.nitems); - } - break; - } - if (event.type == ClientMessage - && event.xclient.data.l[0] == delete_atom) - { - /* printf( "Shutting down now!!!\n"); */ - break; - } - } - } - XCloseDisplay(display); + XCloseDisplay(display); } |