]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/x11/x11_window.cpp
skins2(X11): add missing <limits.h>
[vlc] / modules / gui / skins2 / x11 / x11_window.cpp
index 426d0f3238b042e74f0c6741d65127011b83d599..e9e85de39cc8f67c7d0148623999a5afd5101c04 100644 (file)
@@ -25,6 +25,7 @@
 #ifdef X11_SKINS
 
 #include <X11/Xatom.h>
+#include <limits.h>
 
 #include "../src/generic_window.hpp"
 #include "../src/vlcproc.hpp"
 #include "x11_dragdrop.hpp"
 #include "x11_factory.hpp"
 
+#include <assert.h>
 
 X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
                       X11Display &rDisplay, bool dragDrop, bool playOnDrop,
-                      X11Window *pParentWindow ):
+                      X11Window *pParentWindow, GenericWindow::WindowType_t type ):
     OSWindow( pIntf ), m_rDisplay( rDisplay ), m_pParent( pParentWindow ),
-    m_dragDrop( dragDrop )
+    m_dragDrop( dragDrop ), m_type ( type )
 {
     XSetWindowAttributes attr;
     unsigned long valuemask;
+    string name_type;
 
-    if (pParentWindow)
+    if( type == GenericWindow::FullscreenWindow )
+    {
+        m_wnd_parent = DefaultRootWindow( XDISPLAY );
+
+        int i_screen = DefaultScreen( XDISPLAY );
+
+        attr.event_mask = ExposureMask | StructureNotifyMask;
+        attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
+        attr.backing_store = Always;
+        valuemask = CWBackingStore | CWBackPixel | CWEventMask;
+
+        if( NET_WM_STATE_FULLSCREEN == None )
+        {
+            attr.override_redirect = True;
+            valuemask = valuemask | CWOverrideRedirect;
+        }
+
+        name_type = "Fullscreen";
+    }
+    else if( type == GenericWindow::VoutWindow )
     {
         m_wnd_parent = pParentWindow->m_wnd;
 
@@ -54,6 +76,8 @@ X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
         attr.backing_store = Always;
         attr.background_pixel = BlackPixel( XDISPLAY, i_screen );
         valuemask = CWBackingStore | CWBackPixel | CWEventMask;
+
+        name_type = "VoutWindow";
     }
     else
     {
@@ -61,10 +85,12 @@ X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
 
         attr.event_mask = ExposureMask | StructureNotifyMask;
         valuemask = CWEventMask;
+
+        name_type = "TopWindow";
     }
 
     // Create the window
-    m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 1, 1, 0, 0,
+    m_wnd = XCreateWindow( XDISPLAY, m_wnd_parent, -10, 0, 10, 10, 0, 0,
                            InputOutput, CopyFromParent, valuemask, &attr );
 
     // wait for X server to process the previous commands
@@ -77,9 +103,19 @@ X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
     }
 
     // Select events received by the window
-    XSelectInput( XDISPLAY, m_wnd, ExposureMask|KeyPressMask|
-                  PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
-                  LeaveWindowMask|FocusChangeMask );
+    long event_mask;
+    if( type == GenericWindow::VoutWindow )
+    {
+        event_mask =  ExposureMask|KeyPressMask|
+                      LeaveWindowMask|FocusChangeMask;
+    }
+    else
+    {
+        event_mask =  ExposureMask|KeyPressMask|
+                      PointerMotionMask|ButtonPressMask|ButtonReleaseMask|
+                      LeaveWindowMask|FocusChangeMask;
+    }
+    XSelectInput( XDISPLAY, m_wnd, event_mask );
 
     // Store a pointer on the generic window in a map
     X11Factory *pFactory = (X11Factory*)X11Factory::instance( getIntf() );
@@ -118,11 +154,43 @@ X11Window::X11Window( intf_thread_t *pIntf, GenericWindow &rWindow,
     }
 
     // Change the window title
-    XStoreName( XDISPLAY, m_wnd, "VLC" );
+    string name_window = "VLC (" + name_type + ")";
+    XStoreName( XDISPLAY, m_wnd, name_window.c_str() );
 
     // Associate the window to the main "parent" window
     XSetTransientForHint( XDISPLAY, m_wnd, m_rDisplay.getMainWindow() );
 
+    // initialize Class Hint
+    XClassHint classhint;
+    classhint.res_name = (char*) "vlc";
+    classhint.res_class = (char*) "Vlc";
+    XSetClassHint( XDISPLAY, m_wnd, &classhint );
+
+    // initialize WM_CLIENT_MACHINE
+    char* hostname = NULL;
+    long host_name_max = sysconf( _SC_HOST_NAME_MAX );
+    if( host_name_max <= 0 )
+        host_name_max = _POSIX_HOST_NAME_MAX;
+    hostname = new char[host_name_max];
+    if( hostname && gethostname( hostname, host_name_max ) == 0 )
+    {
+        hostname[host_name_max - 1] = '\0';
+
+        XTextProperty textprop;
+        textprop.value = (unsigned char *) hostname;
+        textprop.encoding = XA_STRING;
+        textprop.format = 8;
+        textprop.nitems = strlen( hostname );
+        XSetWMClientMachine( XDISPLAY, m_wnd, &textprop);
+    }
+    delete[] hostname;
+
+    // initialize EWMH pid
+    pid_t pid = getpid();
+    assert(  NET_WM_PID != None );
+    XChangeProperty( XDISPLAY, m_wnd, NET_WM_PID, XA_CARDINAL, 32,
+                     PropModeReplace, (unsigned char *)&pid, 1 );
+
 }
 
 
@@ -146,10 +214,10 @@ void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
     Window new_parent =
            OSHandle ? (Window) OSHandle : DefaultRootWindow( XDISPLAY );
 
+    XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
     if( w && h )
         XResizeWindow( XDISPLAY, m_wnd, w, h );
 
-    XReparentWindow( XDISPLAY, m_wnd, new_parent, x, y);
     m_wnd_parent = new_parent;
 }
 
@@ -157,7 +225,21 @@ void X11Window::reparent( void* OSHandle, int x, int y, int w, int h )
 void X11Window::show() const
 {
     // Map the window
-    XMapRaised( XDISPLAY, m_wnd );
+    if( m_type == GenericWindow::VoutWindow )
+    {
+       XLowerWindow( XDISPLAY, m_wnd );
+       XMapWindow( XDISPLAY, m_wnd );
+    }
+    else if( m_type == GenericWindow::FullscreenWindow )
+    {
+        XMapRaised( XDISPLAY, m_wnd );
+        setFullscreen();
+        // toggleOnTop( true );
+    }
+    else
+    {
+        XMapRaised( XDISPLAY, m_wnd );
+    }
 }
 
 
@@ -167,7 +249,6 @@ void X11Window::hide() const
     XUnmapWindow( XDISPLAY, m_wnd );
 }
 
-
 void X11Window::moveResize( int left, int top, int width, int height ) const
 {
     if( width && height )
@@ -185,87 +266,80 @@ void X11Window::raise() const
 
 void X11Window::setOpacity( uint8_t value ) const
 {
-    // Sorry, the opacity cannot be changed :)
+    if( NET_WM_WINDOW_OPACITY == None )
+        return;
+
+    if( 255==value )
+        XDeleteProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY);
+    else
+    {
+        uint32_t opacity = value * ((uint32_t)-1/255);
+        XChangeProperty(XDISPLAY, m_wnd, NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
+                        PropModeReplace, (unsigned char *) &opacity, 1L);
+    }
+    XSync( XDISPLAY, False );
 }
 
 
-void X11Window::toggleOnTop( bool onTop ) const
+void X11Window::setFullscreen( ) const
 {
-    int i_ret, i_format;
-    unsigned long i, i_items, i_bytesafter;
-    Atom net_wm_supported, net_wm_state, net_wm_state_on_top,net_wm_state_above;
-    union { Atom *p_atom; unsigned char *p_char; } p_args;
-
-    p_args.p_atom = NULL;
-
-    net_wm_supported = XInternAtom( XDISPLAY, "_NET_SUPPORTED", False );
-
-    i_ret = XGetWindowProperty( XDISPLAY, DefaultRootWindow( XDISPLAY ),
-                                net_wm_supported,
-                                0, 16384, False, AnyPropertyType,
-                                &net_wm_supported,
-                                &i_format, &i_items, &i_bytesafter,
-                                (unsigned char **)&p_args );
-
-    if( i_ret != Success || i_items == 0 ) return; /* Not supported */
-
-    net_wm_state = XInternAtom( XDISPLAY, "_NET_WM_STATE", False );
-    net_wm_state_on_top = XInternAtom( XDISPLAY, "_NET_WM_STATE_STAYS_ON_TOP",
-                                       False );
-
-    for( i = 0; i < i_items; i++ )
+    if( NET_WM_STATE_FULLSCREEN != None )
     {
-        if( p_args.p_atom[i] == net_wm_state_on_top ) break;
-    }
-
-    if( i == i_items )
-    { /* use _NET_WM_STATE_ABOVE if window manager
-       * doesn't handle _NET_WM_STATE_STAYS_ON_TOP */
+        XClientMessageEvent event;
+        memset( &event, 0, sizeof( XClientMessageEvent ) );
 
-        net_wm_state_above = XInternAtom( XDISPLAY, "_NET_WM_STATE_ABOVE",
-                                          False);
-        for( i = 0; i < i_items; i++ )
-        {
-            if( p_args.p_atom[i] == net_wm_state_above ) break;
-        }
+        event.type = ClientMessage;
+        event.message_type = NET_WM_STATE;
+        event.display = XDISPLAY;
+        event.window = m_wnd;
+        event.format = 32;
+        event.data.l[ 0 ] = 1;
+        event.data.l[ 1 ] = NET_WM_STATE_FULLSCREEN;
  
-        XFree( p_args.p_atom );
-        if( i == i_items )
-            return; /* Not supported */
+        XSendEvent( XDISPLAY,
+                    DefaultRootWindow( XDISPLAY ),
+                    False, SubstructureNotifyMask|SubstructureRedirectMask,
+                    (XEvent*)&event );
+    }
+}
+
 
+void X11Window::toggleOnTop( bool onTop ) const
+{
+    if( NET_WM_STAYS_ON_TOP != None )
+    {
         /* Switch "on top" status */
         XClientMessageEvent event;
         memset( &event, 0, sizeof( XClientMessageEvent ) );
 
         event.type = ClientMessage;
-        event.message_type = net_wm_state;
+        event.message_type = NET_WM_STATE;
         event.display = XDISPLAY;
         event.window = m_wnd;
         event.format = 32;
         event.data.l[ 0 ] = onTop; /* set property */
-        event.data.l[ 1 ] = net_wm_state_above;
+        event.data.l[ 1 ] = NET_WM_STAYS_ON_TOP;
 
         XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
-                    False, SubstructureRedirectMask, (XEvent*)&event );
-        return;
+                    False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
     }
+    else if( NET_WM_STATE_ABOVE != None )
+    {
+        /* Switch "above" state */
+        XClientMessageEvent event;
+        memset( &event, 0, sizeof( XClientMessageEvent ) );
 
-    XFree( p_args.p_atom );
-
-    /* Switch "on top" status */
-    XClientMessageEvent event;
-    memset( &event, 0, sizeof( XClientMessageEvent ) );
-
-    event.type = ClientMessage;
-    event.message_type = net_wm_state;
-    event.display = XDISPLAY;
-    event.window = m_wnd;
-    event.format = 32;
-    event.data.l[ 0 ] = onTop; /* set property */
-    event.data.l[ 1 ] = net_wm_state_on_top;
+        event.type = ClientMessage;
+        event.message_type = NET_WM_STATE;
+        event.display = XDISPLAY;
+        event.window = m_wnd;
+        event.format = 32;
+        event.data.l[ 0 ] = onTop; /* set property */
+        event.data.l[ 1 ] = NET_WM_STATE_ABOVE;
 
-    XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
-                False, SubstructureRedirectMask, (XEvent*)&event );
+        XSendEvent( XDISPLAY, DefaultRootWindow( XDISPLAY ),
+                    False, SubstructureNotifyMask|SubstructureRedirectMask, (XEvent*)&event );
+    }
 }
 
 #endif