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) {
41 WindowClosePlayer(main_window);
45 static unsigned int keycodeF;
47 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
48 MainWindow *win = data;
49 if(win->fullscreen && event->xkey.keycode == keycodeF) {
50 WindowFullscreen(main_window, FALSE);
55 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
56 WindowAdjustAspectRatio(data);
59 MainWindow* WindowCreate(Display *display) {
63 MainWindow *window = malloc(sizeof(MainWindow));
64 memset(window, 0, sizeof(MainWindow));
69 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
70 window->window = XtAppCreateShell(
73 applicationShellWidgetClass,
74 //vendorShellWidgetClass,
80 Atom wm_delete_window;
81 wm_delete_window = XmInternAtom(
85 XmAddWMProtocolCallback(
92 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
95 XtSetArg(args[n], XmNwidth, 360); n++;
96 XtSetArg(args[n], XmNheight, 220); n++;
97 XtSetArg(args[n], XmNshadowThickness, 0); n++;
98 Widget container = XmCreateForm(window->window, "form", args, n);
99 XtManageChild(container);
100 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
103 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
104 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
105 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
106 WindowCreateMenu(window, container, args, n);
109 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
110 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
111 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
112 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
113 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
114 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
115 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
116 XtManageChild(window->player_widget);
117 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
120 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
125 MainWindow* GetMainWindow(void) {
129 void WindowShow(MainWindow *win) {
130 XtRealizeWidget(win->window);
134 * Creates a XmPushButton menu item
136 static Widget createMenuItem(
141 const char *accelerator,
142 char *accelerator_text,
143 XtCallbackProc callback,
149 XmString s1 = XmStringCreateSimple(label);
150 XtSetArg(args[n], XmNlabelString, s1); n++;
151 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
153 if(accelerator && accelerator_text) {
154 at = XmStringCreateSimple(accelerator_text);
155 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
156 XtSetArg(args[n], XmNacceleratorText, at); n++;
159 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
160 XtManageChild(menuItem);
162 if(at) XmStringFree(at);
165 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
171 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
172 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
173 XtManageChild(menubar);
174 win->menubar = menubar;
180 XmString s = XmStringCreateSimple("File");
181 Widget fileMenuItem = XtVaCreateManagedWidget(
183 xmCascadeButtonWidgetClass,
188 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
190 s = XmStringCreateSimple("Playback");
191 Widget playMenuItem = XtVaCreateManagedWidget(
193 xmCascadeButtonWidgetClass,
198 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
200 s = XmStringCreateSimple("View");
201 Widget viewMenuItem = XtVaCreateManagedWidget(
203 xmCascadeButtonWidgetClass,
208 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
211 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
214 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
217 void go_fullscreen(Display *dsp, Window win)
220 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
221 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
222 memset(&xev, 0, sizeof(xev));
223 xev.type = ClientMessage;
224 xev.xclient.window = win;
225 xev.xclient.message_type = wm_state;
226 xev.xclient.format = 32;
227 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
228 xev.xclient.data.l[1] = fullscreen;
229 xev.xclient.data.l[2] = 0;
230 XSendEvent(dsp, DefaultRootWindow(dsp), False,
231 SubstructureNotifyMask, &xev);
234 static Atom net_wm_state;
235 static Atom net_wm_state_fullscreen;
236 static int net_wm_atoms_initialized = 0;
238 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
239 Display *dpy = XtDisplay(win->window);
241 // init net_wm_state atoms
242 if(!net_wm_atoms_initialized) {
243 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
244 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
245 net_wm_atoms_initialized = 1;
248 WindowMenubarSetVisible(win, !enableFullscreen);
249 if(enableFullscreen && !win->fullscreen) {
250 XtUnmanageChild(main_window->menubar);
251 main_window->fullscreen = TRUE;
252 } else if(!enableFullscreen && win->fullscreen) {
253 XtManageChild(main_window->menubar);
254 main_window->fullscreen = FALSE;
258 memset(&ev, 0, sizeof(XEvent));
259 ev.type = ClientMessage;
260 ev.xclient.window = XtWindow(win->window);
261 ev.xclient.message_type = net_wm_state;
262 ev.xclient.format = 32;
263 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
264 ev.xclient.data.l[1] = net_wm_state_fullscreen;
265 ev.xclient.data.l[2] = 0;
266 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
269 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
271 XtManageChild(main_window->menubar);
272 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
274 XtUnmanageChild(main_window->menubar);
275 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
281 static void filedialog_end(
284 XmFileSelectionBoxCallbackStruct *selection)
286 XtUnmanageChild(widget);
287 XtDestroyWidget(widget);
290 static void filedialog_select(
293 XmFileSelectionBoxCallbackStruct *selection)
296 if(selection->value) {
297 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
303 // no need to free the value, because it is stored in MainWindow
305 PlayerOpenFile(data);
308 filedialog_end(widget, data, NULL);
314 static void FileOpenCB(Widget w, void *udata, void *cdata) {
315 MainWindow *win = main_window;
320 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
321 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
322 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
323 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
325 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
326 XtUnmanageChild(dirUp);
328 XtManageChild(dialog);
331 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
332 if(main_window->fullscreen) {
333 WindowFullscreen(main_window, FALSE);
335 WindowFullscreen(main_window, TRUE);
340 void WindowAdjustAspectRatio(MainWindow *win) {
341 if(!win->player) return;
342 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
344 // we have a running player width video
345 // adjust window aspect ratio (the window aspect ratio is different from
346 // the video, because of window decoration, menubar and other extra controls)
348 Dimension win_width, win_height;
349 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
350 Dimension player_width, player_height;
351 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
353 double r = (double)win->player->width / (double)win->player->height;
354 double p_width = player_width;
355 double p_height = p_width / r;
357 Dimension new_width = p_width + win_width - player_width;
358 Dimension new_height = p_height + win_height - player_height;
361 hints.flags = PAspect;
362 hints.min_aspect.x = new_width;
363 hints.min_aspect.y = new_height;
364 hints.max_aspect.x = new_width;
365 hints.max_aspect.y = new_height;
366 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
369 void WindowClosePlayer(MainWindow *win) {
371 PlayerDestroy(win->player);