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 WindowRealized(MainWindow *win);
42 static int blank_cursor_init = 0;
43 static Pixmap blank_cursor_pixmap;
44 static Cursor blank_cursor;
46 static void init_blank_cursor(Widget w) {
51 blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
52 if(!blank_cursor_pixmap) return;
54 blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
56 XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
57 blank_cursor_init = 1;
60 static void window_close_handler(Widget window, void *udata, void *cdata) {
61 WindowClosePlayer(main_window);
65 static unsigned int keycodeF;
67 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
68 MainWindow *win = data;
69 if(win->fullscreen && event->xkey.keycode == keycodeF) {
70 WindowFullscreen(main_window, FALSE);
75 static int main_window_is_realized = 0;
77 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
78 if(!main_window_is_realized) {
79 if(XtIsRealized(widget)) {
80 main_window_is_realized = 1;
84 WindowAdjustAspectRatio(data);
87 static void WindowRealized(MainWindow *win) {
88 char *open_file = GetOpenFileArg();
90 size_t len = strlen(open_file);
91 char *file = XtMalloc(len+1);
92 memcpy(file, open_file, len);
94 WindowSetFile(win, file);
99 if(!blank_cursor_init) {
100 init_blank_cursor(win->player_widget);
104 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
106 XmDrawingAreaCallbackStruct *cb = c;
108 if(win->player && win->player->isactive) {
109 PlayerHandleInput(win, win->player, cb);
113 static void windowGrabButton(MainWindow *win) {
120 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
125 win->buttongrab = True;
128 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
129 MainWindow *win = data;
130 int etype = event->type;
133 if(etype == EnterNotify) {
135 windowGrabButton(win);
138 if(etype == LeaveNotify) {
140 //XtUngrabButton(win->player_widget, AnyButton, AnyModifier);
141 //win->buttongrab = False;
146 if(etype == ButtonPress || etype == ButtonRelease || etype == KeyPress || etype == KeyRelease) {
147 //printf("button press\n");
151 if(!win->player || win->player->window == 0) return;
153 WindowHandlePlayerEvent(win, event);
156 // redirect key events to the player window
157 //printf("redirect\n");
158 event->xkey.window = win->player->window;
160 XtDisplay(win->player_widget),
168 #define IGNORE_MOTION_THRESHOLD_MS 1000
169 #define MOTION_POS_THRESHOLD_PIX 5
171 void WindowHandlePlayerEvent(MainWindow *win, XEvent *event) {
172 // event handler for intercepted player mouse events
173 // win->player is not NULL
175 int etype = event->type;
177 if(etype == MotionNotify) {
178 Time cur_motion_time = event->xmotion.time;
179 int x = event->xmotion.x;
180 int y = event->xmotion.y;
181 if(win->cursorhidden && cur_motion_time - win->player_event_time < IGNORE_MOTION_THRESHOLD_MS) {
182 int diff_x = abs(x - win->mouse_x_orig);
183 int diff_y = abs(y - win->mouse_y_orig);
184 if(diff_x > MOTION_POS_THRESHOLD_PIX || diff_y > MOTION_POS_THRESHOLD_PIX) {
185 WindowShowPlayerCursor(win);
188 win->mouse_x_orig = x;
189 win->mouse_y_orig = y;
191 win->player_event_time = cur_motion_time;
192 win->motion_playback_time = win->player->playback_time;
193 } else if(etype == ButtonPress) {
194 win->player_event_time = event->xbutton.time;
195 } else if(etype == ButtonRelease) {
196 win->player_event_time = event->xbutton.time;
200 MainWindow* WindowCreate(Display *display) {
204 MainWindow *window = malloc(sizeof(MainWindow));
205 memset(window, 0, sizeof(MainWindow));
206 main_window = window;
210 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
211 window->window = XtAppCreateShell(
214 applicationShellWidgetClass,
215 //vendorShellWidgetClass,
221 Atom wm_delete_window;
222 wm_delete_window = XmInternAtom(
226 XmAddWMProtocolCallback(
229 window_close_handler,
233 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
236 XtSetArg(args[n], XmNwidth, 360); n++;
237 XtSetArg(args[n], XmNheight, 220); n++;
238 XtSetArg(args[n], XmNshadowThickness, 0); n++;
239 Widget container = XmCreateForm(window->window, "form", args, n);
240 XtManageChild(container);
241 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
244 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
245 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
246 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
247 WindowCreateMenu(window, container, args, n);
250 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
251 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
252 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
253 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
254 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
255 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
256 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
257 XtManageChild(window->player_widget);
258 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
259 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
260 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
261 EnterWindowMask | KeyPressMask | KeyReleaseMask |
262 LeaveWindowMask, FALSE, playerEH, window);
266 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
271 MainWindow* GetMainWindow(void) {
275 void WindowShow(MainWindow *win) {
276 XtRealizeWidget(win->window);
280 * Creates a XmPushButton menu item
282 static Widget createMenuItem(
287 const char *accelerator,
288 char *accelerator_text,
289 XtCallbackProc callback,
295 XmString s1 = XmStringCreateSimple(label);
296 XtSetArg(args[n], XmNlabelString, s1); n++;
297 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
299 if(accelerator && accelerator_text) {
300 at = XmStringCreateSimple(accelerator_text);
301 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
302 XtSetArg(args[n], XmNacceleratorText, at); n++;
305 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
306 XtManageChild(menuItem);
308 if(at) XmStringFree(at);
311 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
317 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
318 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
319 XtManageChild(menubar);
320 win->menubar = menubar;
326 XmString s = XmStringCreateSimple("File");
327 Widget fileMenuItem = XtVaCreateManagedWidget(
329 xmCascadeButtonWidgetClass,
334 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
336 s = XmStringCreateSimple("Playback");
337 Widget playMenuItem = XtVaCreateManagedWidget(
339 xmCascadeButtonWidgetClass,
344 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
346 s = XmStringCreateSimple("View");
347 Widget viewMenuItem = XtVaCreateManagedWidget(
349 xmCascadeButtonWidgetClass,
354 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
357 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
360 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
363 void go_fullscreen(Display *dsp, Window win)
366 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
367 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
368 memset(&xev, 0, sizeof(xev));
369 xev.type = ClientMessage;
370 xev.xclient.window = win;
371 xev.xclient.message_type = wm_state;
372 xev.xclient.format = 32;
373 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
374 xev.xclient.data.l[1] = fullscreen;
375 xev.xclient.data.l[2] = 0;
376 XSendEvent(dsp, DefaultRootWindow(dsp), False,
377 SubstructureNotifyMask, &xev);
380 static Atom net_wm_state;
381 static Atom net_wm_state_fullscreen;
382 static int net_wm_atoms_initialized = 0;
384 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
385 Display *dpy = XtDisplay(win->window);
387 // init net_wm_state atoms
388 if(!net_wm_atoms_initialized) {
389 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
390 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
391 net_wm_atoms_initialized = 1;
394 WindowMenubarSetVisible(win, !enableFullscreen);
395 if(enableFullscreen && !win->fullscreen) {
396 XtUnmanageChild(main_window->menubar);
397 main_window->fullscreen = TRUE;
398 } else if(!enableFullscreen && win->fullscreen) {
399 XtManageChild(main_window->menubar);
400 main_window->fullscreen = FALSE;
404 memset(&ev, 0, sizeof(XEvent));
405 ev.type = ClientMessage;
406 ev.xclient.window = XtWindow(win->window);
407 ev.xclient.message_type = net_wm_state;
408 ev.xclient.format = 32;
409 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
410 ev.xclient.data.l[1] = net_wm_state_fullscreen;
411 ev.xclient.data.l[2] = 0;
412 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
415 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
417 XtManageChild(main_window->menubar);
418 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
420 XtUnmanageChild(main_window->menubar);
421 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
425 void WindowSetFile(MainWindow *win, char *file) {
432 static void filedialog_end(
435 XmFileSelectionBoxCallbackStruct *selection)
437 XtUnmanageChild(widget);
438 XtDestroyWidget(widget);
441 static void filedialog_select(
444 XmFileSelectionBoxCallbackStruct *selection)
447 if(selection->value) {
448 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
450 WindowSetFile(data, value);
451 // no need to free the value, because it is stored in MainWindow
453 PlayerOpenFile(data);
456 filedialog_end(widget, data, NULL);
462 static void FileOpenCB(Widget w, void *udata, void *cdata) {
463 MainWindow *win = main_window;
468 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
469 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
470 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
471 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
473 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
474 XtUnmanageChild(dirUp);
476 XtManageChild(dialog);
479 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
480 if(main_window->fullscreen) {
481 WindowFullscreen(main_window, FALSE);
483 WindowFullscreen(main_window, TRUE);
488 void WindowAdjustAspectRatio(MainWindow *win) {
489 if(!win->player) return;
490 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
492 // we have a running player width video
493 // adjust window aspect ratio (the window aspect ratio is different from
494 // the video, because of window decoration, menubar and other extra controls)
496 Dimension win_width, win_height;
497 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
498 Dimension player_width, player_height;
499 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
501 double r = (double)win->player->width / (double)win->player->height;
502 double p_width = player_width;
503 double p_height = p_width / r;
505 Dimension new_width = p_width + win_width - player_width;
506 Dimension new_height = p_height + win_height - player_height;
509 hints.flags = PAspect;
510 hints.min_aspect.x = new_width;
511 hints.min_aspect.y = new_height;
512 hints.max_aspect.x = new_width;
513 hints.max_aspect.y = new_height;
514 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
517 void WindowClosePlayer(MainWindow *win) {
519 PlayerDestroy(win->player);
522 WindowShowPlayerCursor(win);
525 void WindowHidePlayerCursor(MainWindow *win) {
526 if(!win->cursorhidden && win->player && win->player->window != 0) {
527 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
528 win->cursorhidden = True;
529 XFlush(XtDisplay(win->player_widget));
533 void WindowShowPlayerCursor(MainWindow *win) {
534 if(win->cursorhidden && win->player && win->player->window != 0) {
535 XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
536 win->cursorhidden = False;
537 XFlush(XtDisplay(win->player_widget));