diff --git a/src/conio/x_cio.c b/src/conio/x_cio.c
index 4d19e1e593ca18d15ea20105eb4b027063d7137b..1a13dabef0d066ee9f1aa7cf0adc41edcc52820a 100644
--- a/src/conio/x_cio.c
+++ b/src/conio/x_cio.c
@@ -348,6 +348,14 @@ int x_init(void)
 		xp_dlclose(dl);
 		return(-1);
 	}
+	if((x11.XSetWMProtocols=xp_dlsym(dl,XSetWMProtocols))==NULL) {
+		xp_dlclose(dl);
+		return(-1);
+	}
+	if((x11.XInternAtom=xp_dlsym(dl,XInternAtom))==NULL) {
+		xp_dlclose(dl);
+		return(-1);
+	}
 
 	if(sem_init(&pastebuf_set, 0, 0)) {
 		xp_dlclose(dl);
diff --git a/src/conio/x_events.c b/src/conio/x_events.c
index f55c5622d02bca431c6a3d5e755cc213e70fff24..ff7850476f8b2352b7014f00241d5a9991dde3c2 100644
--- a/src/conio/x_events.c
+++ b/src/conio/x_events.c
@@ -54,6 +54,7 @@ int x11_initialized=0;
 
 /* Sets the atom to be used for copy/paste operations */
 #define CONSOLE_CLIPBOARD	XA_PRIMARY
+static Atom WM_DELETE_WINDOW=0;
 
 static Display *dpy=NULL;
 static Window win;
@@ -218,6 +219,8 @@ static int init_window()
 		x11.XSetWMProperties(dpy, win, NULL, NULL, 0, 0, NULL, wmhints, NULL);
 	}
 
+	WM_DELETE_WINDOW = x11.XInternAtom(dpy, "WM_DELETE_WINDOW", False);
+
 	gcv.function = GXcopy;
     gcv.foreground = white;
     gcv.background = black;
@@ -242,6 +245,7 @@ static int init_window()
 
     x11.XStoreName(dpy, win, "SyncConsole");
 	depth = DefaultDepth(dpy, DefaultScreen(dpy));
+	x11.XSetWMProtocols(dpy, win, &WM_DELETE_WINDOW, 1);
 
 	return(0);
 }
@@ -460,6 +464,12 @@ static void expose_rect(x,y,width,height)
 static int x11_event(XEvent *ev)
 {
 	switch (ev->type) {
+		case ClientMessage:
+			if (ev->xclient.format == 32 && ev->xclient.data.l[0] == WM_DELETE_WINDOW) {
+				uint16_t key=CIO_KEY_QUIT;
+				write(key_pipe[1], &key, 2);
+			}
+			break;
 		/* Graphics related events */
 		case ConfigureNotify:
 			x11_window_xpos=ev->xconfigure.x;
diff --git a/src/conio/x_events.h b/src/conio/x_events.h
index c19e1310e03a66150cefc982f924cf2b596e66b5..366aa51a3fb592f3931e2035434393b79a22b724 100644
--- a/src/conio/x_events.h
+++ b/src/conio/x_events.h
@@ -80,6 +80,8 @@ struct x11 {
 	Status	(*XGetWindowAttributes)	(Display*,Window,XWindowAttributes*);
 	XWMHints* (*XAllocWMHints) (void);
 	void	(*XSetWMProperties) (Display*, Window, XTextProperty*, XTextProperty*, char**, int, XSizeHints*, XWMHints*, XClassHint*);
+	Status	(*XSetWMProtocols) (Display*, Window, Atom *, int);
+	Atom	(*XInternAtom) (Display *, char *, Bool);
 };