1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
|
/**
* Copyright 2019 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
#import <inttypes.h>
#import "jogamp_newt_driver_ios_WindowDriver.h"
#import "IOSNewtUIWindow.h"
#import "MouseEvent.h"
#import "KeyEvent.h"
#import "ScreenMode.h"
#import <stdio.h>
#ifdef DBG_PERF
#include "timespec.h"
#endif
static const char * const ClazzNamePoint = "com/jogamp/nativewindow/util/Point";
static const char * const ClazzAnyCstrName = "<init>";
static const char * const ClazzNamePointCstrSignature = "(II)V";
static jclass pointClz = NULL;
static jmethodID pointCstr = NULL;
static NSString* jstringToNSString(JNIEnv* env, jstring jstr)
{
const jchar* jstrChars = (*env)->GetStringChars(env, jstr, NULL);
NSString* str = [[NSString alloc] initWithCharacters: jstrChars length: (*env)->GetStringLength(env, jstr)];
(*env)->ReleaseStringChars(env, jstr, jstrChars);
return str;
}
static void setWindowClientTopLeftPoint(NewtUIWindow* mWin, jint x, jint y, BOOL doDisplay) {
DBG_PRINT( "setWindowClientTopLeftPoint.0 - window: %p %d/%d, display %d\n", mWin, (int)x, (int)y, (int)doDisplay);
CGPoint pS = CGPointMake(x, y);
CGRect rect = [mWin frame];
rect.origin = pS;
[mWin setFrame: rect];
DBG_PRINT( "setWindowClientTopLeftPoint.X: %d/%d\n", (int)pS.x, (int)pS.y);
if( doDisplay ) {
[mWin setNeedsDisplay];
}
}
static void setWindowClientTopLeftPointAndSize(NewtUIWindow* mWin, jint x, jint y, jint width, jint height, BOOL doDisplay) {
DBG_PRINT( "setWindowClientTopLeftPointAndSize.0 - window: %p %d/%d %dx%d, display %d\n", mWin, (int)x, (int)y, (int)width, (int)height, (int)doDisplay);
CGRect rect = CGRectMake(x, y, width, height);
DBG_PRINT( "setWindowClientTopLeftPointAndSize.1: %d/%d %dx%d\n", (int)rect.origin.x, (int)rect.origin.y, (int)rect.size.width, (int)rect.size.height);
[mWin setFrame: rect];
if( doDisplay ) {
[mWin setNeedsDisplay];
}
DBG_PRINT( "setWindowClientTopLeftPointAndSize.X: %d/%d %dx%d\n", (int)rect.origin.x, (int)rect.origin.y, (int)rect.size.width, (int)rect.size.height);
// if( doDisplay ) {
// [mWin setNeedsDisplay];
// }
}
#ifdef VERBOSE_ON
static int getRetainCount(NSObject * obj) {
return ( NULL == obj ) ? -1 : (int)([obj retainCount]) ;
}
#endif
static void setJavaWindowObject(JNIEnv *env, jobject newJavaWindowObject, NewtUIView *view) {
DBG_PRINT( "setJavaWindowObject.0: View %p\n", view);
if( NULL == newJavaWindowObject ) {
// disable
jobject globJavaWindowObject = [view getJavaWindowObject];
if( NULL != globJavaWindowObject ) {
DBG_PRINT( "setJavaWindowObject.1: View %p - Clear old javaWindowObject %p\n", view, globJavaWindowObject);
(*env)->DeleteGlobalRef(env, globJavaWindowObject);
[view setJavaWindowObject: NULL];
}
} else {
// enable
DBG_PRINT( "setJavaWindowObject.2: View %p - Set new javaWindowObject %p\n", view, newJavaWindowObject);
jobject globJavaWindowObject = (*env)->NewGlobalRef(env, newJavaWindowObject);
[view setJavaWindowObject: globJavaWindowObject];
}
DBG_PRINT( "setJavaWindowObject.X: View %p\n", view);
}
static void changeContentView(JNIEnv *env, jobject javaWindowObject, UIView *pview, NewtUIWindow *win, NewtUIView *newView, BOOL setJavaWindow) {
NewtUIView* oldNewtUIView = [win getContentNewtUIView];
#ifdef VERBOSE_ON
int dbgIdx = 1;
#endif
DBG_PRINT( "changeContentView.%d win %p, view (%p,%d -> %p,%d), parent view %p\n",
dbgIdx++, win, oldNewtUIView, getRetainCount(oldNewtUIView), newView, getRetainCount(newView), pview);
if( NULL != oldNewtUIView ) {
[oldNewtUIView setDestroyNotifySent: false];
setJavaWindowObject(env, NULL, oldNewtUIView);
[win setContentNewtUIView: NULL]; // includes [oldNewtUIView removeFromSuperview];
}
DBG_PRINT( "changeContentView.%d win %p, view (%p,%d -> %p,%d), isHidden %d\n",
dbgIdx++, win, oldNewtUIView, getRetainCount(oldNewtUIView), newView, getRetainCount(newView), [newView isHidden]);
if( NULL!=newView ) {
[newView setDestroyNotifySent: false];
if( setJavaWindow ) {
setJavaWindowObject(env, javaWindowObject, newView);
}
DBG_PRINT( "changeContentView.%d win %p, view (%p,%d -> %p,%d)\n",
dbgIdx++, win, oldNewtUIView, getRetainCount(oldNewtUIView), newView, getRetainCount(newView));
[win setContentNewtUIView: newView]; // includes [win addSubview: newView];
// FIXME: Done in initWindow0: [pview addSubview: newView]
DBG_PRINT( "changeContentView.%d win %p, view (%p,%d -> %p,%d), isHidden %d\n",
dbgIdx++, win, oldNewtUIView, getRetainCount(oldNewtUIView), newView, getRetainCount(newView), [newView isHidden]);
}
DBG_PRINT( "changeContentView.%d win %p, view (%p,%d -> %p,%d), isHidden %d\n",
dbgIdx++, win, oldNewtUIView, getRetainCount(oldNewtUIView), newView, getRetainCount(newView), [newView isHidden]);
// make sure the insets are updated in the java object
[win updateInsets: env jwin:javaWindowObject];
DBG_PRINT( "changeContentView.X win %p, view (%p,%d -> %p,%d)\n",
win, oldNewtUIView, getRetainCount(oldNewtUIView), newView, getRetainCount(newView));
}
/*
* Class: jogamp_newt_driver_ios_DisplayDriver
* Method: initIDs
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_ios_DisplayDriver_initUIApplication0
(JNIEnv *env, jclass clazz)
{
static int initialized = 0;
if(initialized) return JNI_TRUE;
initialized = 1;
NewtCommon_init(env);
// Initialize the shared NSApplication instance
[UIApplication sharedApplication];
// Need this when debugging, as it is necessary to attach gdb to
// the running java process -- "gdb java" doesn't work
// printf("Going to sleep for 10 seconds\n");
// sleep(10);
return (jboolean) JNI_TRUE;
}
static void NewtScreen_dump() {
#ifdef VERBOSE_ON
NSArray *screens = [UIScreen screens];
int i;
for(i=0; i<[screens count]; i++) {
UIScreen * screen = (UIScreen *) [screens objectAtIndex: i];
CGRect screenPxFrame = [screen nativeBounds];
CGRect screenPtFrame = [screen bounds];
CGFloat pixelScale = [screen scale];
CGFloat nativeScale = [screen nativeScale];
int depth = 32; // [screen depth]; // an (int) value!
DBG_PRINT( "UIScreen #%d (%p): Frame %lf/%lf %lfx%lf px (%lf/%lf %lfx%lf pt), scale %lf (%lf native), depth %d\n",
i, screen,
screenPxFrame.origin.x, screenPxFrame.origin.y, screenPxFrame.size.width, screenPxFrame.size.height,
screenPtFrame.origin.x, screenPtFrame.origin.y, screenPtFrame.size.width, screenPtFrame.size.height,
pixelScale, nativeScale, depth);
}
#endif
}
// Duplicate each Mode by all possible rotations (4):
// For each real-mode: [mode, 0], [mode, 90], [mode, 180], [mode, 270]
#define ROTMODES_PER_REALMODE 1
/*
* Class: jogamp_newt_driver_ios_ScreenDriver
* Method: getMonitorDeviceIds0
* Signature: ()I
*/
JNIEXPORT jintArray JNICALL Java_jogamp_newt_driver_ios_ScreenDriver_getMonitorDeviceIds0
(JNIEnv *env, jobject obj)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray *screens = [UIScreen screens];
int count = [screens count];
int32_t displayIDs[count];
int i;
for(i=0; i<count; i++) {
// UIScreen * screen = (UIScreen *) [screens objectAtIndex: i];
displayIDs[i] = i; // TODO no unique screen name?
}
jintArray properties = (*env)->NewIntArray(env, count);
if (properties == NULL) {
NewtCommon_throwNewRuntimeException(env, "Could not allocate int array of size %d", count);
}
(*env)->SetIntArrayRegion(env, properties, 0, count, displayIDs);
[pool release];
return properties;
}
/*
* Class: jogamp_newt_driver_ios_ScreenDriver
* Method: getMonitorProps0
* Signature: (I)[I
*/
JNIEXPORT jintArray JNICALL Java_jogamp_newt_driver_ios_ScreenDriver_getMonitorProps0
(JNIEnv *env, jobject obj, jint crt_id)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
#ifdef DBG_PERF
struct timespec t0, t1, td;
long td_ms;
timespec_now(&t0);
#endif
#ifdef DBG_PERF
timespec_now(&t1); timespec_subtract(&td, &t1, &t0); td_ms = timespec_milliseconds(&td);
fprintf(stderr, "MacScreen_getMonitorProps0.1: %ld ms\n", td_ms); fflush(NULL);
#endif
NSArray *screens = [UIScreen screens];
// int count = [screens count];
UIScreen * screen = (UIScreen *) [screens objectAtIndex: crt_id];
if( NULL == screen ) {
[pool release];
return NULL;
}
BOOL isPrimary = 0 == crt_id;
#ifdef DBG_PERF
timespec_now(&t1); timespec_subtract(&td, &t1, &t0); td_ms = timespec_milliseconds(&td);
fprintf(stderr, "MacScreen_getMonitorProps0.2: %ld ms\n", td_ms); fflush(NULL);
#endif
// UIScreenMode * screenMode = [screen currentMode];
CGSize sizeMM = CGSizeMake(161.0, 228.0); // TODO ???
#ifdef DBG_PERF
timespec_now(&t1); timespec_subtract(&td, &t1, &t0); td_ms = timespec_milliseconds(&td);
fprintf(stderr, "MacScreen_getMonitorProps0.3: %ld ms\n", td_ms); fflush(NULL);
#endif
CGRect bounds = [screen bounds];
#ifdef VERBOSE_ON
{
CGRect nativeBounds = [screen nativeBounds];
CGFloat nativeScale = [screen nativeScale];
CGFloat scale = [screen scale];
DBG_PRINT( "getMonitorProps0: crt_id 0x%X (prim %d) %p[\n native %f/%f %fx%f scale %f; logical %f/%f %fx%f scale %f]\n",
(int)crt_id, isPrimary, screen,
nativeBounds.origin.x, nativeBounds.origin.y, nativeBounds.size.width, nativeBounds.size.height, nativeScale,
bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height, scale);
}
#endif
jsize propCount = MIN_MONITOR_DEVICE_PROPERTIES - 1 - NUM_MONITOR_MODE_PROPERTIES;
jint prop[ propCount ];
int offset = 0;
prop[offset++] = propCount;
prop[offset++] = crt_id;
prop[offset++] = 0; // isClone
prop[offset++] = isPrimary ? 1 : 0; // isPrimary
prop[offset++] = (jint) sizeMM.width;
prop[offset++] = (jint) sizeMM.height;
prop[offset++] = (jint) bounds.origin.x; // rotated viewport x (pixel units, will be fixed in java code)
prop[offset++] = (jint) bounds.origin.y; // rotated viewport y (pixel units, will be fixed in java code)
prop[offset++] = (jint) bounds.size.width; // rotated viewport width (pixel units, will be fixed in java code)
prop[offset++] = (jint) bounds.size.height; // rotated viewport height (pixel units, will be fixed in java code)
prop[offset++] = (jint) bounds.origin.x; // rotated viewport x (window units, will be fixed in java code)
prop[offset++] = (jint) bounds.origin.y; // rotated viewport y (window units, will be fixed in java code)
prop[offset++] = (jint) bounds.size.width; // rotated viewport width (window units, will be fixed in java code)
prop[offset++] = (jint) bounds.size.height; // rotated viewport height (window units, will be fixed in java code)
jintArray properties = (*env)->NewIntArray(env, propCount);
if (properties == NULL) {
NewtCommon_throwNewRuntimeException(env, "Could not allocate int array of size %d", propCount);
}
(*env)->SetIntArrayRegion(env, properties, 0, propCount, prop);
[pool release];
return properties;
}
/*
* Class: jogamp_newt_driver_ios_ScreenDriver
* Method: getMonitorMode0
* Signature: (II)[I
*/
JNIEXPORT jintArray JNICALL Java_jogamp_newt_driver_ios_ScreenDriver_getMonitorMode0
(JNIEnv *env, jobject obj, jint crt_id, jint mode_idx)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray<UIScreen*> *screens = [UIScreen screens];
// int count = [screens count];
UIScreen * screen = (UIScreen *) [screens objectAtIndex: crt_id];
if( NULL == screen ) {
[pool release];
return NULL;
}
NSArray<UIScreenMode*> *availableModes = [screen availableModes];
int numberOfAvailableModes = [availableModes count];
CFIndex numberOfAvailableModesRots = ROTMODES_PER_REALMODE * numberOfAvailableModes;
int currentCCWRot = 0;
jint ccwRot = 0;
int nativeId = 0;
UIScreenMode * mode = NULL;
#ifdef VERBOSE_ON
if(0 >= mode_idx) {
// only for current mode (-1) and first mode (scanning)
CGFloat pixelScale = [screen scale];
DBG_PRINT( "getScreenMode0: crtID 0x%X (s %p, pscale %lf), mode %d, avail: %d/%d, current rot %d ccw\n",
crt_id, screen, pixelScale, (int)mode_idx, (int)numberOfAvailableModes, (int)numberOfAvailableModesRots, currentCCWRot);
}
#endif
if(numberOfAvailableModesRots<=mode_idx) {
// n/a - end of modes
DBG_PRINT( "getScreenMode0: end of modes: mode %d, avail: %d/%d\n",
(int)mode_idx, (int)numberOfAvailableModes, (int)numberOfAvailableModesRots);
[pool release];
return NULL;
} else if(-1 < mode_idx) {
// only at initialization time, where index >= 0
nativeId = mode_idx / ROTMODES_PER_REALMODE;
ccwRot = mode_idx % ROTMODES_PER_REALMODE * 90;
mode = (UIScreenMode*) [availableModes objectAtIndex: nativeId];
} else {
// current mode
mode = [screen currentMode];
ccwRot = 0;
nativeId = 0;
}
// mode = CGDisplayModeRetain(mode); // 10.6 on CGDisplayModeRef
CGSize mSize = [mode size];
int mWidth = (int)mSize.width;
int mHeight = (int)mSize.height;
// swap width and height, since OSX reflects rotated dimension, we don't
if ( 90 == currentCCWRot || 270 == currentCCWRot ) {
int tempWidth = mWidth;
mWidth = mHeight;
mHeight = tempWidth;
}
jint prop[ NUM_MONITOR_MODE_PROPERTIES_ALL ];
int propIndex = 0;
int refreshRate = 60; // TODO
int fRefreshRate = ( 0 < refreshRate ) ? refreshRate : 60; // default .. (experienced on OSX 10.6.8)
prop[propIndex++] = NUM_MONITOR_MODE_PROPERTIES_ALL;
prop[propIndex++] = mWidth;
prop[propIndex++] = mHeight;
prop[propIndex++] = 32; // TODO CGDDGetModeBitsPerPixel(mode);
prop[propIndex++] = fRefreshRate * 100; // Hz*100
prop[propIndex++] = 0; // flags
prop[propIndex++] = nativeId;
prop[propIndex++] = ccwRot;
DBG_PRINT( "getScreenMode0: Mode %d/%d (%d): %dx%d, %d bpp, %d / %d Hz, nativeId %d, rot %d ccw\n",
(int)mode_idx, (int)numberOfAvailableModesRots, (int)numberOfAvailableModes,
(int)prop[1], (int)prop[2], (int)prop[3],
(int)prop[4], refreshRate, (int)prop[6], (int)prop[7]);
jintArray properties = (*env)->NewIntArray(env, NUM_MONITOR_MODE_PROPERTIES_ALL);
if (properties == NULL) {
NewtCommon_throwNewRuntimeException(env, "Could not allocate int array of size %d", NUM_MONITOR_MODE_PROPERTIES_ALL);
}
(*env)->SetIntArrayRegion(env, properties, 0, NUM_MONITOR_MODE_PROPERTIES_ALL, prop);
// CGDisplayModeRelease(mode); // 10.6 on CGDisplayModeRef
[pool release];
return properties;
}
/*
* Class: jogamp_newt_driver_ios_ScreenDriver
* Method: setMonitorMode0
* Signature: (III)Z
*/
JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_ios_ScreenDriver_setMonitorMode0
(JNIEnv *env, jobject object, jint crt_id, jint nativeId, jint ccwRot)
{
return false;
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: initIDs
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_ios_WindowDriver_initIDs0
(JNIEnv *env, jclass clazz)
{
static int initialized = 0;
if(initialized) return JNI_TRUE;
initialized = 1;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NewtScreen_dump();
jclass c;
c = (*env)->FindClass(env, ClazzNamePoint);
if(NULL==c) {
NewtCommon_FatalError(env, "FatalError Java_jogamp_newt_driver_ios_WindowDriver_initIDs0: can't find %s", ClazzNamePoint);
}
pointClz = (jclass)(*env)->NewGlobalRef(env, c);
(*env)->DeleteLocalRef(env, c);
if(NULL==pointClz) {
NewtCommon_FatalError(env, "FatalError Java_jogamp_newt_driver_ios_WindowDriver_initIDs0: can't use %s", ClazzNamePoint);
}
pointCstr = (*env)->GetMethodID(env, pointClz, ClazzAnyCstrName, ClazzNamePointCstrSignature);
if(NULL==pointCstr) {
NewtCommon_FatalError(env, "FatalError Java_jogamp_newt_driver_ios_WindowDriver_initIDs0: can't fetch %s.%s %s",
ClazzNamePoint, ClazzAnyCstrName, ClazzNamePointCstrSignature);
}
// Need this when debugging, as it is necessary to attach gdb to
// the running java process -- "gdb java" doesn't work
// printf("Going to sleep for 10 seconds\n");
// sleep(10);
BOOL res = [NewtUIWindow initNatives: env forClass: clazz];
[pool release];
return (jboolean) res;
}
#ifdef VERBOSE_ON
#define DBG_PRINT_CREATEWIN1(n) NSLog(@"createWindow1.%d - parent(win %p scale %f, view %p scale %f), window %p (scale %f, superview %p, hidden %d), view: %p (scale %f, superview %p, hidden %d), CAEAGLLayer %p (opaque %d, scale %f, isCAEAGLLayer %d, hidden %d)\n", \
(n), parentWindow, (NULL!=parentWindow?[parentWindow contentScaleFactor]:0.0f), parentView, (NULL!=parentView?[parentView contentScaleFactor]:0.0f), \
myWindow, [myWindow contentScaleFactor], [myWindow superview], [myWindow isHidden], \
myView, [myView contentScaleFactor], [myView superview], [myView isHidden], \
l, [l isOpaque], [l contentsScale], [l isKindOfClass:[CAEAGLLayer class]], [l isHidden]); fflush(stderr)
#else
#define DBG_PRINT_CREATEWIN1(n)
#endif
/**
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: createWindow1
* Signature: (JJIIIIFZZZZJ)J
*/
JNIEXPORT jlong JNICALL Java_jogamp_newt_driver_ios_WindowDriver_createWindow1
(JNIEnv *env, jobject jthis, jlong joldwin, jlong jparent, jint x, jint y, jint w, jint h, jfloat reqPixelScale,
jboolean fullscreen, jint styleMask, jint bufferingType,
jboolean opaque, jboolean atop, jboolean abottom, jboolean visible, jlong jview)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[CATransaction begin];
NewtUIWindow* oldWindow = (NewtUIWindow*) (intptr_t) joldwin ;
NewtUIView* myView = (NewtUIView*) (intptr_t) jview ;
NSObject* nsParentObj = (NSObject*) ((intptr_t) jparent);
UIWindow* parentWindow = NULL;
UIView* parentView = NULL;
if( nsParentObj != NULL && [nsParentObj isKindOfClass:[UIWindow class]] ) {
parentWindow = (UIWindow*) nsParentObj;
parentView = (UIView*)nsParentObj;
DBG_PRINT( "createWindow1 - Parent is UIWindow : %p (win) -> %p (view) \n", parentWindow, parentView);
} else if( nsParentObj != NULL && [nsParentObj isKindOfClass:[UIView class]] ) {
parentView = (UIView*) nsParentObj;
parentWindow = [parentView window];
DBG_PRINT( "createWindow1 - Parent is UIView : %p -(view) > %p (win) \n", parentView, parentWindow);
} else {
DBG_PRINT( "createWindow1 - Parent is neither UIWindow nor UIView : %p\n", nsParentObj);
}
DBG_PRINT( "createWindow1.0 - %p (this), oldWin %p, parent(win %p, view %p), oldView %p, %d/%d %dx%d, reqPixelScale %f, fullscreen %d, opaque %d, atop %d, abottom %d, visible %d (START)\n",
(void*)(intptr_t)jthis, oldWindow, parentWindow, parentView, myView, (int)x, (int)y, (int)w, (int)h, (float)reqPixelScale,
fullscreen, opaque, atop, abottom, visible);
if( NULL != oldWindow ) {
if( NULL == myView ) {
NewtCommon_throwNewRuntimeException(env, "oldWindow %p given but no view %p (this %p)", oldWindow, myView, (void*)(intptr_t)jthis);
}
changeContentView(env, jthis, parentView, oldWindow, NULL, NO);
Java_jogamp_newt_driver_ios_WindowDriver_close0(env, NULL, joldwin);
} else if( NULL != myView ) {
NewtCommon_throwNewRuntimeException(env, "view %p given but no oldWindow %p (this %p)", oldWindow, myView, (void*)(intptr_t)jthis);
}
CGRect rectWin = CGRectMake(x, y, w, h);
NewtUIWindow* myWindow = [[[[NewtUIWindow alloc] initWithFrame: rectWin
styleMask: (NSUInteger) styleMask
backing: 0 // TODO (NSBackingStoreType) bufferingType
defer: YES
isFullscreenWindow: fullscreen] autorelease] retain];
if(opaque) {
[myWindow setOpaque: YES];
#ifdef VERBOSE_ON
[myWindow setBackgroundColor: [UIColor redColor]];
#endif
} else {
[myWindow setOpaque: NO];
[myWindow setBackgroundColor: [UIColor clearColor]];
}
if( visible ) {
// Only if calling this before adding the view, the view receives touch events.
// Another 'funny' iOS API nightmare?
[myWindow makeKeyAndVisible];
}
DBG_PRINT( "createWindow1.1 - window %p, isHidden %d, rootViewController %p\n", myWindow, [myWindow isHidden], myWindow.rootViewController);
if( NULL == myView ) {
CGRect rectView = CGRectMake(0, 0, w, h);
myView = [[NewtUIView alloc] initWithFrame: rectView] ;
}
CAEAGLLayer* l = (CAEAGLLayer*)[myView layer];
if(opaque) {
[myView setOpaque: YES];
[l setOpaque: YES];
} else {
[myView setOpaque: NO];
[myView setBackgroundColor: [UIColor clearColor]];
[l setOpaque: NO];
[l setBackgroundColor: [UIColor clearColor]];
}
DBG_PRINT_CREATEWIN1(2);
changeContentView(env, jthis, parentView, myWindow, myView, NO);
DBG_PRINT_CREATEWIN1(3);
if(NULL!=parentWindow) {
[myWindow attachToParent: parentWindow];
}
DBG_PRINT_CREATEWIN1(4);
// Immediately re-position this window based on an upper-left coordinate system
setWindowClientTopLeftPointAndSize(myWindow, x, y, w, h, NO);
// SEE comment above at makeKeyAndVisible call
// if( visible ) {
// [myWindow makeKeyAndVisible];
// }
DBG_PRINT_CREATEWIN1(5);
[myView setDestroyNotifySent: false];
setJavaWindowObject(env, jthis, myView);
DBG_PRINT_CREATEWIN1(6);
[myWindow setPixelScale: (CGFloat)reqPixelScale defer:NO];
DBG_PRINT_CREATEWIN1(88);
[CATransaction commit];
[pool release];
return (jlong) (intptr_t) myWindow;
}
JNIEXPORT jint JNICALL Java_jogamp_newt_driver_ios_WindowDriver_getDisplayID0(JNIEnv *env, jobject jthis, jlong window) {
NewtUIWindow* myWindow = (NewtUIWindow*) ((intptr_t) window);
if( NULL == myWindow ) {
DBG_PRINT( "getDisplayID0 - NULL NEWT win - abort\n");
return 0;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// UIScreen *screen = [myWindow screen];
int32_t displayID = 0; // TODO (int32_t)NewtScreen_getCGDirectDisplayIDByUIScreen(screen);
[pool release];
return (jint) displayID;
}
/**
* Method is called on Main-Thread, hence no special invocation required inside method.
*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: setPixelScale0
* Signature: (JJF)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_setPixelScale0
(JNIEnv *env, jobject jthis, jlong window, jlong view, jfloat reqPixelScale)
{
NewtUIWindow* myWindow = (NewtUIWindow*) ((intptr_t) window);
if( NULL == myWindow ) {
DBG_PRINT( "setPixelScale0 - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[myWindow setPixelScale: (CGFloat)reqPixelScale defer:YES];
[pool release];
}
/**
* Method is called on Main-Thread, hence no special invocation required inside method.
*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: close0
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_close0
(JNIEnv *env, jobject unused, jlong window)
{
NewtUIWindow* mWin = (NewtUIWindow*) ((intptr_t) window);
if( NULL == mWin ) {
DBG_PRINT( "windowClose.0 - NULL NEWT win - abort\n");
return;
}
BOOL isNSWin = [mWin isKindOfClass:[UIWindow class]];
BOOL isNewtWin = [mWin isKindOfClass:[NewtUIWindow class]];
UIWindow *pWin;
{
UIView *superview = [mWin superview];
if( [superview isKindOfClass:[UIWindow class]] ) {
pWin = (UIWindow*)superview;
} else {
pWin = NULL;
}
}
DBG_PRINT( "windowClose.0 - %p [isUIWindow %d, isNewtWin %d], parent %p\n", mWin, isNSWin, isNewtWin, pWin);
(void)isNSWin; // silence
if( !isNewtWin ) {
NewtCommon_throwNewRuntimeException(env, "Not a NewtUIWindow %p", mWin);
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NewtUIView* mView = [mWin getContentNewtUIView];
BOOL fullscreen = mWin->isFullscreenWindow;
BOOL destroyNotifySent, isUIView, isNewtUIView;
if( NULL != mView ) {
isUIView = [mView isKindOfClass:[UIView class]];
isNewtUIView = [mView isKindOfClass:[NewtUIView class]];
destroyNotifySent = isNewtUIView ? [mView getDestroyNotifySent] : false;
} else {
isUIView = false;
isNewtUIView = false;
destroyNotifySent = false;
}
DBG_PRINT( "windowClose.0 - %p, destroyNotifySent %d, view %p [isUIView %d, isNewtUIView %d], fullscreen %d, parent %p\n",
mWin, destroyNotifySent, mView, isUIView, isNewtUIView, (int)fullscreen, pWin);
[mWin setRealized: NO];
if( isNewtUIView ) {
// cleanup view
[mView setDestroyNotifySent: true];
setJavaWindowObject(env, NULL, mView);
}
// Note: mWin's release will also release it's mView!
DBG_PRINT( "windowClose.1a - %p view %p, fullscreen %d\n",
mWin, mView, (int)fullscreen);
if(NULL!=pWin) {
[mWin detachFromParent: pWin];
}
// TODO [mWin orderOut: mWin];
[mWin setHidden: YES]; // no release, close n/a, .. well: ref count counts :-(
DBG_PRINT( "windowClose.2 - %p view %p, parent %p\n", mWin, mView, pWin);
[mWin release];
DBG_PRINT( "windowClose.Xp\n");
[pool release];
}
/*
* Class: Java_jogamp_newt_driver_ios_WindowDriver
* Method: lockSurface0
* Signature: (JJ)Z
*/
JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_ios_WindowDriver_lockSurface0
(JNIEnv *env, jclass clazz, jlong window, jlong view)
{
NewtUIWindow *mWin = (NewtUIWindow*) ((intptr_t) window);
if(NO == [mWin isRealized]) {
return JNI_FALSE;
}
NewtUIView * mView = (NewtUIView *) ((intptr_t) view);
return [mView softLock] == YES ? JNI_TRUE : JNI_FALSE;
/** deadlocks, since we render independent of focus
return [mView lockFocusIfCanDraw] == YES ? JNI_TRUE : JNI_FALSE; */
}
/*
* Class: Java_jogamp_newt_driver_ios_WindowDriver
* Method: unlockSurface0
* Signature: (JJ)Z
*/
JNIEXPORT jboolean JNICALL Java_jogamp_newt_driver_ios_WindowDriver_unlockSurface0
(JNIEnv *env, jclass clazz, jlong window, jlong view)
{
// NewtUIWindow *mWin = (NewtUIWindow*) ((intptr_t) window);
(void) window;
NewtUIView * mView = (NewtUIView *) ((intptr_t) view);
return [mView softUnlock] == YES ? JNI_TRUE : JNI_FALSE;
/** deadlocks, since we render independent of focus
[mView unlockFocus]; */
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: requestFocus0
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_requestFocus0
(JNIEnv *env, jobject window, jlong w, jboolean force)
{
UIWindow* mWin = (UIWindow*) ((intptr_t) w);
if( NULL == mWin ) {
DBG_PRINT( "requestFocus - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
#ifdef VERBOSE_ON
BOOL hasFocus = [mWin isKeyWindow];
#endif
DBG_PRINT( "requestFocus - window: %p, force %d, hasFocus %d (START)\n", mWin, force, hasFocus);
// TODO [mWin setAcceptsMouseMovedEvents: YES];
// TODO [mWin makeFirstResponder: nil];
// TODO [mWin orderFrontRegardless];
[mWin makeKeyWindow];
DBG_PRINT( "requestFocus - window: %p, force %d (END)\n", mWin, force);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: resignFocus0
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_resignFocus0
(JNIEnv *env, jobject window, jlong w)
{
UIWindow* mWin = (UIWindow*) ((intptr_t) w);
if( NULL == mWin ) {
DBG_PRINT( "resignFocus0 - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
UIWindow* pWin = NULL; // TODO [mWin parentWindow];
BOOL hasFocus = [mWin isKeyWindow];
DBG_PRINT( "resignFocus0 - window: %p, parent %p, hasFocus %d (START)\n", mWin, pWin, hasFocus );
if( hasFocus ) {
if(NULL != pWin) {
// [mWin makeFirstResponder: pWin];
[pWin makeKeyWindow];
} else {
[pWin resignKeyWindow];
}
}
DBG_PRINT( "resignFocus0 - window: %p (END)\n", mWin);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: orderFront0
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_orderFront0
(JNIEnv *env, jobject unused, jlong window)
{
UIWindow* mWin = (UIWindow*) ((intptr_t) window);
if( NULL == mWin ) {
DBG_PRINT( "orderFront0 - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
UIWindow* pWin = NULL; // TODO [mWin parentWindow];
DBG_PRINT( "orderFront0 - window: (parent %p) %p hidden %d (START)\n", pWin, mWin, [mWin isHidden]);
if( NULL == pWin ) {
// TODO [mWin orderFrontRegardless];
} else {
// TODO [mWin orderWindow: UIWindowAbove relativeTo: [pWin windowNumber]];
}
DBG_PRINT( "orderFront0 - window: (parent %p) %p (END)\n", pWin, mWin);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: orderOut
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_orderOut0
(JNIEnv *env, jobject unused, jlong window)
{
UIWindow* mWin = (UIWindow*) ((intptr_t) window);
if( NULL == mWin ) {
DBG_PRINT( "orderOut0 - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
UIWindow* pWin = NULL; // TODO [mWin parentWindow];
DBG_PRINT( "orderOut0 - window: (parent %p) %p hidden %d (START)\n", pWin, mWin, [mWin isHidden]);
if( NULL == pWin ) {
// TODO [mWin orderOut: mWin];
} else {
// TODO [mWin orderWindow: UIWindowOut relativeTo: [pWin windowNumber]];
}
DBG_PRINT( "orderOut0 - window: (parent %p) %p (END)\n", pWin, mWin);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: setTitle0
* Signature: (JLjava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_setTitle0
(JNIEnv *env, jobject unused, jlong window, jstring title)
{
UIWindow* win = (UIWindow*) ((intptr_t) window);
if( NULL == win ) {
DBG_PRINT( "setTitle0 - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
DBG_PRINT( "setTitle0 - window: %p (START)\n", win);
NSString* str = jstringToNSString(env, title);
[str autorelease];
// TODO [win setTitle: str];
DBG_PRINT( "setTitle0 - window: %p (END)\n", win);
[pool release];
}
/**
* Method is called on Main-Thread, hence no special invocation required inside method.
*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: changeContentView
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_changeContentView0
(JNIEnv *env, jobject jthis, jlong parentWindowOrView, jlong window, jlong jview)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NewtUIView* newView = (NewtUIView *) ((intptr_t) jview);
NewtUIWindow* win = (NewtUIWindow*) ((intptr_t) window);
DBG_PRINT( "changeContentView0.0 - win %p, view (%p,%d)\n",
win, newView, getRetainCount(newView));
NSObject *nsParentObj = (NSObject*) ((intptr_t) parentWindowOrView);
UIView* pView = NULL;
if( NULL != nsParentObj ) {
if( [nsParentObj isKindOfClass:[UIWindow class]] ) {
UIWindow * pWin = (UIWindow*) nsParentObj;
pView = (UIView*)pWin;
} else if( [nsParentObj isKindOfClass:[UIView class]] ) {
pView = (UIView*) nsParentObj;
}
}
changeContentView(env, jthis, pView, win, newView, YES);
DBG_PRINT( "changeContentView0.X\n");
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: updateSizePosInsets0
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_updateSizePosInsets0
(JNIEnv *env, jobject jthis, jlong window, jboolean defer)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NewtUIWindow* mWin = (NewtUIWindow*) ((intptr_t) window);
DBG_PRINT( "updateSizePosInsets - window: %p, defer %d (START)\n", mWin, (int)defer);
[mWin updateSizePosInsets: env jwin:jthis defer:defer];
DBG_PRINT( "updateSizePosInsets - window: %p, defer %d (END)\n", mWin, (int)defer);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: setWindowClientTopLeftPointAndSize0
* Signature: (JIIIIZ)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_setWindowClientTopLeftPointAndSize0
(JNIEnv *env, jobject unused, jlong window, jint x, jint y, jint w, jint h, jboolean display)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NewtUIWindow* mWin = (NewtUIWindow*) ((intptr_t) window);
DBG_PRINT( "setWindowClientTopLeftPointAndSize - window: %p (START)\n", mWin);
setWindowClientTopLeftPointAndSize(mWin, x, y, w, h, display);
DBG_PRINT( "setWindowClientTopLeftPointAndSize - window: %p (END)\n", mWin);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: setWindowClientTopLeftPoint0
* Signature: (JIIZ)V
*/
JNIEXPORT void JNICALL Java_jogamp_newt_driver_ios_WindowDriver_setWindowClientTopLeftPoint0
(JNIEnv *env, jobject unused, jlong window, jint x, jint y, jboolean display)
{
NewtUIWindow* mWin = (NewtUIWindow*) ((intptr_t) window);
if( NULL == mWin ) {
DBG_PRINT( "setWindowClientTopLeftPoint - NULL NEWT win - abort\n");
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
DBG_PRINT( "setWindowClientTopLeftPoint - window: %p (START)\n", mWin);
setWindowClientTopLeftPoint(mWin, x, y, display);
DBG_PRINT( "setWindowClientTopLeftPoint - window: %p (END)\n", mWin);
[pool release];
}
/*
* Class: jogamp_newt_driver_ios_WindowDriver
* Method: getLocationOnScreen0
* Signature: (JII)Lcom/jogamp/nativewindow/util/Point;
*/
JNIEXPORT jobject JNICALL Java_jogamp_newt_driver_ios_WindowDriver_getLocationOnScreen0
(JNIEnv *env, jclass unused, jlong win, jint src_x, jint src_y)
{
NewtUIWindow *mWin = (NewtUIWindow*) (intptr_t) win;
if( NULL == mWin ) {
DBG_PRINT( "getLocationOnScreen0 - NULL NEWT win - abort\n");
return NULL;
}
if( ![mWin isKindOfClass:[NewtUIWindow class]] ) {
NewtCommon_throwNewRuntimeException(env, "Not a NewtUIWindow %p", mWin);
return NULL;
}
CGPoint p0 = [mWin getLocationOnScreen: CGPointMake(src_x, src_y)];
return (*env)->NewObject(env, pointClz, pointCstr, (jint)p0.x, (jint)p0.y);
}
|