[gaim-migrate @ 5229]
[pidgin-git.git] / src / idle.c
blobf7209f1276732c44eb3f47bf75bfc39472852d24
1 /*
2 * gaim
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <time.h>
26 #include <stdio.h>
27 #include <unistd.h>
29 #ifdef USE_SCREENSAVER
30 #ifndef _WIN32
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/extensions/scrnsaver.h>
34 #include <gdk/gdkx.h>
35 #else
36 #include "idletrack.h"
37 #endif
38 #endif /* USE_SCREENSAVER */
40 #include "gaim.h"
41 #include "multi.h"
42 #include "prpl.h"
44 #define IDLEMARK 600 /* 10 minutes! */
46 gint check_idle(gpointer data)
48 struct gaim_connection *gc = data;
49 time_t t;
50 #ifdef USE_SCREENSAVER
51 #ifndef _WIN32
52 static XScreenSaverInfo *mit_info = NULL;
53 #endif
54 #endif
55 int idle_time;
57 plugin_event(event_blist_update);
59 time(&t);
62 #ifdef USE_SCREENSAVER
63 if (report_idle == IDLE_SCREENSAVER) {
64 #ifndef _WIN32
65 int event_base, error_base;
66 if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base)) {
67 if (mit_info == NULL) {
68 mit_info = XScreenSaverAllocInfo();
70 XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info);
71 idle_time = (mit_info->idle) / 1000;
72 } else
73 idle_time = 0;
74 #else
75 idle_time = (GetTickCount() - wgaim_get_lastactive()) / 1000;
76 #endif
77 } else
78 #endif /* USE_SCREENSAVER */
79 idle_time = t - gc->lastsent;
81 if ((away_options & OPT_AWAY_AUTO) && (idle_time > (60 * auto_away)) && (!gc->is_auto_away)) {
82 if (!gc->away) {
83 debug_printf("making %s away automatically\n", gc->username);
84 if (g_slist_length(connections) == 1)
85 do_away_message(NULL, default_away);
86 else if (default_away)
87 serv_set_away(gc, GAIM_AWAY_CUSTOM, default_away->message);
88 gc->is_auto_away = 1;
89 set_default_away(NULL, (gpointer)g_slist_index(away_messages, default_away));
90 } else
91 gc->is_auto_away = 2;
92 } else if (gc->is_auto_away && idle_time < 60 * auto_away) {
93 if (gc->is_auto_away == 2) {
94 gc->is_auto_away = 0;
95 return TRUE;
97 gc->is_auto_away = 0;
98 if (awaymessage == NULL) {
99 debug_printf("removing auto-away message for %s\n", gc->username);
100 serv_set_away(gc, GAIM_AWAY_CUSTOM, NULL);
101 } else {
102 if (g_slist_length(connections) == 1)
103 do_im_back(0, 0);
104 else {
105 debug_printf("replacing auto-away with global for %s\n", gc->username);
106 serv_set_away(gc, GAIM_AWAY_CUSTOM, awaymessage->message);
112 /* If we're not reporting idle times to the server, still use Gaim
113 usage for auto-away, but quit here so we don't report to the
114 server */
115 if (report_idle == IDLE_NONE) {
116 return TRUE;
119 if (idle_time >= IDLEMARK && !gc->is_idle) {
120 debug_printf("setting %s idle %d seconds\n", gc->username, idle_time);
121 serv_set_idle(gc, idle_time);
122 gc->is_idle = 1;
123 system_log(log_idle, gc, NULL, OPT_LOG_BUDDY_IDLE | OPT_LOG_MY_SIGNON);
124 } else if (idle_time < IDLEMARK && gc->is_idle) {
125 debug_printf("setting %s unidle\n", gc->username);
126 serv_touch_idle(gc);
127 system_log(log_unidle, gc, NULL, OPT_LOG_BUDDY_IDLE | OPT_LOG_MY_SIGNON);
130 return TRUE;