Thu, 16 Nov 2023 23:44:39 -0600
Depend on GPlugin 0.42.1
Testing Done:
Ran `ninja turtles` without issues and noted that the ircv3 introspection was successful.
Reviewed at https://reviews.imfreedom.org/r/2827/
| 4390 | 1 | /* |
| 2 | libgstroke - a GNOME stroke interface library | |
| 3 | Copyright (c) 1996,1997,1998,1999,2000,2001 Mark F. Willey, ETLA Technical | |
| 4 | ||
| 5 | See the file COPYING for distribution information. | |
| 6 | ||
| 7 | This file contains the stroke recognition algorithm. | |
| 8 | */ | |
| 9 | ||
|
42410
563e7a17c220
Fix possible clash of config headers
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
42172
diff
changeset
|
10 | #include "purpleconfig.h" |
| 4390 | 11 | |
|
40669
48dfed6f4f1f
Fix Windows builds and tests
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
40497
diff
changeset
|
12 | #ifdef HAVE_UNISTD_H |
| 4390 | 13 | #include <unistd.h> |
|
40669
48dfed6f4f1f
Fix Windows builds and tests
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
40497
diff
changeset
|
14 | #endif |
| 4390 | 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> | |
| 17 | #include <math.h> | |
| 18 | #include <glib.h> | |
| 19 | #include <gtk/gtk.h> | |
| 20 | #include "gstroke.h" | |
| 21 | #include "gstroke-internal.h" | |
| 22 | ||
| 23 | ||
| 24 | void | |
|
42172
7c2d151b410d
Use g_clear_* helpers where useful
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
40669
diff
changeset
|
25 | _gstroke_init(struct gstroke_metrics *metrics) { |
|
7c2d151b410d
Use g_clear_* helpers where useful
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
40669
diff
changeset
|
26 | g_clear_slist(&metrics->pointList, g_free); |
|
7c2d151b410d
Use g_clear_* helpers where useful
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents:
40669
diff
changeset
|
27 | metrics->point_count = 0; |
| 4390 | 28 | } |
| 29 | ||
| 30 | /* figure out which bin the point falls in */ | |
| 31 | static gint | |
| 32 | _gstroke_bin (p_point point_p, gint bound_x_1, gint bound_x_2, | |
| 33 | gint bound_y_1, gint bound_y_2) | |
| 34 | { | |
| 35 | ||
| 36 | gint bin_num = 1; | |
| 37 | ||
| 38 | if (point_p->x > bound_x_1) bin_num += 1; | |
| 39 | if (point_p->x > bound_x_2) bin_num += 1; | |
| 40 | if (point_p->y > bound_y_1) bin_num += 3; | |
| 41 | if (point_p->y > bound_y_2) bin_num += 3; | |
| 42 | ||
| 43 | return bin_num; | |
| 44 | } | |
| 45 | ||
| 46 | gint | |
| 47 | _gstroke_trans (gchar *sequence, struct gstroke_metrics *metrics) | |
| 48 | { | |
| 49 | GSList *crt_elem; | |
| 50 | /* number of bins recorded in the stroke */ | |
| 51 | guint sequence_count = 0; | |
| 52 | ||
| 53 | /* points-->sequence translation scratch variables */ | |
| 54 | gint prev_bin = 0; | |
| 55 | gint current_bin = 0; | |
| 56 | gint bin_count = 0; | |
| 57 | ||
| 58 | /* flag indicating the start of a stroke - always count it in the sequence */ | |
| 59 | gint first_bin = TRUE; | |
| 60 | ||
| 61 | /* bin boundary and size variables */ | |
| 62 | gint delta_x, delta_y; | |
| 63 | gint bound_x_1, bound_x_2; | |
| 64 | gint bound_y_1, bound_y_2; | |
| 65 | ||
| 66 | ||
| 67 | /* determine size of grid */ | |
| 68 | delta_x = metrics->max_x - metrics->min_x; | |
| 69 | delta_y = metrics->max_y - metrics->min_y; | |
| 70 | ||
| 71 | /* calculate bin boundary positions */ | |
| 72 | bound_x_1 = metrics->min_x + (delta_x / 3); | |
| 73 | bound_x_2 = metrics->min_x + 2 * (delta_x / 3); | |
| 74 | ||
| 75 | bound_y_1 = metrics->min_y + (delta_y / 3); | |
| 76 | bound_y_2 = metrics->min_y + 2 * (delta_y / 3); | |
| 77 | ||
| 78 | if (delta_x > GSTROKE_SCALE_RATIO * delta_y) { | |
| 79 | bound_y_1 = (metrics->max_y + metrics->min_y - delta_x) / 2 + (delta_x / 3); | |
| 80 | bound_y_2 = (metrics->max_y + metrics->min_y - delta_x) / 2 + 2 * (delta_x / 3); | |
| 81 | } else if (delta_y > GSTROKE_SCALE_RATIO * delta_x) { | |
| 82 | bound_x_1 = (metrics->max_x + metrics->min_x - delta_y) / 2 + (delta_y / 3); | |
| 83 | bound_x_2 = (metrics->max_x + metrics->min_x - delta_y) / 2 + 2 * (delta_y / 3); | |
| 84 | } | |
| 85 | ||
| 86 | #if 0 | |
| 87 | printf ("DEBUG:: point count: %d\n", metrics->point_count); | |
| 88 | printf ("DEBUG:: metrics->min_x: %d\n", metrics->min_x); | |
| 89 | printf ("DEBUG:: metrics->max_x: %d\n", metrics->max_x); | |
| 90 | printf ("DEBUG:: metrics->min_y: %d\n", metrics->min_y); | |
| 91 | printf ("DEBUG:: metrics->max_y: %d\n", metrics->max_y); | |
| 92 | printf ("DEBUG:: delta_x: %d\n", delta_x); | |
| 93 | printf ("DEBUG:: delta_y: %d\n", delta_y); | |
| 94 | printf ("DEBUG:: bound_x_1: %d\n", bound_x_1); | |
| 95 | printf ("DEBUG:: bound_x_2: %d\n", bound_x_2); | |
| 96 | printf ("DEBUG:: bound_y_1: %d\n", bound_y_1); | |
| 97 | printf ("DEBUG:: bound_y_2: %d\n", bound_y_2); | |
| 98 | #endif | |
| 99 | ||
| 100 | /* | |
| 101 | build string by placing points in bins, collapsing bins and | |
| 102 | discarding those with too few points... */ | |
| 103 | ||
| 104 | crt_elem = metrics->pointList; | |
| 105 | while (crt_elem != NULL) | |
| 106 | { | |
| 107 | /* figure out which bin the point falls in */ | |
| 108 | ||
| 109 | /*printf ("X = %d Y = %d\n", ((p_point)crt_elem->data)->x, | |
| 110 | ((p_point)crt_elem->data)->y); */ | |
| 111 | ||
| 112 | ||
| 113 | current_bin = _gstroke_bin ((p_point)crt_elem->data, bound_x_1, | |
| 114 | bound_x_2, bound_y_1, bound_y_2); | |
| 115 | ||
| 116 | /* if this is the first point, consider it the previous bin, too. */ | |
| 117 | if (prev_bin == 0) | |
| 118 | prev_bin = current_bin; | |
| 119 | ||
| 120 | /*printf ("DEBUG:: current bin: %d x=%d y = %d\n", current_bin, | |
| 121 | ((p_point)crt_elem->data)->x, | |
| 122 | ((p_point)crt_elem->data)->y); */ | |
| 123 | ||
| 124 | if (prev_bin == current_bin) | |
| 125 | bin_count++; | |
| 126 | else { | |
| 127 | /* we are moving to a new bin -- consider adding to the sequence */ | |
| 128 | if ((bin_count > (metrics->point_count * GSTROKE_BIN_COUNT_PERCENT)) | |
| 129 | || (first_bin == TRUE)) { | |
| 130 | ||
| 6063 | 131 | /* |
| 132 | gchar val = '0' + prev_bin; | |
| 133 | printf ("%c", val);fflush (stdout); | |
| 134 | g_string_append (&sequence, &val); | |
| 135 | */ | |
| 4390 | 136 | |
| 137 | first_bin = FALSE; | |
| 138 | sequence[sequence_count++] = '0' + prev_bin; | |
| 139 | /* printf ("DEBUG:: adding sequence: %d\n", prev_bin); */ | |
| 140 | ||
| 141 | } | |
| 142 | ||
| 143 | /* restart counting points in the new bin */ | |
| 144 | bin_count=0; | |
| 145 | prev_bin = current_bin; | |
| 146 | } | |
| 147 | ||
|
40497
2f45a03838e9
Fix gestures plugin
Elliott S <quantum.analyst@gmail.com>
parents:
39918
diff
changeset
|
148 | /* move to next point, freeing current point from list */ |
|
2f45a03838e9
Fix gestures plugin
Elliott S <quantum.analyst@gmail.com>
parents:
39918
diff
changeset
|
149 | g_free(crt_elem->data); |
|
2f45a03838e9
Fix gestures plugin
Elliott S <quantum.analyst@gmail.com>
parents:
39918
diff
changeset
|
150 | crt_elem = g_slist_next(crt_elem); |
| 4390 | 151 | } |
|
40497
2f45a03838e9
Fix gestures plugin
Elliott S <quantum.analyst@gmail.com>
parents:
39918
diff
changeset
|
152 | metrics->pointList = NULL; |
| 4390 | 153 | /* add the last run of points to the sequence */ |
| 154 | sequence[sequence_count++] = '0' + current_bin; | |
| 155 | /* printf ("DEBUG:: adding final sequence: %d\n", current_bin); */ | |
| 156 | ||
| 157 | _gstroke_init (metrics); | |
| 158 | ||
| 159 | { | |
| 6063 | 160 | /* FIXME: get rid of this block |
| 161 | gchar val = '0' + current_bin; | |
| 162 | printf ("%c\n", val);fflush (stdout); | |
| 163 | g_string_append (&sequence, '\0'); | |
| 164 | */ | |
| 4390 | 165 | sequence[sequence_count] = '\0'; |
| 166 | } | |
| 167 | ||
| 168 | return TRUE; | |
| 169 | } | |
| 170 | ||
| 171 | /* my plan is to make a stroke training program where you can enter all of | |
| 172 | the variations of slop that map to a canonical set of strokes. When the | |
| 173 | application calls gstroke_canonical, it gets one of the recognized strokes, | |
| 174 | or "", if it's not a recognized variation. I will probably use a hash | |
| 175 | table. Right now, it just passes the values through to gstroke_trans */ | |
| 176 | gint | |
| 177 | _gstroke_canonical (gchar *sequence, struct gstroke_metrics *metrics) | |
| 178 | { | |
| 179 | return _gstroke_trans (sequence, metrics); | |
| 180 | } | |
| 181 | ||
| 182 | ||
| 183 | void | |
| 184 | _gstroke_record (gint x, gint y, struct gstroke_metrics *metrics) | |
| 185 | { | |
| 186 | p_point new_point_p; | |
| 187 | gint delx, dely; | |
| 188 | float ix, iy; | |
| 189 | ||
|
9843
4daa0a6b2dd0
[gaim-migrate @ 10721]
Dave West <kat@users.sourceforge.net>
parents:
6063
diff
changeset
|
190 | g_return_if_fail( metrics != NULL ); |
|
4daa0a6b2dd0
[gaim-migrate @ 10721]
Dave West <kat@users.sourceforge.net>
parents:
6063
diff
changeset
|
191 | |
| 4390 | 192 | #if 0 |
| 193 | printf ("%d:%d ", x, y); fflush (stdout); | |
| 194 | #endif | |
| 195 | ||
| 196 | if (metrics->point_count < GSTROKE_MAX_POINTS) { | |
| 197 | if (metrics->pointList == NULL) { | |
| 198 | ||
| 199 | /* first point in list - initialize metrics */ | |
| 200 | metrics->min_x = 10000; | |
| 201 | metrics->min_y = 10000; | |
| 202 | metrics->max_x = -1; | |
| 203 | metrics->max_y = -1; | |
| 204 | ||
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
205 | new_point_p = g_new0(struct s_point, 1); |
|
28417
43c84621ba83
GSList internally doesn't use the same allocator as gmalloc. Refs #400.
Paul Aurich <darkrain42@pidgin.im>
parents:
15435
diff
changeset
|
206 | metrics->pointList = g_slist_prepend(metrics->pointList, new_point_p); |
| 4390 | 207 | metrics->point_count = 0; |
| 208 | ||
| 209 | } else { | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
210 | p_point last_point = (p_point)g_slist_last(metrics->pointList)->data; |
| 4390 | 211 | |
| 212 | /* interpolate between last and current point */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
213 | delx = x - last_point->x; |
|
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
214 | dely = y - last_point->y; |
| 4390 | 215 | |
| 216 | if (abs(delx) > abs(dely)) { /* step by the greatest delta direction */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
217 | iy = last_point->y; |
| 4390 | 218 | |
| 219 | /* go from the last point to the current, whatever direction it may be */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
220 | for (ix = last_point->x; (delx > 0) ? (ix < x) : (ix > x); ix += (delx > 0) ? 1 : -1) { |
| 4390 | 221 | |
| 222 | /* step the other axis by the correct increment */ | |
| 223 | iy += fabs(((float) dely / (float) delx)) * (float) ((dely < 0) ? -1.0 : 1.0); | |
| 224 | ||
| 225 | /* add the interpolated point */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
226 | new_point_p = g_new0(struct s_point, 1); |
| 4390 | 227 | new_point_p->x = ix; |
| 228 | new_point_p->y = iy; | |
| 12551 | 229 | metrics->pointList = g_slist_append (metrics->pointList, new_point_p); |
| 4390 | 230 | |
| 231 | /* update metrics */ | |
| 232 | if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; | |
| 233 | if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; | |
| 234 | if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; | |
| 235 | if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; | |
| 236 | metrics->point_count++; | |
| 237 | ||
| 238 | } | |
| 239 | } else { /* same thing, but for dely larger than delx case... */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
240 | p_point last_point = (p_point)g_slist_last(metrics->pointList)->data; |
|
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
241 | |
|
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
242 | ix = last_point->x; |
| 4390 | 243 | |
| 244 | /* go from the last point to the current, whatever direction it may be | |
| 245 | */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
246 | for (iy = last_point->y; (dely > 0) ? (iy < y) : (iy > y); iy += (dely > 0) ? 1 : -1) { |
| 4390 | 247 | |
| 248 | /* step the other axis by the correct increment */ | |
| 249 | ix += fabs(((float) delx / (float) dely)) * (float) ((delx < 0) ? -1.0 : 1.0); | |
| 250 | ||
| 251 | /* add the interpolated point */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
252 | new_point_p = g_new0(struct s_point, 1); |
| 4390 | 253 | new_point_p->y = iy; |
| 254 | new_point_p->x = ix; | |
| 12551 | 255 | metrics->pointList = g_slist_append(metrics->pointList, new_point_p); |
| 4390 | 256 | |
| 257 | /* update metrics */ | |
| 258 | if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; | |
| 259 | if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; | |
| 260 | if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; | |
| 261 | if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; | |
| 262 | metrics->point_count++; | |
| 263 | } | |
| 264 | } | |
| 265 | ||
| 266 | /* add the sampled point */ | |
|
39918
1c8e11f9274f
Fix several memory leaks.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
39916
diff
changeset
|
267 | new_point_p = g_new0(struct s_point, 1); |
| 12551 | 268 | metrics->pointList = g_slist_append(metrics->pointList, new_point_p); |
| 4390 | 269 | } |
| 270 | ||
| 271 | /* record the sampled point values */ | |
| 272 | new_point_p->x = x; | |
| 273 | new_point_p->y = y; | |
| 274 | ||
| 275 | #if 0 | |
| 276 | { | |
| 277 | GSList *crt = metrics->pointList; | |
| 278 | printf ("Record "); | |
| 279 | while (crt != NULL) | |
| 280 | { | |
| 281 | printf ("(%d,%d)", ((p_point)crt->data)->x, ((p_point)crt->data)->y); | |
| 282 | crt = g_slist_next (crt); | |
| 283 | } | |
| 284 | printf ("\n"); | |
| 285 | } | |
| 286 | #endif | |
| 287 | } | |
| 288 | } |