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.
34 static MainWindow *main_window;
36 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs);
38 static void FileOpenCB(Widget w, void *udata, void *cdata);
39 static void FileQuitCB(Widget w, void *udata, void *cdata);
40 static void PlayRepeatCB(Widget w, void *udata, void *cdata);
41 static void PlayRepeatListCB(Widget w, void *udata, void *cdata);
42 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata);
43 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
44 static void ViewSidebarCB(Widget w, void *udata, void *cdata);
46 static void WindowRealized(MainWindow *win);
48 static int blank_cursor_init = 0;
49 static Pixmap blank_cursor_pixmap;
50 static Cursor blank_cursor;
52 static void init_blank_cursor(Widget w) {
57 blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
58 if(!blank_cursor_pixmap) return;
60 blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
62 XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
63 blank_cursor_init = 1;
66 static void window_close_handler(Widget window, void *udata, void *cdata) {
67 FileQuitCB(window, NULL, NULL);
70 static unsigned int keycodeF;
72 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
73 MainWindow *win = data;
74 if(win->fullscreen && event->xkey.keycode == keycodeF) {
75 WindowFullscreen(main_window, FALSE);
80 static int main_window_is_realized = 0;
82 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
83 if(!main_window_is_realized) {
84 if(XtIsRealized(widget)) {
85 main_window_is_realized = 1;
89 WindowAdjustAspectRatio(data);
92 static void WindowRealized(MainWindow *win) {
93 char *open_file = GetOpenFileArg();
95 size_t len = strlen(open_file);
96 char *file = XtMalloc(len+1);
97 memcpy(file, open_file, len);
99 WindowSetFile(win, file);
104 if(!blank_cursor_init) {
105 init_blank_cursor(win->player_widget);
109 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
111 XmDrawingAreaCallbackStruct *cb = c;
113 if(win->player && win->player->isactive) {
114 PlayerHandleInput(win, win->player, cb);
118 static void windowGrabButton(MainWindow *win) {
125 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
130 win->buttongrab = True;
133 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
134 MainWindow *win = data;
135 int etype = event->type;
138 if(etype == EnterNotify) {
140 windowGrabButton(win);
143 if(etype == LeaveNotify) {
145 //XtUngrabButton(win->player_widget, AnyButton, AnyModifier);
146 //win->buttongrab = False;
151 if(etype == ButtonPress || etype == ButtonRelease || etype == KeyPress || etype == KeyRelease) {
152 //printf("button press\n");
156 if(!win->player || win->player->window == 0) return;
158 WindowHandlePlayerEvent(win, event);
161 // redirect key events to the player window
162 //printf("redirect\n");
163 event->xkey.window = win->player->window;
165 XtDisplay(win->player_widget),
173 #define IGNORE_MOTION_THRESHOLD_MS 1000
174 #define MOTION_POS_THRESHOLD_PIX 5
175 #define OSD_BOTTOM_THRESHOLD 0.09
177 #define DOUBLE_CLICK_TIME_MS 500
179 void WindowHandlePlayerEvent(MainWindow *win, XEvent *event) {
180 // event handler for intercepted player mouse events
181 // win->player is not NULL
183 int etype = event->type;
185 if(etype == MotionNotify) {
186 Time cur_motion_time = event->xmotion.time;
188 win->motion_playback_time = win->player->playback_time;
191 int x = event->xmotion.x_root;
192 int y = event->xmotion.y_root;
193 if(win->cursorhidden && cur_motion_time - win->player_event_time < IGNORE_MOTION_THRESHOLD_MS) {
194 int diff_x = abs(x - win->mouse_x);
195 int diff_y = abs(y - win->mouse_y);
196 if(diff_x > MOTION_POS_THRESHOLD_PIX || diff_y > MOTION_POS_THRESHOLD_PIX) {
197 WindowShowPlayerCursor(win);
203 win->player_event_time = cur_motion_time;
204 win->motion_playback_time = win->player->playback_time;
208 if(win->pwbuttonpressed) {
209 Display *dp = XtDisplay(win->window);
211 XtUngrabPointer(win->player_widget, CurrentTime);
214 memset(&xev, 0, sizeof(xev));
215 xev.type = ClientMessage;
216 xev.xclient.message_type = XInternAtom(dp, "_NET_WM_MOVERESIZE", False);
217 xev.xclient.window = XtWindow(win->window);
218 xev.xclient.format = 32;
219 xev.xclient.data.l[0] = x;
220 xev.xclient.data.l[1] = y;
221 xev.xclient.data.l[2] = 8; // _NET_WM_MOVERESIZE_MOVE
222 xev.xclient.data.l[3] = 1; // button1
223 xev.xclient.data.l[4] = 1; // source indication
225 XSendEvent(dp, DefaultRootWindow(dp), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
227 win->pwbuttonpressed = FALSE;
229 } else if(etype == ButtonPress) {
230 Time t = event->xbutton.time;
232 int yi = win->player_widget->core.height - event->xbutton.y;
233 if((float)yi/(float)win->player_widget->core.height < OSD_BOTTOM_THRESHOLD) {
234 win->button_press_time = 0;
236 if(t - win->button_press_time < DOUBLE_CLICK_TIME_MS) {
238 WindowFullscreen(main_window, !win->fullscreen);
239 win->button_press_time = 0;
241 win->button_press_time = t;
243 win->pwbuttonpressed = 1;
245 } else if(etype == ButtonRelease) {
246 win->player_event_time = event->xbutton.time;
247 win->pwbuttonpressed = FALSE;
251 MainWindow* WindowCreate(Display *display) {
255 MainWindow *window = malloc(sizeof(MainWindow));
256 memset(window, 0, sizeof(MainWindow));
257 main_window = window;
261 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
262 window->window = XtAppCreateShell(
265 applicationShellWidgetClass,
266 //vendorShellWidgetClass,
272 Atom wm_delete_window;
273 wm_delete_window = XmInternAtom(
277 XmAddWMProtocolCallback(
280 window_close_handler,
284 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
287 XtSetArg(args[n], XmNwidth, 360); n++;
288 XtSetArg(args[n], XmNheight, 220); n++;
289 XtSetArg(args[n], XmNshadowThickness, 0); n++;
290 Widget container = XmCreateForm(window->window, "form", args, n);
291 XtManageChild(container);
292 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
295 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
296 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
297 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
298 WindowCreateMenu(window, container, args, n);
301 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
302 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
303 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
304 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
305 XtSetArg(args[n], XmNwidth, 300); n++;
306 window->sidebar = CreateSidebar(container, "sidebar", args, n);
307 //XtManageChild(window->sidebar);
310 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
311 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
312 XtSetArg(args[n], XmNrightWidget, window->sidebar); n++;
313 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
314 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
315 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
316 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
317 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
318 XtManageChild(window->player_widget);
319 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
320 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
321 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
322 EnterWindowMask | KeyPressMask | KeyReleaseMask |
323 LeaveWindowMask, FALSE, playerEH, window);
327 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
332 MainWindow* GetMainWindow(void) {
336 void WindowShow(MainWindow *win) {
337 XtRealizeWidget(win->window);
341 * Creates a XmPushButton menu item
343 static Widget createMenuItem(
348 const char *accelerator,
349 char *accelerator_text,
350 XtCallbackProc callback,
356 XmString s1 = XmStringCreateSimple(label);
357 XtSetArg(args[n], XmNlabelString, s1); n++;
358 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
360 if(accelerator && accelerator_text) {
361 at = XmStringCreateSimple(accelerator_text);
362 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
363 XtSetArg(args[n], XmNacceleratorText, at); n++;
366 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
367 XtManageChild(menuItem);
369 if(at) XmStringFree(at);
372 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
379 * Creates a XmToggleButton menu item
381 static Widget createToggleMenuItem(
386 Boolean defaultValue,
387 const char *accelerator,
388 char *accelerator_text,
389 XtCallbackProc callback,
395 XmString s1 = XmStringCreateSimple(label);
396 XtSetArg(args[n], XmNlabelString, s1); n++;
397 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
398 XtSetArg(args[n], XmNset, defaultValue); n++;
400 if(accelerator && accelerator_text) {
401 at = XmStringCreateSimple(accelerator_text);
402 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
403 XtSetArg(args[n], XmNacceleratorText, at); n++;
406 Widget menuItem = XmCreateToggleButtonGadget(menu, name, args, n);
407 XtManageChild(menuItem);
409 if(at) XmStringFree(at);
412 XtAddCallback(menuItem, XmNvalueChangedCallback, (XtCallbackProc)callback, cbData);
418 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
419 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
420 XtManageChild(menubar);
421 win->menubar = menubar;
427 XmString s = XmStringCreateSimple("File");
428 XtVaCreateManagedWidget(
430 xmCascadeButtonWidgetClass,
435 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
437 s = XmStringCreateSimple("Playback");
438 XtVaCreateManagedWidget(
440 xmCascadeButtonWidgetClass,
445 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
447 s = XmStringCreateSimple("View");
448 Widget viewMenuItem = XtVaCreateManagedWidget(
450 xmCascadeButtonWidgetClass,
455 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
458 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
459 createMenuItem(fileMenu, "fileQuit", "Exit", 'E', "Ctrl<Key>Q", "Ctrl+Q", FileQuitCB, NULL);
462 win->playRepeatTrackButton = createToggleMenuItem(playMenu, "playRepeatTrack", "Repeat", 'R', False, NULL, NULL, PlayRepeatCB, win);
463 win->playRepeatListButton = createToggleMenuItem(playMenu, "playRepeatList", "Repeat List", 'L', False, NULL, NULL, PlayRepeatListCB, win);
464 win->playAutoPlayButton = createToggleMenuItem(playMenu, "playAutoNext", "Autoplay Folder", 'A', False, NULL, NULL, PlayAutoPlayCB, win);
465 XtVaSetValues(win->playRepeatTrackButton, XmNindicatorType, XmONE_OF_MANY, NULL);
466 XtVaSetValues(win->playRepeatListButton, XmNindicatorType, XmONE_OF_MANY, NULL);
467 XtVaSetValues(win->playAutoPlayButton, XmNindicatorType, XmONE_OF_MANY, NULL);
470 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
471 win->viewSidebarButton = createToggleMenuItem(viewMenu, "viewSidebar", "View Sidebar", 'S', False, NULL, NULL, ViewSidebarCB, win);
474 void go_fullscreen(Display *dsp, Window win)
477 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
478 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
479 memset(&xev, 0, sizeof(xev));
480 xev.type = ClientMessage;
481 xev.xclient.window = win;
482 xev.xclient.message_type = wm_state;
483 xev.xclient.format = 32;
484 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
485 xev.xclient.data.l[1] = fullscreen;
486 xev.xclient.data.l[2] = 0;
487 XSendEvent(dsp, DefaultRootWindow(dsp), False,
488 SubstructureNotifyMask, &xev);
491 static Atom net_wm_state;
492 static Atom net_wm_state_fullscreen;
493 static int net_wm_atoms_initialized = 0;
495 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
496 Display *dpy = XtDisplay(win->window);
498 // init net_wm_state atoms
499 if(!net_wm_atoms_initialized) {
500 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
501 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
502 net_wm_atoms_initialized = 1;
505 WindowMenubarSetVisible(win, !enableFullscreen);
506 if(enableFullscreen && !win->fullscreen) {
507 XtUnmanageChild(main_window->menubar);
508 main_window->fullscreen = TRUE;
509 } else if(!enableFullscreen && win->fullscreen) {
510 XtManageChild(main_window->menubar);
511 main_window->fullscreen = FALSE;
515 memset(&ev, 0, sizeof(XEvent));
516 ev.type = ClientMessage;
517 ev.xclient.window = XtWindow(win->window);
518 ev.xclient.message_type = net_wm_state;
519 ev.xclient.format = 32;
520 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
521 ev.xclient.data.l[1] = net_wm_state_fullscreen;
522 ev.xclient.data.l[2] = 0;
523 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
526 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
528 XtManageChild(main_window->menubar);
529 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
531 XtUnmanageChild(main_window->menubar);
532 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
536 void WindowSetFile(MainWindow *win, char *file) {
543 static void filedialog_end(
546 XmFileSelectionBoxCallbackStruct *selection)
548 XtUnmanageChild(widget);
549 XtDestroyWidget(widget);
552 static void filedialog_select(
555 XmFileSelectionBoxCallbackStruct *selection)
558 if(selection->value) {
559 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
561 WindowSetFile(data, value);
562 // no need to free the value, because it is stored in MainWindow
564 PlayerOpenFile(data);
567 filedialog_end(widget, data, NULL);
573 static void FileOpenCB(Widget w, void *udata, void *cdata) {
574 MainWindow *win = main_window;
579 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
580 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
581 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
582 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
584 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
585 XtUnmanageChild(dirUp);
587 XtManageChild(dialog);
590 static void FileQuitCB(Widget w, void *udata, void *cdata) {
591 WindowClosePlayer(main_window);
595 static void PlayRepeatCB(Widget w, void *udata, void *cdata) {
596 MainWindow *win = udata;
597 win->repeatTrack = XmToggleButtonGadgetGetState(w);
599 win->autoplayFolder = 0;
600 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
601 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
604 static void PlayRepeatListCB(Widget w, void *udata, void *cdata) {
605 MainWindow *win = udata;
606 win->repeatList = XmToggleButtonGadgetGetState(w);
607 win->repeatTrack = 0;
608 win->autoplayFolder = 0;
609 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
610 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
613 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata) {
614 MainWindow *win = udata;
615 win->autoplayFolder = XmToggleButtonGadgetGetState(w);
616 win->repeatTrack = 0;
618 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
619 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
622 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
623 if(main_window->fullscreen) {
624 WindowFullscreen(main_window, FALSE);
626 WindowFullscreen(main_window, TRUE);
630 static void ViewSidebarCB(Widget w, void *udata, void *cdata) {
631 MainWindow *win = udata;
632 XmToggleButtonCallbackStruct *cb = cdata;
634 WindowShowSidebar(win);
636 WindowHideSidebar(win);
640 void WindowAdjustAspectRatio(MainWindow *win) {
641 if(!win->player) return;
642 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
644 // we have a running player width video
645 // adjust window aspect ratio (the window aspect ratio is different from
646 // the video, because of window decoration, menubar and other extra controls)
648 Dimension win_width, win_height;
649 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
650 Dimension player_width, player_height;
651 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
653 double r = (double)win->player->width / (double)win->player->height;
654 double p_width = player_width;
655 double p_height = p_width / r;
657 Dimension new_width = p_width + win_width - player_width;
658 Dimension new_height = p_height + win_height - player_height;
661 hints.flags = PAspect;
662 hints.min_aspect.x = new_width;
663 hints.min_aspect.y = new_height;
664 hints.max_aspect.x = new_width;
665 hints.max_aspect.y = new_height;
666 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
669 void WindowClosePlayer(MainWindow *win) {
671 PlayerDestroy(win->player);
674 WindowShowPlayerCursor(win);
677 void WindowHidePlayerCursor(MainWindow *win) {
678 if(!win->cursorhidden && win->player && win->player->window != 0) {
679 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
680 win->cursorhidden = True;
681 XFlush(XtDisplay(win->player_widget));
685 void WindowShowPlayerCursor(MainWindow *win) {
686 if(win->cursorhidden && win->player && win->player->window != 0) {
687 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
688 XFlush(XtDisplay(win->player_widget));
690 win->cursorhidden = False;
693 void WindowHideSidebar(MainWindow *win) {
694 XtUnmanageChild(win->sidebar);
695 XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_FORM, NULL);
698 void WindowShowSidebar(MainWindow *win) {
699 XtManageChild(win->sidebar);
700 XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, win->sidebar, NULL);