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 void window_close_handler(Widget window, void *udata, void *cdata) {
43 WindowClosePlayer(main_window);
47 static unsigned int keycodeF;
49 static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
50 MainWindow *win = data;
51 if(win->fullscreen && event->xkey.keycode == keycodeF) {
52 WindowFullscreen(main_window, FALSE);
57 static int main_window_is_realized = 0;
59 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
60 if(!main_window_is_realized) {
61 if(XtIsRealized(widget)) {
62 main_window_is_realized = 1;
66 WindowAdjustAspectRatio(data);
69 static void WindowRealized(MainWindow *win) {
70 char *open_file = GetOpenFileArg();
72 size_t len = strlen(open_file);
73 char *file = XtMalloc(len+1);
74 memcpy(file, open_file, len);
76 WindowSetFile(win, file);
82 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
84 XmDrawingAreaCallbackStruct *cb = c;
86 if(win->player && win->player->isactive) {
87 PlayerHandleInput(win, win->player, cb);
91 static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
92 MainWindow *win = data;
93 if(!win->player || win->player->window == 0) return;
96 if(event->type == EnterNotify) {
97 printf("enter: grab pointer\n");
102 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
111 if(event->type == LeaveNotify) {
113 XtUngrabPointer(win->player_widget, CurrentTime);
117 if(event->type == MotionNotify) {
118 static int testv = 0;
119 printf("test %d\n", testv++);
123 if(event->type == KeyPress || event->type == KeyRelease) {
124 // redirect key events to the player window
125 event->xkey.window = win->player->window;
127 XtDisplay(win->player_widget),
135 MainWindow* WindowCreate(Display *display) {
139 MainWindow *window = malloc(sizeof(MainWindow));
140 memset(window, 0, sizeof(MainWindow));
141 main_window = window;
145 XtSetArg(args[n], XmNtitle, APP_NAME); n++;
146 window->window = XtAppCreateShell(
149 applicationShellWidgetClass,
150 //vendorShellWidgetClass,
156 Atom wm_delete_window;
157 wm_delete_window = XmInternAtom(
161 XmAddWMProtocolCallback(
164 window_close_handler,
168 XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
171 XtSetArg(args[n], XmNwidth, 360); n++;
172 XtSetArg(args[n], XmNheight, 220); n++;
173 XtSetArg(args[n], XmNshadowThickness, 0); n++;
174 Widget container = XmCreateForm(window->window, "form", args, n);
175 XtManageChild(container);
176 XtAddEventHandler(container, KeyPressMask, FALSE, windowKeyEH, window);
179 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
180 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
181 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
182 WindowCreateMenu(window, container, args, n);
185 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
186 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
187 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
188 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
189 XtSetArg(args[n], XmNtopWidget, window->menubar); n++;
190 XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
191 window->player_widget = XmCreateDrawingArea(container, "player", args, n);
192 XtManageChild(window->player_widget);
193 XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
194 XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
195 XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
196 EnterWindowMask | KeyPressMask | KeyReleaseMask |
197 LeaveWindowMask, FALSE, playerEH, window);
201 keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
206 MainWindow* GetMainWindow(void) {
210 void WindowShow(MainWindow *win) {
211 XtRealizeWidget(win->window);
215 * Creates a XmPushButton menu item
217 static Widget createMenuItem(
222 const char *accelerator,
223 char *accelerator_text,
224 XtCallbackProc callback,
230 XmString s1 = XmStringCreateSimple(label);
231 XtSetArg(args[n], XmNlabelString, s1); n++;
232 XtSetArg(args[n], XmNmnemonic, mnemonic); n++;
234 if(accelerator && accelerator_text) {
235 at = XmStringCreateSimple(accelerator_text);
236 XtSetArg(args[n], XmNaccelerator, accelerator); n++;
237 XtSetArg(args[n], XmNacceleratorText, at); n++;
240 Widget menuItem = XmCreatePushButtonGadget(menu, name, args, n);
241 XtManageChild(menuItem);
243 if(at) XmStringFree(at);
246 XtAddCallback(menuItem, XmNactivateCallback, (XtCallbackProc)callback, cbData);
252 static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nmbargs) {
253 Widget menubar = XmCreateMenuBar(parent, "menubar", mbargs, nmbargs);
254 XtManageChild(menubar);
255 win->menubar = menubar;
261 XmString s = XmStringCreateSimple("File");
262 Widget fileMenuItem = XtVaCreateManagedWidget(
264 xmCascadeButtonWidgetClass,
269 Widget fileMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 0, NULL, NULL);
271 s = XmStringCreateSimple("Playback");
272 Widget playMenuItem = XtVaCreateManagedWidget(
274 xmCascadeButtonWidgetClass,
279 Widget playMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 1, NULL, NULL);
281 s = XmStringCreateSimple("View");
282 Widget viewMenuItem = XtVaCreateManagedWidget(
284 xmCascadeButtonWidgetClass,
289 Widget viewMenu = XmVaCreateSimplePulldownMenu(menubar, "menu", 2, NULL, NULL);
292 createMenuItem(fileMenu, "fileOpen", "Open...", 'O', "Ctrl<Key>O", "Ctrl+O", FileOpenCB, NULL);
295 createMenuItem(viewMenu, "viewFullscreen", "Fullscreen", 'F', "<Key>F", "F", ViewFullscreenCB, NULL);
298 void go_fullscreen(Display *dsp, Window win)
301 Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False);
302 Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False);
303 memset(&xev, 0, sizeof(xev));
304 xev.type = ClientMessage;
305 xev.xclient.window = win;
306 xev.xclient.message_type = wm_state;
307 xev.xclient.format = 32;
308 xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
309 xev.xclient.data.l[1] = fullscreen;
310 xev.xclient.data.l[2] = 0;
311 XSendEvent(dsp, DefaultRootWindow(dsp), False,
312 SubstructureNotifyMask, &xev);
315 static Atom net_wm_state;
316 static Atom net_wm_state_fullscreen;
317 static int net_wm_atoms_initialized = 0;
319 void WindowFullscreen(MainWindow *win, bool enableFullscreen) {
320 Display *dpy = XtDisplay(win->window);
322 // init net_wm_state atoms
323 if(!net_wm_atoms_initialized) {
324 net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
325 net_wm_state_fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
326 net_wm_atoms_initialized = 1;
329 WindowMenubarSetVisible(win, !enableFullscreen);
330 if(enableFullscreen && !win->fullscreen) {
331 XtUnmanageChild(main_window->menubar);
332 main_window->fullscreen = TRUE;
333 } else if(!enableFullscreen && win->fullscreen) {
334 XtManageChild(main_window->menubar);
335 main_window->fullscreen = FALSE;
339 memset(&ev, 0, sizeof(XEvent));
340 ev.type = ClientMessage;
341 ev.xclient.window = XtWindow(win->window);
342 ev.xclient.message_type = net_wm_state;
343 ev.xclient.format = 32;
344 ev.xclient.data.l[0] = enableFullscreen ? 1 : 0;
345 ev.xclient.data.l[1] = net_wm_state_fullscreen;
346 ev.xclient.data.l[2] = 0;
347 XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
350 void WindowMenubarSetVisible(MainWindow *win, bool visible) {
352 XtManageChild(main_window->menubar);
353 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, main_window->menubar, NULL);
355 XtUnmanageChild(main_window->menubar);
356 XtVaSetValues(main_window->player_widget, XmNtopAttachment, XmATTACH_FORM, NULL);
360 void WindowSetFile(MainWindow *win, char *file) {
367 static void filedialog_end(
370 XmFileSelectionBoxCallbackStruct *selection)
372 XtUnmanageChild(widget);
373 XtDestroyWidget(widget);
376 static void filedialog_select(
379 XmFileSelectionBoxCallbackStruct *selection)
382 if(selection->value) {
383 XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
385 WindowSetFile(data, value);
386 // no need to free the value, because it is stored in MainWindow
388 PlayerOpenFile(data);
391 filedialog_end(widget, data, NULL);
397 static void FileOpenCB(Widget w, void *udata, void *cdata) {
398 MainWindow *win = main_window;
403 XtSetArg(args[n], XnNshowViewMenu, 1); n++;
404 Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
405 XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
406 XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
408 Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
409 XtUnmanageChild(dirUp);
411 XtManageChild(dialog);
414 static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
415 if(main_window->fullscreen) {
416 WindowFullscreen(main_window, FALSE);
418 WindowFullscreen(main_window, TRUE);
423 void WindowAdjustAspectRatio(MainWindow *win) {
424 if(!win->player) return;
425 if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
427 // we have a running player width video
428 // adjust window aspect ratio (the window aspect ratio is different from
429 // the video, because of window decoration, menubar and other extra controls)
431 Dimension win_width, win_height;
432 XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
433 Dimension player_width, player_height;
434 XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
436 double r = (double)win->player->width / (double)win->player->height;
437 double p_width = player_width;
438 double p_height = p_width / r;
440 Dimension new_width = p_width + win_width - player_width;
441 Dimension new_height = p_height + win_height - player_height;
444 hints.flags = PAspect;
445 hints.min_aspect.x = new_width;
446 hints.min_aspect.y = new_height;
447 hints.max_aspect.x = new_width;
448 hints.max_aspect.y = new_height;
449 XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
452 void WindowClosePlayer(MainWindow *win) {
454 PlayerDestroy(win->player);