wmtest.c

Go to the documentation of this file.
00001 /*
00002  * File Name: wmtest.c
00003  */
00004 
00005 /*
00006  * This file is part of sysd.
00007  *
00008  * sysd is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * sysd is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 #include "config.h"
00032 
00033 // system include files, between < >
00034 #include <glib.h>
00035 #include <gtk/gtk.h>
00036 #include <gdk/gdkx.h>
00037 #include <stdio.h>
00038 
00039 //#include <libmb/mbdotdesktop.h>
00040 #include <libmb/mbutil.h>
00041 
00042 #include "log.h"
00043 
00044 #define RETURN_NONE_IF_NULL(p) if ((p) == '\0') return None; 
00045 
00046 static Window single_instance_get_window(Display *dpy, const char *bin_name);
00047 
00048 
00049 // main function
00050 int main(int argc, char *argv[])
00051 {
00052     LOGPRINTF("entry");
00053 
00054     Window win_found;
00055     Display *display;
00056     
00057     g_type_init();
00058     gtk_init(&argc, &argv);
00059 
00060     LOGPRINTF("default screen = %d", gdk_x11_get_default_screen());
00061     
00062     LOGPRINTF("get display");
00063     display = gdk_x11_get_default_xdisplay();
00064     LOGPRINTF("display = %p", display);
00065     
00066     LOGPRINTF("get window: %s", argv[1]);
00067     win_found = single_instance_get_window (display, argv[1]);
00068 
00069     if (win_found != None) 
00070     {
00071         LOGPRINTF("window found and activated!");
00072 //        mb_util_window_activate(display, win_found);
00073     }
00074     else
00075     {
00076         LOGPRINTF("window not found");
00077     }
00078 
00079     return 0;
00080 }
00081 
00082 
00083 static Window single_instance_get_window(Display *dpy, const char *bin_name)
00084 {
00085     /* XXX Should really set this on init rather than every time */
00086     Atom atom_exec_map = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
00087     LOGPRINTF("entry");
00088 
00089     Atom type;
00090     int format;
00091     unsigned char *data = NULL;
00092     unsigned long n_items, bytes_after;
00093     int result;
00094     int i;
00095     XClassHint hint;
00096     
00097     LOGPRINTF("enter");
00098 
00099     Atom atom_net_get_active = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
00100     
00101     result = XGetWindowProperty(dpy, RootWindow(dpy, DefaultScreen(dpy)), 
00102                                 atom_net_get_active,
00103                                 0, 10000L,
00104                                 False, XA_WINDOW,
00105                                 &type, &format, &n_items,
00106                                 &bytes_after, (unsigned char **)&data);
00107     
00108     
00109     if (result != Success || data == NULL || n_items == 0)
00110     {
00111         if (data) XFree(data);
00112         LOGPRINTF("failed to get windowproperty 1. result = %d, data = %p, items = %ld", result, data, n_items);
00113         return None;
00114     }
00115 
00116     if (XGetClassHint(dpy, ((Window*)data)[0], &hint))
00117     {
00118         LOGPRINTF("Active window %s, %s", hint.res_name, hint.res_class);
00119     }
00120 
00121     result = XGetWindowProperty(dpy, RootWindow(dpy, DefaultScreen(dpy)), 
00122                                 atom_exec_map,
00123                                 0, 10000L,
00124                                 False, XA_WINDOW,
00125                                 &type, &format, &n_items,
00126                                 &bytes_after, (unsigned char **)&data);
00127 
00128     if (result != Success || data == NULL || n_items == 0)
00129     {
00130         if (data) XFree(data);
00131         LOGPRINTF("failed to get windowproperty 2. result = %d, data = %p, items = %ld", result, data, n_items);
00132         return None;
00133     }
00134 
00135     for (i = 0; i < n_items; i++)
00136     {
00137         if (XGetClassHint(dpy, ((Window*)data)[i], &hint))
00138         {
00139             LOGPRINTF("Window [%d] window:%d, class: %s, name: %s", i, (guint) ((Window*)data)[i], hint.res_name, hint.res_class);
00140             
00141             if (strcmp(hint.res_name, bin_name) == 0)
00142             {
00143                 LOGPRINTF("Found and activated %s",bin_name);
00144 //                mb_util_window_activate(dpy, ((Window*)data)[i]);
00145                 
00146   Atom atom_net_active = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
00147   XEvent ev;
00148 
00149   memset(&ev, 0, sizeof ev);
00150   ev.xclient.type = ClientMessage;
00151   ev.xclient.window = ((Window*)data)[i];
00152   ev.xclient.message_type = atom_net_active;
00153   ev.xclient.format = 32;
00154 
00155   ev.xclient.data.l[0] = 1; 
00156   int retval;
00157   retval = XSendEvent(dpy, RootWindow(dpy, DefaultScreen(dpy)), False, SubstructureRedirectMask, &ev);
00158                 
00159   LOGPRINTF("XSendEvent returned %d", retval);
00160                 
00161                 return ((Window*)data)[i];
00162             }
00163         }
00164     }
00165 
00166     XFree (data);
00167 
00168     return None;
00169 }
Generated by  doxygen 1.6.2-20100208