void App::KillAllClients(Bool top) { Window dummywindow; Window *children; unsigned int nchildren; unsigned int i; XWindowAttributes attr; XSync(Dpy, 0); XSetErrorHandler(CatchErrors); nchildren = 0; XQueryTree(Dpy, Root, &dummywindow, &dummywindow, &children, &nchildren); if(!top) { for(i=0; i<nchildren; i++) { if(XGetWindowAttributes(Dpy, children[i], &attr) && (attr.map_state == IsViewable)) children[i] = XmuClientWindow(Dpy, children[i]); else children[i] = 0; } } for(i=0; i<nchildren; i++) { if(children[i]) XKillClient(Dpy, children[i]); } XFree((char *)children); XSync(Dpy, 0); XSetErrorHandler(NULL); }
static void lookat(Display *dpy, Window root, Bool verbose, int maxcmdlen) { Window dummy, *children = NULL, client; unsigned int i, nchildren = 0; /* * clients are not allowed to stomp on the root and ICCCM doesn't yet * say anything about window managers putting stuff there; but, try * anyway. */ print_client_properties (dpy, root, verbose, maxcmdlen); /* * then, get the list of windows */ if (!XQueryTree (dpy, root, &dummy, &dummy, &children, &nchildren)) { return; } for (i = 0; i < nchildren; i++) { client = XmuClientWindow (dpy, children[i]); if (client != None) print_client_properties (dpy, client, verbose, maxcmdlen); } }
//========================================= // kill all clients... //----------------------------------------- void closeClients (Display *dpy) { Window root = DefaultRootWindow (dpy); Window dummywindow; Window *children; XWindowAttributes attr; unsigned int nchildren; unsigned int i; XSync (dpy, 0); XSetErrorHandler (catchWindowErrors); nchildren = 0; XQueryTree ( dpy, root, &dummywindow, &dummywindow, &children, &nchildren ); for (i = 0; i < nchildren; i++) { if ( XGetWindowAttributes (dpy, children[i], &attr) && (attr.map_state == IsViewable) ) { children[i] = XmuClientWindow(dpy, children[i]); } else { children[i] = 0; } } for (i = 0; i < nchildren; i++) { if (children[i]) XKillClient (dpy, children[i]); } XFree ((char *)children); XSync (dpy, 0); XSetErrorHandler (NULL); }
Window Select_Window( Display * dpy ) { /*{{{ */ /* * Routine to let user select a window using the mouse * Taken from xfree86. */ int status; Cursor cursor; XEvent event; Window target_win = None, root = DefaultRootWindow( dpy ); int buttons = 0; int dummyi; unsigned int dummy; /* Make the target cursor */ cursor = XCreateFontCursor( dpy, XC_crosshair ); /* Grab the pointer using target cursor, letting it room all over */ status = XGrabPointer( dpy, root, False, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, root, cursor, CurrentTime ); if ( status != GrabSuccess ) { fputs( "ERROR: Cannot grab mouse.\n", stderr ); return 0; } /* Let the user select a window... */ while ( ( target_win == None ) || ( buttons != 0 ) ) { /* allow one more event */ XAllowEvents( dpy, SyncPointer, CurrentTime ); XWindowEvent( dpy, root, ButtonPressMask | ButtonReleaseMask, &event ); switch ( event.type ) { case ButtonPress: if ( target_win == None ) { target_win = event.xbutton.subwindow; /* window selected */ if ( target_win == None ) target_win = root; } buttons++; break; case ButtonRelease: if ( buttons > 0 ) /* there may have been some down before we started */ buttons--; break; } } XUngrabPointer( dpy, CurrentTime ); /* Done with pointer */ if ( XGetGeometry( dpy, target_win, &root, &dummyi, &dummyi, &dummy, &dummy, &dummy, &dummy ) && target_win != root ) { target_win = XmuClientWindow( dpy, target_win ); } return ( target_win ); }
static int kill_all_windows(Display *dpy, int screenno, Bool top) { Window root = RootWindow (dpy, screenno); Window dummywindow; Window *children; unsigned int nchildren; unsigned int i; XWindowAttributes attr; XSync (dpy, 0); XSetErrorHandler (catch_window_errors); nchildren = 0; XQueryTree (dpy, root, &dummywindow, &dummywindow, &children, &nchildren); if (!top) { for (i = 0; i < nchildren; i++) { if (XGetWindowAttributes(dpy, children[i], &attr) && (attr.map_state == IsViewable)) children[i] = XmuClientWindow(dpy, children[i]); else children[i] = 0; } } for (i = 0; i < nchildren; i++) { if (children[i]) XKillClient (dpy, children[i]); } XFree ((char *)children); XSync (dpy, 0); XSetErrorHandler (NULL); /* pretty stupid way to do things... */ return 0; }
/* from xwininfo , thanks*/ Window Select_Window(){ // Display *dpy; int status; Cursor cursor; XEvent event; Window target_win = None, root = RootWindow(dpy, default_src); int buttons = 0; /* Make the target cursor */ cursor = XCreateFontCursor(dpy, XC_crosshair); /* Grab the pointer using target cursor, letting it room all over */ status = XGrabPointer( dpy, root, False, ButtonPressMask|ButtonReleaseMask, GrabModeSync, GrabModeAsync, root, cursor, CurrentTime); if (status != GrabSuccess) printf("Can't grab the mouse."); /* Let the user select a window... */ while ((target_win == None) || (buttons != 0)) { /* allow one more event */ XAllowEvents(dpy, SyncPointer, CurrentTime); XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event); switch (event.type) { case ButtonPress: if (target_win == None) { target_win = event.xbutton.subwindow; /* window selected */ if (target_win == None) target_win = root; } buttons++; break; case ButtonRelease: if (buttons > 0) /* there may have been some down before we started */ buttons--; break; } } XUngrabPointer(dpy, CurrentTime); /* Done with pointer */ if (target_win) { Window root; int dummyi; uint32_t dummy; if ( XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi, &dummy, &dummy, &dummy, &dummy) && target_win != root) target_win = XmuClientWindow (dpy, target_win); } return(target_win); }
static gchar * window_get_title (GdkDisplay *display, GdkNativeWindow window) { #if defined(GDK_WINDOWING_X11) #ifdef HAVE_X11_XMU_WINUTIL_H window = XmuClientWindow (GDK_DISPLAY_XDISPLAY (display), window); #endif return window_get_utf8_property (display, window, "_NET_WM_NAME"); #else return NULL; #endif }
/* Routine to let user select a window using the mouse adapted from xfree86. */ XCTRL_API Window select_window(Display *disp, int button) { Cursor cursor; XEvent event; Window target_win = None; Window root = DefaultRootWindow(disp); int buttons = 0; int dumi; unsigned int dum; cursor = XCreateFontCursor(disp, XC_crosshair); if (!GrabMouse()) { XFreeCursor(disp,cursor); return 0; } while ((target_win == None) || (buttons != 0)) { XAllowEvents(disp, SyncPointer, CurrentTime); XWindowEvent(disp, root, ButtonPressMask|ButtonReleaseMask, &event); switch (event.type) { case ButtonPress: if ((button==-1)||(event.xbutton.button==button)) { if (target_win == None) { target_win = event.xbutton.subwindow; /* window selected */ if (target_win == None) target_win = root; } } else { pass_click_to_client(disp,root,&event,ButtonPressMask,cursor); } buttons++; break; case ButtonRelease: if (buttons > 0) { buttons--; } if ((button!=-1)&&(event.xbutton.button!=button)) { pass_click_to_client(disp,root,&event,ButtonReleaseMask,cursor); } break; } } XUngrabPointer(disp, CurrentTime); if (XGetGeometry(disp,target_win,&root,&dumi,&dumi,&dum,&dum,&dum,&dum)&&target_win!=root) { target_win = XmuClientWindow(disp, target_win); } return(target_win); }
// choose a window static void pick_window(int screen, int button) { XID id = wait_click_window(screen, button); if (id) { if (id == RootWindow(dpy, screen)) id = None; else { XID indicated = id = XmuClientWindow(dpy, indicated); if (id == indicated) // Try to not kill the window manager if (!wm_state_set(id) && wm_running(screen)) id = None; } } if (id != None) { printf("0x%lx\n", id); XSync(dpy, 0); } }
/**************************** findNetscapeWindow *************************/ static Window findNetscapeWindow(void) { int screen=DefaultScreen(display); Window rootwindow=RootWindow(display,screen); Window *children,dummy,w,wfound=(Window)0; unsigned int nchildren; int i; /* Get the root window tree */ if(!XQueryTree(display,rootwindow,&dummy,&dummy,&children,&nchildren)) return (Window)0; /* Look at the children from the top of the stacking order */ for(i=nchildren-1; i >= 0; i--) { w=XmuClientWindow(display,children[i]); /* Check if this is the Netscape window */ #if DEBUG printf("Child %d ",i); #endif wfound=checkNetscapeWindow(w); if(wfound) break; } if(children) XFree((void *)children); return wfound; }
/*======================================================== DndHandleDragging * Takes care of the drag and drop process. Wait until the pointer had moved * a little. Then takes control over the pointer until the buttons are * released. After that send a Drag And Drop ClientMessage event. Returns * non-zero if a drop did take place. *===========================================================================*/ int MwDndHandleDragging(Widget widget,XEvent *event) { XEvent Event; Window root = RootWindowOfScreen(XtScreenOfObject(widget)); XtAppContext app= XtWidgetToApplicationContext(widget); Window DispatchWindow; int DropX,DropY; if(Dragging) return 0; if(abs(StartEvent.x_root - event->xmotion.x_root) < DragPrecision && abs(StartEvent.y_root - event->xmotion.y_root) < DragPrecision) return 0; XUngrabPointer(dpy,CurrentTime); /* Take control over the pointer */ XGrabPointer(dpy,root,False, ButtonMotionMask|ButtonPressMask|ButtonReleaseMask, GrabModeSync,GrabModeAsync,root, DndCursor[DataType].CursorID, CurrentTime); /* Wait for button release */ Dragging=1; RootFlag=0; while(Dragging) { XAllowEvents(dpy,SyncPointer,CurrentTime); XtAppNextEvent(app,&Event); switch(Event.type) { case ButtonRelease: if(Event.xbutton.subwindow) RootFlag=0; else RootFlag=1; Dragging=0; break; default: XtDispatchEvent(&Event); break; } } DataOK=0; /* Now release the pointer */ XUngrabPointer(dpy,CurrentTime); /* Try to guess if the drop occurred in the root window */ if(!RootFlag) { Target=XmuClientWindow(dpy,Event.xbutton.subwindow); if(Target==Event.xbutton.subwindow) DispatchWindow=Target; else DispatchWindow=PointerWindow; } else Target=DispatchWindow=XtWindow(MwDndGetMainWidget(widget)); /* Now build the event structure */ DropX=Event.xbutton.x_root; DropY=Event.xbutton.y_root; Event.xclient.type = ClientMessage; Event.xclient.display = dpy; Event.xclient.message_type = MwDndProtocol; Event.xclient.format = 32; Event.xclient.window = Target; Event.xclient.data.l[0] = DataType; Event.xclient.data.l[1] = (long)event->xbutton.state; Event.xclient.data.l[2] = (long)XtWindow(widget); Event.xclient.data.l[3] = DropX + 65536L*(long)DropY; Event.xclient.data.l[4] = 1; /* Send the drop message */ XSendEvent(dpy,DispatchWindow,True,NoEventMask,&Event); /* Send an old style version of the message just in case */ Event.xclient.message_type = OldDndProtocol; XSendEvent(dpy,DispatchWindow,True,NoEventMask,&Event); #ifdef DEBUG fprintf(stderr,"ClientMessage sent to 0x%x(0x%x).\n", DispatchWindow,Target); fprintf(stderr,"The drop coordinates are (%d,%d).\n",DropX,DropY); #endif return 1; }
static GdkNativeWindow select_window_x11 (GdkScreen *screen) { Display *x_dpy = GDK_SCREEN_XDISPLAY (screen); gint x_scr = GDK_SCREEN_XNUMBER (screen); Window x_root = RootWindow (x_dpy, x_scr); Window x_win = None; GC x_gc = None; Cursor x_cursor = XCreateFontCursor (x_dpy, GDK_CROSSHAIR); GdkKeymapKey *keys = NULL; gint status; gint i, num_keys; gint buttons = 0; gint mask = ButtonPressMask | ButtonReleaseMask; gboolean cancel = FALSE; if (shootvals.shoot_type == SHOOT_REGION) mask |= PointerMotionMask; status = XGrabPointer (x_dpy, x_root, False, mask, GrabModeSync, GrabModeAsync, x_root, x_cursor, CurrentTime); if (status != GrabSuccess) { gint x, y; guint xmask; /* if we can't grab the pointer, return the window under the pointer */ XQueryPointer (x_dpy, x_root, &x_root, &x_win, &x, &y, &x, &y, &xmask); if (x_win == None || x_win == x_root) g_message (_("Error selecting the window")); } if (shootvals.shoot_type == SHOOT_REGION) { XGCValues gc_values; gc_values.function = GXxor; gc_values.plane_mask = AllPlanes; gc_values.foreground = WhitePixel (x_dpy, x_scr); gc_values.background = BlackPixel (x_dpy, x_scr); gc_values.line_width = 0; gc_values.line_style = LineSolid; gc_values.fill_style = FillSolid; gc_values.cap_style = CapButt; gc_values.join_style = JoinMiter; gc_values.graphics_exposures = FALSE; gc_values.clip_x_origin = 0; gc_values.clip_y_origin = 0; gc_values.clip_mask = None; gc_values.subwindow_mode = IncludeInferiors; x_gc = XCreateGC (x_dpy, x_root, GCFunction | GCPlaneMask | GCForeground | GCLineWidth | GCLineStyle | GCCapStyle | GCJoinStyle | GCGraphicsExposures | GCBackground | GCFillStyle | GCClipXOrigin | GCClipYOrigin | GCClipMask | GCSubwindowMode, &gc_values); } if (gdk_keymap_get_entries_for_keyval (NULL, GDK_Escape, &keys, &num_keys)) { gdk_error_trap_push (); #define X_GRAB_KEY(index, modifiers) \ XGrabKey (x_dpy, keys[index].keycode, modifiers, x_root, False, \ GrabModeAsync, GrabModeAsync) for (i = 0; i < num_keys; i++) { X_GRAB_KEY (i, 0); X_GRAB_KEY (i, LockMask); /* CapsLock */ X_GRAB_KEY (i, Mod2Mask); /* NumLock */ X_GRAB_KEY (i, Mod5Mask); /* ScrollLock */ X_GRAB_KEY (i, LockMask | Mod2Mask); /* CapsLock + NumLock */ X_GRAB_KEY (i, LockMask | Mod5Mask); /* CapsLock + ScrollLock */ X_GRAB_KEY (i, Mod2Mask | Mod5Mask); /* NumLock + ScrollLock */ X_GRAB_KEY (i, LockMask | Mod2Mask | Mod5Mask); /* all */ } #undef X_GRAB_KEY gdk_flush (); gdk_error_trap_pop (); } while (! cancel && ((x_win == None) || (buttons != 0))) { XEvent x_event; gint x, y, w, h; XAllowEvents (x_dpy, SyncPointer, CurrentTime); XWindowEvent (x_dpy, x_root, mask | KeyPressMask, &x_event); switch (x_event.type) { case ButtonPress: if (x_win == None) { x_win = x_event.xbutton.subwindow; if (x_win == None) x_win = x_root; #ifdef HAVE_X11_XMU_WINUTIL_H else if (! shootvals.decorate) x_win = XmuClientWindow (x_dpy, x_win); #endif shootvals.x2 = shootvals.x1 = x_event.xbutton.x_root; shootvals.y2 = shootvals.y1 = x_event.xbutton.y_root; } buttons++; break; case ButtonRelease: if (buttons > 0) buttons--; if (! buttons && shootvals.shoot_type == SHOOT_REGION) { x = MIN (shootvals.x1, shootvals.x2); y = MIN (shootvals.y1, shootvals.y2); w = ABS (shootvals.x2 - shootvals.x1); h = ABS (shootvals.y2 - shootvals.y1); if (w > 0 && h > 0) XDrawRectangle (x_dpy, x_root, x_gc, x, y, w, h); shootvals.x2 = x_event.xbutton.x_root; shootvals.y2 = x_event.xbutton.y_root; } break; case MotionNotify: if (buttons > 0) { x = MIN (shootvals.x1, shootvals.x2); y = MIN (shootvals.y1, shootvals.y2); w = ABS (shootvals.x2 - shootvals.x1); h = ABS (shootvals.y2 - shootvals.y1); if (w > 0 && h > 0) XDrawRectangle (x_dpy, x_root, x_gc, x, y, w, h); shootvals.x2 = x_event.xmotion.x_root; shootvals.y2 = x_event.xmotion.y_root; x = MIN (shootvals.x1, shootvals.x2); y = MIN (shootvals.y1, shootvals.y2); w = ABS (shootvals.x2 - shootvals.x1); h = ABS (shootvals.y2 - shootvals.y1); if (w > 0 && h > 0) XDrawRectangle (x_dpy, x_root, x_gc, x, y, w, h); } break; case KeyPress: { guint *keyvals; gint n; if (gdk_keymap_get_entries_for_keycode (NULL, x_event.xkey.keycode, NULL, &keyvals, &n)) { gint i; for (i = 0; i < n && ! cancel; i++) if (keyvals[i] == GDK_Escape) cancel = TRUE; g_free (keyvals); } } break; default: break; } } if (keys) { #define X_UNGRAB_KEY(index, modifiers) \ XUngrabKey (x_dpy, keys[index].keycode, modifiers, x_root) for (i = 0; i < num_keys; i++) { X_UNGRAB_KEY (i, 0); X_UNGRAB_KEY (i, LockMask); /* CapsLock */ X_UNGRAB_KEY (i, Mod2Mask); /* NumLock */ X_UNGRAB_KEY (i, Mod5Mask); /* ScrollLock */ X_UNGRAB_KEY (i, LockMask | Mod2Mask); /* CapsLock + NumLock */ X_UNGRAB_KEY (i, LockMask | Mod5Mask); /* CapsLock + ScrollLock */ X_UNGRAB_KEY (i, Mod2Mask | Mod5Mask); /* NumLock + ScrollLock */ X_UNGRAB_KEY (i, LockMask | Mod2Mask | Mod5Mask); /* all */ } #undef X_UNGRAB_KEY g_free (keys); } if (status == GrabSuccess) XUngrabPointer (x_dpy, CurrentTime); XFreeCursor (x_dpy, x_cursor); if (x_gc != None) XFreeGC (x_dpy, x_gc); return x_win; }
GwyRemote* gwy_remote_get(void) { GwyRemote *remote; GdkWindow *root; Display *xdisplay; Window xroot, xparent, *xchildren; Atom role_xatom; guint nchildren; gint i; remote = g_new0(GwyRemote, 1); remote->display = gdk_display_get_default(); g_object_ref(remote->display); root = gdk_screen_get_root_window(gdk_screen_get_default()); xdisplay = gdk_x11_display_get_xdisplay(remote->display); if (!XQueryTree(xdisplay, GDK_WINDOW_XID(root), &xroot, &xparent, &xchildren, &nchildren) || !nchildren || !xchildren) { gwy_remote_free(remote); return NULL; } role_xatom = gdk_x11_get_xatom_by_name_for_display(remote->display, "WM_WINDOW_ROLE"); for (i = nchildren-1; !remote->toolbox && i >= 0; i--) { Window xwindow; Atom ret_type_xatom; gint ret_format; gulong bytes_after, nitems; guchar *data; /* * The XmuClientWindow() function finds a window at or below the * specified window, that has a WM_STATE property. If such a * window is found, it is returned; otherwise the argument window * is returned. */ xwindow = XmuClientWindow(xdisplay, xchildren[i]); if (XGetWindowProperty(xdisplay, xwindow, role_xatom, 0, 32, FALSE, XA_STRING, &ret_type_xatom, &ret_format, &nitems, &bytes_after, &data) == Success && ret_type_xatom) { if (gwy_strequal(data, GWY_TOOLBOX_WM_ROLE)) { remote->winid = xwindow; remote->toolbox = gdk_window_foreign_new_for_display(remote->display, remote->winid); g_object_ref(remote->toolbox); } XFree(data); } } XFree(xchildren); if (!remote->winid) { gwy_remote_free(remote); remote = NULL; } return remote; }
int main(int argc, char *argv[]) { int i; /* iterator, temp variable */ char *displayname = NULL; /* name of server to contact */ int screenno; /* screen number of dpy */ XID id = None; /* resource to kill */ char *button_name = NULL; /* name of button for window select */ int button; /* button number or negative for all */ Bool kill_all = False; Bool top = False; ProgramName = argv[0]; button = SelectButtonFirst; for (i = 1; i < argc; i++) { char *arg = argv[i]; if (arg[0] == '-') { switch (arg[1]) { case 'd': /* -display displayname */ if (++i >= argc) usage (); displayname = argv[i]; continue; case 'i': /* -id resourceid */ if (++i >= argc) usage (); id = parse_id (argv[i]); continue; case 'b': /* -button number */ if (++i >= argc) usage (); button_name = argv[i]; continue; case 'f': /* -frame */ top = True; continue; case 'a': /* -all */ kill_all = True; continue; default: usage (); } } else { usage (); } } /* end for */ dpy = XOpenDisplay (displayname); if (!dpy) { fprintf (stderr, "%s: unable to open display \"%s\"\n", ProgramName, XDisplayName (displayname)); Exit (1); } screenno = DefaultScreen (dpy); if (kill_all) { if (verify_okay_to_kill (dpy, screenno)) kill_all_windows (dpy, screenno, top); Exit (0); } /* * if no id was given, we need to choose a window */ if (id == None) { if (!button_name) button_name = XGetDefault (dpy, ProgramName, "Button"); if (button_name && !parse_button (button_name, &button)) { fprintf (stderr, "%s: invalid button specification \"%s\"\n", ProgramName, button_name); Exit (1); } if (button >= 0 || button == SelectButtonFirst) { unsigned char pointer_map[256]; /* 8 bits of pointer num */ int count, j; unsigned int ub = (unsigned int) button; count = XGetPointerMapping (dpy, pointer_map, 256); if (count <= 0) { fprintf (stderr, "%s: no pointer mapping, can't select window\n", ProgramName); Exit (1); } if (button >= 0) { /* check button */ for (j = 0; j < count; j++) { if (ub == (unsigned int) pointer_map[j]) break; } if (j == count) { fprintf (stderr, "%s: no button number %u in pointer map, can't select window\n", ProgramName, ub); Exit (1); } } else { /* get first entry */ button = (int) ((unsigned int) pointer_map[0]); } } if ((id = get_window_id (dpy, screenno, button, "the window whose client you wish to kill"))) { if (id == RootWindow(dpy,screenno)) id = None; else if (!top) { XID indicated = id; if ((id = XmuClientWindow(dpy, indicated)) == indicated) { /* Try not to kill the window manager when the user * indicates an icon to xkill. */ if (! wm_state_set(dpy, id) && wm_running(dpy, screenno)) id = None; } } } } if (id != None) { printf ("%s: killing creator of resource 0x%lx\n", ProgramName, id); XSync (dpy, 0); /* give xterm a chance */ XKillClient (dpy, id); XSync (dpy, 0); } Exit (0); /*NOTREACHED*/ return 0; }
Window GetClientWindow(Widget w, int *x, int *y) { int status; Cursor cursor; XEvent event; int buttons = 0; Display * dpy = XtDisplayOfObject(w); Window target_win = None, root = RootWindowOfScreen(XtScreenOfObject(w)); XtAppContext app = XtWidgetToApplicationContext(w); /* Make the target cursor */ cursor = XCreateFontCursor(dpy, XC_crosshair); /* Grab the pointer using target cursor, letting it room all over */ status = XGrabPointer(dpy, root, False, ButtonPressMask|ButtonReleaseMask, GrabModeSync, GrabModeAsync, root, cursor, CurrentTime); if (status != GrabSuccess) { SetMessage(global_screen_data.info_label, res_labels[5]); return(None); } /* Let the user select a window... */ while ((target_win == None) || (buttons != 0)) { /* allow one more event */ XAllowEvents(dpy, SyncPointer, CurrentTime); XtAppNextEvent(app, &event); switch (event.type) { case ButtonPress: if (event.xbutton.window != root) { XtDispatchEvent(&event); break; } if (target_win == None) { target_win = event.xbutton.subwindow; /* window selected */ if (x != NULL) *x = event.xbutton.x_root; if (y != NULL) *y = event.xbutton.y_root; } buttons++; break; case ButtonRelease: if (event.xbutton.window != root) { XtDispatchEvent(&event); break; } if (buttons > 0) /* There may have been some down before we started */ buttons--; break; default: XtDispatchEvent(&event); break; } } XUngrabPointer(dpy, CurrentTime); /* Done with pointer */ return(XmuClientWindow(dpy, target_win)); }
GdkWindow * gimp_remote_find_toolbox (GdkDisplay *display, GdkScreen *screen) { GdkWindow *result = NULL; Display *xdisplay; Window root, parent; Window *children; Atom role_atom; Atom string_atom; guint nchildren; gint i; GdkWindow *root_window = gdk_screen_get_root_window (screen); xdisplay = gdk_x11_display_get_xdisplay (display); role_atom = XInternAtom (xdisplay, "WM_WINDOW_ROLE", TRUE); string_atom = XInternAtom (xdisplay, "STRING", TRUE); if (role_atom == None || string_atom == None) return NULL; if (XQueryTree (xdisplay, GDK_WINDOW_XID (root_window), &root, &parent, &children, &nchildren) == 0) return NULL; if (! (children && nchildren)) return NULL; for (i = nchildren - 1; i >= 0; i--) { Window window; Atom ret_type; gint ret_format; gulong bytes_after; gulong nitems; guchar *data; /* The XmuClientWindow() function finds a window at or below the * specified window, that has a WM_STATE property. If such a * window is found, it is returned; otherwise the argument window * is returned. */ window = XmuClientWindow (xdisplay, children[i]); /* We are searching for the GIMP toolbox: Its WM_WINDOW_ROLE Property * (as set by gtk_window_set_role ()) has the value "gimp-toolbox". * This is pretty reliable, since it ask for a special property, * explicitly set by GIMP. See below... :-) */ if (XGetWindowProperty (xdisplay, window, role_atom, 0, 32, FALSE, string_atom, &ret_type, &ret_format, &nitems, &bytes_after, &data) == Success && ret_type) { if (nitems > 11 && strcmp ((const gchar *) data, "gimp-toolbox") == 0) { XFree (data); result = gdk_window_foreign_new_for_display (display, window); break; } XFree (data); } } XFree (children); return result; }
/* Function Name: * GetClientWindow * * Description: * Gets the Client's window by asking the user. * * Arguments: * w - a widget. * * Returns: * a clients window, or None. * * Calls: * SetMessage - utils.c * * Global Data: */ Window GetClientWindow(Widget w, int *x, int *y) { int status; Cursor cursor; XEvent event; int buttons = 0; int keys = 0; Display *dpy = XtDisplayOfObject(w); Window target_win = None; Window root_win = RootWindowOfScreen(XtScreenOfObject(w)); XtAppContext context = XtWidgetToApplicationContext(w); char buffer[10]; int len; KeySym keycode; /* >>>>> HACK for batchres */ if (global_winID != None) { /* we submit the winID given by parameter only once! */ /* otherwise there is an infinite loop */ target_win = global_winID; global_winID = None; return(target_win); } /* <<<<< HACK for batchres */ /* Make the target cursor */ cursor = XCreateFontCursor(dpy, XC_crosshair); /* Grab the pointer using target cursor, letting it room all over */ status = XGrabPointer(dpy, /* Display */ root_win, /* grab_window */ False, /* owner_events */ ButtonPressMask | ButtonReleaseMask, GrabModeSync, /* pointer_mode */ GrabModeAsync, /* keyboard_mode */ root_win, /* confine_to */ cursor, /* cursor */ CurrentTime); /* time, when grab took place */ if (status != GrabSuccess) { /* "Can't grab the mouse" */ SetMessage(global_screen_data.info_label, res_labels[5], "Can't grab the mouse"); return(None); } /* if (grab of mouse pointer unsuccessful) */ /* Grab the keyboard, letting it room all over */ status = XGrabKeyboard(dpy, /* Display */ root_win, /* grab_window */ False, /* owner_events */ GrabModeAsync, /* pointer_mode */ GrabModeAsync, /* keyboard_mode */ CurrentTime); /* time, when grab took place */ if (status != GrabSuccess) { /* "Can't grab the keyboard" */ SetMessage(global_screen_data.info_label, res_labels[37], "Can't grab the keyboard"); return(None); } /* if (grab of keyboard unsuccessful) */ /* Let the user select a window... */ while ( (target_win == None) || (buttons != 0) ) { /* printf("targetwin: 0x%x, buttons: %d\n", target_win, buttons); */ /* allow one more event */ XAllowEvents(dpy, SyncPointer, CurrentTime); XtAppNextEvent(context, &event); switch (event.type) { case ButtonPress: /* printf("ButtonWindow 0x%x\n", event.xbutton.window); */ if (event.xbutton.window != root_win) { XtDispatchEvent(&event); break; } if (target_win == None) { target_win = event.xbutton.subwindow; /* window selected */ if (x != NULL) { *x = event.xbutton.x_root; } if (y != NULL) { *y = event.xbutton.y_root; } } buttons++; break; /* ButtonPress */ case ButtonRelease: if (event.xbutton.window != root_win) { XtDispatchEvent(&event); break; } if (buttons > 0) /* There may have been some */ { /* down before we started */ buttons--; } break; /* ButtonRelease */ case KeyPress: /* <Key>ESC aborts, <Key>Return + <Key>Spacebar select */ len = (XLookupString(&(event.xkey), buffer, sizeof(buffer), &keycode, (XComposeStatus*)NULL)); /* printf("keycode: 0x%x\n", keycode); */ switch (keycode) { case XK_Return: case XK_space: case XK_Select: case XK_KP_Space: case XK_KP_Enter: /* printf("Ret, space, Select, ...\n"); */ /* printf("KeyWindow 0x%x\n", event.xkey.window); */ /* if (event.xkey.window != root_win) */ /* { */ /* XtDispatchEvent(&event); */ /* break; */ /* } */ /* printf(" window root \n"); */ if (target_win == None) { /* printf(" subwindow 0x%x\n", event.xkey.subwindow); */ target_win = event.xkey.subwindow; /* window selected */ if (x != NULL) { *x = event.xkey.x_root; } if (y != NULL) { *y = event.xkey.y_root; } } break; /* XK_Return */ case XK_Escape: case XK_Cancel: case XK_Break: XUngrabPointer(dpy, CurrentTime); /* Done with pointer */ XUngrabKeyboard(dpy, CurrentTime); /* Done with keyboard */ return ((Window)None); break; /* XK_Escape */ default: fprintf(stderr, "Unknown key! Press <ESC> to abort, <Return> to select\n"); break; } /* switch (keycode) */ break; /* KeyPress */ default: XtDispatchEvent(&event); break; /* default */ } /* switch (event.type) */ } /* while () */ /* If the XServer supports KeyRelease, we throw it away */ while ((event.type == KeyRelease) || (event.type == KeyPress)) { XtDispatchEvent(&event); } /* while (discard Keyboard-Events) */ XUngrabPointer(dpy, CurrentTime); /* Done with pointer */ XUngrabKeyboard(dpy, CurrentTime); /* Done with keyboard */ /* printf("[GetClientWindow] target_win 0x%x, XmuClientWindow 0x%x\n", */ /* target_win, */ /* XmuClientWindow(dpy, target_win)); */ return (XmuClientWindow(dpy, target_win)); } /* GetClientWindow() */
static Window hgu_remote_find_window (Display *dpy) { int i; Window root = RootWindowOfScreen (DefaultScreenOfDisplay (dpy)); Window root2, parent, *kids; unsigned int nkids; Window result = 0; Window tenative = 0; unsigned char *tenative_version = 0; if (! XQueryTree (dpy, root, &root2, &parent, &kids, &nkids)) { fprintf (stderr, "%s: XQueryTree failed on display %s\n", progname, DisplayString (dpy)); return( (Window) 0 ); } /* root != root2 is possible with virtual root WMs. */ if (! (kids && nkids)) { fprintf (stderr, "%s: root window has no children on display %s\n", progname, DisplayString (dpy)); return( (Window) 0 ); } for (i = nkids-1; i >= 0; i--) { Atom type; int format; unsigned long nitems, bytesafter; unsigned char *version = 0; Window w = XmuClientWindow (dpy, kids[i]); int status = XGetWindowProperty (dpy, w, XA_MOZILLA_VERSION, 0, (65536 / sizeof (long)), False, XA_STRING, &type, &format, &nitems, &bytesafter, &version); if (! version) continue; if (strcmp ((char *) version, expected_mozilla_version) && !tenative) { tenative = w; tenative_version = version; continue; } XFree (version); if (status == Success && type != None) { result = w; break; } } if (result && tenative) { fprintf (stderr, "%s: warning: both version %s (0x%x) and version\n" "\t%s (0x%x) are running. Using version %s.\n", progname, tenative_version, (unsigned int) tenative, expected_mozilla_version, (unsigned int) result, expected_mozilla_version); XFree (tenative_version); return result; } else if (tenative) { fprintf (stderr, "%s: warning: expected version %s but found version\n" "\t%s (0x%x) instead.\n", progname, expected_mozilla_version, tenative_version, (unsigned int) tenative); XFree (tenative_version); return tenative; } else if (result) { return result; } else { fprintf (stderr, "%s: not running on display %s\n", progname, DisplayString (dpy)); return( (Window) 0 ); } }