PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/brlcad/branches/STABLE/src/mged/vrlink.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 304 lines | 188 code | 48 blank | 68 comment | 38 complexity | 0d42f1cd617f09c8462d496c15ce6e35 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /* V R L I N K . C
  2. * BRL-CAD
  3. *
  4. * Copyright (c) 1992-2012 United States Government as represented by
  5. * the U.S. Army Research Laboratory.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this file; see the file named COPYING for more
  18. * information.
  19. */
  20. /** @file mged/vrlink.c
  21. *
  22. */
  23. #include "common.h"
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include <string.h>
  28. #include "tcl.h"
  29. #include "vmath.h"
  30. #include "bu.h"
  31. #include "raytrace.h"
  32. #include "pkg.h"
  33. #include "./mged.h"
  34. #include "./mged_dm.h"
  35. extern int (*cmdline_hook)(); /* cmd.c */
  36. static struct pkg_conn *vrmgr; /* PKG connection to VR mgr */
  37. static char *vr_host = "None"; /* host running VR mgr */
  38. static char *tcp_port = "5555"; /* "gedd", remote mged */
  39. #define VRMSG_ROLE 1 /* from MGED: Identify role of machine */
  40. #define VRMSG_CMD 2 /* to MGED: Command to process */
  41. #define VRMSG_EVENT 3 /* from MGED: device event */
  42. #define VRMSG_POV 4 /* from MGED: point of view info */
  43. #define VRMSG_VLIST 5 /* transfer binary vlist block */
  44. #define VRMSG_CMD_REPLY 6 /* from MGED: reply to VRMSG_CMD */
  45. void ph_cmd(struct pkg_conn *pc, char *buf);
  46. void ph_vlist(struct pkg_conn *pc, char *buf);
  47. static struct pkg_switch pkgswitch[] = {
  48. { VRMSG_CMD, ph_cmd, "Command", NULL },
  49. { VRMSG_VLIST, ph_vlist, "Import vlist", NULL },
  50. { 0, 0, NULL, NULL }
  51. };
  52. /*
  53. * Called from cmdline() for now.
  54. * Returns -
  55. * !0 Print prompt for user. Should always be this.
  56. * 0 Don't print a prompt
  57. */
  58. int
  59. vr_event_hook(struct bu_vls *vp)
  60. {
  61. BU_CK_VLS(vp);
  62. if (vrmgr == PKC_NULL) {
  63. cmdline_hook = 0; /* Relinquish this hook */
  64. return 1;
  65. }
  66. if (pkg_send_vls(VRMSG_EVENT, vp, vrmgr) < 0) {
  67. bu_log("event: pkg_send VRMSG_EVENT failed, disconnecting\n");
  68. pkg_close(vrmgr);
  69. vrmgr = PKC_NULL;
  70. cmdline_hook = 0; /* Relinquish this hook */
  71. }
  72. return 1;
  73. }
  74. /*
  75. * Called from the Tk event handler
  76. */
  77. void
  78. vr_input_hook(void)
  79. {
  80. int val;
  81. val = pkg_suckin(vrmgr);
  82. if (val < 0) {
  83. bu_log("pkg_suckin() error\n");
  84. } else if (val == 0) {
  85. bu_log("vrmgr sent us an EOF\n");
  86. }
  87. if (val <= 0) {
  88. Tcl_DeleteFileHandler(vrmgr->pkc_fd);
  89. pkg_close(vrmgr);
  90. vrmgr = PKC_NULL;
  91. return;
  92. }
  93. if (pkg_process(vrmgr) < 0)
  94. bu_log("vrmgr: pkg_process error encountered\n");
  95. }
  96. /*
  97. * Called from refresh().
  98. */
  99. void
  100. vr_viewpoint_hook(void)
  101. {
  102. struct bu_vls str = BU_VLS_INIT_ZERO;
  103. static struct bu_vls *old_str = NULL;
  104. quat_t orient;
  105. if (vrmgr == PKC_NULL) {
  106. cmdline_hook = 0; /* Relinquish this hook */
  107. return;
  108. }
  109. if (!old_str) {
  110. BU_GET(old_str, struct bu_vls);
  111. bu_vls_init(old_str);
  112. }
  113. quat_mat2quat(orient, view_state->vs_gvp->gv_rotation);
  114. /* Need to send current viewpoint to VR mgr */
  115. /* XXX more will be needed */
  116. /* Eye point, quaternion for orientation */
  117. bu_vls_printf(&str, "pov {%e %e %e} {%e %e %e %e} %e {%e %e %e} %e\n",
  118. -view_state->vs_gvp->gv_center[MDX],
  119. -view_state->vs_gvp->gv_center[MDY],
  120. -view_state->vs_gvp->gv_center[MDZ],
  121. V4ARGS(orient),
  122. view_state->vs_gvp->gv_scale,
  123. V3ARGS(view_state->vs_gvp->gv_eye_pos),
  124. view_state->vs_gvp->gv_perspective);
  125. if (bu_vls_strcmp(old_str, &str) == 0) {
  126. bu_vls_free(&str);
  127. return;
  128. }
  129. if (pkg_send_vls(VRMSG_POV, &str, vrmgr) < 0) {
  130. bu_log("viewpoint: pkg_send VRMSG_POV failed, disconnecting\n");
  131. pkg_close(vrmgr);
  132. vrmgr = PKC_NULL;
  133. viewpoint_hook = 0; /* Relinquish this hook */
  134. }
  135. bu_vls_trunc(old_str, 0);
  136. bu_vls_vlscat(old_str, &str);
  137. bu_vls_free(&str);
  138. }
  139. /*
  140. * F _ V R M G R
  141. *
  142. * Establish a network link to the VR manager, using libpkg.
  143. *
  144. * Syntax: vrmgr host role
  145. */
  146. int
  147. f_vrmgr(ClientData UNUSED(clientData), Tcl_Interp *interp, int argc, const char *argv[])
  148. {
  149. struct bu_vls str = BU_VLS_INIT_ZERO;
  150. const char *role;
  151. if (argc < 3) {
  152. bu_vls_printf(&str, "help vrmgr");
  153. Tcl_Eval(interp, bu_vls_addr(&str));
  154. bu_vls_free(&str);
  155. return TCL_ERROR;
  156. }
  157. if (vrmgr != PKC_NULL) {
  158. Tcl_AppendResult(interp, "Closing link to VRmgr ",
  159. vr_host, "\n", (char *)NULL);
  160. pkg_close(vrmgr);
  161. vrmgr = PKC_NULL;
  162. vr_host = "none";
  163. }
  164. vr_host = bu_strdup(argv[1]);
  165. role = argv[2];
  166. if (BU_STR_EQUAL(role, "master")) {
  167. } else if (BU_STR_EQUAL(role, "slave")) {
  168. } else if (BU_STR_EQUAL(role, "overview")) {
  169. } else {
  170. Tcl_AppendResult(interp, "role '", role, "' unknown, must be master/slave/overview\n",
  171. (char *)NULL);
  172. return TCL_ERROR;
  173. }
  174. vrmgr = pkg_open(vr_host, tcp_port, "tcp", "", "",
  175. pkgswitch, NULL);
  176. if (vrmgr == PKC_ERROR) {
  177. Tcl_AppendResult(interp, "mged/f_vrmgr: unable to contact ", vr_host,
  178. ", port ", tcp_port, "\n", (char *)NULL);
  179. vrmgr = PKC_NULL;
  180. return TCL_ERROR;
  181. }
  182. bu_vls_from_argv(&str, argc-2, (const char **)argv+2);
  183. /* Send initial message declaring our role */
  184. if (pkg_send_vls(VRMSG_ROLE, &str, vrmgr) < 0) {
  185. Tcl_AppendResult(interp, "pkg_send VRMSG_ROLE failed, disconnecting\n", (char *)NULL);
  186. pkg_close(vrmgr);
  187. vrmgr = NULL;
  188. return TCL_ERROR;
  189. }
  190. /* Establish appropriate hooks */
  191. if (BU_STR_EQUAL(role, "master")) {
  192. viewpoint_hook = vr_viewpoint_hook;
  193. } else if (BU_STR_EQUAL(role, "slave")) {
  194. cmdline_hook = vr_event_hook;
  195. } else if (BU_STR_EQUAL(role, "overview")) {
  196. /* No hooks required, just listen */
  197. }
  198. Tcl_CreateFileHandler(vrmgr->pkc_fd, TCL_READABLE,
  199. (Tcl_FileProc (*))vr_input_hook, (ClientData)NULL);
  200. bu_vls_free(&str);
  201. return TCL_OK;
  202. }
  203. /*
  204. * P H _ C M D
  205. *
  206. * Package handler for incomming commands. Do whatever he says,
  207. * and send a reply back.
  208. */
  209. void
  210. ph_cmd(struct pkg_conn *pc, char *buf)
  211. {
  212. int status;
  213. const char *result;
  214. #define CMD_BUFSIZE 1024
  215. char buffer[CMD_BUFSIZE] = {0};
  216. status = Tcl_Eval(INTERP, buf);
  217. result = Tcl_GetStringResult(INTERP);
  218. snprintf(buffer, CMD_BUFSIZE, "%s", result);
  219. if (pkg_2send(VRMSG_CMD_REPLY,
  220. (status == TCL_OK) ? "Y" : "N", 1,
  221. buffer, CMD_BUFSIZE, pc) < 0) {
  222. bu_log("ph_cmd: pkg_2send reply to vrmgr failed, disconnecting\n");
  223. pkg_close(vrmgr);
  224. vrmgr = PKC_NULL;
  225. cmdline_hook = 0; /* Relinquish this hook */
  226. }
  227. if (buf) (void)free(buf);
  228. }
  229. /*
  230. * P H _ V L I S T
  231. *
  232. * Package handler for incomming vlist.
  233. * Install whatever phantom solids he wants.
  234. */
  235. void
  236. ph_vlist(struct pkg_conn *UNUSED(pc), char *buf)
  237. {
  238. struct bu_list vhead;
  239. struct bu_vls name = BU_VLS_INIT_ZERO;
  240. if(buf == NULL)
  241. bu_bomb("ph_vlist: buf is null");
  242. BU_LIST_INIT(&vhead);
  243. rt_vlist_import(&vhead, &name, (unsigned char *)buf);
  244. invent_solid(bu_vls_addr(&name), &vhead, 0x0000FF00L, 0);
  245. bu_vls_free(&name);
  246. if (buf) (void)free(buf);
  247. }
  248. /*
  249. * Local Variables:
  250. * mode: C
  251. * tab-width: 8
  252. * indent-tabs-mode: t
  253. * c-file-style: "stroustrup"
  254. * End:
  255. * ex: shiftwidth=4 tabstop=8
  256. */