412381f64a4a4f91d4cabeb0a3e089257d4a1f28
[uwplayer.git] / application / window.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 <string.h>
26
27 #include "window.h"
28 #include "main.h"
29 #include "player.h"
30
31 #include "Fsb.h"
32
33 static MainWindow *main_window;
34
35 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs);
36
37 static void FileOpenCB(Widget w, void *udata, void *cdata);
38 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
39
40 static void window_close_handler(Widget window, void *udata, void *cdata) {
41     ApplicationExit();
42 }
43
44 static unsigned int keycodeF;
45
46 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
47     MainWindow *win = data;
48     if(win->fullscreen && event->xkey.keycode == keycodeF) {
49         WindowFullscreen(main_window, FALSE);
50         *dispatch = FALSE;
51     }
52 }
53
54 MainWindow* WindowCreate(Display *display) {
55     Arg args[32];
56     int n;
57     
58     MainWindow *window = malloc(sizeof(MainWindow));
59     memset(window, 0, sizeof(MainWindow));
60     main_window = window;
61     
62     // toplevel window
63     n = 0;
64     XtSetArg(args[n], XmNtitle, "MediaPlayer"); n++;
65     window->window = XtAppCreateShell(
66             "mediaplayer",
67             "mediaplayer",
68             //applicationShellWidgetClass,
69             vendorShellWidgetClass,
70             display,
71             args,
72             n);
73     
74     
75     Atom wm_delete_window;
76     wm_delete_window = XmInternAtom(
77             display,
78             "WM_DELETE_WINDOW",
79             0);
80     XmAddWMProtocolCallback(
81             window->window,
82             wm_delete_window,
83             window_close_handler,
84             window);
85     
86     
87     n = 0;
88     XtSetArg(args[n], XmNwidth, 360); n++;
89     XtSetArg(args[n], XmNheight, 220); n++;
90     XtSetArg(args[n], XmNshadowThickness, 0); n++;
91     Widget container = XmCreateForm(window->window, "form", args, n);
92     XtManageChild(container);
93     XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
94     
95     n = 0;
96     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
97     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
98     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
99     WindowCreateMenu(window, container, args, n);
100     
101     n = 0;
102     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
103     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
104     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
105     XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
106     XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
107     XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
108     window->player_widget = XmCreateDrawingArea(container, "player", args, n);
109     XtManageChild(window->player_widget);
110     
111     // get F keycode
112     keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
113     
114     return window;
115 }
116
117 void WindowShow(MainWindow *win) {
118     XtRealizeWidget(win->window);
119 }
120
121 /*
122  * Creates a XmPushButton menu item
123  */
124 static Widget createMenuItem(
125         Widget menu,
126         char *name,
127         char *label,
128         char mnemonic,
129         const char *accelerator,
130         char *accelerator_text,
131         XtCallbackProc callback,
132         void *cbData)
133 {
134     Arg args[16];
135     int n = 0;
136     
137     XmString s1 = XmStringCreateSimple(label);
138     XtSetArg(args[n], XmNlabelString, s1); n++;
139     XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
140     XmString at = NULL;
141     if(accelerator && accelerator_text) {
142         at = XmStringCreateSimple(accelerator_text);
143         XtSetArg(args[n], XmNaccelerator, accelerator); n++;
144         XtSetArg(args[n], XmNacceleratorText, at); n++;
145     }
146     
147     Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
148     XtManageChild(menuItem);
149     XmStringFree(s1);
150     if(at) XmStringFree(at);
151     
152     if(callback) {
153         XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
154     }
155     
156     return menuItem;
157 }
158
159 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
160     Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
161     XtManageChild(menubar);
162     win->menubar = menubar;
163     
164     Arg args[16];
165     int n;
166     
167     // menus
168     XmString s = XmStringCreateSimple("File");
169     Widget fileMenuItem = XtVaCreateManagedWidget(
170             "menuitem",
171             xmCascadeButtonWidgetClass,
172             menubar,
173             XmNlabelString, s,
174             NULL);
175     XmStringFree(s);
176     Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
177     
178     s = XmStringCreateSimple("Playback");
179     Widget playMenuItem = XtVaCreateManagedWidget(
180             "menuitem",
181             xmCascadeButtonWidgetClass,
182             menubar,
183             XmNlabelString, s,
184             NULL);
185     XmStringFree(s);
186     Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
187     
188     s = XmStringCreateSimple("View");
189     Widget viewMenuItem = XtVaCreateManagedWidget(
190             "menuitem",
191             xmCascadeButtonWidgetClass,
192             menubar,
193             XmNlabelString, s,
194             NULL);
195     XmStringFree(s);
196     Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
197     
198     // file menu
199     createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
200     
201     // view menu
202     createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
203 }
204
205 void go_fullscreen(Display *dsp, Window win)
206 {
207   XEvent xev;
208   Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
209   Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
210   memset(&xev, 0, sizeof(xev));
211   xev.type = ClientMessage;
212   xev.xclient.window = win;
213   xev.xclient.message_type = wm_state;
214   xev.xclient.format = 32;
215   xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
216   xev.xclient.data.l[1] = fullscreen;
217   xev.xclient.data.l[2] = 0;
218   XSendEvent(dsp, DefaultRootWindow(dsp), False,
219     SubstructureNotifyMask, &xev);
220 }
221
222 static Atom net_wm_state;
223 static Atom net_wm_state_fullscreen;
224 static int net_wm_atoms_initialized = 0;
225
226 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
227     Display *dpy = XtDisplay(win->window);
228     
229     // init net_wm_state atoms
230     if(!net_wm_atoms_initialized) {
231         net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
232         net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
233         net_wm_atoms_initialized = 1;
234     }
235     
236     WindowMenubarSetVisible(win, !enableFullscreen);
237     if(enableFullscreen && !win->fullscreen) {
238         XtUnmanageChild(main_window->menubar);
239         main_window->fullscreen = TRUE;
240     } else if(!enableFullscreen && win->fullscreen) {
241         XtManageChild(main_window->menubar);
242         main_window->fullscreen = FALSE;
243     }
244     
245     XEvent ev;
246     memset(&ev, 0, sizeof(XEvent));
247     ev.type = ClientMessage;
248     ev.xclient.window = XtWindow(win->window);
249     ev.xclient.message_type = net_wm_state;
250     ev.xclient.format = 32;
251     ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
252     ev.xclient.data.l[1] = net_wm_state_fullscreen;
253     ev.xclient.data.l[2] = 0;
254     XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
255 }
256
257 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
258     if(visible) {
259         XtManageChild(main_window->menubar);
260         XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
261     } else {
262         XtUnmanageChild(main_window->menubar);
263         XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
264     }
265 }
266
267
268
269 static void filedialog_end(
270         Widget widget,
271         MainWindow *data,
272         XmFileSelectionBoxCallbackStruct *selection)
273 {
274     XtUnmanageChild(widget);
275     XtDestroyWidget(widget);
276 }
277
278 static void filedialog_select(
279         Widget widget,
280         MainWindow *data,
281         XmFileSelectionBoxCallbackStruct *selection)
282 {
283     char *value = NULL;
284     if(selection->value) {
285         XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
286         if(value) {
287             if(data->file) {
288                 XtFree(data->file);
289             }
290             data->file = value;
291             // no need to free the value, because it is stored in MainWindow
292             
293             PlayerOpenFile(data);
294         }
295     }
296     filedialog_end(widget, data, NULL);
297 }
298
299
300
301
302 static void FileOpenCB(Widget w, void *udata, void *cdata) {
303     MainWindow *win = main_window;
304     Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", NULL, 0);
305     XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
306     XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
307     XtManageChild(dialog);
308 }
309
310 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
311     if(main_window->fullscreen) {
312         WindowFullscreen(main_window, FALSE);
313     } else {
314         WindowFullscreen(main_window, TRUE);
315     }
316     
317 }