From c4f1bdb16b560d813e6ded83c2d7a4f4d280a90a Mon Sep 17 00:00:00 2001 From: Tim Hentenaar Date: Sat, 22 Mar 2014 02:47:33 +0100 Subject: Add bitwise ops and base conversion (DEC/OCT/HEX) in TI mode These operations implicitly truncate their parameters, and result to integers: * not * and * or * xor * shl * shr * mod * trunc Base 2 was left out of the base conversion code intentionally as it would require making the UI at least one third wider. Attempts to change base with negative values will simply display "error." Note that with larger numbers, the result may be inaccurate due to rounding. I've also bound the Return key to the equal() action. Signed-off-by: Tim Hentenaar --- actions.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) (limited to 'actions.c') diff --git a/actions.c b/actions.c index 50bf7b4..2857f96 100644 --- a/actions.c +++ b/actions.c @@ -46,7 +46,9 @@ from the X Consortium. #endif static void add(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void and(Widget w, XEvent *ev, String *vector, Cardinal *count); static void back(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void base(Widget w, XEvent *ev, String *vector, Cardinal *count); static void bell(Widget w, XEvent *ev, String *vector, Cardinal *count); static void clearit(Widget w, XEvent *ev, String *vector, Cardinal *count); static void cosine(Widget w, XEvent *ev, String *vector, Cardinal *count); @@ -63,11 +65,14 @@ static void factorial(Widget w, XEvent *ev, String *vector, Cardinal *count); static void inverse(Widget w, XEvent *ev, String *vector, Cardinal *count); static void leftParen(Widget w, XEvent *ev, String *vector, Cardinal *count); static void logarithm(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void mod(Widget w, XEvent *ev, String *vector, Cardinal *count); static void multiply(Widget w, XEvent *ev, String *vector, Cardinal *count); static void naturalLog(Widget w, XEvent *ev, String *vector, Cardinal *count); static void negate(Widget w, XEvent *ev, String *vector, Cardinal *count); static void nop(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void not(Widget w, XEvent *ev, String *vector, Cardinal *count); static void off(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void or(Widget w, XEvent *ev, String *vector, Cardinal *count); static void pi(Widget w, XEvent *ev, String *vector, Cardinal *count); static void power(Widget w, XEvent *ev, String *vector, Cardinal *count); static void quit(Widget w, XEvent *ev, String *vector, Cardinal *count); @@ -77,6 +82,8 @@ static void rightParen(Widget w, XEvent *ev, String *vector, Cardinal *count); static void roll(Widget w, XEvent *ev, String *vector, Cardinal *count); static void scientific(Widget w, XEvent *ev, String *vector, Cardinal *count); static void selection(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void shl(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void shr(Widget w, XEvent *ev, String *vector, Cardinal *count); static void sine(Widget w, XEvent *ev, String *vector, Cardinal *count); static void square(Widget w, XEvent *ev, String *vector, Cardinal *count); static void squareRoot(Widget w, XEvent *ev, String *vector, Cardinal *count); @@ -85,6 +92,8 @@ static void subtract(Widget w, XEvent *ev, String *vector, Cardinal *count); static void sum(Widget w, XEvent *ev, String *vector, Cardinal *count); static void tangent(Widget w, XEvent *ev, String *vector, Cardinal *count); static void tenpower(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void xtrunc(Widget w, XEvent *ev, String *vector, Cardinal *count); +static void xor(Widget w, XEvent *ev, String *vector, Cardinal *count); static void XexchangeY(Widget w, XEvent *ev, String *vector, Cardinal *count); /* @@ -93,7 +102,9 @@ static void XexchangeY(Widget w, XEvent *ev, String *vector, Cardinal *count); XtActionsRec Actions[] = { {"add", add}, /* addition */ +{"and", and}, /* bitwise and */ {"back", back}, /* HP-specific backspace */ +{"base", base}, /* base conversion */ {"bell", bell}, /* ring bell */ {"clear", clearit}, /* TI-specific clear calculator state */ {"cosine", cosine}, /* trigonometric function cosine */ @@ -110,11 +121,14 @@ XtActionsRec Actions[] = { {"inverse", inverse}, /* inverse */ {"leftParen", leftParen}, /* TI-specific left parenthesis */ {"logarithm", logarithm}, /* logarithm base 10 */ +{"mod", mod}, /* modulus */ {"multiply", multiply}, /* multiplication */ {"naturalLog", naturalLog}, /* natural logarithm base e */ {"negate", negate}, /* change sign */ {"nop", nop}, /* no operation, rings bell */ +{"not", not}, /* bitwise not */ {"off", off}, /* clear state */ +{"or", or}, /* bitwise or */ {"pi", pi}, /* the number pi */ {"power", power}, /* raise to an arbitrary power */ {"quit", quit}, /* quit */ @@ -124,6 +138,8 @@ XtActionsRec Actions[] = { {"roll", roll}, /* HP-specific roll stack */ {"scientific", scientific}, /* scientfic notation (EE) */ {"selection", selection}, /* copy selection */ +{"shl", shl}, /* arithmetic shift left */ +{"shr", shr}, /* arithmetic shift right */ {"sine", sine}, /* trigonometric function sine */ {"square", square}, /* square */ {"squareRoot", squareRoot}, /* square root */ @@ -132,6 +148,8 @@ XtActionsRec Actions[] = { {"sum", sum}, /* memory summation */ {"tangent", tangent}, /* trigonometric function tangent */ {"tenpower", tenpower}, /* 10 raised to to an arbitrary power */ +{"trunc", xtrunc}, /* truncate to integer */ +{"xor", xor}, /* bitwise xor */ {"XexchangeY", XexchangeY} /* HP-specific exchange X and Y registers */ }; @@ -145,6 +163,14 @@ static void add(Widget w, XEvent *ev, String *vector, Cardinal *count) post_op(); } +/*ARGSUSED*/ +static void and(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kAND); + rpn ? twof(kAND) : twoop(kAND); + post_op(); +} + /*ARGSUSED*/ static void back(Widget w, XEvent *ev, String *vector, Cardinal *count) { @@ -153,6 +179,14 @@ static void back(Widget w, XEvent *ev, String *vector, Cardinal *count) post_op(); } +/*ARGSUSED*/ +static void base(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kBASE); + change_base(); + post_op(); +} + /*ARGSUSED*/ static void bell(Widget w, XEvent *ev, String *vector, Cardinal *count) { @@ -196,6 +230,7 @@ static void digit(Widget w, XEvent *ev, String *vector, Cardinal *count) { switch (vector[0][0]) { + case '0': XCALC_PRE_OP(kZERO); numeric(kZERO); break; case '1': XCALC_PRE_OP(kONE); numeric(kONE); break; case '2': XCALC_PRE_OP(kTWO); numeric(kTWO); break; case '3': XCALC_PRE_OP(kTHREE); numeric(kTHREE); break; @@ -205,7 +240,12 @@ static void digit(Widget w, XEvent *ev, String *vector, Cardinal *count) case '7': XCALC_PRE_OP(kSEVEN); numeric(kSEVEN); break; case '8': XCALC_PRE_OP(kEIGHT); numeric(kEIGHT); break; case '9': XCALC_PRE_OP(kNINE); numeric(kNINE); break; - case '0': XCALC_PRE_OP(kZERO); numeric(kZERO); break; + case 'A': XCALC_PRE_OP(kxA); numeric(kxA); break; + case 'B': XCALC_PRE_OP(kxB); numeric(kxB); break; + case 'C': XCALC_PRE_OP(kxC); numeric(kxC); break; + case 'D': XCALC_PRE_OP(kxD); numeric(kxD); break; + case 'E': XCALC_PRE_OP(kxE); numeric(kxE); break; + case 'F': XCALC_PRE_OP(kxF); numeric(kxF); break; } post_op(); } @@ -290,6 +330,14 @@ static void logarithm(Widget w, XEvent *ev, String *vector, Cardinal *count) post_op(); } +/*ARGSUSED*/ +static void mod(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kMOD); + rpn ? twof(kMOD) : twoop(kMOD); + post_op(); +} + /*ARGSUSED*/ static void multiply(Widget w, XEvent *ev, String *vector, Cardinal *count) { @@ -320,6 +368,14 @@ static void nop(Widget w, XEvent *ev, String *vector, Cardinal *count) ringbell(); } +/*ARGSUSED*/ +static void not(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kNOT); + oneop(kNOT); + post_op(); +} + /*ARGSUSED*/ static void off(Widget w, XEvent *ev, String *vector, Cardinal *count) { @@ -328,6 +384,14 @@ static void off(Widget w, XEvent *ev, String *vector, Cardinal *count) post_op(); } +/*ARGSUSED*/ +static void or(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kOR); + rpn ? twof(kOR) : twoop(kOR); + post_op(); +} + /*ARGSUSED*/ static void pi(Widget w, XEvent *ev, String *vector, Cardinal *count) { @@ -399,6 +463,22 @@ static void selection(Widget w, XEvent *ev, String *vector, Cardinal *count) do_select(((XButtonReleasedEvent *)ev)->time); } +/*ARGSUSED*/ +static void shl(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kSHL); + rpn ? twof(kSHL) : twoop(kSHL); + post_op(); +} + +/*ARGSUSED*/ +static void shr(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kSHR); + rpn ? twof(kSHR) : twoop(kSHR); + post_op(); +} + /*ARGSUSED*/ static void sine(Widget w, XEvent *ev, String *vector, Cardinal *count) { @@ -463,6 +543,22 @@ static void tenpower(Widget w, XEvent *ev, String *vector, Cardinal *count) post_op(); } +/*ARGSUSED*/ +static void xtrunc(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kTRUNC); + oneop(kTRUNC); + post_op(); +} + +/*ARGSUSED*/ +static void xor(Widget w, XEvent *ev, String *vector, Cardinal *count) +{ + XCALC_PRE_OP(kXOR); + rpn ? twof(kXOR) : twoop(kXOR); + post_op(); +} + /*ARGSUSED*/ static void XexchangeY(Widget w, XEvent *ev, String *vector, Cardinal *count) { -- cgit v1.2.3