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.
36 static MainWindow *main_window;
38 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int nargs);
40 static void FileOpenCB(Widget w, void *udata, void *cdata);
41 static void FileQuitCB(Widget w, void *udata, void *cdata);
42 static void PlayRepeatCB(Widget w, void *udata, void *cdata);
43 static void PlayRepeatListCB(Widget w, void *udata, void *cdata);
44 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata);
45 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
46 static void ViewSidebarCB(Widget w, void *udata, void *cdata);
48 static void WindowRealized(MainWindow *win);
50 static int blank_cursor_init = 0;
51 static Pixmap blank_cursor_pixmap;
52 static Cursor blank_cursor;
54 static void init_blank_cursor(Widget w) {
59 blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
60 if(!blank_cursor_pixmap) return;
62 blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
64 XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
65 blank_cursor_init = 1;
68 static void window_close_handler(Widget window, void *udata, void *cdata) {
69 FileQuitCB(window, NULL, NULL);
72 static unsigned int keycodeF;
74 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
75 MainWindow *win = data;
76 if(win->fullscreen && event->xkey.keycode == keycodeF) {
77 WindowFullscreen(main_window, FALSE);
82 static int main_window_is_realized = 0;
84 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
85 if(!main_window_is_realized) {
86 if(XtIsRealized(widget)) {
87 main_window_is_realized = 1;
91 WindowAdjustAspectRatio(data);
94 static void WindowRealized(MainWindow *win) {
95 char *open_file = GetOpenFileArg();
97 PlayListAddFile(win, open_file);
98 PlayListPlayNext(win, true);
102 if(!blank_cursor_init) {
103 init_blank_cursor(win->player_widget);
106 XdndEnable(win->window);
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;
253 MainWindow* WindowCreate(Display *display) {
257 MainWindow *window = malloc(sizeof(MainWindow));
258 memset(window, 0, sizeof(MainWindow));
259 main_window = window;
263 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
264 window->window = XtAppCreateShell(
267 applicationShellWidgetClass,
268 //vendorShellWidgetClass,
274 Atom wm_delete_window;
275 wm_delete_window = XmInternAtom(
279 XmAddWMProtocolCallback(
282 window_close_handler,
286 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
289 XtSetArg(args[n], XmNwidth, 360); n++;
290 XtSetArg(args[n], XmNheight, 220); n++;
291 XtSetArg(args[n], XmNshadowThickness, 0); n++;
292 Widget container = XmCreateForm(window->window, "form", args, n);
293 XtManageChild(container);
294 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
297 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
298 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
299 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
300 WindowCreateMenu(window, container, args, n);
303 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
304 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
305 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
306 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
307 XtSetArg(args[n], XmNwidth, 300); n++;
308 window->sidebar = CreateSidebar(container, "sidebar", args, n);
309 //XtManageChild(window->sidebar);
312 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
313 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
314 XtSetArg(args[n], XmNrightWidget, window->sidebar); n++;
315 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
316 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
317 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
318 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
319 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
320 XtManageChild(window->player_widget);
321 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
322 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
323 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
324 EnterWindowMask | KeyPressMask | KeyReleaseMask |
325 LeaveWindowMask, FALSE, playerEH, window);
329 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
332 PlayListInit(window);
337 MainWindow* GetMainWindow(void) {
341 void WindowShow(MainWindow *win) {
342 XtRealizeWidget(win->window);
346 * Creates a XmPushButton menu item
348 static Widget createMenuItem(
353 const char *accelerator,
354 char *accelerator_text,
355 XtCallbackProc callback,
361 XmString s1 = XmStringCreateSimple(label);
362 XtSetArg(args[n], XmNlabelString, s1); n++;
363 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
365 if(accelerator && accelerator_text) {
366 at = XmStringCreateSimple(accelerator_text);
367 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
368 XtSetArg(args[n], XmNacceleratorText, at); n++;
371 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
372 XtManageChild(menuItem);
374 if(at) XmStringFree(at);
377 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
384 * Creates a XmToggleButton menu item
386 static Widget createToggleMenuItem(
391 Boolean defaultValue,
392 const char *accelerator,
393 char *accelerator_text,
394 XtCallbackProc callback,
400 XmString s1 = XmStringCreateSimple(label);
401 XtSetArg(args[n], XmNlabelString, s1); n++;
402 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
403 XtSetArg(args[n], XmNset, defaultValue); n++;
405 if(accelerator && accelerator_text) {
406 at = XmStringCreateSimple(accelerator_text);
407 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
408 XtSetArg(args[n], XmNacceleratorText, at); n++;
411 Widget menuItem = XmCreateToggleButtonGadget(menu, name, args, n);
412 XtManageChild(menuItem);
414 if(at) XmStringFree(at);
417 XtAddCallback(menuItem, XmNvalueChangedCallback, (XtCallbackProc)callback, cbData);
423 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
424 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
425 XtManageChild(menubar);
426 win->menubar = menubar;
432 XmString s = XmStringCreateSimple("File");
433 XtVaCreateManagedWidget(
435 xmCascadeButtonWidgetClass,
440 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
442 s = XmStringCreateSimple("Playback");
443 XtVaCreateManagedWidget(
445 xmCascadeButtonWidgetClass,
450 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
452 s = XmStringCreateSimple("View");
453 Widget viewMenuItem = XtVaCreateManagedWidget(
455 xmCascadeButtonWidgetClass,
460 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
463 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
464 createMenuItem(fileMenu, "fileQuit", "Exit", 'E', "Ctrl<Key>Q", "Ctrl+Q", FileQuitCB, NULL);
467 win->playRepeatTrackButton = createToggleMenuItem(playMenu, "playRepeatTrack", "Repeat", 'R', False, NULL, NULL, PlayRepeatCB, win);
468 win->playRepeatListButton = createToggleMenuItem(playMenu, "playRepeatList", "Repeat List", 'L', False, NULL, NULL, PlayRepeatListCB, win);
469 win->playAutoPlayButton = createToggleMenuItem(playMenu, "playAutoNext", "Autoplay Folder", 'A', False, NULL, NULL, PlayAutoPlayCB, win);
470 XtVaSetValues(win->playRepeatTrackButton, XmNindicatorType, XmONE_OF_MANY, NULL);
471 XtVaSetValues(win->playRepeatListButton, XmNindicatorType, XmONE_OF_MANY, NULL);
472 XtVaSetValues(win->playAutoPlayButton, XmNindicatorType, XmONE_OF_MANY, NULL);
475 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
476 win->viewSidebarButton = createToggleMenuItem(viewMenu, "viewSidebar", "View Sidebar", 'S', False, NULL, NULL, ViewSidebarCB, win);
479 void go_fullscreen(Display *dsp, Window win)
482 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
483 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
484 memset(&xev, 0, sizeof(xev));
485 xev.type = ClientMessage;
486 xev.xclient.window = win;
487 xev.xclient.message_type = wm_state;
488 xev.xclient.format = 32;
489 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
490 xev.xclient.data.l[1] = fullscreen;
491 xev.xclient.data.l[2] = 0;
492 XSendEvent(dsp, DefaultRootWindow(dsp), False,
493 SubstructureNotifyMask, &xev);
496 static Atom net_wm_state;
497 static Atom net_wm_state_fullscreen;
498 static int net_wm_atoms_initialized = 0;
500 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
501 Display *dpy = XtDisplay(win->window);
503 // init net_wm_state atoms
504 if(!net_wm_atoms_initialized) {
505 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
506 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
507 net_wm_atoms_initialized = 1;
510 WindowMenubarSetVisible(win, !enableFullscreen);
511 if(enableFullscreen && !win->fullscreen) {
512 XtUnmanageChild(main_window->menubar);
513 main_window->fullscreen = TRUE;
514 } else if(!enableFullscreen && win->fullscreen) {
515 XtManageChild(main_window->menubar);
516 main_window->fullscreen = FALSE;
520 memset(&ev, 0, sizeof(XEvent));
521 ev.type = ClientMessage;
522 ev.xclient.window = XtWindow(win->window);
523 ev.xclient.message_type = net_wm_state;
524 ev.xclient.format = 32;
525 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
526 ev.xclient.data.l[1] = net_wm_state_fullscreen;
527 ev.xclient.data.l[2] = 0;
528 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
531 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
533 XtManageChild(main_window->menubar);
534 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
536 XtUnmanageChild(main_window->menubar);
537 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
541 static void filedialog_end(
544 XmFileSelectionBoxCallbackStruct *selection)
546 XtUnmanageChild(widget);
547 XtDestroyWidget(widget);
550 static void filedialog_select(
553 XmFileSelectionBoxCallbackStruct *selection)
556 if(selection->value) {
557 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
559 PlayListAddFile(data, value);
560 PlayListPlayNext(data, true);
564 filedialog_end(widget, data, NULL);
570 static void FileOpenCB(Widget w, void *udata, void *cdata) {
571 MainWindow *win = main_window;
576 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
577 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
578 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
579 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
581 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
582 XtUnmanageChild(dirUp);
584 XtManageChild(dialog);
587 static void FileQuitCB(Widget w, void *udata, void *cdata) {
588 WindowClosePlayer(main_window);
592 static void PlayRepeatCB(Widget w, void *udata, void *cdata) {
593 MainWindow *win = udata;
594 win->playlist.repeatTrack = XmToggleButtonGadgetGetState(w);
595 win->playlist.repeatList = 0;
596 win->playlist.autoplayFolder = 0;
597 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
598 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
601 static void PlayRepeatListCB(Widget w, void *udata, void *cdata) {
602 MainWindow *win = udata;
603 win->playlist.repeatList = XmToggleButtonGadgetGetState(w);
604 win->playlist.repeatTrack = 0;
605 win->playlist.autoplayFolder = 0;
606 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
607 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
610 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata) {
611 MainWindow *win = udata;
612 win->playlist.autoplayFolder = XmToggleButtonGadgetGetState(w);
613 win->playlist.repeatTrack = 0;
614 win->playlist.repeatList = 0;
615 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
616 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
619 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
620 if(main_window->fullscreen) {
621 WindowFullscreen(main_window, FALSE);
623 WindowFullscreen(main_window, TRUE);
627 static void ViewSidebarCB(Widget w, void *udata, void *cdata) {
628 MainWindow *win = udata;
629 XmToggleButtonCallbackStruct *cb = cdata;
631 WindowShowSidebar(win);
633 WindowHideSidebar(win);
637 void WindowAdjustAspectRatio(MainWindow *win) {
638 if(!win->player) return;
639 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
641 // we have a running player width video
642 // adjust window aspect ratio (the window aspect ratio is different from
643 // the video, because of window decoration, menubar and other extra controls)
645 Dimension win_width, win_height;
646 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
647 Dimension player_width, player_height;
648 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
650 double r = (double)win->player->width / (double)win->player->height;
651 double p_width = player_width;
652 double p_height = p_width / r;
654 Dimension new_width = p_width + win_width - player_width;
655 Dimension new_height = p_height + win_height - player_height;
658 hints.flags = PAspect;
659 hints.min_aspect.x = new_width;
660 hints.min_aspect.y = new_height;
661 hints.max_aspect.x = new_width;
662 hints.max_aspect.y = new_height;
663 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
666 void WindowClosePlayer(MainWindow *win) {
668 PlayerDestroy(win->player);
671 WindowShowPlayerCursor(win);
674 void WindowHidePlayerCursor(MainWindow *win) {
675 if(!win->cursorhidden && win->player && win->player->window != 0) {
676 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
677 win->cursorhidden = True;
678 XFlush(XtDisplay(win->player_widget));
682 void WindowShowPlayerCursor(MainWindow *win) {
683 if(win->cursorhidden && win->player && win->player->window != 0) {
684 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
685 XFlush(XtDisplay(win->player_widget));
687 win->cursorhidden = False;
690 void WindowHideSidebar(MainWindow *win) {
691 XtUnmanageChild(win->sidebar);
692 XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_FORM, NULL);
695 void WindowShowSidebar(MainWindow *win) {
696 XtManageChild(win->sidebar);
697 XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, win->sidebar, NULL);