SheafSystem  0.0.0.0
Screen.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // JNI code for Screen.java
19 
20 // See jawt.h in jdk for more information on what is being done here.
21 
22 #include "jawt_md.h"
23 #include <jni.h>
24 #include "vtkRenderWindow.h"
25 #include "vtkJavaUtil.h"
26 
27 
31 jlong cptr_to_jlong(void* p)
32 {
33  jlong pointer;
34  memset(&pointer, 0, sizeof(jlong)); // most likely 'pointer= 0;' works too
35  memcpy(&pointer, &p, sizeof(p));
36  return pointer;
37 }
38 
39 extern "C" JNIEXPORT jlong JNICALL
40  Java_tools_viewer_user_Screen_GetDisplay(JNIEnv* xenv,
41  jobject xcanvas)
42 {
43  // Get the AWT.
44 
45  JAWT awt;
46  awt.version = JAWT_VERSION_1_3;
47  if(JAWT_GetAWT(xenv, &awt) == JNI_FALSE)
48  {
49  return -1;
50  }
51 
52  // Get the drawing surface.
53 
54  JAWT_DrawingSurface* ds = awt.GetDrawingSurface(xenv, xcanvas);
55  if(ds == NULL)
56  {
57  std::cout << "Error getting drawing surface" << std::endl;
58  return -1;
59  }
60 
61  // Lock the drawing surface.
62 
63  jint lock = ds->Lock(ds)
64  ;
65  if((lock & JAWT_LOCK_ERROR) != 0)
66  {
67  std::cout << "Error locking drawing surface" << std::endl;
68  awt.FreeDrawingSurface(ds);
69  return -1;
70  }
71 
72  // Get the drawing surface info.
73 
74  JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
75  if(dsi == NULL)
76  {
77  std::cout << "Error getting surface info" << std::endl;
78  ds->Unlock(ds);
79  awt.FreeDrawingSurface(ds);
80  return -1;
81  }
82 
83  #if defined(_WIN32) || defined(WIN32)
84 
85  JAWT_Win32DrawingSurfaceInfo* dsi_win =
86  (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
87 
88  // Get the render window pointer.
89 
90  jlong result = cptr_to_jlong(dsi_win->hwnd);
91 
92 #else
93 
94  JAWT_X11DrawingSurfaceInfo* dsi_x11 =
95  (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
96 
97  // Get the render window pointer.
98 
99  jlong result = cptr_to_jlong(dsi_x11->display);
100 
101 #endif
102 
103  // Free the drawing surface info.
104 
105  ds->FreeDrawingSurfaceInfo(dsi);
106 
107  // Unlock the drawing surface.
108 
109  ds->Unlock(ds);
110 
111  // Free the drawing surface.
112 
113  awt.FreeDrawingSurface(ds);
114 
115  return result;
116 
117 }
118 
119 extern "C" JNIEXPORT jlong JNICALL
120  Java_tools_viewer_user_Screen_GetDrawable(JNIEnv* xenv,
121  jobject xcanvas)
122 {
123  // Get the AWT.
124 
125  JAWT awt;
126  awt.version = JAWT_VERSION_1_3;
127  if(JAWT_GetAWT(xenv, &awt) == JNI_FALSE)
128  {
129  return -1;
130  }
131 
132  // Get the drawing surface.
133 
134  JAWT_DrawingSurface* ds = awt.GetDrawingSurface(xenv, xcanvas);
135  if(ds == NULL)
136  {
137  return -1;
138  }
139 
140  // Lock the drawing surface.
141 
142  jint lock = ds->Lock(ds)
143  ;
144  if((lock & JAWT_LOCK_ERROR) != 0)
145  {
146  awt.FreeDrawingSurface(ds);
147  return -1;
148  }
149 
150  // Get the drawing surface info.
151 
152  JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
153  if(dsi == NULL)
154  {
155  cout << "Error getting surface info" << endl;
156  ds->Unlock(ds);
157  awt.FreeDrawingSurface(ds);
158  return -1;
159  }
160 
161 #if defined(_WIN32) || defined(WIN32)
162 
163  JAWT_Win32DrawingSurfaceInfo* dsi_win =
164  (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
165  jlong result = (jlong)dsi_win->hwnd;
166 
167 #else
168 
169  JAWT_X11DrawingSurfaceInfo* dsi_x11 =
170  (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
171 
172  // Get the render window pointer.
173  jlong result = dsi_x11->drawable;
174 
175 #endif
176 
177  // Free the drawing surface info.
178 
179  ds->FreeDrawingSurfaceInfo(dsi);
180 
181  // Unlock the drawing surface.
182 
183  ds->Unlock(ds);
184 
185  // Free the drawing surface.
186 
187  awt.FreeDrawingSurface(ds);
188 
189  return result;
190 
191 }
192 
193 // Lock must be called prior to render or anything which might
194 // cause vtkRenderWindow to make an XLib call or to call Render().
195 // The Lock() and UnLock() functions are necessary for drawing in
196 // JAWT, but they also provide a form of mutex locking so that multiple
197 // java threads are prevented from accessing X at the same time. The only
198 // requirement JAWT has is that all operations on a JAWT_DrawingSurface
199 // MUST be performed from the same thread as the call to GetDrawingSurface.
200 
201 extern "C" JNIEXPORT jlong JNICALL
202  Java_tools_viewer_user_Screen_Lock(JNIEnv* xenv,
203  jobject xcanvas)
204 {
205  // Get the AWT.
206 
207  JAWT awt;
208  awt.version = JAWT_VERSION_1_3;
209 
210  if(JAWT_GetAWT(xenv, &awt) == JNI_FALSE)
211  {
212  return 1;
213  }
214 
215  // Get the drawing surface.
216 
217  JAWT_DrawingSurface* ds = awt.GetDrawingSurface(xenv, xcanvas);
218 
219  if(ds == NULL)
220  {
221  return 1;
222  }
223 
224  // Lock the drawing surface.
225 
226  jint lock = ds->Lock(ds)
227  ;
228 
229  if((lock & JAWT_LOCK_ERROR) != 0)
230  {
231  awt.FreeDrawingSurface(ds);
232  return 1;
233  }
234 
235  return 0;
236 
237 }
238 
239 // UnLock() must be called after a Lock() and execution of a
240 // function which might change the drawing surface. See Lock().
241 
242 extern "C" JNIEXPORT jint JNICALL
243  Java_tools_viewer_user_Screen_UnLock(JNIEnv* xenv,
244  jobject xcanvas)
245 {
246  // Get the AWT.
247 
248  JAWT awt;
249  awt.version = JAWT_VERSION_1_3;
250 
251  if(JAWT_GetAWT(xenv, &awt) == JNI_FALSE)
252  {
253  return 1;
254  }
255 
256  // Get the drawing surface.
257 
258  JAWT_DrawingSurface* ds = awt.GetDrawingSurface(xenv, xcanvas);
259 
260  if(ds == NULL)
261  {
262  return 1;
263  }
264 
265  // Unlock the drawing surface.
266 
267  ds->Unlock(ds);
268 
269  // Free the drawing surface.
270 
271  awt.FreeDrawingSurface(ds);
272 
273  return 0;
274 }