26092cdabd622e4c4fd5d99c5910078c3fd8cfb6
[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 WindowRealized(MainWindow *win);
41
42 static int blank_cursor_init = 0;
43 static Pixmap blank_cursor_pixmap;
44 static Cursor blank_cursor;
45
46 static void init_blank_cursor(Widget w) {
47     char data = 0;
48     
49     XColor c;
50     
51     blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
52     if(!blank_cursor_pixmap) return;
53     
54     blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
55     
56     XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
57     blank_cursor_init = 1;
58 }
59
60 static void window_close_handler(Widget window, void *udata, void *cdata) {
61     WindowClosePlayer(main_window);
62     ApplicationExit();
63 }
64
65 static unsigned int keycodeF;
66
67 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
68     MainWindow *win = data;
69     if(win->fullscreen && event->xkey.keycode == keycodeF) {
70         WindowFullscreen(main_window, FALSE);
71         *dispatch = FALSE;
72     }
73 }
74
75 static int main_window_is_realized = 0;
76
77 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {  
78     if(!main_window_is_realized) {
79         if(XtIsRealized(widget)) {
80             main_window_is_realized = 1;
81             WindowRealized(data);
82         }
83     }
84     WindowAdjustAspectRatio(data);
85 }
86
87 static void WindowRealized(MainWindow *win) {
88     char *open_file = GetOpenFileArg();
89     if(open_file) {
90         size_t len = strlen(open_file);
91         char *file = XtMalloc(len+1);
92         memcpy(file, open_file, len);
93         file[len] = 0;
94         WindowSetFile(win, file);
95         PlayerOpenFile(win);
96         CleanOpenFileArg();
97     }
98     
99     if(!blank_cursor_init) {
100         init_blank_cursor(win->player_widget);
101     }
102 }
103
104 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
105     MainWindow *win = u;
106     XmDrawingAreaCallbackStruct *cb = c;
107     
108     if(win->player && win->player->isactive) {
109         PlayerHandleInput(win, win->player, cb);
110     }
111 }
112
113 static void windowGrabButton(MainWindow *win) {
114     //printf("grab\n");
115     XtGrabButton(
116                 win->player_widget,
117                 AnyButton,
118                 AnyModifier,
119                 True,
120                 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
121                 GrabModeAsync,
122                 GrabModeAsync,
123                 None,
124                 None);
125     win->buttongrab = True;
126 }
127
128 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
129     MainWindow *win = data;
130     int etype = event->type;
131     
132     ///*
133     if(etype == EnterNotify) {
134         //printf("enter\n");
135         windowGrabButton(win);
136         return;
137     }
138     if(etype == LeaveNotify) {
139         //printf("leave\n");
140         //XtUngrabButton(win->player_widget, AnyButton, AnyModifier); 
141         //win->buttongrab = False;
142         return;
143     }
144     
145     int pass = 0;
146     if(etype == ButtonPress || etype == ButtonRelease || etype == KeyPress || etype == KeyRelease) {
147         //printf("button press\n");
148         pass = 1;
149     }
150     
151     if(!win->player || win->player->window == 0) return;
152     
153     WindowPlayerWidgetEvent(win, event);
154     
155     if(pass) {
156         // redirect key events to the player window
157         //printf("redirect\n");
158         event->xkey.window = win->player->window;
159         XSendEvent(
160                 XtDisplay(win->player_widget),
161                 win->player->window,
162                 True,
163                 0,
164                 event);
165     }
166 }
167
168 void WindowPlayerWidgetEvent(MainWindow *win, XEvent *event) {
169     int etype = event->type;
170     
171     if(etype == MotionNotify) {
172         
173     } else if(etype == ButtonPress) {
174         
175     } else if(etype == ButtonRelease) {
176         
177     }
178 }
179
180 MainWindow* WindowCreate(Display *display) {
181     Arg args[32];
182     int n;
183      
184     MainWindow *window = malloc(sizeof(MainWindow));
185     memset(window, 0, sizeof(MainWindow));
186     main_window = window;
187     
188     // toplevel window
189     n = 0;
190     XtSetArg(args[n], XmNtitle, APP_NAME); n++;
191     window->window = XtAppCreateShell(
192             APP_NAME,
193             APP_CLASS,
194             applicationShellWidgetClass,
195             //vendorShellWidgetClass,
196             display,
197             args,
198             n);
199     
200     // close handler
201     Atom wm_delete_window;
202     wm_delete_window = XmInternAtom(
203             display,
204             "WM_DELETE_WINDOW",
205             0);
206     XmAddWMProtocolCallback(
207             window->window,
208             wm_delete_window,
209             window_close_handler,
210             window);
211     
212     // resize handler
213     XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
214     
215     n = 0;
216     XtSetArg(args[n], XmNwidth, 360); n++;
217     XtSetArg(args[n], XmNheight, 220); n++;
218     XtSetArg(args[n], XmNshadowThickness, 0); n++;
219     Widget container = XmCreateForm(window->window, "form", args, n);
220     XtManageChild(container);
221     XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
222     
223     n = 0;
224     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
225     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
226     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
227     WindowCreateMenu(window, container, args, n);
228     
229     n = 0;
230     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
231     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
232     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
233     XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
234     XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
235     XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
236     window->player_widget = XmCreateDrawingArea(container, "player", args, n);
237     XtManageChild(window->player_widget);
238     XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
239     XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
240     XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
241                  EnterWindowMask | KeyPressMask | KeyReleaseMask |
242                   LeaveWindowMask, FALSE, playerEH, window);
243     
244     
245     // get F keycode
246     keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
247     
248     return window;
249 }
250
251 MainWindow* GetMainWindow(void) {
252     return main_window;
253 }
254
255 void WindowShow(MainWindow *win) {
256     XtRealizeWidget(win->window);
257 }
258
259 /*
260  * Creates a XmPushButton menu item
261  */
262 static Widget createMenuItem(
263         Widget menu,
264         char *name,
265         char *label,
266         char mnemonic,
267         const char *accelerator,
268         char *accelerator_text,
269         XtCallbackProc callback,
270         void *cbData)
271 {
272     Arg args[16];
273     int n = 0;
274     
275     XmString s1 = XmStringCreateSimple(label);
276     XtSetArg(args[n], XmNlabelString, s1); n++;
277     XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
278     XmString at = NULL;
279     if(accelerator && accelerator_text) {
280         at = XmStringCreateSimple(accelerator_text);
281         XtSetArg(args[n], XmNaccelerator, accelerator); n++;
282         XtSetArg(args[n], XmNacceleratorText, at); n++;
283     }
284     
285     Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
286     XtManageChild(menuItem);
287     XmStringFree(s1);
288     if(at) XmStringFree(at);
289     
290     if(callback) {
291         XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
292     }
293     
294     return menuItem;
295 }
296
297 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
298     Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
299     XtManageChild(menubar);
300     win->menubar = menubar;
301     
302     Arg args[16];
303     int n;
304     
305     // menus
306     XmString s = XmStringCreateSimple("File");
307     Widget fileMenuItem = XtVaCreateManagedWidget(
308             "menuitem",
309             xmCascadeButtonWidgetClass,
310             menubar,
311             XmNlabelString, s,
312             NULL);
313     XmStringFree(s);
314     Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
315     
316     s = XmStringCreateSimple("Playback");
317     Widget playMenuItem = XtVaCreateManagedWidget(
318             "menuitem",
319             xmCascadeButtonWidgetClass,
320             menubar,
321             XmNlabelString, s,
322             NULL);
323     XmStringFree(s);
324     Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
325     
326     s = XmStringCreateSimple("View");
327     Widget viewMenuItem = XtVaCreateManagedWidget(
328             "menuitem",
329             xmCascadeButtonWidgetClass,
330             menubar,
331             XmNlabelString, s,
332             NULL);
333     XmStringFree(s);
334     Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
335     
336     // file menu
337     createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
338     
339     // view menu
340     createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
341 }
342
343 void go_fullscreen(Display *dsp, Window win)
344 {
345   XEvent xev;
346   Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
347   Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
348   memset(&xev, 0, sizeof(xev));
349   xev.type = ClientMessage;
350   xev.xclient.window = win;
351   xev.xclient.message_type = wm_state;
352   xev.xclient.format = 32;
353   xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
354   xev.xclient.data.l[1] = fullscreen;
355   xev.xclient.data.l[2] = 0;
356   XSendEvent(dsp, DefaultRootWindow(dsp), False,
357     SubstructureNotifyMask, &xev);
358 }
359
360 static Atom net_wm_state;
361 static Atom net_wm_state_fullscreen;
362 static int net_wm_atoms_initialized = 0;
363
364 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
365     Display *dpy = XtDisplay(win->window);
366     
367     // init net_wm_state atoms
368     if(!net_wm_atoms_initialized) {
369         net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
370         net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
371         net_wm_atoms_initialized = 1;
372     }
373     
374     WindowMenubarSetVisible(win, !enableFullscreen);
375     if(enableFullscreen && !win->fullscreen) {
376         XtUnmanageChild(main_window->menubar);
377         main_window->fullscreen = TRUE;
378     } else if(!enableFullscreen && win->fullscreen) {
379         XtManageChild(main_window->menubar);
380         main_window->fullscreen = FALSE;
381     }
382     
383     XEvent ev;
384     memset(&ev, 0, sizeof(XEvent));
385     ev.type = ClientMessage;
386     ev.xclient.window = XtWindow(win->window);
387     ev.xclient.message_type = net_wm_state;
388     ev.xclient.format = 32;
389     ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
390     ev.xclient.data.l[1] = net_wm_state_fullscreen;
391     ev.xclient.data.l[2] = 0;
392     XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
393 }
394
395 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
396     if(visible) {
397         XtManageChild(main_window->menubar);
398         XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
399     } else {
400         XtUnmanageChild(main_window->menubar);
401         XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
402     }
403 }
404
405 void WindowSetFile(MainWindow *win, char *file) {
406     if(win->file) {
407         XtFree(win->file);
408     }
409     win->file = file;
410 }
411
412 static void filedialog_end(
413         Widget widget,
414         MainWindow *data,
415         XmFileSelectionBoxCallbackStruct *selection)
416 {
417     XtUnmanageChild(widget);
418     XtDestroyWidget(widget);
419 }
420
421 static void filedialog_select(
422         Widget widget,
423         MainWindow *data,
424         XmFileSelectionBoxCallbackStruct *selection)
425 {
426     char *value = NULL;
427     if(selection->value) {
428         XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
429         if(value) {
430             WindowSetFile(data, value);
431             // no need to free the value, because it is stored in MainWindow
432             
433             PlayerOpenFile(data);
434         }
435     }
436     filedialog_end(widget, data, NULL);
437 }
438
439
440
441
442 static void FileOpenCB(Widget w, void *udata, void *cdata) {
443     MainWindow *win = main_window;
444     
445     Arg args[16];
446     int n = 0;
447     
448     XtSetArg(args[n], XnNshowViewMenu, 1); n++;
449     Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
450     XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
451     XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
452     
453     Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
454     XtUnmanageChild(dirUp);
455     
456     XtManageChild(dialog);
457 }
458
459 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
460     if(main_window->fullscreen) {
461         WindowFullscreen(main_window, FALSE);
462     } else {
463         WindowFullscreen(main_window, TRUE);
464     }
465     
466 }
467
468 void WindowAdjustAspectRatio(MainWindow *win) {
469     if(!win->player) return;
470     if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
471       
472     // we have a running player width video
473     // adjust window aspect ratio (the window aspect ratio is different from
474     // the video, because of window decoration, menubar and other extra controls)
475     
476     Dimension win_width, win_height;
477     XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
478     Dimension player_width, player_height;
479     XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
480     
481     double r = (double)win->player->width / (double)win->player->height;
482     double p_width = player_width;
483     double p_height = p_width / r;
484     
485     Dimension new_width = p_width + win_width - player_width;
486     Dimension new_height = p_height + win_height - player_height;
487     
488     XSizeHints hints;
489     hints.flags = PAspect;
490     hints.min_aspect.x = new_width;
491     hints.min_aspect.y = new_height;
492     hints.max_aspect.x = new_width;
493     hints.max_aspect.y = new_height;
494     XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
495 }
496
497 void WindowClosePlayer(MainWindow *win) {
498     if(win->player) {
499         PlayerDestroy(win->player);
500     }
501     win->player = NULL;
502 }
503
504 void WindowHidePlayerCursor(MainWindow *win) {
505     if(!win->cursorhidden && win->player && win->player->window != 0) {
506         //XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
507         win->cursorhidden = True;
508         XFlush(XtDisplay(win->player_widget));
509     }
510 }
511
512 void WindowShowPlayerCursor(MainWindow *win) {
513     if(win->cursorhidden && win->player && win->player->window != 0) {
514         XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
515         win->cursorhidden = False;
516         XFlush(XtDisplay(win->player_widget));
517     }
518 }