Back to index

bamf  0.2.120
bamf-xutils.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Jason Smith <jason.smith@canonical.com>
00017  *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  *
00019  */
00020 
00021 #include "bamf-xutils.h"
00022 #include <string.h>
00023 
00024 static Display *
00025 get_xdisplay (gboolean *opened)
00026 {
00027   Display *xdisplay;
00028   xdisplay = gdk_x11_get_default_xdisplay ();
00029 
00030   if (opened)
00031     *opened = FALSE;
00032 
00033   if (!xdisplay)
00034     {
00035       xdisplay = XOpenDisplay (NULL);
00036 
00037       if (xdisplay)
00038         {
00039           if (opened)
00040             *opened = TRUE;
00041         }
00042     }
00043 
00044   return xdisplay;
00045 }
00046 
00047 char *
00048 bamf_xutils_get_window_hint (Window xid, const char *atom_name, Atom type)
00049 {
00050   Display *XDisplay;
00051   Atom atom;
00052   char *hint = NULL;
00053   Atom result_type;
00054   gint format;
00055   gulong numItems;
00056   gulong bytesAfter;
00057   unsigned char *buffer;
00058   gboolean close_display = FALSE;
00059 
00060   g_return_val_if_fail ((xid != 0), NULL);
00061   g_return_val_if_fail (atom_name, NULL);
00062 
00063   XDisplay = get_xdisplay (&close_display);
00064 
00065   if (!XDisplay)
00066   {
00067     g_warning ("%s: Unable to get a valid XDisplay", G_STRFUNC);
00068     return NULL;
00069   }
00070 
00071   atom = XInternAtom (XDisplay, atom_name, FALSE);
00072 
00073   int result = XGetWindowProperty (XDisplay,
00074                                    xid,
00075                                    atom,
00076                                    0,
00077                                    G_MAXINT,
00078                                    FALSE,
00079                                    type,
00080                                    &result_type,
00081                                    &format,
00082                                    &numItems,
00083                                    &bytesAfter,
00084                                    &buffer);
00085 
00086   if (close_display)
00087     XCloseDisplay (XDisplay);
00088 
00089   if (result == Success && numItems > 0)
00090     {
00091       if (buffer && buffer[0] != '\0')
00092         hint = g_strdup ((char*) buffer);
00093 
00094       XFree (buffer);
00095     }
00096 
00097   return hint;
00098 }
00099 
00100 void
00101 bamf_xutils_set_window_hint (Window xid, const char *atom_name, Atom type, const char *data)
00102 {
00103   Display *XDisplay;
00104   gboolean close_display = FALSE;
00105 
00106   g_return_if_fail (xid != 0);
00107   g_return_if_fail (atom_name);
00108   g_return_if_fail (data);
00109   
00110   XDisplay = get_xdisplay (&close_display);
00111 
00112   if (!XDisplay)
00113   {
00114     g_warning ("%s: Unable to get a valid XDisplay", G_STRFUNC);
00115     return;
00116   }
00117 
00118   XChangeProperty (XDisplay,
00119                    xid,
00120                    XInternAtom (XDisplay,
00121                    atom_name,
00122                    FALSE),
00123                    type,
00124                    8,
00125                    PropModeReplace,
00126                    (unsigned char *) data,
00127                    strlen (data));
00128 
00129   if (close_display)
00130     XCloseDisplay (XDisplay);
00131 }
00132 
00133 void
00134 bamf_xutils_get_window_class_hints (Window xid, char **class_instance_name, char **class_name)
00135 {
00136   Display *xdisplay;
00137   gboolean close_display = FALSE;
00138 
00139   xdisplay = get_xdisplay (&close_display);
00140 
00141   if (!xdisplay)
00142   {
00143     g_warning ("%s: Unable to get a valid XDisplay", G_STRFUNC);
00144     return;
00145   }
00146 
00147   XClassHint class_hint;
00148   class_hint.res_name = NULL;
00149   class_hint.res_class = NULL;
00150 
00151   XGetClassHint(xdisplay, xid, &class_hint);
00152 
00153   if (class_name && class_hint.res_class && class_hint.res_class[0] != 0)
00154     *class_name = g_convert (class_hint.res_class, -1, "utf-8", "iso-8859-1",
00155                              NULL, NULL, NULL);
00156 
00157   if (class_instance_name && class_hint.res_name && class_hint.res_name[0] != 0)
00158     *class_instance_name = g_convert (class_hint.res_name, -1, "utf-8", "iso-8859-1",
00159                                       NULL, NULL, NULL);
00160 
00161   XFree (class_hint.res_class);
00162   XFree (class_hint.res_name);
00163 }