27
27
#include <sys/types.h>
28
28
#include <sys/socket.h>
29
29
#include <netinet/in.h>
30
+ #include <netinet/tcp.h>
30
31
#include <fcntl.h>
31
32
#include <pthread.h>
32
33
#include <unistd.h>
@@ -92,6 +93,8 @@ BOOL restartOnUserSwitch = FALSE;
92
93
BOOL useIP4 = TRUE;
93
94
BOOL unregisterWhenNoConnections = FALSE;
94
95
BOOL nonBlocking = FALSE;
96
+ BOOL logEnable = TRUE;
97
+ BOOL useOpenGL = FALSE;
95
98
96
99
// OSXvnc 0.8 This flag will use a local buffer which will allow us to display the mouse cursor
97
100
// Bool rfbLocalBuffer = FALSE;
@@ -134,16 +137,20 @@ static bool rfbScreenInit(void);
134
137
*/
135
138
136
139
void rfbLog (char * format , ...) {
137
- va_list args ;
138
- NSString * nsFormat = [[NSString alloc ] initWithCString :format ];
139
-
140
- pthread_mutex_lock (& logMutex );
141
- va_start (args , format );
142
- NSLogv (nsFormat , args );
143
- va_end (args );
144
-
145
- [nsFormat release ];
146
- pthread_mutex_unlock (& logMutex );
140
+ if (logEnable && format != NULL ) {
141
+ va_list args ;
142
+ NSString * nsFormat = [[NSString alloc ] initWithCString :format ];
143
+ pthread_mutex_lock (& logMutex );
144
+ NS_DURING {
145
+ va_start (args , format );
146
+ NSLogv (nsFormat , args );
147
+ va_end (args );
148
+ };
149
+ NS_HANDLER
150
+ NS_ENDHANDLER
151
+ pthread_mutex_unlock (& logMutex );
152
+ [nsFormat release ];
153
+ }
147
154
}
148
155
149
156
void rfbDebugLog (char * format , ...) {
@@ -280,13 +287,13 @@ void refreshCallback(CGRectCount count, const CGRect *rectArray, void *ignore) {
280
287
// return 0;
281
288
//}
282
289
283
-
284
290
void rfbCheckForScreenResolutionChange () {
285
291
BOOL sizeChange = (rfbScreen .width != CGDisplayPixelsWide (displayID ) ||
286
292
rfbScreen .height != CGDisplayPixelsHigh (displayID ));
287
-
293
+ BOOL colorChange = (CGDisplayBitsPerPixel (displayID ) > 0 && rfbScreen .bitsPerPixel != CGDisplayBitsPerPixel (displayID ));
294
+
288
295
// See if screen changed
289
- if (sizeChange || rfbScreen . bitsPerPixel != CGDisplayBitsPerPixel ( displayID ) ) {
296
+ if (sizeChange || colorChange ) {
290
297
rfbClientIteratorPtr iterator ;
291
298
rfbClientPtr cl = NULL ;
292
299
BOOL screenOK = TRUE;
@@ -328,18 +335,17 @@ void rfbCheckForScreenResolutionChange() {
328
335
329
336
rfbSendScreenUpdateEncoding (cl );
330
337
331
- // Reset Frame Buffer
332
- if (cl -> scalingFrameBuffer && cl -> scalingFrameBuffer != rfbGetFramebuffer ())
333
- free (cl -> scalingFrameBuffer );
334
-
338
+ cl -> screenBuffer = rfbGetFramebuffer ();
335
339
if (cl -> scalingFactor == 1 ) {
336
- cl -> scalingFrameBuffer = rfbGetFramebuffer () ;
340
+ cl -> scalingFrameBuffer = cl -> screenBuffer ;
337
341
cl -> scalingPaddedWidthInBytes = rfbScreen .paddedWidthInBytes ;
338
342
}
339
343
else {
340
344
const unsigned long csh = (rfbScreen .height + cl -> scalingFactor - 1 )/ cl -> scalingFactor ;
341
345
const unsigned long csw = (rfbScreen .width + cl -> scalingFactor - 1 )/ cl -> scalingFactor ;
342
346
347
+ // Reset Frame Buffer
348
+ free (cl -> scalingFrameBuffer );
343
349
cl -> scalingFrameBuffer = malloc ( csw * csh * rfbScreen .bitsPerPixel /8 );
344
350
cl -> scalingPaddedWidthInBytes = csw * rfbScreen .bitsPerPixel /8 ;
345
351
}
@@ -612,20 +618,21 @@ void connectReverseClient(char *hostName, int portNum) {
612
618
}
613
619
614
620
char * rfbGetFramebuffer (void ) {
615
- int maxWait = 5000000 ;
616
- int retryWait = 500000 ;
621
+ int maxWait = 5000000 ;
622
+ int retryWait = 500000 ;
617
623
618
624
char * returnValue = CGDisplayBaseAddress (displayID );
619
625
while (!returnValue && maxWait > 0 ) {
626
+ NSLog (@"Unable to obtain base address" );
620
627
usleep (retryWait ); // Buffer goes away while screen is "switching", it'll be back
621
628
maxWait -= retryWait ;
622
629
if ([[NSProcessInfo processInfo ] respondsToSelector :@selector (CGMainDisplayID )]) {
623
630
displayID = [[NSProcessInfo processInfo ] CGMainDisplayID ];
624
- //NSLog(@"Loading New DisplayID: %d", displayID);
625
631
}
626
632
returnValue = CGDisplayBaseAddress (displayID );
627
633
}
628
634
if (!returnValue ) {
635
+ NSLog (@"Unable to obtain base address -- Giving up" );
629
636
exit (1 );
630
637
}
631
638
@@ -634,6 +641,7 @@ char *rfbGetFramebuffer(void) {
634
641
635
642
static bool rfbScreenInit (void ) {
636
643
int bitsPerSample = 8 ;
644
+ int samplesPerPixel = 3 ;
637
645
638
646
if (floor (NSAppKitVersionNumber ) <= floor (NSAppKitVersionNumber10_3 ))
639
647
(void ) GetMainDevice ();
@@ -649,17 +657,25 @@ static bool rfbScreenInit(void) {
649
657
// otherwise CGDisplayBitsPerPixel doesn't
650
658
// always works correctly after a resolution change
651
659
bitsPerSample = CGDisplayBitsPerSample (displayID );
660
+ samplesPerPixel = CGDisplaySamplesPerPixel (displayID );
652
661
653
- if (CGDisplaySamplesPerPixel (displayID ) != 3 ) {
654
- rfbLog ("screen format not supported.\n" );
662
+ if (!bitsPerSample && !samplesPerPixel ) {
663
+ // Let's presume 8x3 and hope for the best.....
664
+ bitsPerSample = 8 ;
665
+ samplesPerPixel = 3 ;
666
+ } else if (samplesPerPixel != 3 ) {
667
+ rfbLog ("screen format not supported.\n" );
655
668
return FALSE;
656
- }
669
+ }
657
670
658
671
rfbScreen .width = CGDisplayPixelsWide (displayID );
659
672
rfbScreen .height = CGDisplayPixelsHigh (displayID );
660
673
rfbScreen .bitsPerPixel = CGDisplayBitsPerPixel (displayID );
661
- rfbScreen .depth = CGDisplaySamplesPerPixel (displayID ) * bitsPerSample ;
662
- rfbScreen .paddedWidthInBytes = CGDisplayBytesPerRow (displayID );
674
+ rfbScreen .depth = samplesPerPixel * bitsPerSample ;
675
+ if (useOpenGL )
676
+ rfbScreen .paddedWidthInBytes = rfbScreen .width * rfbScreen .bitsPerPixel /8 ;
677
+ else
678
+ rfbScreen .paddedWidthInBytes = CGDisplayBytesPerRow (displayID );
663
679
664
680
rfbServerFormat .bitsPerPixel = rfbScreen .bitsPerPixel ;
665
681
rfbServerFormat .depth = rfbScreen .depth ;
@@ -770,6 +786,8 @@ static void usage(void) {
770
786
fprintf (stderr , " (default: no, allow remote connections)\n" );
771
787
fprintf (stderr , "-restartonuserswitch flag For Use on Panther 10.3 systems, this will cause the server to restart when a fast user switch occurs" );
772
788
fprintf (stderr , " (default: no)\n" );
789
+ fprintf (stderr , "-disableLog Don't log anything in console\n" );
790
+ fprintf (stderr , "-useOpenGL Uses OpenGL to capture screen buffer\n" );
773
791
bundlesPerformSelector (@selector (rfbUsage ));
774
792
fprintf (stderr , "\n" );
775
793
@@ -918,6 +936,11 @@ static void processArguments(int argc, char *argv[]) {
918
936
char * argument = argv [++ i ];
919
937
restartOnUserSwitch = (argument [0 ] == 'y' || argument [0 ] == 'Y' || argument [0 ] == 't' || argument [0 ] == 'T' || atoi (argument ));
920
938
}
939
+ } else if (strcmp (argv [i ], "-disablelog" ) == 0 ) {
940
+ logEnable = FALSE;
941
+ } else if (strcmp (argv [i ], "-useopengl" ) == 0 ) {
942
+ rfbLog ("Using OpenGL display" );
943
+ useOpenGL = TRUE;
921
944
}
922
945
}
923
946
0 commit comments