Skip to content
Snippets Groups Projects
session.c 14 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* Initially inspired by xfwm, fvwm2, enlightment and twm implementations */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <signal.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <glib.h>

#include "session.h"
#include "main.h"
#include "hints.h"
#include "client.h"

typedef struct _match
{
    unsigned long win;
    unsigned long client_leader;
    char *client_id;
    char *res_name;
    char *res_class;
    char *window_role;
    char *wm_name;
    int wm_command_count;
    char **wm_command;
    int x, y, w, h;
    int desktop;
    unsigned long flags;
    gboolean used;
}
Match;

static int num_match = 0;
static Match *matches = NULL;

/* 
   2-pass function to compute new string length,
   allocate memory and finally copy string 
   - Returned value must be freed -
 */
static gchar *escape_quote(gchar * s)
{
    gchar *ns;
    gchar *idx1, *idx2;
    gint nbquotes = 0;
    gint lg = 0;

    g_return_val_if_fail(s != NULL, NULL);

    lg = strlen(s);
    /* First, count quotes in string */
    idx1 = s;
    while(*idx1)
    {
        if(*(idx1++) == '"')
            nbquotes++;
    }
    /* If there is no quote in the string, return it */
    if(!nbquotes)
        return (g_strdup(s));

    /* Or else, allocate memory for the new string */
    ns = g_new(gchar, lg + nbquotes + 1);
    /* And prepend a backslash before any quote found in string */
    idx1 = s;
    idx2 = ns;
    while(*idx1)
    {
        if(*idx1 == '"')
        {
            *(idx2++) = '\\';
            *(idx2++) = '"';
        }
        else
        {
            *(idx2++) = *idx1;
        }
        idx1++;
    }
    /* Add null char */
    *idx2 = '\0';
    return ns;
}

/* 
   single-pass function to replace backslash+quotes
   by quotes. 
   - Returned value must be freed -
 */
static gchar *unescape_quote(gchar * s)
{
    gchar *ns;
    gboolean backslash;
    gchar *idx1, *idx2;
    gint lg;

    g_return_val_if_fail(s != NULL, NULL);

    lg = strlen(s);
    backslash = FALSE;
    ns = g_new(gchar, lg + 1);
    idx1 = s;
    idx2 = ns;
    while(*idx1)
    {
        if(*idx1 == '\\')
        {
            *(idx2++) = *idx1;
            backslash = TRUE;
        }
        else if((*idx1 == '"') && backslash)
        {
            /* Move backward to override the "\" */
            *(--idx2) = *idx1;
            idx2++;
            backslash = FALSE;
        }
        else
        {
            *(idx2++) = *idx1;
            backslash = FALSE;
        }
        idx1++;
    }
    *idx2 = '\0';
    return ns;
}

static gchar *getsubstring(gchar * s, gint * length)
{
    gchar pbrk;
    gchar *ns;
    gchar *end, *idx1, *idx2, *skip;
    gint lg;
    gboolean finished = FALSE, backslash = FALSE;

    lg = *length = 0;
    g_return_val_if_fail(s != NULL, NULL);

    end = skip = s;
    while((*skip == ' ') || (*skip == '\t'))
    {
        end = ++skip;
        (*length)++;
    }
    if(*skip == '"')
    {
        pbrk = '"';
        end = ++skip;
        (*length)++;
    }
    else
    {
        pbrk = ' ';
    }

    finished = FALSE;
    while((!finished) && (*end))
    {
        if(*end == '\\')
        {
            backslash = TRUE;
        }
        else if((*end == pbrk) && backslash)
        {
            backslash = FALSE;
        }
        else if(*end == pbrk)
        {
            finished = TRUE;
        }
        end++;
        lg++;
        (*length)++;
    }
    ns = g_new(gchar, lg + 1);
    /* Skip pbrk character */
    end--;
    idx1 = skip;
    idx2 = ns;
    do
    {
        *(idx2++) = *idx1;
    }
    while(++idx1 < end);
    *idx2 = '\0';
    return ns;
}

gboolean sessionSaveWindowStates(gchar *filename)
{
    FILE *f;
    Client *c;
    gint client_idx;

    g_return_val_if_fail(filename != NULL, FALSE);
    
    if((f = fopen(filename, "w")))
    {
	for(c = clients, client_idx = 0; client_idx < client_count; c = c->next, client_idx++)
	{
            fprintf(f, "[CLIENT] 0x%lx\n", c->window);

            if(c->client_id)
            {
        	fprintf(f, "  [CLIENT_ID] %s\n", c->client_id);
            }

            if(c->client_leader)
            {
        	fprintf(f, "  [CLIENT_LEADER] 0x%lx\n", c->client_leader);
            }

            if(c->window_role)
            {
        	fprintf(f, "  [WINDOW_ROLE] %s\n", c->window_role);
            }

            if(c->class.res_class)
            {
        	fprintf(f, "  [RES_NAME] %s\n", c->class.res_name);
            }

            if(c->class.res_name)
            {
        	fprintf(f, "  [RES_CLASS] %s\n", c->class.res_class);
            }

            if(c->name)
            {
        	fprintf(f, "  [WM_NAME] %s\n", c->name);
            }

            if(c->wm_command_count > 0)
            {
        	gint j;
		fprintf(f, "  [WM_COMMAND] (%i)", c->wm_command_count);
        	for(j = 0; j < c->wm_command_count; j++)
        	{
                    gchar *escaped_string;
                    escaped_string = escape_quote(c->wm_command[j]);
                    fprintf(f, " \"%s\"", escaped_string);
                    g_free(escaped_string);
        	}
        	fprintf(f, "\n");
            }

            fprintf(f, "  [GEOMETRY] (%i,%i,%i,%i)\n", c->x, c->y, c->width, c->height);
            fprintf(f, "  [DESK] %i\n", c->win_workspace);
            fprintf(f, "  [FLAGS] 0x%lx\n", CLIENT_FLAG_TEST(c, CLIENT_FLAG_STICKY | CLIENT_FLAG_HIDDEN | CLIENT_FLAG_SHADED | CLIENT_FLAG_NAME_CHANGED));
	}
	fclose(f);
	return TRUE;
    }
    return FALSE;
}

gboolean sessionLoadWindowStates(gchar *filename)
{
    FILE *f;
    char s[4096], s1[4096];
    int i, pos, pos1;
    unsigned long w;
    
    g_return_val_if_fail(filename != NULL, FALSE);
    if((f = fopen(filename, "r")))
    {
        while(fgets(s, sizeof(s), f))
        {
            sscanf(s, "%4000s", s1);
            if(!strcmp(s1, "[CLIENT]"))
            {
                sscanf(s, "%*s %lx", &w);
                num_match++;
                matches = realloc(matches, sizeof(Match) * num_match);
                matches[num_match - 1].win = w;
                matches[num_match - 1].client_id = NULL;
                matches[num_match - 1].res_name = NULL;
                matches[num_match - 1].res_class = NULL;
                matches[num_match - 1].window_role = NULL;
                matches[num_match - 1].wm_name = NULL;
                matches[num_match - 1].wm_command_count = 0;
                matches[num_match - 1].wm_command = NULL;
                matches[num_match - 1].x = 0;
                matches[num_match - 1].y = 0;
                matches[num_match - 1].w = 100;
                matches[num_match - 1].h = 100;
                matches[num_match - 1].desktop = 0;
                matches[num_match - 1].used = FALSE;
                matches[num_match - 1].flags = 0;
            }
            else if(!strcmp(s1, "[GEOMETRY]"))
            {
                sscanf(s, "%*s (%i,%i,%i,%i)", &(matches[num_match - 1].x), &(matches[num_match - 1].y), &(matches[num_match - 1].w), &(matches[num_match - 1].h));
            }
            else if(!strcmp(s1, "[DESK]"))
            {
                sscanf(s, "%*s %i", &(matches[num_match - 1].desktop));
            }
            else if(!strcmp(s1, "[CLIENT_LEADER]"))
            {
                sscanf(s, "%*s 0x%lx", &(matches[num_match - 1].client_leader));
            }
            else if(!strcmp(s1, "[FLAGS]"))
            {
                sscanf(s, "%*s 0x%lx", &(matches[num_match - 1].flags));
            }
            else if(!strcmp(s1, "[CLIENT_ID]"))
            {
                sscanf(s, "%*s %[^\n]", s1);
                matches[num_match - 1].client_id = strdup(s1);
            }
            else if(!strcmp(s1, "[WINDOW_ROLE]"))
            {
                sscanf(s, "%*s %[^\n]", s1);
                matches[num_match - 1].window_role = strdup(s1);
            }
            else if(!strcmp(s1, "[RES_NAME]"))
            {
                sscanf(s, "%*s %[^\n]", s1);
                matches[num_match - 1].res_name = strdup(s1);
            }
            else if(!strcmp(s1, "[RES_CLASS]"))
            {
                sscanf(s, "%*s %[^\n]", s1);
                matches[num_match - 1].res_class = strdup(s1);
            }
            else if(!strcmp(s1, "[WM_NAME]"))
            {
                sscanf(s, "%*s %[^\n]", s1);
                matches[num_match - 1].wm_name = strdup(s1);
            }
            else if(!strcmp(s1, "[WM_COMMAND]"))
            {
                sscanf(s, "%*s (%i)%n", &matches[num_match - 1].wm_command_count, &pos);
                matches[num_match - 1].wm_command = (char **) malloc (sizeof (char *) * (matches[num_match - 1].wm_command_count + 1));
                for(i = 0; i < matches[num_match - 1].wm_command_count; i++)
                {
                    gchar *substring;
                    substring = getsubstring(s + pos, &pos1);
                    pos += pos1;
                    matches[num_match - 1].wm_command[i] = unescape_quote(substring);
                    g_free(substring);
                }
                matches[num_match - 1].wm_command[matches[num_match - 1].wm_command_count] = NULL;
            }

        }
        fclose(f);
	return TRUE;
    }
    return FALSE;
}

void sessionFreeWindowStates(void)
{
    int i;
    for(i = 0; i < num_match; i++)
    {
        if(matches[i].client_id)
        {
            free(matches[i].client_id);
	    matches[i].client_id = NULL;
        }
        if(matches[i].res_name)
        {
            free(matches[i].res_name);
	    matches[i].res_name = NULL;
        }
        if(matches[i].res_class)
        {
            free(matches[i].res_class);
	    matches[i].res_class = NULL;
        }
        if(matches[i].window_role)
        {
            free(matches[i].window_role);
	    matches[i].window_role = NULL;
        }
        if(matches[i].wm_name)
        {
            free(matches[i].wm_name);
	    matches[i].wm_name = NULL;
        }
        if((matches[i].wm_command_count) && (matches[i].wm_command))
        {
            XFreeStringList(matches[i].wm_command);
	    matches[i].wm_command_count = 0;
	    matches[i].wm_command = NULL;
        }
    }
    if (matches)
    {
        free(matches);
	matches = NULL;
	num_match = 0;
    }
}

/* This complicated logic is from twm, where it is explained */

#define xstreq(a,b) ((!a && !b) || (a && b && (strcmp(a,b)==0)))

static gboolean matchWin(Client * c, Match * m)
{
    gboolean found;
    int i;

    g_return_val_if_fail (c != NULL, FALSE);

    found = FALSE;
    if(xstreq(c->client_id, m->client_id))
    {
        /* client_id's match */
        if((c->window_role) || (m->window_role))
        {
            /* We have or had a window role, base decision on it */
            found = xstreq(c->window_role, m->window_role);
        }
	else
	{
            /* Compare res_class, res_name and WM_NAME, unless the
             * WM_NAME has changed
             */
            if(xstreq(c->class.res_name, m->res_name) && (CLIENT_FLAG_TEST(c, CLIENT_FLAG_NAME_CHANGED) || (m->flags & CLIENT_FLAG_NAME_CHANGED) || xstreq(c->name, m->wm_name)))
            {
        	if(c->client_id)
        	{
                    /* If we have a client_id, we don't compare
                       WM_COMMAND, since it will be different. */
                    found = 1;
        	}
        	else
        	{
                    /* for non-SM-aware clients we also compare WM_COMMAND */
                    if(c->wm_command_count == m->wm_command_count)
                    {
                	for(i = 0; i < c->wm_command_count; i++)
                	{
                            if(strcmp(c->wm_command[i], m->wm_command[i]) != 0)
                        	break;
                	}

                	if((i == c->wm_command_count) && (c->wm_command_count))
                	{
                            found = TRUE;
                	}
                    }           /* if (wm_command_count ==... */
                    /* We have to deal with a now-SM-aware client, it means that it won't probably
                     * restore its state in a proper manner.
                     * Thus, we also mark all other instances of this application as used, to avoid
                     * dummy side effects in case we found a matching entry.
                     */
                    if(found)
                    {
                	for(i = 0; i < num_match; i++)
                	{
                            if(!(matches[i].used) && !(&matches[i] == m) && (m->client_leader) && (matches[i].client_leader == m->client_leader))
                            {
                        	matches[i].used = TRUE;
                            }
                	}
                    }
        	}
            }
	}
    }

    return found;
}

gboolean sessionMatchWinToSM(Client *c)
{
    int i;
    
    g_return_if_fail (c != NULL);
    for(i = 0; i < num_match; i++)
    {
        if(!matches[i].used && matchWin(c, &matches[i]))
        {
	    matches[i].used = TRUE;
            c->x = matches[i].x;
            c->y = matches[i].y;
            c->width = matches[i].w;
            c->height = matches[i].h;
            c->win_workspace = matches[i].desktop;
            CLIENT_FLAG_SET(c, matches[i].flags & (CLIENT_FLAG_STICKY | CLIENT_FLAG_SHADED | CLIENT_FLAG_HIDDEN));
            return TRUE;
        }
    }
    return FALSE;
}