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
176 #define DOUBLE_CLICK_TIME_MS 500
178 void WindowHandlePlayerEvent(MainWindow *win, XEvent *event) {
179 // event handler for intercepted player mouse events
180 // win->player is not NULL
182 int etype = event->type;
184 if(etype == MotionNotify) {
185 Time cur_motion_time = event->xmotion.time;
187 win->motion_playback_time = win->player->playback_time;
190 int x = event->xmotion.x_root;
191 int y = event->xmotion.y_root;
192 if(win->cursorhidden && cur_motion_time - win->player_event_time < IGNORE_MOTION_THRESHOLD_MS) {
193 int diff_x = abs(x - win->mouse_x);
194 int diff_y = abs(y - win->mouse_y);
195 if(diff_x > MOTION_POS_THRESHOLD_PIX || diff_y > MOTION_POS_THRESHOLD_PIX) {
196 WindowShowPlayerCursor(win);
202 win->player_event_time = cur_motion_time;
203 win->motion_playback_time = win->player->playback_time;
207 if(win->pwbuttonpressed) {
208 Display *dp = XtDisplay(win->window);
210 XtUngrabPointer(win->player_widget, CurrentTime);
213 memset(&xev, 0, sizeof(xev));
214 xev.type = ClientMessage;
215 xev.xclient.message_type = XInternAtom(dp, "_NET_WM_MOVERESIZE", False);
216 xev.xclient.window = XtWindow(win->window);
217 xev.xclient.format = 32;
218 xev.xclient.data.l[0] = x;
219 xev.xclient.data.l[1] = y;
220 xev.xclient.data.l[2] = 8; // _NET_WM_MOVERESIZE_MOVE
221 xev.xclient.data.l[3] = 1; // button1
222 xev.xclient.data.l[4] = 1; // source indication
224 XSendEvent(dp, DefaultRootWindow(dp), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
226 win->pwbuttonpressed = FALSE;
228 } else if(etype == ButtonPress) {
229 Time t = event->xbutton.time;
230 if(t - win->button_press_time < DOUBLE_CLICK_TIME_MS) {
232 WindowFullscreen(main_window, !win->fullscreen);
233 win->button_press_time = 0;
235 win->button_press_time = t;
237 win->pwbuttonpressed = 1;
238 } else if(etype == ButtonRelease) {
239 win->player_event_time = event->xbutton.time;
240 win->pwbuttonpressed = FALSE;
244 MainWindow* WindowCreate(Display *display) {
248 MainWindow *window = malloc(sizeof(MainWindow));
249 memset(window, 0, sizeof(MainWindow));
250 main_window = window;
254 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
255 window->window = XtAppCreateShell(
258 applicationShellWidgetClass,
259 //vendorShellWidgetClass,
265 Atom wm_delete_window;
266 wm_delete_window = XmInternAtom(
270 XmAddWMProtocolCallback(
273 window_close_handler,
277 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
280 XtSetArg(args[n], XmNwidth, 360); n++;
281 XtSetArg(args[n], XmNheight, 220); n++;
282 XtSetArg(args[n], XmNshadowThickness, 0); n++;
283 Widget container = XmCreateForm(window->window, "form", args, n);
284 XtManageChild(container);
285 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
288 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
289 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
290 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
291 WindowCreateMenu(window, container, args, n);
294 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
295 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
296 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
297 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
298 XtSetArg(args[n], XmNwidth, 300); n++;
299 window->sidebar = CreateSidebar(container, "sidebar", args, n);
300 //XtManageChild(window->sidebar);
303 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
304 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
305 XtSetArg(args[n], XmNrightWidget, window->sidebar); n++;
306 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
307 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
308 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
309 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
310 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
311 XtManageChild(window->player_widget);
312 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
313 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
314 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
315 EnterWindowMask | KeyPressMask | KeyReleaseMask |
316 LeaveWindowMask, FALSE, playerEH, window);
320 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
325 MainWindow* GetMainWindow(void) {
329 void WindowShow(MainWindow *win) {
330 XtRealizeWidget(win->window);
334 * Creates a XmPushButton menu item
336 static Widget createMenuItem(
341 const char *accelerator,
342 char *accelerator_text,
343 XtCallbackProc callback,
349 XmString s1 = XmStringCreateSimple(label);
350 XtSetArg(args[n], XmNlabelString, s1); n++;
351 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
353 if(accelerator && accelerator_text) {
354 at = XmStringCreateSimple(accelerator_text);
355 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
356 XtSetArg(args[n], XmNacceleratorText, at); n++;
359 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
360 XtManageChild(menuItem);
362 if(at) XmStringFree(at);
365 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
372 * Creates a XmToggleButton menu item
374 static Widget createToggleMenuItem(
379 Boolean defaultValue,
380 const char *accelerator,
381 char *accelerator_text,
382 XtCallbackProc callback,
388 XmString s1 = XmStringCreateSimple(label);
389 XtSetArg(args[n], XmNlabelString, s1); n++;
390 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
391 XtSetArg(args[n], XmNset, defaultValue); n++;
393 if(accelerator && accelerator_text) {
394 at = XmStringCreateSimple(accelerator_text);
395 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
396 XtSetArg(args[n], XmNacceleratorText, at); n++;
399 Widget menuItem = XmCreateToggleButtonGadget(menu, name, args, n);
400 XtManageChild(menuItem);
402 if(at) XmStringFree(at);
405 XtAddCallback(menuItem, XmNvalueChangedCallback, (XtCallbackProc)callback, cbData);
411 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
412 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
413 XtManageChild(menubar);
414 win->menubar = menubar;
420 XmString s = XmStringCreateSimple("File");
421 XtVaCreateManagedWidget(
423 xmCascadeButtonWidgetClass,
428 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
430 s = XmStringCreateSimple("Playback");
431 XtVaCreateManagedWidget(
433 xmCascadeButtonWidgetClass,
438 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
440 s = XmStringCreateSimple("View");
441 Widget viewMenuItem = XtVaCreateManagedWidget(
443 xmCascadeButtonWidgetClass,
448 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
451 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
452 createMenuItem(fileMenu, "fileQuit", "Exit", 'E', "Ctrl<Key>Q", "Ctrl+Q", FileQuitCB, NULL);
455 win->playRepeatTrackButton = createToggleMenuItem(playMenu, "playRepeatTrack", "Repeat", 'R', False, NULL, NULL, PlayRepeatCB, win);
456 win->playRepeatListButton = createToggleMenuItem(playMenu, "playRepeatList", "Repeat List", 'L', False, NULL, NULL, PlayRepeatListCB, win);
457 win->playAutoPlayButton = createToggleMenuItem(playMenu, "playAutoNext", "Autoplay Folder", 'A', False, NULL, NULL, PlayAutoPlayCB, win);
458 XtVaSetValues(win->playRepeatTrackButton, XmNindicatorType, XmONE_OF_MANY, NULL);
459 XtVaSetValues(win->playRepeatListButton, XmNindicatorType, XmONE_OF_MANY, NULL);
460 XtVaSetValues(win->playAutoPlayButton, XmNindicatorType, XmONE_OF_MANY, NULL);
463 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
464 win->viewSidebarButton = createToggleMenuItem(viewMenu, "viewSidebar", "View Sidebar", 'S', False, NULL, NULL, ViewSidebarCB, win);
467 void go_fullscreen(Display *dsp, Window win)
470 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
471 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
472 memset(&xev, 0, sizeof(xev));
473 xev.type = ClientMessage;
474 xev.xclient.window = win;
475 xev.xclient.message_type = wm_state;
476 xev.xclient.format = 32;
477 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
478 xev.xclient.data.l[1] = fullscreen;
479 xev.xclient.data.l[2] = 0;
480 XSendEvent(dsp, DefaultRootWindow(dsp), False,
481 SubstructureNotifyMask, &xev);
484 static Atom net_wm_state;
485 static Atom net_wm_state_fullscreen;
486 static int net_wm_atoms_initialized = 0;
488 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
489 Display *dpy = XtDisplay(win->window);
491 // init net_wm_state atoms
492 if(!net_wm_atoms_initialized) {
493 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
494 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
495 net_wm_atoms_initialized = 1;
498 WindowMenubarSetVisible(win, !enableFullscreen);
499 if(enableFullscreen && !win->fullscreen) {
500 XtUnmanageChild(main_window->menubar);
501 main_window->fullscreen = TRUE;
502 } else if(!enableFullscreen && win->fullscreen) {
503 XtManageChild(main_window->menubar);
504 main_window->fullscreen = FALSE;
508 memset(&ev, 0, sizeof(XEvent));
509 ev.type = ClientMessage;
510 ev.xclient.window = XtWindow(win->window);
511 ev.xclient.message_type = net_wm_state;
512 ev.xclient.format = 32;
513 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
514 ev.xclient.data.l[1] = net_wm_state_fullscreen;
515 ev.xclient.data.l[2] = 0;
516 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
519 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
521 XtManageChild(main_window->menubar);
522 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
524 XtUnmanageChild(main_window->menubar);
525 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
529 void WindowSetFile(MainWindow *win, char *file) {
536 static void filedialog_end(
539 XmFileSelectionBoxCallbackStruct *selection)
541 XtUnmanageChild(widget);
542 XtDestroyWidget(widget);
545 static void filedialog_select(
548 XmFileSelectionBoxCallbackStruct *selection)
551 if(selection->value) {
552 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
554 WindowSetFile(data, value);
555 // no need to free the value, because it is stored in MainWindow
557 PlayerOpenFile(data);
560 filedialog_end(widget, data, NULL);
566 static void FileOpenCB(Widget w, void *udata, void *cdata) {
567 MainWindow *win = main_window;
572 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
573 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
574 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
575 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
577 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
578 XtUnmanageChild(dirUp);
580 XtManageChild(dialog);
583 static void FileQuitCB(Widget w, void *udata, void *cdata) {
584 WindowClosePlayer(main_window);
588 static void PlayRepeatCB(Widget w, void *udata, void *cdata) {
589 MainWindow *win = udata;
590 win->repeatTrack = XmToggleButtonGadgetGetState(w);
592 win->autoplayFolder = 0;
593 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
594 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
597 static void PlayRepeatListCB(Widget w, void *udata, void *cdata) {
598 MainWindow *win = udata;
599 win->repeatList = XmToggleButtonGadgetGetState(w);
600 win->repeatTrack = 0;
601 win->autoplayFolder = 0;
602 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
603 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
606 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata) {
607 MainWindow *win = udata;
608 win->autoplayFolder = XmToggleButtonGadgetGetState(w);
609 win->repeatTrack = 0;
611 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
612 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
615 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
616 if(main_window->fullscreen) {
617 WindowFullscreen(main_window, FALSE);
619 WindowFullscreen(main_window, TRUE);
623 static void ViewSidebarCB(Widget w, void *udata, void *cdata) {
624 MainWindow *win = udata;
625 XmToggleButtonCallbackStruct *cb = cdata;
627 WindowShowSidebar(win);
629 WindowHideSidebar(win);
633 void WindowAdjustAspectRatio(MainWindow *win) {
634 if(!win->player) return;
635 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
637 // we have a running player width video
638 // adjust window aspect ratio (the window aspect ratio is different from
639 // the video, because of window decoration, menubar and other extra controls)
641 Dimension win_width, win_height;
642 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
643 Dimension player_width, player_height;
644 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
646 double r = (double)win->player->width / (double)win->player->height;
647 double p_width = player_width;
648 double p_height = p_width / r;
650 Dimension new_width = p_width + win_width - player_width;
651 Dimension new_height = p_height + win_height - player_height;
654 hints.flags = PAspect;
655 hints.min_aspect.x = new_width;
656 hints.min_aspect.y = new_height;
657 hints.max_aspect.x = new_width;
658 hints.max_aspect.y = new_height;
659 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
662 void WindowClosePlayer(MainWindow *win) {
664 PlayerDestroy(win->player);
667 WindowShowPlayerCursor(win);
670 void WindowHidePlayerCursor(MainWindow *win) {
671 if(!win->cursorhidden && win->player && win->player->window != 0) {
672 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
673 win->cursorhidden = True;
674 XFlush(XtDisplay(win->player_widget));
678 void WindowShowPlayerCursor(MainWindow *win) {
679 if(win->cursorhidden && win->player && win->player->window != 0) {
680 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
681 XFlush(XtDisplay(win->player_widget));
683 win->cursorhidden = False;
686 void WindowHideSidebar(MainWindow *win) {
687 XtUnmanageChild(win->sidebar);
688 XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_FORM, NULL);
691 void WindowShowSidebar(MainWindow *win) {
692 XtManageChild(win->sidebar);
693 XtVaSetValues(win->player_widget, XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, win->sidebar, NULL);