gs-manager.c 48.5 KB
Newer Older
Perberos's avatar
Perberos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2004-2008 William Jon McCann <mccann@jhu.edu>
 *
 * 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
Steve Zesch's avatar
Steve Zesch committed
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
Perberos's avatar
Perberos committed
18 19 20 21 22 23 24 25 26 27 28
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"

#include <time.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>

raveit's avatar
raveit committed
29
#include <gio/gio.h>
Perberos's avatar
Perberos committed
30 31

#define MATE_DESKTOP_USE_UNSTABLE_API
Stefano Karapetsas's avatar
Stefano Karapetsas committed
32
#include <libmate-desktop/mate-bg.h>
Perberos's avatar
Perberos committed
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

#include "gs-prefs.h"        /* for GSSaverMode */

#include "gs-manager.h"
#include "gs-window.h"
#include "gs-theme-manager.h"
#include "gs-job.h"
#include "gs-grab.h"
#include "gs-fade.h"
#include "gs-debug.h"

static void gs_manager_class_init (GSManagerClass *klass);
static void gs_manager_init       (GSManager      *manager);
static void gs_manager_finalize   (GObject        *object);

#define GS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_MANAGER, GSManagerPrivate))

struct GSManagerPrivate
{
	GSList      *windows;
	GHashTable  *jobs;

	GSThemeManager *theme_manager;
	MateBG        *bg;

	/* Policy */
	glong        lock_timeout;
	glong        cycle_timeout;
	glong        logout_timeout;

	guint        lock_enabled : 1;
	guint        logout_enabled : 1;
	guint        keyboard_enabled : 1;
	guint        user_switch_enabled : 1;
	guint        throttled : 1;

	char        *logout_command;
	char        *keyboard_command;

	char        *status_message;

	/* State */
	guint        active : 1;
	guint        lock_active : 1;

	guint        fading : 1;
	guint        dialog_up : 1;

	time_t       activate_time;

	guint        lock_timeout_id;
	guint        cycle_timeout_id;

	GSList      *themes;
	GSSaverMode  saver_mode;
	GSGrab      *grab;
	GSFade      *fade;
	guint        unfade_idle_id;
};

enum
{
    ACTIVATED,
    DEACTIVATED,
    AUTH_REQUEST_BEGIN,
    AUTH_REQUEST_END,
    LAST_SIGNAL
};

enum
{
    PROP_0,
    PROP_LOCK_ENABLED,
    PROP_LOGOUT_ENABLED,
    PROP_USER_SWITCH_ENABLED,
    PROP_KEYBOARD_ENABLED,
    PROP_LOCK_TIMEOUT,
    PROP_CYCLE_TIMEOUT,
    PROP_LOGOUT_TIMEOUT,
    PROP_LOGOUT_COMMAND,
    PROP_KEYBOARD_COMMAND,
    PROP_STATUS_MESSAGE,
    PROP_ACTIVE,
    PROP_THROTTLED,
};

119
#define FADE_TIMEOUT 250
Perberos's avatar
Perberos committed
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

static guint         signals [LAST_SIGNAL] = { 0, };

G_DEFINE_TYPE (GSManager, gs_manager, G_TYPE_OBJECT)

static void
manager_add_job_for_window (GSManager *manager,
                            GSWindow  *window,
                            GSJob     *job)
{
	if (manager->priv->jobs == NULL)
	{
		return;
	}

	g_hash_table_insert (manager->priv->jobs, window, job);
}

static const char *
select_theme (GSManager *manager)
{
	const char *theme = NULL;

	g_return_val_if_fail (manager != NULL, NULL);
	g_return_val_if_fail (GS_IS_MANAGER (manager), NULL);

	if (manager->priv->saver_mode == GS_MODE_BLANK_ONLY)
	{
		return NULL;
	}

	if (manager->priv->themes)
	{
		int number = 0;

		if (manager->priv->saver_mode == GS_MODE_RANDOM)
		{
			g_random_set_seed (time (NULL));
			number = g_random_int_range (0, g_slist_length (manager->priv->themes));
		}
		theme = g_slist_nth_data (manager->priv->themes, number);
	}

	return theme;
}

static GSJob *
lookup_job_for_window (GSManager *manager,
                       GSWindow  *window)
{
	GSJob *job;

	if (manager->priv->jobs == NULL)
	{
		return NULL;
	}

	job = g_hash_table_lookup (manager->priv->jobs, window);

	return job;
}

static void
manager_maybe_stop_job_for_window (GSManager *manager,
                                   GSWindow  *window)
{
	GSJob *job;

	job = lookup_job_for_window (manager, window);

	if (job == NULL)
	{
		gs_debug ("Job not found for window");
		return;
	}

	gs_job_stop (job);
}

static void
manager_maybe_start_job_for_window (GSManager *manager,
                                    GSWindow  *window)
{
	GSJob *job;

	job = lookup_job_for_window (manager, window);

	if (job == NULL)
	{
		gs_debug ("Job not found for window");
		return;
	}

	if (! manager->priv->dialog_up)
	{
		if (! manager->priv->throttled)
		{
			if (! gs_job_is_running (job))
			{
				if (! gs_window_is_obscured (window))
				{
					gs_debug ("Starting job for window");
					gs_job_start (job);
				}
				else
				{
					gs_debug ("Window is obscured deferring start of job");
				}
			}
			else
			{
				gs_debug ("Not starting job because job is running");
			}
		}
		else
		{
			gs_debug ("Not starting job because throttled");
		}
	}
	else
	{
		gs_debug ("Not starting job because dialog is up");
	}
}

static void
manager_select_theme_for_job (GSManager *manager,
                              GSJob     *job)
{
	const char *theme;

	theme = select_theme (manager);

	if (theme != NULL)
	{
		GSThemeInfo    *info;
		const char     *command;

		command = NULL;

		info = gs_theme_manager_lookup_theme_info (manager->priv->theme_manager, theme);
		if (info != NULL)
		{
			command = gs_theme_info_get_exec (info);
		}
		else
		{
			gs_debug ("Could not find information for theme: %s",
			          theme);
		}

		gs_job_set_command (job, command);


		if (info != NULL)
		{
			gs_theme_info_unref (info);
		}
	}
	else
	{
		gs_job_set_command (job, NULL);
	}
}

static void
cycle_job (GSWindow  *window,
           GSJob     *job,
           GSManager *manager)
{
	gs_job_stop (job);
	manager_select_theme_for_job (manager, job);
	manager_maybe_start_job_for_window (manager, window);
}

static void
manager_cycle_jobs (GSManager *manager)
{
	if (manager->priv->jobs != NULL)
	{
		g_hash_table_foreach (manager->priv->jobs, (GHFunc) cycle_job, manager);
	}
}

static void
throttle_job (GSWindow  *window,
              GSJob     *job,
              GSManager *manager)
{
	if (manager->priv->throttled)
	{
		gs_job_stop (job);
	}
	else
	{
		manager_maybe_start_job_for_window (manager, window);
	}
}

static void
manager_throttle_jobs (GSManager *manager)
{
	if (manager->priv->jobs != NULL)
	{
		g_hash_table_foreach (manager->priv->jobs, (GHFunc) throttle_job, manager);
	}
}

static void
resume_job (GSWindow  *window,
            GSJob     *job,
            GSManager *manager)
{
	if (gs_job_is_running (job))
	{
		gs_job_suspend (job, FALSE);
	}
	else
	{
		manager_maybe_start_job_for_window (manager, window);
	}
}

static void
manager_resume_jobs (GSManager *manager)
{
	if (manager->priv->jobs != NULL)
	{
		g_hash_table_foreach (manager->priv->jobs, (GHFunc) resume_job, manager);
	}
}

static void
suspend_job (GSWindow  *window,
             GSJob     *job,
             GSManager *manager)
{
	gs_job_suspend (job, TRUE);
}

static void
manager_suspend_jobs (GSManager *manager)
{
	if (manager->priv->jobs != NULL)
	{
		g_hash_table_foreach (manager->priv->jobs, (GHFunc) suspend_job, manager);
	}
}

static void
manager_stop_jobs (GSManager *manager)
{
	if (manager->priv->jobs != NULL)
	{
		g_hash_table_destroy (manager->priv->jobs);

	}
	manager->priv->jobs = NULL;
}

void
gs_manager_set_mode (GSManager  *manager,
                     GSSaverMode mode)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	manager->priv->saver_mode = mode;
}

static void
free_themes (GSManager *manager)
{
	if (manager->priv->themes)
	{
		g_slist_foreach (manager->priv->themes, (GFunc)g_free, NULL);
		g_slist_free (manager->priv->themes);
	}
}

void
gs_manager_set_themes (GSManager *manager,
                       GSList    *themes)
{
	GSList *l;

	g_return_if_fail (GS_IS_MANAGER (manager));

	free_themes (manager);
	manager->priv->themes = NULL;

	for (l = themes; l; l = l->next)
	{
		manager->priv->themes = g_slist_append (manager->priv->themes, g_strdup (l->data));
	}
}

void
gs_manager_set_throttled (GSManager *manager,
                          gboolean   throttled)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->throttled != throttled)
	{
		GSList *l;

		manager->priv->throttled = throttled;

		if (! manager->priv->dialog_up)
		{

			manager_throttle_jobs (manager);

			for (l = manager->priv->windows; l; l = l->next)
			{
				gs_window_clear (l->data);
			}
		}
	}
}

void
gs_manager_get_lock_active (GSManager *manager,
                            gboolean  *lock_active)
{
	if (lock_active != NULL)
	{
		*lock_active = FALSE;
	}

	g_return_if_fail (GS_IS_MANAGER (manager));

Monsta's avatar
Monsta committed
452 453 454 455
	if (lock_active != NULL)
	{
		*lock_active = manager->priv->lock_active;
	}
Perberos's avatar
Perberos committed
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
}

void
gs_manager_set_lock_active (GSManager *manager,
                            gboolean   lock_active)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	gs_debug ("Setting lock active: %d", lock_active);

	if (manager->priv->lock_active != lock_active)
	{
		GSList *l;

		manager->priv->lock_active = lock_active;
		for (l = manager->priv->windows; l; l = l->next)
		{
			gs_window_set_lock_enabled (l->data, lock_active);
		}
	}
}

void
gs_manager_get_lock_enabled (GSManager *manager,
                             gboolean  *lock_enabled)
{
	if (lock_enabled != NULL)
	{
		*lock_enabled = FALSE;
	}

	g_return_if_fail (GS_IS_MANAGER (manager));

Monsta's avatar
Monsta committed
489 490 491 492
	if (lock_enabled != NULL)
	{
		*lock_enabled = manager->priv->lock_enabled;
	}
Perberos's avatar
Perberos committed
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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
}

void
gs_manager_set_lock_enabled (GSManager *manager,
                             gboolean   lock_enabled)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->lock_enabled != lock_enabled)
	{
		manager->priv->lock_enabled = lock_enabled;
	}
}

void
gs_manager_set_logout_enabled (GSManager *manager,
                               gboolean   logout_enabled)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->logout_enabled != logout_enabled)
	{
		GSList *l;

		manager->priv->logout_enabled = logout_enabled;
		for (l = manager->priv->windows; l; l = l->next)
		{
			gs_window_set_logout_enabled (l->data, logout_enabled);
		}
	}
}

void
gs_manager_set_keyboard_enabled (GSManager *manager,
                                 gboolean   enabled)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->keyboard_enabled != enabled)
	{
		GSList *l;

		manager->priv->keyboard_enabled = enabled;
		for (l = manager->priv->windows; l; l = l->next)
		{
			gs_window_set_keyboard_enabled (l->data, enabled);
		}
	}
}

void
gs_manager_set_user_switch_enabled (GSManager *manager,
                                    gboolean   user_switch_enabled)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->user_switch_enabled != user_switch_enabled)
	{
		GSList *l;

		manager->priv->user_switch_enabled = user_switch_enabled;
		for (l = manager->priv->windows; l; l = l->next)
		{
			gs_window_set_user_switch_enabled (l->data, user_switch_enabled);
		}
	}
}

static gboolean
activate_lock_timeout (GSManager *manager)
{
	if (manager->priv->lock_enabled)
	{
		gs_manager_set_lock_active (manager, TRUE);
	}

	manager->priv->lock_timeout_id = 0;

	return FALSE;
}

static void
remove_lock_timer (GSManager *manager)
{
	if (manager->priv->lock_timeout_id != 0)
	{
		g_source_remove (manager->priv->lock_timeout_id);
		manager->priv->lock_timeout_id = 0;
	}
}

static void
add_lock_timer (GSManager *manager,
                glong      timeout)
{
	manager->priv->lock_timeout_id = g_timeout_add (timeout,
	                                 (GSourceFunc)activate_lock_timeout,
	                                 manager);
}

void
gs_manager_set_lock_timeout (GSManager *manager,
                             glong      lock_timeout)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->lock_timeout != lock_timeout)
	{

		manager->priv->lock_timeout = lock_timeout;

		if (manager->priv->active
		        && ! manager->priv->lock_active
		        && (lock_timeout >= 0))
		{

			glong elapsed = (time (NULL) - manager->priv->activate_time) * 1000;

			remove_lock_timer (manager);

			if (elapsed >= lock_timeout)
			{
				activate_lock_timeout (manager);
			}
			else
			{
				add_lock_timer (manager, lock_timeout - elapsed);
			}
		}
	}
}

void
gs_manager_set_logout_timeout (GSManager *manager,
                               glong      logout_timeout)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->logout_timeout != logout_timeout)
	{
		GSList *l;

		manager->priv->logout_timeout = logout_timeout;
		for (l = manager->priv->windows; l; l = l->next)
		{
			gs_window_set_logout_timeout (l->data, logout_timeout);
		}
	}
}

void
gs_manager_set_logout_command (GSManager  *manager,
                               const char *command)
{
	GSList *l;

	g_return_if_fail (GS_IS_MANAGER (manager));

	g_free (manager->priv->logout_command);

	if (command)
	{
		manager->priv->logout_command = g_strdup (command);
	}
	else
	{
		manager->priv->logout_command = NULL;
	}

	for (l = manager->priv->windows; l; l = l->next)
	{
		gs_window_set_logout_command (l->data, manager->priv->logout_command);
	}
}

void
gs_manager_set_keyboard_command (GSManager  *manager,
                                 const char *command)
{
	GSList *l;

	g_return_if_fail (GS_IS_MANAGER (manager));

	g_free (manager->priv->keyboard_command);

	if (command)
	{
		manager->priv->keyboard_command = g_strdup (command);
	}
	else
	{
		manager->priv->keyboard_command = NULL;
	}

	for (l = manager->priv->windows; l; l = l->next)
	{
		gs_window_set_keyboard_command (l->data, manager->priv->keyboard_command);
	}
}

void
gs_manager_set_status_message (GSManager  *manager,
                               const char *status_message)
{
	GSList *l;

	g_return_if_fail (GS_IS_MANAGER (manager));

	g_free (manager->priv->status_message);

	manager->priv->status_message = g_strdup (status_message);

	for (l = manager->priv->windows; l; l = l->next)
	{
		gs_window_set_status_message (l->data, manager->priv->status_message);
	}
}

gboolean
gs_manager_cycle (GSManager *manager)
{
	g_return_val_if_fail (manager != NULL, FALSE);
	g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);

	gs_debug ("cycling jobs");

	if (! manager->priv->active)
	{
		return FALSE;
	}

	if (manager->priv->dialog_up)
	{
		return FALSE;
	}

	if (manager->priv->throttled)
	{
		return FALSE;
	}

	manager_cycle_jobs (manager);

	return TRUE;
}

static gboolean
cycle_timeout (GSManager *manager)
{
	g_return_val_if_fail (manager != NULL, FALSE);
	g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);

	if (! manager->priv->dialog_up)
	{
		gs_manager_cycle (manager);
	}

	return TRUE;
}

static void
remove_cycle_timer (GSManager *manager)
{
	if (manager->priv->cycle_timeout_id != 0)
	{
		g_source_remove (manager->priv->cycle_timeout_id);
		manager->priv->cycle_timeout_id = 0;
	}
}

static void
add_cycle_timer (GSManager *manager,
                 glong      timeout)
{
	manager->priv->cycle_timeout_id = g_timeout_add (timeout,
	                                  (GSourceFunc)cycle_timeout,
	                                  manager);
}

void
gs_manager_set_cycle_timeout (GSManager *manager,
                              glong      cycle_timeout)
{
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->cycle_timeout != cycle_timeout)
	{

		manager->priv->cycle_timeout = cycle_timeout;

		if (manager->priv->active && (cycle_timeout >= 0))
		{
			glong timeout;
			glong elapsed = (time (NULL) - manager->priv->activate_time) * 1000;

			remove_cycle_timer (manager);

			if (elapsed >= cycle_timeout)
			{
				timeout = 0;
			}
			else
			{
				timeout = cycle_timeout - elapsed;
			}

			add_cycle_timer (manager, timeout);

		}
	}
}

static void
gs_manager_set_property (GObject            *object,
                         guint               prop_id,
                         const GValue       *value,
                         GParamSpec         *pspec)
{
	GSManager *self;

	self = GS_MANAGER (object);

	switch (prop_id)
	{
	case PROP_THROTTLED:
		gs_manager_set_throttled (self, g_value_get_boolean (value));
		break;
	case PROP_LOCK_ENABLED:
		gs_manager_set_lock_enabled (self, g_value_get_boolean (value));
		break;
	case PROP_LOCK_TIMEOUT:
		gs_manager_set_lock_timeout (self, g_value_get_long (value));
		break;
	case PROP_LOGOUT_ENABLED:
		gs_manager_set_logout_enabled (self, g_value_get_boolean (value));
		break;
	case PROP_KEYBOARD_ENABLED:
		gs_manager_set_keyboard_enabled (self, g_value_get_boolean (value));
		break;
	case PROP_USER_SWITCH_ENABLED:
		gs_manager_set_user_switch_enabled (self, g_value_get_boolean (value));
		break;
	case PROP_LOGOUT_TIMEOUT:
		gs_manager_set_logout_timeout (self, g_value_get_long (value));
		break;
	case PROP_LOGOUT_COMMAND:
		gs_manager_set_logout_command (self, g_value_get_string (value));
		break;
	case PROP_KEYBOARD_COMMAND:
		gs_manager_set_keyboard_command (self, g_value_get_string (value));
		break;
	case PROP_STATUS_MESSAGE:
		gs_manager_set_status_message (self, g_value_get_string (value));
		break;
	case PROP_CYCLE_TIMEOUT:
		gs_manager_set_cycle_timeout (self, g_value_get_long (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_manager_get_property (GObject            *object,
                         guint               prop_id,
                         GValue             *value,
                         GParamSpec         *pspec)
{
	GSManager *self;

	self = GS_MANAGER (object);

	switch (prop_id)
	{
	case PROP_THROTTLED:
		g_value_set_boolean (value, self->priv->throttled);
		break;
	case PROP_LOCK_ENABLED:
		g_value_set_boolean (value, self->priv->lock_enabled);
		break;
	case PROP_LOCK_TIMEOUT:
		g_value_set_long (value, self->priv->lock_timeout);
		break;
	case PROP_LOGOUT_ENABLED:
		g_value_set_boolean (value, self->priv->logout_enabled);
		break;
	case PROP_KEYBOARD_ENABLED:
		g_value_set_boolean (value, self->priv->keyboard_enabled);
		break;
	case PROP_USER_SWITCH_ENABLED:
		g_value_set_boolean (value, self->priv->user_switch_enabled);
		break;
	case PROP_LOGOUT_TIMEOUT:
		g_value_set_long (value, self->priv->logout_timeout);
		break;
	case PROP_LOGOUT_COMMAND:
		g_value_set_string (value, self->priv->logout_command);
		break;
	case PROP_KEYBOARD_COMMAND:
		g_value_set_string (value, self->priv->keyboard_command);
		break;
	case PROP_STATUS_MESSAGE:
		g_value_set_string (value, self->priv->status_message);
		break;
	case PROP_CYCLE_TIMEOUT:
		g_value_set_long (value, self->priv->cycle_timeout);
		break;
	case PROP_ACTIVE:
		g_value_set_boolean (value, self->priv->active);
Stefano Karapetsas's avatar
Stefano Karapetsas committed
903
		break;
Perberos's avatar
Perberos committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_manager_class_init (GSManagerClass *klass)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize     = gs_manager_finalize;
	object_class->get_property = gs_manager_get_property;
	object_class->set_property = gs_manager_set_property;

	signals [ACTIVATED] =
	    g_signal_new ("activated",
	                  G_TYPE_FROM_CLASS (object_class),
	                  G_SIGNAL_RUN_LAST,
	                  G_STRUCT_OFFSET (GSManagerClass, activated),
	                  NULL,
	                  NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE,
	                  0);
	signals [DEACTIVATED] =
	    g_signal_new ("deactivated",
	                  G_TYPE_FROM_CLASS (object_class),
	                  G_SIGNAL_RUN_LAST,
	                  G_STRUCT_OFFSET (GSManagerClass, deactivated),
	                  NULL,
	                  NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE,
	                  0);
	signals [AUTH_REQUEST_BEGIN] =
	    g_signal_new ("auth-request-begin",
	                  G_TYPE_FROM_CLASS (object_class),
	                  G_SIGNAL_RUN_LAST,
	                  G_STRUCT_OFFSET (GSManagerClass, auth_request_begin),
	                  NULL,
	                  NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE,
	                  0);
	signals [AUTH_REQUEST_END] =
	    g_signal_new ("auth-request-end",
	                  G_TYPE_FROM_CLASS (object_class),
	                  G_SIGNAL_RUN_LAST,
	                  G_STRUCT_OFFSET (GSManagerClass, auth_request_end),
	                  NULL,
	                  NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE,
	                  0);

	g_object_class_install_property (object_class,
	                                 PROP_ACTIVE,
	                                 g_param_spec_boolean ("active",
	                                         NULL,
	                                         NULL,
	                                         FALSE,
	                                         G_PARAM_READABLE));
	g_object_class_install_property (object_class,
	                                 PROP_LOCK_ENABLED,
	                                 g_param_spec_boolean ("lock-enabled",
	                                         NULL,
	                                         NULL,
	                                         FALSE,
	                                         G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
	                                 PROP_LOCK_TIMEOUT,
	                                 g_param_spec_long ("lock-timeout",
	                                         NULL,
	                                         NULL,
	                                         -1,
	                                         G_MAXLONG,
	                                         0,
	                                         G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
	                                 PROP_LOGOUT_ENABLED,
	                                 g_param_spec_boolean ("logout-enabled",
	                                         NULL,
	                                         NULL,
	                                         FALSE,
	                                         G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
	                                 PROP_USER_SWITCH_ENABLED,
	                                 g_param_spec_boolean ("user-switch-enabled",
	                                         NULL,
	                                         NULL,
	                                         FALSE,
	                                         G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
	                                 PROP_LOGOUT_TIMEOUT,
	                                 g_param_spec_long ("logout-timeout",
	                                         NULL,
	                                         NULL,
	                                         -1,
	                                         G_MAXLONG,
	                                         0,
	                                         G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
	                                 PROP_LOGOUT_COMMAND,
	                                 g_param_spec_string ("logout-command",
	                                         NULL,
	                                         NULL,
	                                         NULL,
	                                         G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
	                                 PROP_CYCLE_TIMEOUT,
	                                 g_param_spec_long ("cycle-timeout",
	                                         NULL,
	                                         NULL,
	                                         10000,
	                                         G_MAXLONG,
	                                         300000,
	                                         G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
	                                 PROP_THROTTLED,
	                                 g_param_spec_boolean ("throttled",
	                                         NULL,
	                                         NULL,
	                                         TRUE,
	                                         G_PARAM_READWRITE));

	g_type_class_add_private (klass, sizeof (GSManagerPrivate));
}

static void
on_bg_changed (MateBG   *bg,
               GSManager *manager)
{
	gs_debug ("background changed");
}

static void
gs_manager_init (GSManager *manager)
{
	manager->priv = GS_MANAGER_GET_PRIVATE (manager);

	manager->priv->fade = gs_fade_new ();
	manager->priv->grab = gs_grab_new ();
	manager->priv->theme_manager = gs_theme_manager_new ();

1050
	manager->priv->bg = mate_bg_new ();
Perberos's avatar
Perberos committed
1051

raveit's avatar
raveit committed
1052
	g_signal_connect (manager->priv->bg,
1053 1054 1055
					  "changed",
					  G_CALLBACK (on_bg_changed),
					  manager);
Perberos's avatar
Perberos committed
1056

1057
	mate_bg_load_from_preferences (manager->priv->bg);
Perberos's avatar
Perberos committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
}

static void
remove_timers (GSManager *manager)
{
	remove_lock_timer (manager);
	remove_cycle_timer (manager);
}

static void
remove_unfade_idle (GSManager *manager)
{
	if (manager->priv->unfade_idle_id > 0)
	{
		g_source_remove (manager->priv->unfade_idle_id);
		manager->priv->unfade_idle_id = 0;
	}
}


static gboolean
window_deactivated_idle (GSManager *manager)
{
	g_return_val_if_fail (manager != NULL, FALSE);
	g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);

	/* don't deactivate directly but only emit a signal
	   so that we let the parent deactivate */
	g_signal_emit (manager, signals [DEACTIVATED], 0);

	return FALSE;
}

static void
window_deactivated_cb (GSWindow  *window,
                       GSManager *manager)
{
	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));

	g_idle_add ((GSourceFunc)window_deactivated_idle, manager);
}

static GSWindow *
find_window_at_pointer (GSManager *manager)
{
	GdkDisplay *display;
1105
	GdkDevice  *device;
1106 1107 1108 1109
#if GTK_CHECK_VERSION (3, 22, 0)
	GdkMonitor *monitor;
#else
	GdkScreen  *screen;
Perberos's avatar
Perberos committed
1110
	int         monitor;
1111
#endif
Perberos's avatar
Perberos committed
1112 1113 1114 1115 1116
	int         x, y;
	GSWindow   *window;
	GSList     *l;

	display = gdk_display_get_default ();
1117

monsta's avatar
monsta committed
1118
#if GTK_CHECK_VERSION (3, 20, 0)
1119 1120 1121 1122
	device = gdk_seat_get_pointer (gdk_display_get_default_seat (display));
#else
	device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
#endif
1123 1124 1125 1126
#if GTK_CHECK_VERSION (3, 22, 0)
	gdk_device_get_position (device, NULL, &x, &y);
	monitor = gdk_display_get_monitor_at_point (display, x, y);
#else
1127
	gdk_device_get_position (device, &screen, &x, &y);
Perberos's avatar
Perberos committed
1128
	monitor = gdk_screen_get_monitor_at_point (screen, x, y);
1129
#endif
Perberos's avatar
Perberos committed
1130

1131
	/* Find the gs-window that is on that monitor */
Perberos's avatar
Perberos committed
1132 1133 1134 1135
	window = NULL;
	for (l = manager->priv->windows; l; l = l->next)
	{
		GSWindow *win = GS_WINDOW (l->data);
1136 1137
		if (gs_window_get_display (win) == display &&
		    gs_window_get_monitor (win) == monitor)
Perberos's avatar
Perberos committed
1138 1139 1140 1141 1142 1143 1144
		{
			window = win;
		}
	}

	if (window == NULL)
	{
1145 1146
		gs_debug ("WARNING: Could not find the GSWindow for display %s",
		          gdk_display_get_name (display));
Perberos's avatar
Perberos committed
1147 1148 1149 1150 1151
		/* take the first one */
		window = manager->priv->windows->data;
	}
	else
	{
1152 1153
		gs_debug ("Requesting unlock for display %s",
		          gdk_display_get_name (display));
Perberos's avatar
Perberos committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	}

	return window;
}

void
gs_manager_show_message (GSManager  *manager,
                         const char *summary,
                         const char *body,
                         const char *icon)
{
	GSWindow *window;

	g_return_if_fail (GS_IS_MANAGER (manager));

	/* Find the GSWindow that contains the pointer */
	window = find_window_at_pointer (manager);
	gs_window_show_message (window, summary, body, icon);

	gs_manager_request_unlock (manager);
}

static gboolean
manager_maybe_grab_window (GSManager *manager,
                           GSWindow  *window)
{
	GdkDisplay *display;
1181
	GdkDevice  *device;
1182 1183 1184 1185
#if GTK_CHECK_VERSION (3, 22, 0)
	GdkMonitor *monitor;
#else
	GdkScreen  *screen;
Perberos's avatar
Perberos committed
1186
	int         monitor;
1187
#endif
Perberos's avatar
Perberos committed
1188 1189 1190 1191
	int         x, y;
	gboolean    grabbed;

	display = gdk_display_get_default ();
monsta's avatar
monsta committed
1192
#if GTK_CHECK_VERSION (3, 20, 0)
1193 1194 1195 1196
	device = gdk_seat_get_pointer (gdk_display_get_default_seat (display));
#else
	device = gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
#endif
1197 1198 1199 1200
#if GTK_CHECK_VERSION (3, 22, 0)
	gdk_device_get_position (device, NULL, &x, &y);
	monitor = gdk_display_get_monitor_at_point (display, x, y);
#else
1201
	gdk_device_get_position (device, &screen, &x, &y);
Perberos's avatar
Perberos committed
1202
	monitor = gdk_screen_get_monitor_at_point (screen, x, y);
1203
#endif
Perberos's avatar
Perberos committed
1204 1205 1206

	gdk_flush ();
	grabbed = FALSE;
1207 1208
	if (gs_window_get_display (window) == display &&
	    gs_window_get_monitor (window) == monitor)
Perberos's avatar
Perberos committed
1209
	{
Alexei Sorokin's avatar
Alexei Sorokin committed
1210
		gs_debug ("Initiate grab move to %p", window);
Perberos's avatar
Perberos committed
1211 1212
		gs_grab_move_to_window (manager->priv->grab,
		                        gs_window_get_gdk_window (window),
1213
		                        gs_window_get_display (window),
Alexei Sorokin's avatar
Alexei Sorokin committed
1214
		                        FALSE, FALSE);
Perberos's avatar
Perberos committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
		grabbed = TRUE;
	}

	return grabbed;
}

static void
window_grab_broken_cb (GSWindow           *window,
                       GdkEventGrabBroken *event,
                       GSManager          *manager)
{
Alexei Sorokin's avatar
Alexei Sorokin committed
1226 1227 1228 1229 1230 1231 1232 1233 1234
#if GTK_CHECK_VERSION (3, 20, 0)
	GdkDisplay *display;
	GdkSeat    *seat;
	GdkDevice  *device;

	display = gdk_window_get_display (gs_window_get_gdk_window (window));
	seat = gdk_display_get_default_seat (display);

#endif
Perberos's avatar
Perberos committed
1235 1236
	if (event->keyboard)
	{
Alexei Sorokin's avatar
Alexei Sorokin committed
1237 1238 1239 1240 1241 1242 1243 1244 1245
		gs_debug ("KEYBOARD GRAB BROKEN!");
#if GTK_CHECK_VERSION (3, 20, 0)
		device = gdk_seat_get_pointer (seat);
		if (!gdk_display_device_is_grabbed (display, device))
			gs_grab_reset (manager->priv->grab);
#else
		if (!gdk_pointer_is_grabbed ())
			gs_grab_reset (manager->priv->grab);
#endif
Perberos's avatar
Perberos committed
1246 1247 1248
	}
	else
	{
Alexei Sorokin's avatar
Alexei Sorokin committed
1249 1250 1251 1252 1253 1254
		gs_debug ("POINTER GRAB BROKEN!");
#if GTK_CHECK_VERSION (3, 20, 0)
		device = gdk_seat_get_keyboard (seat);
		if (!gdk_display_device_is_grabbed (display, device))
			gs_grab_reset (manager->priv->grab);
#endif
Perberos's avatar
Perberos committed
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	}
}

static gboolean
unfade_idle (GSManager *manager)
{
	gs_debug ("resetting fade");
	gs_fade_reset (manager->priv->fade);
	manager->priv->unfade_idle_id = 0;
	return FALSE;
}


static void
add_unfade_idle (GSManager *manager)
{
	remove_unfade_idle (manager);
	manager->priv->unfade_idle_id = g_timeout_add (500, (GSourceFunc)unfade_idle, manager);
}

static gboolean
window_map_event_cb (GSWindow  *window,
                     GdkEvent  *event,
                     GSManager *manager)
{
	gs_debug ("Handling window map_event event");

	manager_maybe_grab_window (manager, window);

	manager_maybe_start_job_for_window (manager, window);

	return FALSE;
}

static void
window_map_cb (GSWindow  *window,
               GSManager *manager)
{
	gs_debug ("Handling window map event");
}

static void
window_unmap_cb (GSWindow  *window,
                 GSManager *manager)
{
	gs_debug ("window unmapped!");
}

static void
apply_background_to_window (GSManager *manager,
                            GSWindow  *window)
{
Stefano Karapetsas's avatar
Stefano Karapetsas committed
1307
	cairo_surface_t *surface;
1308
	GdkDisplay      *display;
Perberos's avatar
Perberos committed
1309 1310 1311 1312
	GdkScreen       *screen;
	int              width;
	int              height;

1313 1314
        mate_bg_load_from_preferences (manager->priv->bg);

Perberos's avatar
Perberos committed
1315 1316 1317
	if (manager->priv->bg == NULL)
	{
		gs_debug ("No background available");
Stefano Karapetsas's avatar
Stefano Karapetsas committed
1318
		gs_window_set_background_surface (window, NULL);
Perberos's avatar
Perberos committed
1319 1320
	}

1321 1322
	display = gs_window_get_display (window);
	screen = gdk_display_get_default_screen (display);
1323 1324
	width = WidthOfScreen (gdk_x11_screen_get_xscreen (screen));
	height = HeightOfScreen (gdk_x11_screen_get_xscreen (screen));
Stefano Karapetsas's avatar
Stefano Karapetsas committed
1325 1326 1327 1328 1329 1330 1331 1332
	gs_debug ("Creating background w:%d h:%d", width, height);
	surface = mate_bg_create_surface (manager->priv->bg,
	                                  gs_window_get_gdk_window (window),
	                                  width,
	                                  height,
	                                  FALSE);
	gs_window_set_background_surface (window, surface);
	cairo_surface_destroy (surface);
Perberos's avatar
Perberos committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
}

static void
manager_show_window (GSManager *manager,
                     GSWindow  *window)
{
	GSJob *job;

	apply_background_to_window (manager, window);

	job = gs_job_new_for_widget (gs_window_get_drawing_area (window));

	manager_select_theme_for_job (manager, job);
	manager_add_job_for_window (manager, window, job);

	manager->priv->activate_time = time (NULL);

	if (manager->priv->lock_timeout >= 0)
	{
		remove_lock_timer (manager);
		add_lock_timer (manager, manager->priv->lock_timeout);
	}

	if (manager->priv->cycle_timeout >= 10000)
	{
		remove_cycle_timer (manager);
		add_cycle_timer (manager, manager->priv->cycle_timeout);
	}

	add_unfade_idle (manager);

	/* FIXME: only emit signal once */
	g_signal_emit (manager, signals [ACTIVATED], 0);
}

static void
window_show_cb (GSWindow  *window,
                GSManager *manager)
{

	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));
	g_return_if_fail (window != NULL);
	g_return_if_fail (GS_IS_WINDOW (window));

	gs_debug ("Handling window show");
	manager_show_window (manager, window);
}

static void
maybe_set_window_throttle (GSManager *manager,
                           GSWindow  *window,
                           gboolean   throttled)
{
	if (throttled)
	{
		manager_maybe_stop_job_for_window (manager, window);
	}
	else
	{
		manager_maybe_start_job_for_window (manager, window);
	}
}

static void
window_obscured_cb (GSWindow   *window,
                    GParamSpec *pspec,
                    GSManager  *manager)
{
	gboolean obscured;

	obscured = gs_window_is_obscured (window);
	gs_debug ("Handling window obscured: %s", obscured ? "obscured" : "unobscured");

	maybe_set_window_throttle (manager, window, obscured);

	if (! obscured)
	{
		gs_manager_request_unlock (manager);
	}
}

static void
handle_window_dialog_up (GSManager *manager,
                         GSWindow  *window)
{
	GSList *l;

	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));

	gs_debug ("Handling dialog up");

	g_signal_emit (manager, signals [AUTH_REQUEST_BEGIN], 0);

	manager->priv->dialog_up = TRUE;
Alexei Sorokin's avatar
Alexei Sorokin committed
1429
	/* make all other windows insensitive to not get events */
Perberos's avatar
Perberos committed
1430 1431 1432 1433 1434 1435 1436 1437
	for (l = manager->priv->windows; l; l = l->next)
	{
		if (l->data != window)
		{
			gtk_widget_set_sensitive (GTK_WIDGET (l->data), FALSE);
		}
	}

Alexei Sorokin's avatar
Alexei Sorokin committed
1438 1439 1440 1441
	/* move devices grab so that dialog can be used;
	   release the pointer grab while dialog is up so that
	   the dialog can be used. We'll regrab it when the dialog goes down */
	gs_debug ("Initiate pointer-less grab move to %p", window);
Perberos's avatar
Perberos committed
1442 1443
	gs_grab_move_to_window (manager->priv->grab,
	                        gs_window_get_gdk_window (window),
1444
	                        gs_window_get_display (window),
Alexei Sorokin's avatar
Alexei Sorokin committed
1445
	                        TRUE, FALSE);
Perberos's avatar
Perberos committed
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

	if (! manager->priv->throttled)
	{
		gs_debug ("Suspending jobs");

		manager_suspend_jobs (manager);
	}
}

static void
handle_window_dialog_down (GSManager *manager,
                           GSWindow  *window)
{
	GSList *l;

	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));

	gs_debug ("Handling dialog down");

Alexei Sorokin's avatar
Alexei Sorokin committed
1466
	/* regrab pointer */
Perberos's avatar
Perberos committed
1467 1468
	gs_grab_move_to_window (manager->priv->grab,
	                        gs_window_get_gdk_window (window),
1469
	                        gs_window_get_display (window),
Alexei Sorokin's avatar
Alexei Sorokin committed
1470
	                        FALSE, FALSE);
Perberos's avatar
Perberos committed
1471

Alexei Sorokin's avatar
Alexei Sorokin committed
1472
	/* make all windows sensitive to get events */
Perberos's avatar
Perberos committed
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	for (l = manager->priv->windows; l; l = l->next)
	{
		gtk_widget_set_sensitive (GTK_WIDGET (l->data), TRUE);
	}

	manager->priv->dialog_up = FALSE;

	if (! manager->priv->throttled)
	{
		manager_resume_jobs (manager);
	}

	g_signal_emit (manager, signals [AUTH_REQUEST_END], 0);
}

static void
window_dialog_up_changed_cb (GSWindow   *window,
                             GParamSpec *pspec,
                             GSManager  *manager)
{
	gboolean up;

	up = gs_window_is_dialog_up (window);
	gs_debug ("Handling window dialog up changed: %s", up ? "up" : "down");
	if (up)
	{
		handle_window_dialog_up (manager, window);
	}
	else
	{
		handle_window_dialog_down (manager, window);
	}
}

static gboolean
window_activity_cb (GSWindow  *window,
                    GSManager *manager)
{
	gboolean handled;

	handled = gs_manager_request_unlock (manager);

	return handled;
}

static void
disconnect_window_signals (GSManager *manager,
                           GSWindow  *window)
{
	g_signal_handlers_disconnect_by_func (window, window_deactivated_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_activity_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_show_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_map_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_map_event_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_obscured_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_dialog_up_changed_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_unmap_cb, manager);
	g_signal_handlers_disconnect_by_func (window, window_grab_broken_cb, manager);
}

static void
window_destroyed_cb (GtkWindow *window,
                     GSManager *manager)
{
	disconnect_window_signals (manager, GS_WINDOW (window));
}

static void
connect_window_signals (GSManager *manager,
                        GSWindow  *window)
{
	g_signal_connect_object (window, "destroy",
	                         G_CALLBACK (window_destroyed_cb), manager, 0);
	g_signal_connect_object (window, "activity",
	                         G_CALLBACK (window_activity_cb), manager, 0);
	g_signal_connect_object (window, "deactivated",
	                         G_CALLBACK (window_deactivated_cb), manager, 0);
	g_signal_connect_object (window, "show",
	                         G_CALLBACK (window_show_cb), manager, G_CONNECT_AFTER);
	g_signal_connect_object (window, "map",
	                         G_CALLBACK (window_map_cb), manager, G_CONNECT_AFTER);
	g_signal_connect_object (window, "map_event",
	                         G_CALLBACK (window_map_event_cb), manager, G_CONNECT_AFTER);
	g_signal_connect_object (window, "notify::obscured",
	                         G_CALLBACK (window_obscured_cb), manager, G_CONNECT_AFTER);
	g_signal_connect_object (window, "notify::dialog-up",
	                         G_CALLBACK (window_dialog_up_changed_cb), manager, 0);
	g_signal_connect_object (window, "unmap",
	                         G_CALLBACK (window_unmap_cb), manager, G_CONNECT_AFTER);
	g_signal_connect_object (window, "grab_broken_event",
	                         G_CALLBACK (window_grab_broken_cb), manager, G_CONNECT_AFTER);
}

static void
1567 1568 1569 1570 1571 1572 1573
gs_manager_create_window_for_monitor (GSManager  *manager,
#if GTK_CHECK_VERSION (3, 22, 0)
                                      GdkMonitor *monitor)
#else
                                      GdkDisplay *display,
                                      int         monitor)
#endif
Perberos's avatar
Perberos committed
1574 1575
{
	GSWindow    *window;
1576 1577 1578
#if GTK_CHECK_VERSION (3, 22, 0)
	GdkDisplay  *display;
#endif
Perberos's avatar
Perberos committed
1579 1580
	GdkRectangle rect;

1581 1582 1583 1584 1585 1586 1587
#if GTK_CHECK_VERSION (3, 22, 0)
	display = gdk_monitor_get_display (monitor);
	gdk_monitor_get_geometry (monitor, &rect);
#else
	gdk_screen_get_monitor_geometry (gdk_display_get_default_screen (display),
	                                 monitor, &rect);
#endif
Perberos's avatar
Perberos committed
1588

1589
	gs_debug ("Creating a window for the monitor [%d,%d] (%dx%d)",
Perberos's avatar
Perberos committed
1590 1591
	          monitor, rect.x, rect.y, rect.width, rect.height);

1592
	window = gs_window_new (display, monitor, manager->priv->lock_active);
Perberos's avatar
Perberos committed
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615

	gs_window_set_user_switch_enabled (window, manager->priv->user_switch_enabled);
	gs_window_set_logout_enabled (window, manager->priv->logout_enabled);
	gs_window_set_logout_timeout (window, manager->priv->logout_timeout);
	gs_window_set_logout_command (window, manager->priv->logout_command);
	gs_window_set_keyboard_enabled (window, manager->priv->keyboard_enabled);
	gs_window_set_keyboard_command (window, manager->priv->keyboard_command);
	gs_window_set_status_message (window, manager->priv->status_message);

	connect_window_signals (manager, window);

	manager->priv->windows = g_slist_append (manager->priv->windows, window);

	if (manager->priv->active && !manager->priv->fading)
	{
		gtk_widget_show (GTK_WIDGET (window));
	}
}

static void
on_screen_monitors_changed (GdkScreen *screen,
                            GSManager *manager)
{
1616 1617 1618 1619 1620
	GSList     *l;
	GdkDisplay *display;
	int         n_monitors;
	int         n_windows;
	int         i;
Perberos's avatar
Perberos committed
1621

1622 1623 1624 1625
	display = gdk_screen_get_display (screen);
#if GTK_CHECK_VERSION (3, 22, 0)
	n_monitors = gdk_display_get_n_monitors (display);
#else
Perberos's avatar
Perberos committed
1626
	n_monitors = gdk_screen_get_n_monitors (screen);
1627
#endif
Perberos's avatar
Perberos committed
1628 1629
	n_windows = g_slist_length (manager->priv->windows);

1630 1631
	gs_debug ("Monitors changed for display %s: num=%d",
	          gdk_display_get_name (display),
Perberos's avatar
Perberos committed
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	          n_monitors);

	if (n_monitors > n_windows)
	{

		/* Tear down unlock dialog in case we want to move it
		 * to a new monitor
		 */
		l = manager->priv->windows;
		while (l != NULL)
		{
			gs_window_cancel_unlock_request (GS_WINDOW (l->data));
			l = l->next;
		}

		/* add more windows */
		for (i = n_windows; i < n_monitors; i++)
		{
1650 1651 1652 1653 1654 1655 1656
#if GTK_CHECK_VERSION (3, 22, 0)
			GdkMonitor *mon = gdk_display_get_monitor (display, i);
			gs_manager_create_window_for_monitor (manager, mon);
#else
			gs_manager_create_window_for_monitor (manager,
			                                      display, i);
#endif
Perberos's avatar
Perberos committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
		}

		/* And put unlock dialog up where ever it's supposed to be
		 */
		gs_manager_request_unlock (manager);
	}
	else
	{

		gdk_x11_grab_server ();

		/* remove the extra windows */
		l = manager->priv->windows;
		while (l != NULL)
		{
1672 1673 1674 1675 1676 1677 1678
			GdkDisplay *this_display;
#if GTK_CHECK_VERSION (3, 22, 0)
			GdkMonitor *this_monitor;
#else
			int         this_monitor;
#endif
			GSList     *next = l->next;
Perberos's avatar
Perberos committed
1679

1680
			this_display = gs_window_get_display (GS_WINDOW (l->data));
Perberos's avatar
Perberos committed
1681
			this_monitor = gs_window_get_monitor (GS_WINDOW (l->data));
1682 1683 1684 1685 1686 1687 1688
#if GTK_CHECK_VERSION (3, 22, 0)
			if (this_display == display &&
			    !GDK_IS_MONITOR (this_monitor))
#else
			if (this_display == display &&
			    this_monitor >= n_monitors)
#endif
Perberos's avatar
Perberos committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
			{
				manager_maybe_stop_job_for_window (manager, GS_WINDOW (l->data));
				g_hash_table_remove (manager->priv->jobs, l->data);
				gs_window_destroy (GS_WINDOW (l->data));
				manager->priv->windows = g_slist_delete_link (manager->priv->windows, l);
			}
			l = next;
		}

		/* make sure there is a lock dialog on a connected monitor,
		 * and that the keyboard is still properly grabbed after all
		 * the windows above got destroyed*/
		if (n_windows > n_monitors)
		{
			gs_manager_request_unlock (manager);
		}

		gdk_flush ();
		gdk_x11_ungrab_server ();
	}
}

static void
gs_manager_destroy_windows (GSManager *manager)
{
	GdkDisplay  *display;
	GSList      *l;

	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));

	if (manager->priv->windows == NULL)
	{
		return;
	}

	display = gdk_display_get_default ();

1727 1728 1729
	g_signal_handlers_disconnect_by_func (gdk_display_get_default_screen (display),
	                                      on_screen_monitors_changed,
	                                      manager);
Perberos's avatar
Perberos committed
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

	for (l = manager->priv->windows; l; l = l->next)
	{
		gs_window_destroy (l->data);
	}
	g_slist_free (manager->priv->windows);
	manager->priv->windows = NULL;
}

static void
gs_manager_finalize (GObject *object)
{
	GSManager *manager;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GS_IS_MANAGER (object));

	manager = GS_MANAGER (object);

	g_return_if_fail (manager->priv != NULL);

	if (manager->priv->bg != NULL)
	{
		g_object_unref (manager->priv->bg);
	}

	free_themes (manager);
	g_free (manager->priv->logout_command);
	g_free (manager->priv->keyboard_command);
	g_free (manager->priv->status_message);

	remove_unfade_idle (manager);
	remove_timers (manager);

Alexei Sorokin's avatar
Alexei Sorokin committed
1764
	gs_grab_release (manager->priv->grab, TRUE);
Perberos's avatar
Perberos committed
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

	manager_stop_jobs (manager);

	gs_manager_destroy_windows (manager);

	manager->priv->active = FALSE;
	manager->priv->activate_time = 0;
	manager->priv->lock_enabled = FALSE;

	g_object_unref (manager->priv->fade);
	g_object_unref (manager->priv->grab);
	g_object_unref (manager->priv->theme_manager);

	G_OBJECT_CLASS (gs_manager_parent_class)->finalize (object);
}

static void
1782 1783
gs_manager_create_windows_for_display (GSManager  *manager,
                                       GdkDisplay *display)
Perberos's avatar
Perberos committed
1784
{
1785 1786 1787 1788 1789
#if !GTK_CHECK_VERSION (3, 22, 0)
	GdkScreen *screen;
#endif
	int n_monitors;
	int i;
Perberos's avatar
Perberos committed
1790 1791 1792

	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));
1793
	g_return_if_fail (GDK_IS_DISPLAY (display));
Perberos's avatar
Perberos committed
1794 1795

	g_object_ref (manager);
1796
	g_object_ref (display);
Perberos's avatar
Perberos committed
1797

1798 1799 1800 1801
#if GTK_CHECK_VERSION (3, 22, 0)
	n_monitors = gdk_display_get_n_monitors (display);
#else
	screen = gdk_display_get_default_screen (display);
Perberos's avatar
Perberos committed
1802
	n_monitors = gdk_screen_get_n_monitors (screen);
1803
#endif
Perberos's avatar
Perberos committed
1804

1805 1806
	gs_debug ("Creating %d windows for display %s",
	          n_monitors, gdk_display_get_name (display));
Perberos's avatar
Perberos committed
1807 1808 1809

	for (i = 0; i < n_monitors; i++)
	{
1810 1811 1812 1813 1814 1815
#if GTK_CHECK_VERSION (3, 22, 0)
		GdkMonitor *mon = gdk_display_get_monitor (display, i);
		gs_manager_create_window_for_monitor (manager, mon);
#else
		gs_manager_create_window_for_monitor (manager, display, i);
#endif
Perberos's avatar
Perberos committed
1816 1817
	}

1818
	g_object_unref (display);
Perberos's avatar
Perberos committed
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	g_object_unref (manager);
}

static void
gs_manager_create_windows (GSManager *manager)
{
	GdkDisplay  *display;

	g_return_if_fail (manager != NULL);
	g_return_if_fail (GS_IS_MANAGER (manager));

	g_assert (manager->priv->windows == NULL);

	display = gdk_display_get_default ();
1833 1834 1835 1836 1837
	g_signal_connect (gdk_display_get_default_screen (display),
	                  "monitors-changed",
	                  G_CALLBACK (on_screen_monitors_changed),
	                  manager);

1838
	gs_manager_create_windows_for_display (manager, display);
Perberos's avatar
Perberos committed
1839 1840 1841