diff options
author | Keith Packard <keithp@keithp.com> | 2021-07-15 23:24:30 -0700 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2021-07-15 23:25:15 -0700 |
commit | 30be448a408bf8543047008ef823321016b73132 (patch) | |
tree | 1111749a2c212a6b1620d114d26fb40509741bcb /Clock.c | |
parent | ff94d3f086479724261b764b11a0012f5e6cdc63 (diff) |
Update hands smoothly in Render mode
Draw hour and minute hand every time the clock repaints, placing them
at the correct position for the current time.
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'Clock.c')
-rw-r--r-- | Clock.c | 164 |
1 files changed, 16 insertions, 148 deletions
@@ -78,6 +78,7 @@ SOFTWARE. #include "config.h" #endif +#define _GNU_SOURCE #include <X11/Xlib.h> #include <X11/StringDefs.h> #include <X11/IntrinsicP.h> @@ -86,6 +87,7 @@ SOFTWARE. #include <stdio.h> #include <X11/Xos.h> #include <X11/Xaw/XawInit.h> +#include <math.h> #if !defined(NO_I18N) && defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) #include <iconv.h> #include <langinfo.h> @@ -211,7 +213,7 @@ static void Resize(Widget gw); static void Redisplay(Widget gw, XEvent * event, Region region); static void clock_tic(XtPointer client_data, XtIntervalId * id); static void erase_hands(ClockWidget w, struct tm *tm); -static void ClockAngle(int tick_units, double *sinp, double *cosp); +static void ClockAngle(double tick_units, double *sinp, double *cosp); static void DrawLine(ClockWidget w, Dimension blank_length, Dimension length, int tick_units); static void DrawHand(ClockWidget w, Dimension length, Dimension width, @@ -1033,7 +1035,7 @@ RenderRotate(ClockWidget w, XPointDouble * out, double x, double y, double s, } static void -RenderHand(ClockWidget w, int tick_units, int size, XftColor * color, +RenderHand(ClockWidget w, double tick_units, double size, XftColor * color, Boolean draw) { double c, s; @@ -1068,11 +1070,13 @@ RenderHand(ClockWidget w, int tick_units, int size, XftColor * color, } static void -RenderHands(ClockWidget w, struct tm *tm, Boolean draw) +RenderHands(ClockWidget w, struct tm *tm, struct timeval *tv, Boolean draw) { - RenderHand(w, tm->tm_hour * 300 + tm->tm_min * 5, HOUR_HAND_FRACT, + double sec = tm->tm_sec + tv->tv_usec / 1000000.0; + + RenderHand(w, tm->tm_hour * 300 + tm->tm_min * 5 + sec / 12.0, HOUR_HAND_FRACT, &w->clock.hour_color, draw); - RenderHand(w, tm->tm_min * 60 + tm->tm_sec, MINUTE_HAND_FRACT, + RenderHand(w, tm->tm_min * 60 + sec, MINUTE_HAND_FRACT, &w->clock.min_color, draw); } @@ -1592,9 +1596,10 @@ clock_tic(XtPointer client_data, XtIntervalId * id) */ if (tm.tm_min != w->clock.otm.tm_min || tm.tm_hour != w->clock.otm.tm_hour || - tm.tm_sec != w->clock.otm.tm_sec) { - RenderHands(w, &w->clock.otm, False); - RenderHands(w, &tm, False); + tm.tm_sec != w->clock.otm.tm_sec || + tv.tv_usec != w->clock.otv.tv_usec) { + RenderHands(w, &w->clock.otm, &w->clock.otv, False); + RenderHands(w, &tm, &tv, False); } if (w->clock.show_second_hand && (tm.tm_sec != w->clock.otm.tm_sec || @@ -1605,7 +1610,7 @@ clock_tic(XtPointer client_data, XtIntervalId * id) if (w->clock.damage.width && w->clock.damage.height) { RenderClip(w); DrawClockFace(w); - RenderHands(w, &tm, True); + RenderHands(w, &tm, &tv, True); if (w->clock.show_second_hand == TRUE) RenderSec(w, &tm, &tv, True); } @@ -1726,147 +1731,10 @@ erase_hands(ClockWidget w, struct tm *tm) } } -static double const Sines[] = { - 0.000000, 0.001745, 0.003490, 0.005235, 0.006981, 0.008726, 0.010471, 0.012217, - 0.013962, 0.015707, 0.017452, 0.019197, 0.020942, 0.022687, 0.024432, 0.026176, - 0.027921, 0.029666, 0.031410, 0.033155, 0.034899, 0.036643, 0.038387, 0.040131, - 0.041875, 0.043619, 0.045362, 0.047106, 0.048849, 0.050592, 0.052335, 0.054078, - 0.055821, 0.057564, 0.059306, 0.061048, 0.062790, 0.064532, 0.066273, 0.068015, - 0.069756, 0.071497, 0.073238, 0.074978, 0.076719, 0.078459, 0.080198, 0.081938, - 0.083677, 0.085416, 0.087155, 0.088894, 0.090632, 0.092370, 0.094108, 0.095845, - 0.097582, 0.099319, 0.101056, 0.102792, 0.104528, 0.106264, 0.107999, 0.109734, - 0.111468, 0.113203, 0.114937, 0.116670, 0.118403, 0.120136, 0.121869, 0.123601, - 0.125333, 0.127064, 0.128795, 0.130526, 0.132256, 0.133986, 0.135715, 0.137444, - 0.139173, 0.140901, 0.142628, 0.144356, 0.146083, 0.147809, 0.149535, 0.151260, - 0.152985, 0.154710, 0.156434, 0.158158, 0.159881, 0.161603, 0.163325, 0.165047, - 0.166768, 0.168489, 0.170209, 0.171929, 0.173648, 0.175366, 0.177084, 0.178802, - 0.180519, 0.182235, 0.183951, 0.185666, 0.187381, 0.189095, 0.190808, 0.192521, - 0.194234, 0.195946, 0.197657, 0.199367, 0.201077, 0.202787, 0.204496, 0.206204, - 0.207911, 0.209618, 0.211324, 0.213030, 0.214735, 0.216439, 0.218143, 0.219846, - 0.221548, 0.223250, 0.224951, 0.226651, 0.228350, 0.230049, 0.231747, 0.233445, - 0.235142, 0.236838, 0.238533, 0.240228, 0.241921, 0.243615, 0.245307, 0.246999, - 0.248689, 0.250380, 0.252069, 0.253757, 0.255445, 0.257132, 0.258819, 0.260504, - 0.262189, 0.263873, 0.265556, 0.267238, 0.268919, 0.270600, 0.272280, 0.273959, - 0.275637, 0.277314, 0.278991, 0.280666, 0.282341, 0.284015, 0.285688, 0.287360, - 0.289031, 0.290702, 0.292371, 0.294040, 0.295708, 0.297374, 0.299040, 0.300705, - 0.302369, 0.304033, 0.305695, 0.307356, 0.309016, 0.310676, 0.312334, 0.313992, - 0.315649, 0.317304, 0.318959, 0.320612, 0.322265, 0.323917, 0.325568, 0.327217, - 0.328866, 0.330514, 0.332161, 0.333806, 0.335451, 0.337095, 0.338737, 0.340379, - 0.342020, 0.343659, 0.345298, 0.346935, 0.348572, 0.350207, 0.351841, 0.353474, - 0.355106, 0.356737, 0.358367, 0.359996, 0.361624, 0.363251, 0.364876, 0.366501, - 0.368124, 0.369746, 0.371367, 0.372987, 0.374606, 0.376224, 0.377840, 0.379456, - 0.381070, 0.382683, 0.384295, 0.385906, 0.387515, 0.389123, 0.390731, 0.392337, - 0.393941, 0.395545, 0.397147, 0.398749, 0.400349, 0.401947, 0.403545, 0.405141, - 0.406736, 0.408330, 0.409923, 0.411514, 0.413104, 0.414693, 0.416280, 0.417867, - 0.419452, 0.421035, 0.422618, 0.424199, 0.425779, 0.427357, 0.428935, 0.430511, - 0.432085, 0.433659, 0.435231, 0.436801, 0.438371, 0.439939, 0.441505, 0.443071, - 0.444635, 0.446197, 0.447759, 0.449318, 0.450877, 0.452434, 0.453990, 0.455544, - 0.457097, 0.458649, 0.460199, 0.461748, 0.463296, 0.464842, 0.466386, 0.467929, - 0.469471, 0.471011, 0.472550, 0.474088, 0.475624, 0.477158, 0.478691, 0.480223, - 0.481753, 0.483282, 0.484809, 0.486335, 0.487859, 0.489382, 0.490903, 0.492423, - 0.493941, 0.495458, 0.496973, 0.498487, 0.499999, 0.501510, 0.503019, 0.504527, - 0.506033, 0.507538, 0.509041, 0.510542, 0.512042, 0.513541, 0.515038, 0.516533, - 0.518027, 0.519519, 0.521009, 0.522498, 0.523985, 0.525471, 0.526955, 0.528438, - 0.529919, 0.531398, 0.532876, 0.534352, 0.535826, 0.537299, 0.538770, 0.540240, - 0.541708, 0.543174, 0.544639, 0.546101, 0.547563, 0.549022, 0.550480, 0.551936, - 0.553391, 0.554844, 0.556295, 0.557745, 0.559192, 0.560638, 0.562083, 0.563526, - 0.564967, 0.566406, 0.567843, 0.569279, 0.570713, 0.572145, 0.573576, 0.575005, - 0.576432, 0.577857, 0.579281, 0.580702, 0.582122, 0.583541, 0.584957, 0.586372, - 0.587785, 0.589196, 0.590605, 0.592013, 0.593418, 0.594822, 0.596224, 0.597625, - 0.599023, 0.600420, 0.601815, 0.603207, 0.604599, 0.605988, 0.607375, 0.608761, - 0.610145, 0.611527, 0.612907, 0.614285, 0.615661, 0.617035, 0.618408, 0.619779, - 0.621147, 0.622514, 0.623879, 0.625242, 0.626603, 0.627963, 0.629320, 0.630675, - 0.632029, 0.633380, 0.634730, 0.636078, 0.637423, 0.638767, 0.640109, 0.641449, - 0.642787, 0.644123, 0.645457, 0.646789, 0.648119, 0.649448, 0.650774, 0.652098, - 0.653420, 0.654740, 0.656059, 0.657375, 0.658689, 0.660001, 0.661311, 0.662620, - 0.663926, 0.665230, 0.666532, 0.667832, 0.669130, 0.670426, 0.671720, 0.673012, - 0.674302, 0.675590, 0.676875, 0.678159, 0.679441, 0.680720, 0.681998, 0.683273, - 0.684547, 0.685818, 0.687087, 0.688354, 0.689619, 0.690882, 0.692143, 0.693401, - 0.694658, 0.695912, 0.697165, 0.698415, 0.699663, 0.700909, 0.702153, 0.703394, - 0.704634, 0.705871, 0.707106, -}; - -static double const Cosines[] = { - 1.000000, 0.999998, 0.999993, 0.999986, 0.999975, 0.999961, 0.999945, 0.999925, - 0.999902, 0.999876, 0.999847, 0.999815, 0.999780, 0.999742, 0.999701, 0.999657, - 0.999610, 0.999559, 0.999506, 0.999450, 0.999390, 0.999328, 0.999262, 0.999194, - 0.999122, 0.999048, 0.998970, 0.998889, 0.998806, 0.998719, 0.998629, 0.998536, - 0.998440, 0.998341, 0.998239, 0.998134, 0.998026, 0.997915, 0.997801, 0.997684, - 0.997564, 0.997440, 0.997314, 0.997185, 0.997052, 0.996917, 0.996778, 0.996637, - 0.996492, 0.996345, 0.996194, 0.996041, 0.995884, 0.995724, 0.995561, 0.995396, - 0.995227, 0.995055, 0.994880, 0.994702, 0.994521, 0.994337, 0.994150, 0.993960, - 0.993767, 0.993571, 0.993372, 0.993170, 0.992965, 0.992757, 0.992546, 0.992331, - 0.992114, 0.991894, 0.991671, 0.991444, 0.991215, 0.990983, 0.990747, 0.990509, - 0.990268, 0.990023, 0.989776, 0.989525, 0.989272, 0.989015, 0.988756, 0.988493, - 0.988228, 0.987959, 0.987688, 0.987413, 0.987136, 0.986855, 0.986572, 0.986285, - 0.985996, 0.985703, 0.985407, 0.985109, 0.984807, 0.984503, 0.984195, 0.983885, - 0.983571, 0.983254, 0.982935, 0.982612, 0.982287, 0.981958, 0.981627, 0.981292, - 0.980955, 0.980614, 0.980271, 0.979924, 0.979575, 0.979222, 0.978867, 0.978508, - 0.978147, 0.977783, 0.977415, 0.977045, 0.976672, 0.976296, 0.975916, 0.975534, - 0.975149, 0.974761, 0.974370, 0.973975, 0.973578, 0.973178, 0.972775, 0.972369, - 0.971961, 0.971549, 0.971134, 0.970716, 0.970295, 0.969872, 0.969445, 0.969015, - 0.968583, 0.968147, 0.967709, 0.967267, 0.966823, 0.966376, 0.965925, 0.965472, - 0.965016, 0.964557, 0.964095, 0.963630, 0.963162, 0.962691, 0.962217, 0.961741, - 0.961261, 0.960779, 0.960293, 0.959805, 0.959313, 0.958819, 0.958322, 0.957822, - 0.957319, 0.956813, 0.956304, 0.955793, 0.955278, 0.954760, 0.954240, 0.953716, - 0.953190, 0.952661, 0.952129, 0.951594, 0.951056, 0.950515, 0.949972, 0.949425, - 0.948876, 0.948323, 0.947768, 0.947210, 0.946649, 0.946085, 0.945518, 0.944948, - 0.944376, 0.943800, 0.943222, 0.942641, 0.942057, 0.941470, 0.940880, 0.940288, - 0.939692, 0.939094, 0.938493, 0.937888, 0.937281, 0.936672, 0.936059, 0.935444, - 0.934825, 0.934204, 0.933580, 0.932953, 0.932323, 0.931691, 0.931055, 0.930417, - 0.929776, 0.929132, 0.928485, 0.927836, 0.927183, 0.926528, 0.925870, 0.925209, - 0.924546, 0.923879, 0.923210, 0.922538, 0.921863, 0.921185, 0.920504, 0.919821, - 0.919135, 0.918446, 0.917754, 0.917060, 0.916362, 0.915662, 0.914959, 0.914253, - 0.913545, 0.912834, 0.912120, 0.911403, 0.910683, 0.909961, 0.909236, 0.908508, - 0.907777, 0.907044, 0.906307, 0.905568, 0.904827, 0.904082, 0.903335, 0.902585, - 0.901832, 0.901077, 0.900318, 0.899557, 0.898794, 0.898027, 0.897258, 0.896486, - 0.895711, 0.894934, 0.894154, 0.893371, 0.892585, 0.891797, 0.891006, 0.890212, - 0.889416, 0.888617, 0.887815, 0.887010, 0.886203, 0.885393, 0.884580, 0.883765, - 0.882947, 0.882126, 0.881303, 0.880477, 0.879648, 0.878817, 0.877982, 0.877146, - 0.876306, 0.875464, 0.874619, 0.873772, 0.872922, 0.872069, 0.871213, 0.870355, - 0.869494, 0.868631, 0.867765, 0.866896, 0.866025, 0.865151, 0.864274, 0.863395, - 0.862513, 0.861629, 0.860742, 0.859852, 0.858959, 0.858064, 0.857167, 0.856267, - 0.855364, 0.854458, 0.853550, 0.852640, 0.851726, 0.850811, 0.849892, 0.848971, - 0.848048, 0.847121, 0.846193, 0.845261, 0.844327, 0.843391, 0.842452, 0.841510, - 0.840566, 0.839619, 0.838670, 0.837718, 0.836764, 0.835807, 0.834847, 0.833885, - 0.832921, 0.831954, 0.830984, 0.830012, 0.829037, 0.828060, 0.827080, 0.826098, - 0.825113, 0.824126, 0.823136, 0.822144, 0.821149, 0.820151, 0.819152, 0.818149, - 0.817144, 0.816137, 0.815127, 0.814115, 0.813100, 0.812083, 0.811063, 0.810041, - 0.809016, 0.807989, 0.806960, 0.805928, 0.804893, 0.803856, 0.802817, 0.801775, - 0.800731, 0.799684, 0.798635, 0.797583, 0.796529, 0.795473, 0.794414, 0.793353, - 0.792289, 0.791223, 0.790155, 0.789084, 0.788010, 0.786935, 0.785856, 0.784776, - 0.783693, 0.782608, 0.781520, 0.780430, 0.779337, 0.778243, 0.777145, 0.776046, - 0.774944, 0.773840, 0.772733, 0.771624, 0.770513, 0.769399, 0.768283, 0.767165, - 0.766044, 0.764921, 0.763796, 0.762668, 0.761538, 0.760405, 0.759271, 0.758134, - 0.756995, 0.755853, 0.754709, 0.753563, 0.752414, 0.751264, 0.750111, 0.748955, - 0.747798, 0.746638, 0.745475, 0.744311, 0.743144, 0.741975, 0.740804, 0.739631, - 0.738455, 0.737277, 0.736097, 0.734914, 0.733729, 0.732542, 0.731353, 0.730162, - 0.728968, 0.727772, 0.726574, 0.725374, 0.724171, 0.722967, 0.721760, 0.720551, - 0.719339, 0.718126, 0.716910, 0.715692, 0.714472, 0.713250, 0.712026, 0.710799, - 0.709570, 0.708339, 0.707106, -}; - static void -ClockAngle(int tick_units, double *sinp, double *cosp) +ClockAngle(double tick_units, double *sinp, double *cosp) { - int reduced, upper; - - reduced = tick_units % 450; - upper = tick_units / 450; - if (upper & 1) - reduced = 450 - reduced; - if ((upper + 1) & 2) { - *sinp = Cosines[reduced]; - *cosp = Sines[reduced]; - } - else { - *sinp = Sines[reduced]; - *cosp = Cosines[reduced]; - } - if (upper >= 2 && upper < 6) - *cosp = -*cosp; - if (upper >= 4) - *sinp = -*sinp; + sincos(tick_units * (M_PI / 180 / 10.0), sinp, cosp); } /* |