2 * Copyright 2022 Olaf Wintermann
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
33 static MainWindow *main_window;
35 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs);
37 static void FileOpenCB(Widget w, void *udata, void *cdata);
38 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
40 static void window_close_handler(Widget window, void *udata, void *cdata) {
44 static unsigned int keycodeF;
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);
54 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
55 WindowAdjustAspectRatio(data);
58 MainWindow* WindowCreate(Display *display) {
62 MainWindow *window = malloc(sizeof(MainWindow));
63 memset(window, 0, sizeof(MainWindow));
68 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
69 window->window = XtAppCreateShell(
72 applicationShellWidgetClass,
73 //vendorShellWidgetClass,
79 Atom wm_delete_window;
80 wm_delete_window = XmInternAtom(
84 XmAddWMProtocolCallback(
91 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
94 XtSetArg(args[n], XmNwidth, 360); n++;
95 XtSetArg(args[n], XmNheight, 220); n++;
96 XtSetArg(args[n], XmNshadowThickness, 0); n++;
97 Widget container = XmCreateForm(window->window, "form", args, n);
98 XtManageChild(container);
99 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
102 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
103 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
104 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
105 WindowCreateMenu(window, container, args, n);
108 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
109 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
110 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
111 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
112 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
113 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
114 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
115 XtManageChild(window->player_widget);
116 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
119 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
124 MainWindow* GetMainWindow(void) {
128 void WindowShow(MainWindow *win) {
129 XtRealizeWidget(win->window);
133 * Creates a XmPushButton menu item
135 static Widget createMenuItem(
140 const char *accelerator,
141 char *accelerator_text,
142 XtCallbackProc callback,
148 XmString s1 = XmStringCreateSimple(label);
149 XtSetArg(args[n], XmNlabelString, s1); n++;
150 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
152 if(accelerator && accelerator_text) {
153 at = XmStringCreateSimple(accelerator_text);
154 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
155 XtSetArg(args[n], XmNacceleratorText, at); n++;
158 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
159 XtManageChild(menuItem);
161 if(at) XmStringFree(at);
164 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
170 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
171 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
172 XtManageChild(menubar);
173 win->menubar = menubar;
179 XmString s = XmStringCreateSimple("File");
180 Widget fileMenuItem = XtVaCreateManagedWidget(
182 xmCascadeButtonWidgetClass,
187 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
189 s = XmStringCreateSimple("Playback");
190 Widget playMenuItem = XtVaCreateManagedWidget(
192 xmCascadeButtonWidgetClass,
197 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
199 s = XmStringCreateSimple("View");
200 Widget viewMenuItem = XtVaCreateManagedWidget(
202 xmCascadeButtonWidgetClass,
207 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
210 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
213 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
216 void go_fullscreen(Display *dsp, Window win)
219 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
220 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
221 memset(&xev, 0, sizeof(xev));
222 xev.type = ClientMessage;
223 xev.xclient.window = win;
224 xev.xclient.message_type = wm_state;
225 xev.xclient.format = 32;
226 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
227 xev.xclient.data.l[1] = fullscreen;
228 xev.xclient.data.l[2] = 0;
229 XSendEvent(dsp, DefaultRootWindow(dsp), False,
230 SubstructureNotifyMask, &xev);
233 static Atom net_wm_state;
234 static Atom net_wm_state_fullscreen;
235 static int net_wm_atoms_initialized = 0;
237 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
238 Display *dpy = XtDisplay(win->window);
240 // init net_wm_state atoms
241 if(!net_wm_atoms_initialized) {
242 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
243 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
244 net_wm_atoms_initialized = 1;
247 WindowMenubarSetVisible(win, !enableFullscreen);
248 if(enableFullscreen && !win->fullscreen) {
249 XtUnmanageChild(main_window->menubar);
250 main_window->fullscreen = TRUE;
251 } else if(!enableFullscreen && win->fullscreen) {
252 XtManageChild(main_window->menubar);
253 main_window->fullscreen = FALSE;
257 memset(&ev, 0, sizeof(XEvent));
258 ev.type = ClientMessage;
259 ev.xclient.window = XtWindow(win->window);
260 ev.xclient.message_type = net_wm_state;
261 ev.xclient.format = 32;
262 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
263 ev.xclient.data.l[1] = net_wm_state_fullscreen;
264 ev.xclient.data.l[2] = 0;
265 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
268 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
270 XtManageChild(main_window->menubar);
271 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
273 XtUnmanageChild(main_window->menubar);
274 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
280 static void filedialog_end(
283 XmFileSelectionBoxCallbackStruct *selection)
285 XtUnmanageChild(widget);
286 XtDestroyWidget(widget);
289 static void filedialog_select(
292 XmFileSelectionBoxCallbackStruct *selection)
295 if(selection->value) {
296 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
302 // no need to free the value, because it is stored in MainWindow
304 PlayerOpenFile(data);
307 filedialog_end(widget, data, NULL);
313 static void FileOpenCB(Widget w, void *udata, void *cdata) {
314 MainWindow *win = main_window;
319 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
320 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
321 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
322 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
324 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
325 XtUnmanageChild(dirUp);
327 XtManageChild(dialog);
330 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
331 if(main_window->fullscreen) {
332 WindowFullscreen(main_window, FALSE);
334 WindowFullscreen(main_window, TRUE);
339 void WindowAdjustAspectRatio(MainWindow *win) {
340 if(!win->player) return;
341 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
343 // we have a running player width video
344 // adjust window aspect ratio (the window aspect ratio is different from
345 // the video, because of window decoration, menubar and other extra controls)
347 Dimension win_width, win_height;
348 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
349 Dimension player_width, player_height;
350 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
352 double r = (double)win->player->width / (double)win->player->height;
353 double p_width = player_width;
354 double p_height = p_width / r;
356 Dimension new_width = p_width + win_width - player_width;
357 Dimension new_height = p_height + win_height - player_height;
360 hints.flags = PAspect;
361 hints.min_aspect.x = new_width;
362 hints.min_aspect.y = new_height;
363 hints.max_aspect.x = new_width;
364 hints.max_aspect.y = new_height;
365 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);