PageRenderTime 79ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/micropolis-activity/src/tk/main.c

https://github.com/liu-chong/micropolis
C | 334 lines | 271 code | 27 blank | 36 comment | 61 complexity | 4a0f3c561884b51393fcce174b5d8631 MD5 | raw file
  1. /*
  2. * main.c --
  3. *
  4. * A simple program to test the toolkit facilities.
  5. *
  6. * Copyright 1990-1992 Regents of the University of California.
  7. * Permission to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose and without
  9. * fee is hereby granted, provided that the above copyright
  10. * notice appear in all copies. The University of California
  11. * makes no representations about the suitability of this
  12. * software for any purpose. It is provided "as is" without
  13. * express or implied warranty.
  14. */
  15. #ifndef lint
  16. static char rcsid[] = "$Header: /user6/ouster/wish/RCS/main.c,v 1.68 92/05/07 08:52:02 ouster Exp $ SPRITE (Berkeley)";
  17. #endif
  18. #include "tkconfig.h"
  19. #include "tkint.h"
  20. /*
  21. * Declarations for library procedures:
  22. */
  23. extern int isatty();
  24. /*
  25. * Command used to initialize wish:
  26. */
  27. char initCmd[] = "source $tk_library/wish.tcl";
  28. Tk_Window w; /* NULL means window has been deleted. */
  29. Tk_TimerToken timeToken = 0;
  30. int idleHandler = 0;
  31. Tcl_Interp *interp;
  32. int x, y;
  33. Tcl_CmdBuf buffer;
  34. int tty;
  35. extern int Tk_SquareCmd _ANSI_ARGS_((ClientData clientData,
  36. Tcl_Interp *interp, int argc, char **argv));
  37. /*
  38. * Information for testing out command-line options:
  39. */
  40. int synchronize = 0;
  41. char *fileName = NULL;
  42. char *name = NULL;
  43. char *display = NULL;
  44. char *geometry = NULL;
  45. Tk_ArgvInfo argTable[] = {
  46. {"-file", TK_ARGV_STRING, (char *) NULL, (char *) &fileName,
  47. "File from which to read commands"},
  48. {"-geometry", TK_ARGV_STRING, (char *) NULL, (char *) &geometry,
  49. "Initial geometry for window"},
  50. {"-display", TK_ARGV_STRING, (char *) NULL, (char *) &display,
  51. "Display to use"},
  52. {"-name", TK_ARGV_STRING, (char *) NULL, (char *) &name,
  53. "Name to use for application"},
  54. {"-sync", TK_ARGV_CONSTANT, (char *) 1, (char *) &synchronize,
  55. "Use synchronous mode for display server"},
  56. {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
  57. (char *) NULL}
  58. };
  59. /* ARGSUSED */
  60. void
  61. StdinProc(clientData, mask)
  62. ClientData clientData; /* Not used. */
  63. int mask;
  64. {
  65. char line[200];
  66. static int gotPartial = 0;
  67. char *cmd;
  68. int result;
  69. if (mask & TK_READABLE) {
  70. if (fgets(line, 200, stdin) == NULL) {
  71. if (!gotPartial) {
  72. if (tty) {
  73. Tcl_Eval(interp, "destroy .", 0, (char **) NULL);
  74. exit(0);
  75. } else {
  76. Tk_DeleteFileHandler(0);
  77. }
  78. return;
  79. } else {
  80. line[0] = 0;
  81. }
  82. }
  83. cmd = Tcl_AssembleCmd(buffer, line);
  84. if (cmd == NULL) {
  85. gotPartial = 1;
  86. return;
  87. }
  88. gotPartial = 0;
  89. result = Tcl_RecordAndEval(interp, cmd, 0);
  90. if (*interp->result != 0) {
  91. if ((result != TCL_OK) || (tty)) {
  92. printf("%s\n", interp->result);
  93. }
  94. }
  95. if (tty) {
  96. printf("wish: ");
  97. fflush(stdout);
  98. }
  99. }
  100. }
  101. /* ARGSUSED */
  102. static void
  103. StructureProc(clientData, eventPtr)
  104. ClientData clientData; /* Information about window. */
  105. XEvent *eventPtr; /* Information about event. */
  106. {
  107. if (eventPtr->type == DestroyNotify) {
  108. w = NULL;
  109. }
  110. }
  111. /*
  112. * Procedure to map initial window. This is invoked as a do-when-idle
  113. * handler. Wait for all other when-idle handlers to be processed
  114. * before mapping the window, so that the window's correct geometry
  115. * has been determined.
  116. */
  117. /* ARGSUSED */
  118. static void
  119. DelayedMap(clientData)
  120. ClientData clientData; /* Not used. */
  121. {
  122. while (Tk_DoOneEvent(TK_IDLE_EVENTS) != 0) {
  123. /* Empty loop body. */
  124. }
  125. if (w == NULL) {
  126. return;
  127. }
  128. Tk_MapWindow(w);
  129. }
  130. /* ARGSUSED */
  131. int
  132. DotCmd(dummy, interp, argc, argv)
  133. ClientData dummy; /* Not used. */
  134. Tcl_Interp *interp; /* Current interpreter. */
  135. int argc; /* Number of arguments. */
  136. char **argv; /* Argument strings. */
  137. {
  138. int x, y;
  139. if (argc != 3) {
  140. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  141. " x y\"", (char *) NULL);
  142. return TCL_ERROR;
  143. }
  144. x = strtol(argv[1], (char **) NULL, 0);
  145. y = strtol(argv[2], (char **) NULL, 0);
  146. Tk_MakeWindowExist(w);
  147. XDrawPoint(Tk_Display(w), Tk_WindowId(w),
  148. DefaultGCOfScreen(Tk_Screen(w)), x, y);
  149. return TCL_OK;
  150. }
  151. /* ARGSUSED */
  152. int
  153. MovetoCmd(dummy, interp, argc, argv)
  154. ClientData dummy; /* Not used. */
  155. Tcl_Interp *interp; /* Current interpreter. */
  156. int argc; /* Number of arguments. */
  157. char **argv; /* Argument strings. */
  158. {
  159. if (argc != 3) {
  160. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  161. " x y\"", (char *) NULL);
  162. return TCL_ERROR;
  163. }
  164. x = strtol(argv[1], (char **) NULL, 0);
  165. y = strtol(argv[2], (char **) NULL, 0);
  166. return TCL_OK;
  167. }
  168. /* ARGSUSED */
  169. int
  170. LinetoCmd(dummy, interp, argc, argv)
  171. ClientData dummy; /* Not used. */
  172. Tcl_Interp *interp; /* Current interpreter. */
  173. int argc; /* Number of arguments. */
  174. char **argv; /* Argument strings. */
  175. {
  176. int newX, newY;
  177. if (argc != 3) {
  178. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  179. " x y\"", (char *) NULL);
  180. return TCL_ERROR;
  181. }
  182. newX = strtol(argv[1], (char **) NULL, 0);
  183. newY = strtol(argv[2], (char **) NULL, 0);
  184. Tk_MakeWindowExist(w);
  185. XDrawLine(Tk_Display(w), Tk_WindowId(w),
  186. DefaultGCOfScreen(Tk_Screen(w)), x, y, newX, newY);
  187. x = newX;
  188. y = newY;
  189. return TCL_OK;
  190. }
  191. int
  192. main(argc, argv)
  193. int argc;
  194. char **argv;
  195. {
  196. char *args, *p, *msg;
  197. char buf[20];
  198. int result;
  199. Tk_3DBorder border;
  200. { extern char *TCL_Library, *TK_Library;
  201. extern int TK_CreateColormap;
  202. char *tcllib = getenv("TCL_LIBRARY");
  203. char *tklib = getenv("TK_LIBRARY");
  204. char *create = getenv("CREATE_COLORMAP");
  205. if (tklib != NULL)
  206. TK_Library = tklib;
  207. if (tcllib != NULL)
  208. TCL_Library = tcllib;
  209. if (create != NULL)
  210. TK_CreateColormap = 1;
  211. }
  212. interp = Tcl_CreateInterp();
  213. #ifdef TCL_MEM_DEBUG
  214. Tcl_InitMemory(interp);
  215. #endif
  216. if (Tk_ParseArgv(interp, (Tk_Window) NULL, &argc, argv, argTable, 0)
  217. != TCL_OK) {
  218. fprintf(stderr, "%s\n", interp->result);
  219. exit(1);
  220. }
  221. if (name == NULL) {
  222. if (fileName != NULL) {
  223. p = fileName;
  224. } else {
  225. p = argv[0];
  226. }
  227. name = strrchr(p, '/');
  228. if (name != NULL) {
  229. name++;
  230. } else {
  231. name = p;
  232. }
  233. }
  234. w = Tk_CreateMainWindow(interp, display, name);
  235. if (w == NULL) {
  236. fprintf(stderr, "%s\n", interp->result);
  237. exit(1);
  238. }
  239. Tk_SetClass(w, "Tk");
  240. Tk_CreateEventHandler(w, StructureNotifyMask, StructureProc,
  241. (ClientData) NULL);
  242. Tk_DoWhenIdle(DelayedMap, (ClientData) NULL);
  243. tty = isatty(0);
  244. args = Tcl_Merge(argc-1, argv+1);
  245. Tcl_SetVar(interp, "argv", args, TCL_GLOBAL_ONLY);
  246. ckfree(args);
  247. sprintf(buf, "%d", argc-1);
  248. Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY);
  249. if (synchronize) {
  250. XSynchronize(Tk_Display(w), True);
  251. }
  252. Tk_GeometryRequest(w, 200, 200);
  253. border = Tk_Get3DBorder(interp, w, None, "#4eee94");
  254. if (border == NULL) {
  255. Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  256. Tk_SetWindowBackground(w, WhitePixelOfScreen(Tk_Screen(w)));
  257. } else {
  258. Tk_SetBackgroundFromBorder(w, border);
  259. }
  260. XSetForeground(Tk_Display(w), DefaultGCOfScreen(Tk_Screen(w)),
  261. BlackPixelOfScreen(Tk_Screen(w)));
  262. Tcl_CreateCommand(interp, "dot", DotCmd, (ClientData) w,
  263. (void (*)()) NULL);
  264. Tcl_CreateCommand(interp, "lineto", LinetoCmd, (ClientData) w,
  265. (void (*)()) NULL);
  266. Tcl_CreateCommand(interp, "moveto", MovetoCmd, (ClientData) w,
  267. (void (*)()) NULL);
  268. #ifdef SQUARE_DEMO
  269. Tcl_CreateCommand(interp, "square", Tk_SquareCmd, (ClientData) w,
  270. (void (*)()) NULL);
  271. #endif
  272. if (geometry != NULL) {
  273. Tcl_SetVar(interp, "geometry", geometry, TCL_GLOBAL_ONLY);
  274. }
  275. result = Tcl_Eval(interp, initCmd, 0, (char **) NULL);
  276. if (result != TCL_OK) {
  277. goto error;
  278. }
  279. if (fileName != NULL) {
  280. result = Tcl_VarEval(interp, "source ", fileName, (char *) NULL);
  281. if (result != TCL_OK) {
  282. goto error;
  283. }
  284. tty = 0;
  285. } else {
  286. tty = isatty(0);
  287. Tk_CreateFileHandler(0, TK_READABLE, StdinProc, (ClientData) 0);
  288. if (tty) {
  289. printf("wish: ");
  290. }
  291. }
  292. fflush(stdout);
  293. buffer = Tcl_CreateCmdBuf();
  294. (void) Tcl_Eval(interp, "update", 0, (char **) NULL);
  295. Tk_MainLoop();
  296. Tcl_DeleteInterp(interp);
  297. Tcl_DeleteCmdBuf(buffer);
  298. exit(0);
  299. error:
  300. msg = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
  301. if (msg == NULL) {
  302. msg = interp->result;
  303. }
  304. fprintf(stderr, "%s\n", msg);
  305. Tcl_Eval(interp, "destroy .", 0, (char **) NULL);
  306. exit(1);
  307. }