其實EWMH_NET_CURRENT_DESKTOP
給你這是X目前的桌面,而不是相對於應用程序。這裏有一個C代碼片段來獲取應用程序的_WM_DESKTOP
。如果從有問題的KDE Konsole運行,它將爲您提供它所在的桌面,即使它不是活動桌面或不在焦點。
#include <X11/Xlib.h>
#include <X11/Shell.h>
...
Atom net_wm_desktop = 0;
long desktop;
Status ret;
/* see if we've got a desktop atom */
Atom net_wm_desktop = XInternAtom(display, "_NET_WM_DESKTOP", False);
if(net_wm_desktop == None) {
return;
}
/* find out what desktop we're currently on */
if (XGetWindowProperty(display, window, net_wm_desktop, 0, 1,
False, XA_CARDINAL, (Atom *) &type_ret, &fmt_ret,
&nitems_ret, &bytes_after_ret,
(unsigned char**)&data) != Success || data == NULL
) {
fprintf(stderr, "XGetWindowProperty() failed");
if (data == NULL) {
fprintf(stderr, "No data returned from XGetWindowProperty()");
}
return;
}
desktop = *data;
XFree(data);
和desktop
應該是虛擬桌面的Konsole當前處於的索引。這不是其頭相同的多頭的顯示器。如果你想確定哪個頭,你需要使用XineramaQueryScreens
(Xinerama擴展,不知道是否有XRandR等價物,不適用於nVidia的TwinView。)
下面是我寫的一些代碼的摘錄, ax和y,計算屏幕邊界(sx,sy,sw與屏幕寬度和sh屏幕高度)。您可以輕鬆地調整它以簡單地返回「屏幕」或頭部x和y所在的位置(屏幕有一個特殊的在X11的意思)。
#include <X11/X.h>
#include <X11/extensions/Xinerama.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
Bool xy2bounds(Display* d,int x, int y, int* sx, int* sy, int* sw, int* sh) {
*sx = *sy = *sw = *sh = -1; /* Set to invalid, for error condition */
XineramaScreenInfo *XinInfo;
int xin_screens = -1;
int i;
int x_origin, y_origin, width, height;
Bool found = False;
if (d == NULL)
return False;
if ((x < 0) || (y < 0))
return False;
if (True == XineramaIsActive(d)) {
XinInfo = XineramaQueryScreens(d, &xin_screens);
if ((NULL == XinInfo) || (0 == xin_screens)) {
return False;
}
} else {
/* Xinerama is not active, so return usual width/height values */
*sx = 0;
*sy = 0;
*sw = DisplayWidth(d, XDefaultScreen(d));
*sh = DisplayHeight(d, XDefaultScreen(d));
return True;
}
for (i = 0; i < xin_screens; i++) {
x_origin = XinInfo[i].x_org;
y_origin = XinInfo[i].y_org;
width = XinInfo[i].width;
height = XinInfo[i].height;
printf("Screens: (%d) %dx%d - %dx%d\n", i,
x_origin, y_origin, width, height);
if ((x >= x_origin) && (y >= y_origin)) {
if ((x <= x_origin+width) && (y <= y_origin+height)) {
printf("Found Screen[%d] %dx%d - %dx%d\n",
i, x_origin, y_origin, width, height);
*sx = x_origin;
*sy = y_origin;
*sw = width;
*sh = height;
found = True;
break;
}
}
}
assert(found == True);
return found;
}
你爲什麼試圖確定這一點?一旦找到結果,你將如何處理結果?在不知道的情況下,最好的答案是「單擊任務欄上的konsole按鈕,並查看切換到的桌面。」 – davr 2009-04-10 16:46:29
至少需要一些信息。 kde4,kde3?什麼konsole版本? – 2009-04-10 17:01:39
我需要在代碼中確定桌面,而不是要求用戶告訴我他們在哪個桌面上。 – 2009-04-10 17:21:57