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 FileQuitCB(Widget w, void *udata, void *cdata);
39 static void PlayRepeatCB(Widget w, void *udata, void *cdata);
40 static void PlayRepeatListCB(Widget w, void *udata, void *cdata);
41 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata);
42 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
44 static void WindowRealized(MainWindow *win);
46 static int blank_cursor_init = 0;
47 static Pixmap blank_cursor_pixmap;
48 static Cursor blank_cursor;
50 static void init_blank_cursor(Widget w) {
55 blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
56 if(!blank_cursor_pixmap) return;
58 blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
60 XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
61 blank_cursor_init = 1;
64 static void window_close_handler(Widget window, void *udata, void *cdata) {
65 FileQuitCB(window, NULL, NULL);
68 static unsigned int keycodeF;
70 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
71 MainWindow *win = data;
72 if(win->fullscreen && event->xkey.keycode == keycodeF) {
73 WindowFullscreen(main_window, FALSE);
78 static int main_window_is_realized = 0;
80 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
81 if(!main_window_is_realized) {
82 if(XtIsRealized(widget)) {
83 main_window_is_realized = 1;
87 WindowAdjustAspectRatio(data);
90 static void WindowRealized(MainWindow *win) {
91 char *open_file = GetOpenFileArg();
93 size_t len = strlen(open_file);
94 char *file = XtMalloc(len+1);
95 memcpy(file, open_file, len);
97 WindowSetFile(win, file);
102 if(!blank_cursor_init) {
103 init_blank_cursor(win->player_widget);
107 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
109 XmDrawingAreaCallbackStruct *cb = c;
111 if(win->player && win->player->isactive) {
112 PlayerHandleInput(win, win->player, cb);
116 static void windowGrabButton(MainWindow *win) {
123 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
128 win->buttongrab = True;
131 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
132 MainWindow *win = data;
133 int etype = event->type;
136 if(etype == EnterNotify) {
138 windowGrabButton(win);
141 if(etype == LeaveNotify) {
143 //XtUngrabButton(win->player_widget, AnyButton, AnyModifier);
144 //win->buttongrab = False;
149 if(etype == ButtonPress || etype == ButtonRelease || etype == KeyPress || etype == KeyRelease) {
150 //printf("button press\n");
154 if(!win->player || win->player->window == 0) return;
156 WindowHandlePlayerEvent(win, event);
159 // redirect key events to the player window
160 //printf("redirect\n");
161 event->xkey.window = win->player->window;
163 XtDisplay(win->player_widget),
171 #define IGNORE_MOTION_THRESHOLD_MS 1000
172 #define MOTION_POS_THRESHOLD_PIX 5
174 #define DOUBLE_CLICK_TIME_MS 500
176 void WindowHandlePlayerEvent(MainWindow *win, XEvent *event) {
177 // event handler for intercepted player mouse events
178 // win->player is not NULL
180 int etype = event->type;
182 if(etype == MotionNotify) {
183 Time cur_motion_time = event->xmotion.time;
184 int x = event->xmotion.x;
185 int y = event->xmotion.y;
186 if(win->cursorhidden && cur_motion_time - win->player_event_time < IGNORE_MOTION_THRESHOLD_MS) {
187 int diff_x = abs(x - win->mouse_x_orig);
188 int diff_y = abs(y - win->mouse_y_orig);
189 if(diff_x > MOTION_POS_THRESHOLD_PIX || diff_y > MOTION_POS_THRESHOLD_PIX) {
190 WindowShowPlayerCursor(win);
193 win->mouse_x_orig = x;
194 win->mouse_y_orig = y;
196 win->player_event_time = cur_motion_time;
197 win->motion_playback_time = win->player->playback_time;
198 } else if(etype == ButtonPress) {
199 Time t = event->xbutton.time;
200 if(t - win->button_press_time < DOUBLE_CLICK_TIME_MS) {
202 WindowFullscreen(main_window, !win->fullscreen);
203 win->button_press_time = 0;
205 win->button_press_time = t;
207 } else if(etype == ButtonRelease) {
208 win->player_event_time = event->xbutton.time;
212 MainWindow* WindowCreate(Display *display) {
216 MainWindow *window = malloc(sizeof(MainWindow));
217 memset(window, 0, sizeof(MainWindow));
218 main_window = window;
222 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
223 window->window = XtAppCreateShell(
226 applicationShellWidgetClass,
227 //vendorShellWidgetClass,
233 Atom wm_delete_window;
234 wm_delete_window = XmInternAtom(
238 XmAddWMProtocolCallback(
241 window_close_handler,
245 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
248 XtSetArg(args[n], XmNwidth, 360); n++;
249 XtSetArg(args[n], XmNheight, 220); n++;
250 XtSetArg(args[n], XmNshadowThickness, 0); n++;
251 Widget container = XmCreateForm(window->window, "form", args, n);
252 XtManageChild(container);
253 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
256 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
257 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
258 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
259 WindowCreateMenu(window, container, args, n);
262 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
263 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
264 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
265 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
266 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
267 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
268 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
269 XtManageChild(window->player_widget);
270 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
271 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
272 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
273 EnterWindowMask | KeyPressMask | KeyReleaseMask |
274 LeaveWindowMask, FALSE, playerEH, window);
278 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
283 MainWindow* GetMainWindow(void) {
287 void WindowShow(MainWindow *win) {
288 XtRealizeWidget(win->window);
292 * Creates a XmPushButton menu item
294 static Widget createMenuItem(
299 const char *accelerator,
300 char *accelerator_text,
301 XtCallbackProc callback,
307 XmString s1 = XmStringCreateSimple(label);
308 XtSetArg(args[n], XmNlabelString, s1); n++;
309 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
311 if(accelerator && accelerator_text) {
312 at = XmStringCreateSimple(accelerator_text);
313 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
314 XtSetArg(args[n], XmNacceleratorText, at); n++;
317 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
318 XtManageChild(menuItem);
320 if(at) XmStringFree(at);
323 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
330 * Creates a XmToggleButton menu item
332 static Widget createToggleMenuItem(
337 Boolean defaultValue,
338 const char *accelerator,
339 char *accelerator_text,
340 XtCallbackProc callback,
346 XmString s1 = XmStringCreateSimple(label);
347 XtSetArg(args[n], XmNlabelString, s1); n++;
348 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
349 XtSetArg(args[n], XmNset, defaultValue); n++;
351 if(accelerator && accelerator_text) {
352 at = XmStringCreateSimple(accelerator_text);
353 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
354 XtSetArg(args[n], XmNacceleratorText, at); n++;
357 Widget menuItem = XmCreateToggleButtonGadget(menu, name, args, n);
358 XtManageChild(menuItem);
360 if(at) XmStringFree(at);
363 XtAddCallback(menuItem, XmNvalueChangedCallback, (XtCallbackProc)callback, cbData);
369 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
370 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
371 XtManageChild(menubar);
372 win->menubar = menubar;
378 XmString s = XmStringCreateSimple("File");
379 XtVaCreateManagedWidget(
381 xmCascadeButtonWidgetClass,
386 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
388 s = XmStringCreateSimple("Playback");
389 XtVaCreateManagedWidget(
391 xmCascadeButtonWidgetClass,
396 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
398 s = XmStringCreateSimple("View");
399 Widget viewMenuItem = XtVaCreateManagedWidget(
401 xmCascadeButtonWidgetClass,
406 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
409 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
410 createMenuItem(fileMenu, "fileQuit", "Exit", 'E', "Ctrl<Key>Q", "Ctrl+Q", FileQuitCB, NULL);
413 win->playRepeatTrackButton = createToggleMenuItem(playMenu, "playRepeatTrack", "Repeat", 'R', False, NULL, NULL, PlayRepeatCB, win);
414 win->playRepeatListButton = createToggleMenuItem(playMenu, "playRepeatList", "Repeat List", 'L', False, NULL, NULL, PlayRepeatListCB, win);
415 win->playAutoPlayButton = createToggleMenuItem(playMenu, "playAutoNext", "Autoplay Folder", 'A', False, NULL, NULL, PlayAutoPlayCB, win);
416 XtVaSetValues(win->playRepeatTrackButton, XmNindicatorType, XmONE_OF_MANY, NULL);
417 XtVaSetValues(win->playRepeatListButton, XmNindicatorType, XmONE_OF_MANY, NULL);
418 XtVaSetValues(win->playAutoPlayButton, XmNindicatorType, XmONE_OF_MANY, NULL);
421 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
424 void go_fullscreen(Display *dsp, Window win)
427 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
428 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
429 memset(&xev, 0, sizeof(xev));
430 xev.type = ClientMessage;
431 xev.xclient.window = win;
432 xev.xclient.message_type = wm_state;
433 xev.xclient.format = 32;
434 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
435 xev.xclient.data.l[1] = fullscreen;
436 xev.xclient.data.l[2] = 0;
437 XSendEvent(dsp, DefaultRootWindow(dsp), False,
438 SubstructureNotifyMask, &xev);
441 static Atom net_wm_state;
442 static Atom net_wm_state_fullscreen;
443 static int net_wm_atoms_initialized = 0;
445 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
446 Display *dpy = XtDisplay(win->window);
448 // init net_wm_state atoms
449 if(!net_wm_atoms_initialized) {
450 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
451 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
452 net_wm_atoms_initialized = 1;
455 WindowMenubarSetVisible(win, !enableFullscreen);
456 if(enableFullscreen && !win->fullscreen) {
457 XtUnmanageChild(main_window->menubar);
458 main_window->fullscreen = TRUE;
459 } else if(!enableFullscreen && win->fullscreen) {
460 XtManageChild(main_window->menubar);
461 main_window->fullscreen = FALSE;
465 memset(&ev, 0, sizeof(XEvent));
466 ev.type = ClientMessage;
467 ev.xclient.window = XtWindow(win->window);
468 ev.xclient.message_type = net_wm_state;
469 ev.xclient.format = 32;
470 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
471 ev.xclient.data.l[1] = net_wm_state_fullscreen;
472 ev.xclient.data.l[2] = 0;
473 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
476 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
478 XtManageChild(main_window->menubar);
479 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
481 XtUnmanageChild(main_window->menubar);
482 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
486 void WindowSetFile(MainWindow *win, char *file) {
493 static void filedialog_end(
496 XmFileSelectionBoxCallbackStruct *selection)
498 XtUnmanageChild(widget);
499 XtDestroyWidget(widget);
502 static void filedialog_select(
505 XmFileSelectionBoxCallbackStruct *selection)
508 if(selection->value) {
509 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
511 WindowSetFile(data, value);
512 // no need to free the value, because it is stored in MainWindow
514 PlayerOpenFile(data);
517 filedialog_end(widget, data, NULL);
523 static void FileOpenCB(Widget w, void *udata, void *cdata) {
524 MainWindow *win = main_window;
529 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
530 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
531 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
532 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
534 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
535 XtUnmanageChild(dirUp);
537 XtManageChild(dialog);
540 static void FileQuitCB(Widget w, void *udata, void *cdata) {
541 WindowClosePlayer(main_window);
545 static void PlayRepeatCB(Widget w, void *udata, void *cdata) {
546 MainWindow *win = udata;
547 win->repeatTrack = XmToggleButtonGadgetGetState(w);
549 win->autoplayFolder = 0;
550 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
551 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
554 static void PlayRepeatListCB(Widget w, void *udata, void *cdata) {
555 MainWindow *win = udata;
556 win->repeatList = XmToggleButtonGadgetGetState(w);
557 win->repeatTrack = 0;
558 win->autoplayFolder = 0;
559 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
560 XtVaSetValues(win->playAutoPlayButton, XmNset, 0, NULL);
563 static void PlayAutoPlayCB(Widget w, void *udata, void *cdata) {
564 MainWindow *win = udata;
565 win->autoplayFolder = XmToggleButtonGadgetGetState(w);
566 win->repeatTrack = 0;
568 XtVaSetValues(win->playRepeatTrackButton, XmNset, 0, NULL);
569 XtVaSetValues(win->playRepeatListButton, XmNset, 0, NULL);
572 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
573 if(main_window->fullscreen) {
574 WindowFullscreen(main_window, FALSE);
576 WindowFullscreen(main_window, TRUE);
581 void WindowAdjustAspectRatio(MainWindow *win) {
582 if(!win->player) return;
583 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
585 // we have a running player width video
586 // adjust window aspect ratio (the window aspect ratio is different from
587 // the video, because of window decoration, menubar and other extra controls)
589 Dimension win_width, win_height;
590 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
591 Dimension player_width, player_height;
592 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
594 double r = (double)win->player->width / (double)win->player->height;
595 double p_width = player_width;
596 double p_height = p_width / r;
598 Dimension new_width = p_width + win_width - player_width;
599 Dimension new_height = p_height + win_height - player_height;
602 hints.flags = PAspect;
603 hints.min_aspect.x = new_width;
604 hints.min_aspect.y = new_height;
605 hints.max_aspect.x = new_width;
606 hints.max_aspect.y = new_height;
607 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
610 void WindowClosePlayer(MainWindow *win) {
612 PlayerDestroy(win->player);
615 WindowShowPlayerCursor(win);
618 void WindowHidePlayerCursor(MainWindow *win) {
619 if(!win->cursorhidden && win->player && win->player->window != 0) {
620 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
621 win->cursorhidden = True;
622 XFlush(XtDisplay(win->player_widget));
626 void WindowShowPlayerCursor(MainWindow *win) {
627 if(win->cursorhidden && win->player && win->player->window != 0) {
628 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
629 XFlush(XtDisplay(win->player_widget));
631 win->cursorhidden = False;