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 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
61 XmDrawingAreaCallbackStruct *cb = c;
63 if(win->player && win->player->isactive) {
64 PlayerHandleInput(win, win->player, cb);
68 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
69 MainWindow *win = data;
70 if(!win->player || win->player->window == 0) return;
73 if(event->type == EnterNotify) {
74 printf("enter: grab pointer\n");
79 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
88 if(event->type == LeaveNotify) {
90 XtUngrabPointer(win->player_widget, CurrentTime);
94 if(event->type == MotionNotify) {
96 printf("test %d\n", testv++);
100 if(event->type == KeyPress || event->type == KeyRelease) {
101 // redirect key events to the player window
102 event->xkey.window = win->player->window;
104 XtDisplay(win->player_widget),
112 MainWindow* WindowCreate(Display *display) {
116 MainWindow *window = malloc(sizeof(MainWindow));
117 memset(window, 0, sizeof(MainWindow));
118 main_window = window;
122 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
123 window->window = XtAppCreateShell(
126 applicationShellWidgetClass,
127 //vendorShellWidgetClass,
133 Atom wm_delete_window;
134 wm_delete_window = XmInternAtom(
138 XmAddWMProtocolCallback(
141 window_close_handler,
145 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
148 XtSetArg(args[n], XmNwidth, 360); n++;
149 XtSetArg(args[n], XmNheight, 220); n++;
150 XtSetArg(args[n], XmNshadowThickness, 0); n++;
151 Widget container = XmCreateForm(window->window, "form", args, n);
152 XtManageChild(container);
153 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
156 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
157 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
158 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
159 WindowCreateMenu(window, container, args, n);
162 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
163 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
164 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
165 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
166 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
167 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
168 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
169 XtManageChild(window->player_widget);
170 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
171 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
172 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
173 EnterWindowMask | KeyPressMask | KeyReleaseMask |
174 LeaveWindowMask, FALSE, playerEH, window);
178 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
183 MainWindow* GetMainWindow(void) {
187 void WindowShow(MainWindow *win) {
188 XtRealizeWidget(win->window);
192 * Creates a XmPushButton menu item
194 static Widget createMenuItem(
199 const char *accelerator,
200 char *accelerator_text,
201 XtCallbackProc callback,
207 XmString s1 = XmStringCreateSimple(label);
208 XtSetArg(args[n], XmNlabelString, s1); n++;
209 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
211 if(accelerator && accelerator_text) {
212 at = XmStringCreateSimple(accelerator_text);
213 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
214 XtSetArg(args[n], XmNacceleratorText, at); n++;
217 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
218 XtManageChild(menuItem);
220 if(at) XmStringFree(at);
223 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
229 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
230 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
231 XtManageChild(menubar);
232 win->menubar = menubar;
238 XmString s = XmStringCreateSimple("File");
239 Widget fileMenuItem = XtVaCreateManagedWidget(
241 xmCascadeButtonWidgetClass,
246 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
248 s = XmStringCreateSimple("Playback");
249 Widget playMenuItem = XtVaCreateManagedWidget(
251 xmCascadeButtonWidgetClass,
256 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
258 s = XmStringCreateSimple("View");
259 Widget viewMenuItem = XtVaCreateManagedWidget(
261 xmCascadeButtonWidgetClass,
266 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
269 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
272 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
275 void go_fullscreen(Display *dsp, Window win)
278 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
279 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
280 memset(&xev, 0, sizeof(xev));
281 xev.type = ClientMessage;
282 xev.xclient.window = win;
283 xev.xclient.message_type = wm_state;
284 xev.xclient.format = 32;
285 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
286 xev.xclient.data.l[1] = fullscreen;
287 xev.xclient.data.l[2] = 0;
288 XSendEvent(dsp, DefaultRootWindow(dsp), False,
289 SubstructureNotifyMask, &xev);
292 static Atom net_wm_state;
293 static Atom net_wm_state_fullscreen;
294 static int net_wm_atoms_initialized = 0;
296 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
297 Display *dpy = XtDisplay(win->window);
299 // init net_wm_state atoms
300 if(!net_wm_atoms_initialized) {
301 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
302 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
303 net_wm_atoms_initialized = 1;
306 WindowMenubarSetVisible(win, !enableFullscreen);
307 if(enableFullscreen && !win->fullscreen) {
308 XtUnmanageChild(main_window->menubar);
309 main_window->fullscreen = TRUE;
310 } else if(!enableFullscreen && win->fullscreen) {
311 XtManageChild(main_window->menubar);
312 main_window->fullscreen = FALSE;
316 memset(&ev, 0, sizeof(XEvent));
317 ev.type = ClientMessage;
318 ev.xclient.window = XtWindow(win->window);
319 ev.xclient.message_type = net_wm_state;
320 ev.xclient.format = 32;
321 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
322 ev.xclient.data.l[1] = net_wm_state_fullscreen;
323 ev.xclient.data.l[2] = 0;
324 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
327 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
329 XtManageChild(main_window->menubar);
330 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
332 XtUnmanageChild(main_window->menubar);
333 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
339 static void filedialog_end(
342 XmFileSelectionBoxCallbackStruct *selection)
344 XtUnmanageChild(widget);
345 XtDestroyWidget(widget);
348 static void filedialog_select(
351 XmFileSelectionBoxCallbackStruct *selection)
354 if(selection->value) {
355 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
361 // no need to free the value, because it is stored in MainWindow
363 PlayerOpenFile(data);
366 filedialog_end(widget, data, NULL);
372 static void FileOpenCB(Widget w, void *udata, void *cdata) {
373 MainWindow *win = main_window;
378 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
379 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
380 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
381 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
383 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
384 XtUnmanageChild(dirUp);
386 XtManageChild(dialog);
389 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
390 if(main_window->fullscreen) {
391 WindowFullscreen(main_window, FALSE);
393 WindowFullscreen(main_window, TRUE);
398 void WindowAdjustAspectRatio(MainWindow *win) {
399 if(!win->player) return;
400 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
402 // we have a running player width video
403 // adjust window aspect ratio (the window aspect ratio is different from
404 // the video, because of window decoration, menubar and other extra controls)
406 Dimension win_width, win_height;
407 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
408 Dimension player_width, player_height;
409 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
411 double r = (double)win->player->width / (double)win->player->height;
412 double p_width = player_width;
413 double p_height = p_width / r;
415 Dimension new_width = p_width + win_width - player_width;
416 Dimension new_height = p_height + win_height - player_height;
419 hints.flags = PAspect;
420 hints.min_aspect.x = new_width;
421 hints.min_aspect.y = new_height;
422 hints.max_aspect.x = new_width;
423 hints.max_aspect.y = new_height;
424 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
427 void WindowClosePlayer(MainWindow *win) {
429 PlayerDestroy(win->player);