/* * Copyright 2022 Olaf Wintermann * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include "window.h" #include "main.h" #include "player.h" #include "Fsb.h" static MainWindow *main_window; static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs); static void FileOpenCB(Widget w, void *udata, void *cdata); static void ViewFullscreenCB(Widget w, void *udata, void *cdata); static void WindowRealized(MainWindow *win); static void window_close_handler(Widget window, void *udata, void *cdata) { WindowClosePlayer(main_window); ApplicationExit(); } static unsigned int keycodeF; static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) { MainWindow *win = data; if(win->fullscreen && event->xkey.keycode == keycodeF) { WindowFullscreen(main_window, FALSE); *dispatch = FALSE; } } static int main_window_is_realized = 0; static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) { if(!main_window_is_realized) { if(XtIsRealized(widget)) { main_window_is_realized = 1; WindowRealized(data); } } WindowAdjustAspectRatio(data); } static void WindowRealized(MainWindow *win) { char *open_file = GetOpenFileArg(); if(open_file) { size_t len = strlen(open_file); char *file = XtMalloc(len+1); memcpy(file, open_file, len); file[len] = 0; WindowSetFile(win, file); PlayerOpenFile(win); CleanOpenFileArg(); } } static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) { MainWindow *win = u; XmDrawingAreaCallbackStruct *cb = c; if(win->player && win->player->isactive) { PlayerHandleInput(win, win->player, cb); } } static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) { MainWindow *win = data; if(!win->player || win->player->window == 0) return; /* if(event->type == EnterNotify) { printf("enter: grab pointer\n"); XtGrabPointer( win->player_widget, True, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); return; } if(event->type == LeaveNotify) { printf("leave\n"); XtUngrabPointer(win->player_widget, CurrentTime); return; } if(event->type == MotionNotify) { static int testv = 0; printf("test %d\n", testv++); } */ if(event->type == KeyPress || event->type == KeyRelease) { // redirect key events to the player window event->xkey.window = win->player->window; XSendEvent( XtDisplay(win->player_widget), win->player->window, True, 0, event); } } MainWindow* WindowCreate(Display *display) { Arg args[32]; int n; MainWindow *window = malloc(sizeof(MainWindow)); memset(window, 0, sizeof(MainWindow)); main_window = window; // toplevel window n = 0; XtSetArg(args[n], XmNtitle, APP_NAME); n++; window->window = XtAppCreateShell( APP_NAME, APP_CLASS, applicationShellWidgetClass, //vendorShellWidgetClass, display, args, n); // close handler Atom wm_delete_window; wm_delete_window = XmInternAtom( display, "WM_DELETE_WINDOW", 0); XmAddWMProtocolCallback( window->window, wm_delete_window, window_close_handler, window); // resize handler XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window); n = 0; XtSetArg(args[n], XmNwidth, 360); n++; XtSetArg(args[n], XmNheight, 220); n++; XtSetArg(args[n], XmNshadowThickness, 0); n++; Widget container = XmCreateForm(window->window, "form", args, n); XtManageChild(container); XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window); n = 0; XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++; WindowCreateMenu(window, container, args, n); n = 0; XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++; XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++; XtSetArg(args[n], XmNtopWidget, window->menubar); n++; XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++; window->player_widget = XmCreateDrawingArea(container, "player", args, n); XtManageChild(window->player_widget); XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window); XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT); XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask | EnterWindowMask | KeyPressMask | KeyReleaseMask | LeaveWindowMask, FALSE, playerEH, window); // get F keycode keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F")); return window; } MainWindow* GetMainWindow(void) { return main_window; } void WindowShow(MainWindow *win) { XtRealizeWidget(win->window); } /* * Creates a XmPushButton menu item */ static Widget createMenuItem( Widget menu, char *name, char *label, char mnemonic, const char *accelerator, char *accelerator_text, XtCallbackProc callback, void *cbData) { Arg args[16]; int n = 0; XmString s1 = XmStringCreateSimple(label); XtSetArg(args[n], XmNlabelString, s1); n++; XtSetArg(args[n], XmNmnemonic, mnemonic); n++; XmString at = NULL; if(accelerator && accelerator_text) { at = XmStringCreateSimple(accelerator_text); XtSetArg(args[n], XmNaccelerator, accelerator); n++; XtSetArg(args[n], XmNacceleratorText, at); n++; } Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n); XtManageChild(menuItem); XmStringFree(s1); if(at) XmStringFree(at); if(callback) { XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData); } return menuItem; } static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) { Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs); XtManageChild(menubar); win->menubar = menubar; Arg args[16]; int n; // menus XmString s = XmStringCreateSimple("File"); Widget fileMenuItem = XtVaCreateManagedWidget( "menuitem", xmCascadeButtonWidgetClass, menubar, XmNlabelString, s, NULL); XmStringFree(s); Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL); s = XmStringCreateSimple("Playback"); Widget playMenuItem = XtVaCreateManagedWidget( "menuitem", xmCascadeButtonWidgetClass, menubar, XmNlabelString, s, NULL); XmStringFree(s); Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL); s = XmStringCreateSimple("View"); Widget viewMenuItem = XtVaCreateManagedWidget( "menuitem", xmCascadeButtonWidgetClass, menubar, XmNlabelString, s, NULL); XmStringFree(s); Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL); // file menu createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "CtrlO", "Ctrl+O", FileOpenCB, NULL); // view menu createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "F", "F", ViewFullscreenCB, NULL); } void go_fullscreen(Display *dsp, Window win) { XEvent xev; Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False); Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False); memset(&xev, 0, sizeof(xev)); xev.type = ClientMessage; xev.xclient.window = win; xev.xclient.message_type = wm_state; xev.xclient.format = 32; xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD xev.xclient.data.l[1] = fullscreen; xev.xclient.data.l[2] = 0; XSendEvent(dsp, DefaultRootWindow(dsp), False, SubstructureNotifyMask, &xev); } static Atom net_wm_state; static Atom net_wm_state_fullscreen; static int net_wm_atoms_initialized = 0; void WindowFullscreen(MainWindow *win, bool enableFullscreen) { Display *dpy = XtDisplay(win->window); // init net_wm_state atoms if(!net_wm_atoms_initialized) { net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False); net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); net_wm_atoms_initialized = 1; } WindowMenubarSetVisible(win, !enableFullscreen); if(enableFullscreen && !win->fullscreen) { XtUnmanageChild(main_window->menubar); main_window->fullscreen = TRUE; } else if(!enableFullscreen && win->fullscreen) { XtManageChild(main_window->menubar); main_window->fullscreen = FALSE; } XEvent ev; memset(&ev, 0, sizeof(XEvent)); ev.type = ClientMessage; ev.xclient.window = XtWindow(win->window); ev.xclient.message_type = net_wm_state; ev.xclient.format = 32; ev.xclient.data.l[0] = enableFullscreen ? 1 : 0; ev.xclient.data.l[1] = net_wm_state_fullscreen; ev.xclient.data.l[2] = 0; XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev); } void WindowMenubarSetVisible(MainWindow *win, bool visible) { if(visible) { XtManageChild(main_window->menubar); XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL); } else { XtUnmanageChild(main_window->menubar); XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL); } } void WindowSetFile(MainWindow *win, char *file) { if(win->file) { XtFree(win->file); } win->file = file; } static void filedialog_end( Widget widget, MainWindow *data, XmFileSelectionBoxCallbackStruct *selection) { XtUnmanageChild(widget); XtDestroyWidget(widget); } static void filedialog_select( Widget widget, MainWindow *data, XmFileSelectionBoxCallbackStruct *selection) { char *value = NULL; if(selection->value) { XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value); if(value) { WindowSetFile(data, value); // no need to free the value, because it is stored in MainWindow PlayerOpenFile(data); } } filedialog_end(widget, data, NULL); } static void FileOpenCB(Widget w, void *udata, void *cdata) { MainWindow *win = main_window; Arg args[16]; int n = 0; XtSetArg(args[n], XnNshowViewMenu, 1); n++; Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n); XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win); XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win); Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON); XtUnmanageChild(dirUp); XtManageChild(dialog); } static void ViewFullscreenCB(Widget w, void *udata, void *cdata) { if(main_window->fullscreen) { WindowFullscreen(main_window, FALSE); } else { WindowFullscreen(main_window, TRUE); } } void WindowAdjustAspectRatio(MainWindow *win) { if(!win->player) return; if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return; // we have a running player width video // adjust window aspect ratio (the window aspect ratio is different from // the video, because of window decoration, menubar and other extra controls) Dimension win_width, win_height; XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL); Dimension player_width, player_height; XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL); double r = (double)win->player->width / (double)win->player->height; double p_width = player_width; double p_height = p_width / r; Dimension new_width = p_width + win_width - player_width; Dimension new_height = p_height + win_height - player_height; XSizeHints hints; hints.flags = PAspect; hints.min_aspect.x = new_width; hints.min_aspect.y = new_height; hints.max_aspect.x = new_width; hints.max_aspect.y = new_height; XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints); } void WindowClosePlayer(MainWindow *win) { if(win->player) { PlayerDestroy(win->player); } win->player = NULL; }