graffiti.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Graffiti.c is based on the file Scribble.c copyrighted
  11. * by Keith Packard:
  12. *
  13. * Copyright © 1999 Keith Packard
  14. *
  15. * Permission to use, copy, modify, distribute, and sell this software and its
  16. * documentation for any purpose is hereby granted without fee, provided that
  17. * the above copyright notice appear in all copies and that both that
  18. * copyright notice and this permission notice appear in supporting
  19. * documentation, and that the name of Keith Packard not be used in
  20. * advertising or publicity pertaining to distribution of the software without
  21. * specific, written prior permission. Keith Packard makes no
  22. * representations about the suitability of this software for any purpose. It
  23. * is provided "as is" without express or implied warranty.
  24. *
  25. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  26. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  27. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  28. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  29. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  30. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  31. * PERFORMANCE OF THIS SOFTWARE.
  32. */
  33. #include <u.h>
  34. #include <libc.h>
  35. #include <draw.h>
  36. #include <scribble.h>
  37. #include "scribbleimpl.h"
  38. #include "graffiti.h"
  39. int ScribbleDebug;
  40. char *cl_name[3] = {
  41. DEFAULT_LETTERS_FILE,
  42. DEFAULT_DIGITS_FILE,
  43. DEFAULT_PUNC_FILE
  44. };
  45. Rune
  46. recognize (Scribble *s)
  47. {
  48. struct graffiti *graf = s->graf;
  49. Stroke *ps = &s->ps;
  50. Rune rune;
  51. int c;
  52. int nr;
  53. rec_alternative *ret;
  54. if (ps->npts == 0)
  55. return '\0';
  56. c = recognizer_translate(
  57. graf->rec[s->puncShift ? CS_PUNCTUATION : s->curCharSet],
  58. 1, ps, false, &nr, &ret);
  59. if (c != -1)
  60. delete_rec_alternative_array(nr, ret, false);
  61. rune = '\0';
  62. switch (c) {
  63. case '\0':
  64. if(ScribbleDebug)fprint(2, "(case '\\0')\n");
  65. break;
  66. case 'A': /* space */
  67. rune = ' ';
  68. if(ScribbleDebug)fprint(2, "(case A) character = ' %C' (0x%x)\n", rune, rune);
  69. break;
  70. case 'B': /* backspace */
  71. rune = '\b';
  72. if(ScribbleDebug)fprint(2, "(case B) character = \\b (0x%x)\n", rune);
  73. break;
  74. case 'N': /* numlock */
  75. if(ScribbleDebug)fprint(2, "(case N)\n");
  76. if (s->curCharSet == CS_DIGITS) {
  77. s->curCharSet = CS_LETTERS;
  78. } else {
  79. s->curCharSet = CS_DIGITS;
  80. }
  81. s->tmpShift = 0;
  82. s->puncShift = 0;
  83. s->ctrlShift = 0;
  84. break;
  85. case 'P': /* usually puncshift, but we'll make it CTRL */
  86. if(ScribbleDebug)fprint(2, "(case P)\n");
  87. s->ctrlShift = !s->ctrlShift;
  88. s->tmpShift = 0;
  89. s->puncShift = 0;
  90. break;
  91. case 'R': /* newline */
  92. rune = '\n';
  93. if(ScribbleDebug)fprint(2, "(case R) character = \\n (0x%x)\n", rune);
  94. break;
  95. case 'S': /* shift */
  96. if(ScribbleDebug)fprint(2, "(case S)\n");
  97. s->puncShift = 0;
  98. s->ctrlShift = 0;
  99. if (s->capsLock) {
  100. s->capsLock = 0;
  101. s->tmpShift = 0;
  102. break;
  103. }
  104. if (s->tmpShift == 0) {
  105. s->tmpShift++;
  106. break;
  107. }
  108. /* fall through */
  109. case 'L': /* caps lock */
  110. if(ScribbleDebug)fprint(2, "(case L)\n");
  111. s->capsLock = !s->capsLock;
  112. break;
  113. case '.': /* toggle punctuation mode */
  114. if (s->puncShift) {
  115. s->puncShift = 0;
  116. } else {
  117. s->puncShift = 1;
  118. s->ctrlShift = 0;
  119. s->tmpShift = 0;
  120. return rune;
  121. }
  122. rune = '.';
  123. if(0)fprint(2, "(case .) character = %c (0x%x)\n", rune, rune);
  124. break;
  125. default:
  126. if ('A' <= c && c <= 'Z') {
  127. if(ScribbleDebug)fprint(2, "(bad case?) character = %c (0x%x)\n", c, c);
  128. return rune;
  129. }
  130. rune = c;
  131. if (s->ctrlShift)
  132. {
  133. if (c < 'a' || 'z' < c)
  134. {
  135. if(ScribbleDebug)fprint(2, "(default) character = %c (0x%x)\n", rune, rune);
  136. return rune;
  137. }
  138. rune = rune & 0x1f;
  139. } else if ((s->capsLock && !s->tmpShift) ||
  140. (!s->capsLock && s->tmpShift))
  141. {
  142. if (rune < 0xff)
  143. rune = toupper(rune);
  144. }
  145. s->tmpShift = 0;
  146. s->puncShift = 0;
  147. s->ctrlShift = 0;
  148. if(ScribbleDebug)fprint(2, "(default) character = %c (0x%x)\n", rune, rune);
  149. }
  150. return rune;
  151. }
  152. /* This procedure is called to initialize pg by loading the three
  153. * recognizers, loading the initial set of three classifiers, and
  154. * loading & verifying the recognizer extension functions. If the
  155. * directory $HOME/.recognizers exists, the classifier files will be
  156. * loaded from that directory. If not, or if there is an error, the
  157. * default files (directory specified in Makefile) will be loaded
  158. * instead. Returns non-zero on success, 0 on failure. (Adapted from
  159. * package tkgraf/src/GraffitiPkg.c.
  160. */
  161. static int
  162. graffiti_load_recognizers(struct graffiti *pg)
  163. {
  164. bool usingDefault;
  165. char* homedir;
  166. int i;
  167. rec_fn *fns;
  168. /* First, load the recognizers... */
  169. /* call recognizer_unload if an error ? */
  170. for (i = 0; i < NUM_RECS; i++) {
  171. /* Load the recognizer itself... */
  172. pg->rec[i] = recognizer_load(DEFAULT_REC_DIR, "", nil);
  173. if (pg->rec[i] == nil) {
  174. fprint(2,"Error loading recognizer from %s.", DEFAULT_REC_DIR);
  175. return 0;
  176. }
  177. if ((* (int *)(pg->rec[i])) != 0xfeed) {
  178. fprint(2,"Error in recognizer_magic.");
  179. return 0;
  180. }
  181. }
  182. /* ...then figure out where the classifiers are... */
  183. if ( (homedir = (char*)getenv("home")) == nil ) {
  184. if(0)fprint(2, "no homedir, using = %s\n", REC_DEFAULT_USER_DIR);
  185. strecpy(pg->cldir, pg->cldir+sizeof pg->cldir, REC_DEFAULT_USER_DIR);
  186. usingDefault = true;
  187. } else {
  188. if(0)fprint(2, "homedir = %s\n", homedir);
  189. snprint(pg->cldir, sizeof pg->cldir, "%s/%s", homedir, CLASSIFIER_DIR);
  190. usingDefault = false;
  191. }
  192. /* ...then load the classifiers... */
  193. for (i = 0; i < NUM_RECS; i++) {
  194. int rec_return;
  195. char *s;
  196. rec_return = recognizer_load_state(pg->rec[i], pg->cldir, cl_name[i]);
  197. if ((rec_return == -1) && (usingDefault == false)) {
  198. if(0)fprint(2, "Unable to load custom classifier file %s/%s.\nTrying default classifier file instead.\nOriginal error: %s\n ",
  199. pg->cldir, cl_name[i],
  200. (s = recognizer_error(pg->rec[i])) ? s : "(none)");
  201. rec_return = recognizer_load_state(pg->rec[i],
  202. REC_DEFAULT_USER_DIR, cl_name[i]);
  203. }
  204. if (rec_return == -1) {
  205. fprint(2, "Unable to load default classifier file %s.\nOriginal error: %s\n",
  206. cl_name[i],
  207. (s = recognizer_error(pg->rec[i])) ? s : "(none)");
  208. return 0;
  209. }
  210. }
  211. /* We have recognizers and classifiers now. */
  212. /* Get the vector of LIextension functions.. */
  213. fns = recognizer_get_extension_functions(pg->rec[CS_LETTERS]);
  214. if (fns == nil) {
  215. fprint(2, "LI Recognizer Training:No extension functions!");
  216. return 0;
  217. }
  218. /* ... and make sure the training & get-classes functions are okay. */
  219. if( (pg->rec_train = (li_recognizer_train)fns[LI_TRAIN]) == nil ) {
  220. fprint(2,
  221. "LI Recognizer Training:li_recognizer_train() not found!");
  222. if (fns != nil) {
  223. free(fns);
  224. }
  225. return 0;
  226. }
  227. if( (pg->rec_getClasses = (li_recognizer_getClasses)fns[LI_GET_CLASSES]) == nil ) {
  228. fprint(2,
  229. "LI Recognizer Training:li_recognizer_getClasses() not found!");
  230. if (fns != nil) {
  231. free(fns);
  232. }
  233. return 0;
  234. }
  235. free(fns);
  236. return 1;
  237. }
  238. Scribble *
  239. scribblealloc(void)
  240. {
  241. Scribble *s;
  242. s = mallocz(sizeof(Scribble), 1);
  243. if (s == nil)
  244. sysfatal("Initialize: %r");
  245. s->curCharSet = CS_LETTERS;
  246. s->graf = mallocz(sizeof(struct graffiti), 1);
  247. if (s->graf == nil)
  248. sysfatal("Initialize: %r");
  249. graffiti_load_recognizers(s->graf);
  250. return s;
  251. }