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