Skip to content

Commit d29d85d

Browse files
author
jonathanosx
committed
Added options to enable OpenGL support and disable logging.
NSLog locking. More tolerant of not getting screen parameters or screen base address.
1 parent e15504b commit d29d85d

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

OSXvnc-server/main.c

+49-26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <sys/types.h>
2828
#include <sys/socket.h>
2929
#include <netinet/in.h>
30+
#include <netinet/tcp.h>
3031
#include <fcntl.h>
3132
#include <pthread.h>
3233
#include <unistd.h>
@@ -92,6 +93,8 @@ BOOL restartOnUserSwitch = FALSE;
9293
BOOL useIP4 = TRUE;
9394
BOOL unregisterWhenNoConnections = FALSE;
9495
BOOL nonBlocking = FALSE;
96+
BOOL logEnable = TRUE;
97+
BOOL useOpenGL = FALSE;
9598

9699
// OSXvnc 0.8 This flag will use a local buffer which will allow us to display the mouse cursor
97100
// Bool rfbLocalBuffer = FALSE;
@@ -134,16 +137,20 @@ static bool rfbScreenInit(void);
134137
*/
135138

136139
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+
}
147154
}
148155

149156
void rfbDebugLog(char *format, ...) {
@@ -280,13 +287,13 @@ void refreshCallback(CGRectCount count, const CGRect *rectArray, void *ignore) {
280287
// return 0;
281288
//}
282289

283-
284290
void rfbCheckForScreenResolutionChange() {
285291
BOOL sizeChange = (rfbScreen.width != CGDisplayPixelsWide(displayID) ||
286292
rfbScreen.height != CGDisplayPixelsHigh(displayID));
287-
293+
BOOL colorChange = (CGDisplayBitsPerPixel(displayID) > 0 && rfbScreen.bitsPerPixel != CGDisplayBitsPerPixel(displayID));
294+
288295
// See if screen changed
289-
if (sizeChange || rfbScreen.bitsPerPixel != CGDisplayBitsPerPixel(displayID)) {
296+
if (sizeChange || colorChange) {
290297
rfbClientIteratorPtr iterator;
291298
rfbClientPtr cl = NULL;
292299
BOOL screenOK = TRUE;
@@ -328,18 +335,17 @@ void rfbCheckForScreenResolutionChange() {
328335

329336
rfbSendScreenUpdateEncoding(cl);
330337

331-
// Reset Frame Buffer
332-
if (cl->scalingFrameBuffer && cl->scalingFrameBuffer != rfbGetFramebuffer())
333-
free(cl->scalingFrameBuffer);
334-
338+
cl->screenBuffer = rfbGetFramebuffer();
335339
if (cl->scalingFactor == 1) {
336-
cl->scalingFrameBuffer = rfbGetFramebuffer();
340+
cl->scalingFrameBuffer = cl->screenBuffer;
337341
cl->scalingPaddedWidthInBytes = rfbScreen.paddedWidthInBytes;
338342
}
339343
else {
340344
const unsigned long csh = (rfbScreen.height+cl->scalingFactor-1)/ cl->scalingFactor;
341345
const unsigned long csw = (rfbScreen.width +cl->scalingFactor-1)/ cl->scalingFactor;
342346

347+
// Reset Frame Buffer
348+
free(cl->scalingFrameBuffer);
343349
cl->scalingFrameBuffer = malloc( csw*csh*rfbScreen.bitsPerPixel/8 );
344350
cl->scalingPaddedWidthInBytes = csw * rfbScreen.bitsPerPixel/8;
345351
}
@@ -612,20 +618,21 @@ void connectReverseClient(char *hostName, int portNum) {
612618
}
613619

614620
char *rfbGetFramebuffer(void) {
615-
int maxWait = 5000000;
616-
int retryWait = 500000;
621+
int maxWait = 5000000;
622+
int retryWait = 500000;
617623

618624
char *returnValue = CGDisplayBaseAddress(displayID);
619625
while (!returnValue && maxWait > 0) {
626+
NSLog(@"Unable to obtain base address");
620627
usleep(retryWait); // Buffer goes away while screen is "switching", it'll be back
621628
maxWait -= retryWait;
622629
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(CGMainDisplayID)]) {
623630
displayID = [[NSProcessInfo processInfo] CGMainDisplayID];
624-
//NSLog(@"Loading New DisplayID: %d", displayID);
625631
}
626632
returnValue = CGDisplayBaseAddress(displayID);
627633
}
628634
if (!returnValue) {
635+
NSLog(@"Unable to obtain base address -- Giving up");
629636
exit(1);
630637
}
631638

@@ -634,6 +641,7 @@ char *rfbGetFramebuffer(void) {
634641

635642
static bool rfbScreenInit(void) {
636643
int bitsPerSample = 8;
644+
int samplesPerPixel = 3;
637645

638646
if (floor(NSAppKitVersionNumber) <= floor(NSAppKitVersionNumber10_3))
639647
(void) GetMainDevice();
@@ -649,17 +657,25 @@ static bool rfbScreenInit(void) {
649657
// otherwise CGDisplayBitsPerPixel doesn't
650658
// always works correctly after a resolution change
651659
bitsPerSample = CGDisplayBitsPerSample(displayID);
660+
samplesPerPixel = CGDisplaySamplesPerPixel(displayID);
652661

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");
655668
return FALSE;
656-
}
669+
}
657670

658671
rfbScreen.width = CGDisplayPixelsWide(displayID);
659672
rfbScreen.height = CGDisplayPixelsHigh(displayID);
660673
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);
663679

664680
rfbServerFormat.bitsPerPixel = rfbScreen.bitsPerPixel;
665681
rfbServerFormat.depth = rfbScreen.depth;
@@ -770,6 +786,8 @@ static void usage(void) {
770786
fprintf(stderr, " (default: no, allow remote connections)\n");
771787
fprintf(stderr, "-restartonuserswitch flag For Use on Panther 10.3 systems, this will cause the server to restart when a fast user switch occurs");
772788
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");
773791
bundlesPerformSelector(@selector(rfbUsage));
774792
fprintf(stderr, "\n");
775793

@@ -918,6 +936,11 @@ static void processArguments(int argc, char *argv[]) {
918936
char *argument = argv[++i];
919937
restartOnUserSwitch = (argument[0] == 'y' || argument[0] == 'Y' || argument[0] == 't' || argument[0] == 'T' || atoi(argument));
920938
}
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;
921944
}
922945
}
923946

0 commit comments

Comments
 (0)