6aaac8918a9b7b2c98f55edcda9b5c1f6667568d
[uwplayer.git] / application / main.c
1 /*
2  * Copyright 2022 Olaf Wintermann
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"), 
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <locale.h>
27 #include <time.h>
28 #include <inttypes.h>
29
30 #include "window.h"
31 #include "main.h"
32 #include "settings.h"
33
34 #include <ucx/buffer.h>
35 #include <ucx/utils.h>
36
37 static XtAppContext app;
38 static Display *display;
39 static Widget toplevel_window;
40
41 static int event_pipe[2];
42
43 static String fallback[] = {
44         "*renderTable: rt",
45         "*rt*fontType: FONT_IS_XFT",
46         "*rt*fontName: Sans",
47         "*rt*fontSize: 9",
48         
49         "*pbbutton.shadowThickness: 1",
50         "*pbbutton.highlightThickness: 1",
51     
52         "*XmText.baseTranslations: #override\\n" \
53                                 "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n" \
54                                 "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n" \
55                                 "Ctrl~Alt~Meta<KeyPress>x: cut-clipboard()\\n" \
56                                 "Ctrl~Alt~Meta<KeyPress>u: delete-to-start-of-line()\\n",
57         "*XmTextField.baseTranslations: #override\\n" \
58                                 "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n" \
59                                 "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n" \
60                                 "Ctrl~Alt~Meta<KeyPress>x: cut-clipboard()\\n" \
61                                 "Ctrl~Alt~Meta<KeyPress>u: delete-to-start-of-line()\\n",
62         NULL
63 };
64
65 static String langProc(Display *dp, String xnl, XtPointer closure) {
66     setlocale(LC_ALL, xnl);
67     setlocale(LC_NUMERIC, "C");
68     return setlocale(LC_ALL, NULL);
69 }
70
71 typedef struct EventLoopCB {
72     XtWorkProc proc;
73     XtPointer data;
74 } EventLoopCB;
75
76 static void input_proc(XtPointer data, int *source, XtInputId *iid) {
77     EventLoopCB cb[16];
78     ssize_t r = read(event_pipe[0], cb, sizeof(EventLoopCB)*16);
79     size_t n = r / sizeof(EventLoopCB);
80     for(int i=0;i<n;i++) {
81         cb[i].proc(cb[i].data);
82     }
83 }
84
85 int main(int argc, char** argv) {  
86     // disable stdout buffering, because the netbeans's internal terminal
87     // has a bug on freebsd and doesn't flush the output after a newline
88     setvbuf(stdout, NULL, _IONBF, 0);
89     
90     // init event pipe for xt event loop
91     if(pipe(event_pipe)) {
92         perror("pipe");
93         return 2;
94     }
95      
96     // initialize toolkit
97     XtToolkitInitialize();
98     XtSetLanguageProc(NULL, langProc, NULL);
99     app = XtCreateApplicationContext();
100     XtAppSetFallbackResources(app, fallback);
101        
102     display =  XtOpenDisplay(app, NULL, APP_NAME, APP_CLASS, NULL, 0, &argc, argv);
103     
104     XtAppAddInput(
105             app,
106             event_pipe[0],
107             (XtPointer)XtInputReadMask,
108             input_proc,
109             NULL);
110     
111     // load settings
112     if(load_settings()) {
113         return 1;
114     }
115     
116     MainWindow *window = WindowCreate(display);
117     toplevel_window = window->window;
118     
119     // random numbers only used for creating tmp dirs
120     srand(time(NULL));
121     
122     WindowShow(window);
123     XtAppMainLoop(app);
124     
125     return 0;
126 }
127
128 XtAppContext* GetAppContext(void) {
129     return &app;
130 }
131
132 void ApplicationExit(void) {
133     XtAppSetExitFlag(app);
134 }
135
136 void AppExecProc(XtWorkProc proc, XtPointer data) {
137     EventLoopCB cb;
138     cb.proc = proc;
139     cb.data = data;
140     write(event_pipe[1], &cb, sizeof(cb));
141 }