Skip to content

Commit 82bdfca

Browse files
committed
examples/blooptest: add a way of shutting down from the main thread
1 parent 89a03f0 commit 82bdfca

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

examples/example.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@
3131
#include <netdb.h>
3232
#endif
3333

34+
#include <signal.h>
35+
3436
#include <rfb/rfb.h>
3537
#include <rfb/keysym.h>
3638

3739
static const int bpp=4;
3840
static int maxx=800, maxy=600;
3941
/* TODO: odd maxx doesn't work (vncviewer bug) */
4042

43+
/* Flag used for threaded mode that's global so we can set it via
44+
a signal handler... */
45+
static int maintain_connection = 1;
46+
4147
/* This initializes a nice (?) background */
4248

4349
static void initBuffer(unsigned char* buffer)
@@ -277,6 +283,10 @@ static void MakeRichCursor(rfbScreenInfoPtr rfbScreen)
277283
}
278284
}
279285

286+
void intHandler(int dummy) {
287+
maintain_connection = 0;
288+
}
289+
280290
/* Initialization */
281291

282292
int main(int argc,char** argv)
@@ -324,12 +334,17 @@ int main(int argc,char** argv)
324334
#if !defined(LIBVNCSERVER_HAVE_LIBPTHREAD) && !defined(LIBVNCSERVER_HAVE_WIN32THREADS)
325335
#error "I need pthreads or win32 threads for that."
326336
#endif
327-
337+
/* catch Ctrl-C to set a flag for the main thread */
338+
signal(SIGINT, intHandler);
328339
/* this is the non-blocking event loop; a background thread is started */
329340
rfbRunEventLoop(rfbScreen,-1,TRUE);
330341
fprintf(stderr, "Running background loop...\n");
331342
/* now we could do some cool things like rendering in idle time */
332-
while(1) sleep(5); /* render(); */
343+
while (maintain_connection) {
344+
sleep(5); /* render(); */
345+
}
346+
fprintf(stderr, "\nShutting down...\n");
347+
rfbShutdownServer(rfbScreen, TRUE);
333348
#endif /* BACKGROUND_LOOP */
334349

335350
free(rfbScreen->frameBuffer);

0 commit comments

Comments
 (0)