force window aspect ratio
[uwplayer.git] / application / main.c
index 55a9d4f..6aaac89 100644 (file)
@@ -38,6 +38,8 @@ static XtAppContext app;
 static Display *display;
 static Widget toplevel_window;
 
+static int event_pipe[2];
+
 static String fallback[] = {
         "*renderTable: rt",
         "*rt*fontType: FONT_IS_XFT",
@@ -66,10 +68,30 @@ static String langProc(Display *dp, String xnl, XtPointer closure) {
     return setlocale(LC_ALL, NULL);
 }
 
+typedef struct EventLoopCB {
+    XtWorkProc proc;
+    XtPointer data;
+} EventLoopCB;
+
+static void input_proc(XtPointer data, int *source, XtInputId *iid) {
+    EventLoopCB cb[16];
+    ssize_t r = read(event_pipe[0], cb, sizeof(EventLoopCB)*16);
+    size_t n = r / sizeof(EventLoopCB);
+    for(int i=0;i<n;i++) {
+        cb[i].proc(cb[i].data);
+    }
+}
+
 int main(int argc, char** argv) {  
     // disable stdout buffering, because the netbeans's internal terminal
     // has a bug on freebsd and doesn't flush the output after a newline
     setvbuf(stdout, NULL, _IONBF, 0);
+    
+    // init event pipe for xt event loop
+    if(pipe(event_pipe)) {
+        perror("pipe");
+        return 2;
+    }
      
     // initialize toolkit
     XtToolkitInitialize();
@@ -79,6 +101,13 @@ int main(int argc, char** argv) {
        
     display =  XtOpenDisplay(app, NULL, APP_NAME, APP_CLASS, NULL, 0, &argc, argv);
     
+    XtAppAddInput(
+            app,
+            event_pipe[0],
+            (XtPointer)XtInputReadMask,
+            input_proc,
+            NULL);
+    
     // load settings
     if(load_settings()) {
         return 1;
@@ -104,18 +133,9 @@ void ApplicationExit(void) {
     XtAppSetExitFlag(app);
 }
 
-void AppAddTimeOut(unsigned long interval, XtTimerCallbackProc proc, XtPointer data) {
-    XtAppAddTimeOut(app, interval, proc, data);
-    
-    if(!toplevel_window) return;
-    
-    // send a dummy X11 event, because the event loop may be waiting
-    // and the timeout proc is only called when an event is processed
-    XClientMessageEvent event;
-    memset(&event, 0, sizeof(XClientMessageEvent));
-    event.type = ClientMessage;
-    event.window = XtWindow(toplevel_window);
-    event.format = 32;
-    XSendEvent(display, XtWindow(toplevel_window), 0, 0, (XEvent*)&event);
-    XFlush(display);
+void AppExecProc(XtWorkProc proc, XtPointer data) {
+    EventLoopCB cb;
+    cb.proc = proc;
+    cb.data = data;
+    write(event_pipe[1], &cb, sizeof(cb));
 }