检测系统托盘\任务栏方向(X11)

时间:2012-05-18 19:12:06

标签: c++ qt x11 taskbar tray

我创建了一个TrayPopupWidget,它应该在托盘附近弹出。 然后我意识到如果用户改变任务栏的方向或高度,它将弹出错误的位置。所以我创建了TaskbarDetector类。

我试图获取托盘|任务栏的窗口几何图形,但我只得到错误的窗口属性... 我试过KDE,LXDE - >同样的坏行为......

代码: http://bzfriendsplasm.svn.sourceforge.net/viewvc/bzfriendsplasm/BZFriends/taskbardetector.cpp?revision=156&view=markup

   //Getting screen resolutoin
int num_sizes;

Rotation original_rotation;

Display *display = XOpenDisplay(NULL);

Window root = RootWindow(display, 0);

XRRScreenSize *xrrs = XRRSizes(display, 0, &num_sizes);

XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root);

XRRConfigCurrentRate(conf);

SizeID original_size_id = XRRConfigCurrentConfiguration(conf, &original_rotation);

p_screenWidth = xrrs[original_size_id].width;
p_screenHeight = xrrs[original_size_id].height;


//Getting tray position

unsigned long  sysTraySelection = 0;

Screen *screen = XDefaultScreenOfDisplay(display);


//FIXME !!!
QString  *net_sys_tray = new QString("_NET_SYSTEM_TRAY_S%i");


(*net_sys_tray) = net_sys_tray->replace ("%i",QString::number (XScreenNumberOfScreen(screen)));


sysTraySelection = XInternAtom(display, net_sys_tray->toLocal8Bit (), False);

if ( sysTraySelection == None)
    return  Unknown;

trayWindow = XGetSelectionOwner(display, sysTraySelection);

XWindowAttributes w_attr;
unsigned long status = XGetWindowAttributes (display,trayWindow,&w_attr);

if ( status == 0)
    return Unknown;

p_taskBarLeft       = w_attr.y;
p_taskBarTop       = w_attr.x;
p_taskBarBottom  = w_attr.x + w_attr.height;
p_taskBarRight     = w_attr.y + w_attr.width;


qDebug () << QString("Window id:  " ) + QString::number (trayWindow);
 qDebug() << QString("SysTraySelection: ") + QString::number (sysTraySelection );
 qDebug() << QString("Top ") + QString::number (p_taskBarTop);
 qDebug() << QString("Left ") + QString::number (p_taskBarLeft);
 qDebug() << QString("Bottom ") + QString::number (p_taskBarBottom);
 qDebug() << QString("Right " ) + QString::number (p_taskBarRight);

XCloseDisplay(display);

delete net_sys_tray;

return decideOrientation ();

2 个答案:

答案 0 :(得分:2)

最后,我找到了一种方法来检测它!

  1. 我正在搜索一个具有dock属性且可见的窗口,然后我在其上调用XGetWindowAttributes(...)。
  2. 如果我将过滤方法设置为QApplication :: setEventFilter(),我也可以获取每个XEvent,_NET_WORKAREA事件(当您调整大小或移动任务栏时会发生此事件),然后我重新调用任务栏/托盘检测方法。
  3. 然而,这适用于KDE4和GNOME以及LXDE,我打算允许用户自己设置弹出位置。

    bool TaskBarDetector::lookUpDockWindow ( unsigned long &rootWindow, bool check)
    {
    Display *display = QX11Info::display ();
    
    Window parent;
    Window *children;
    unsigned int noOfChildren;
    int status;
    
    if ( check && checkDockProperty(rootWindow)  )
    {
        trayWindow = rootWindow;
        return true;
    }
    
    status = XQueryTree (display, rootWindow, &rootWindow, &parent, &children, &noOfChildren);
    
    if (status == 0)
    {
        qDebug() << "ERROR - Could not query the window tree. Aborting.";
        trayWindow = 0;
        return false;
    }
    
    if (noOfChildren == 0)
    {
        trayWindow = 0;
        return false;
    }
    
    for (unsigned int ind = 0 ; ind < noOfChildren; ++ind )
    {
    
        if ( lookUpDockWindow ( children[ind] ,true) )
            return true;
    }
    
    XFree ((char*) children);
    
    trayWindow = 0;
    
    return false;
    }
    
    bool TaskBarDetector::checkDockProperty(unsigned long window)
    {
    Display *x11display = QX11Info::display ();
    Atom *atoms;
    int numberAtoms = 0;
    
    char *atomName;
    XTextProperty prop;
    XWindowAttributes windowattr;
    
    atoms = XListProperties (x11display, window, &numberAtoms);
    
    for (int ind = 0; ind < numberAtoms; ++ind )
    {
        atomName = XGetAtomName(x11display, atoms[ind]);
    
        if (QString(atomName).compare ("_NET_WM_WINDOW_TYPE" )  != 0 )
            continue;
    
        unsigned long status = XGetTextProperty (x11display,window,&prop,atoms[ind]);
    
        if ( status == 0 )
            continue;
    
        int value = (int) (*prop.value);
    
        if (value != 151 )
            continue;
    
        if (XGetWindowAttributes(x11display,window,&windowattr) == 0)
           continue;
    
        return windowattr.map_state == 2;
    
    }
    
    return false;
    }
    
     void TaskBarDetector::saveWindowAttr(unsigned long root)
    {
    XWindowAttributes windowattr;
    
    Display *x11display =QX11Info::display ();
    
    if (XGetWindowAttributes(x11display,trayWindow,&windowattr) == 0)
    {
        trayWindow = 0;
        return;
    }
    
    int x = 0;
    int y = 0;
    
    Window *w = &trayWindow;
    
    if(  XTranslateCoordinates(x11display,trayWindow,root,windowattr.x,windowattr.y,&x,&y,w) == True)
    {
    
        p_taskBarTop = y;
        p_taskBarLeft = x;
        p_taskBarRight = p_taskBarLeft + windowattr.width;
        p_taskBarBottom = p_taskBarTop + windowattr.height;
        p_taskBarHeight = windowattr.height;
        p_taskBarWidth = windowattr.width;
    
    } else
            {
               p_orientation           = Unknown;
               p_taskBarTop         = 0;
               p_taskBarLeft         = 0;
               p_taskBarRight       =  0;
               p_taskBarBottom    = 0;
               p_taskBarHeight     = 0;
               p_taskBarWidth      = 0;
    
            }
    
    bool TaskBarDetector::appEventFilter(void *msg, long *result)
    {
    
    
    Q_UNUSED(result);
    
    if ( !TaskBarDetector::hasInstance() )
        return false;
    
    TaskBarDetector *detector = TaskBarDetector::getInstance();
    
    #ifdef Q_WS_WIN
    MSG *seged = static_cast<MSG*>(msg);
    if ( seged->message == WM_SETTINGCHANGE && seged->wParam == SPI_SETWORKAREA )
    {
        detector->processDetectEvent();
        return false;
    }
    
    return false;
    #endif
    
    #ifdef Q_WS_X11
    XEvent *xevent = static_cast<XEvent*> (msg);
    
    
    if ( xevent->type == PropertyNotify  )
    {
        XPropertyEvent xpe = xevent->xproperty;
    
        char * ch_atom_name = XGetAtomName(QX11Info::display(),xpe.atom);
    
        QString atom_name = QString(ch_atom_name).trimmed ();
    
        if (  atom_name == "_NET_WORKAREA" )
        {
            detector->processDetectEvent ();
            return false;
        }
    
    
    }
    
    return false;
    
    #endif
    }
    

    }

答案 1 :(得分:0)

系统托盘本身不一定是窗口。在KDE中,这只是任务栏中的一个区域(它与_NET_SYSTEM_TRAY_S%i选择所有者无关。)

您可能想要尝试嵌入托盘图标并获取其几何图形,然后在所述图标“附近”显示您的小部件(对于某些合理的“近”值)。您可以在知道其几何图形后删除该图标(但如果用户移动托盘,您将无法知道其新坐标。)

这不是100%可靠,因为托盘绝对不能显示您可以扔到它的所有图标。此外,由于图标闪烁,在视觉上不愉快。但它总比没有好。