Mercurial > pidgin
annotate src/idle.c @ 11684:c1906dc6257c
[gaim-migrate @ 13970]
"This patch fixes the tab-color change prob (hopefully
this is the last one in the series). Bleeter also
thinks I have nailed it this time." -- Sadrul Habib Chowdhury
This also seems to fix a case of it I was having this morning.
committer: Tailor Script <tailor@pidgin.im>
| author | Luke Schierer <lschiere@pidgin.im> |
|---|---|
| date | Mon, 17 Oct 2005 16:42:10 +0000 |
| parents | 8004885fabbe |
| children | 186d2d293ef4 |
| rev | line source |
|---|---|
| 1026 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 8046 | 4 * Gaim is the legal property of its developers, whose names are too numerous |
| 5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 6 * source distribution. | |
| 1 | 7 * |
| 1026 | 8 * This program is free software; you can redistribute it and/or modify |
| 9 * it under the terms of the GNU General Public License as published by | |
| 10 * the Free Software Foundation; either version 2 of the License, or | |
| 11 * (at your option) any later version. | |
| 1 | 12 * |
| 1026 | 13 * This program is distributed in the hope that it will be useful, |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 * GNU General Public License for more details. | |
| 1 | 17 * |
| 1026 | 18 * You should have received a copy of the GNU General Public License |
| 19 * along with this program; if not, write to the Free Software | |
| 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 1 | 21 * |
| 22 */ | |
| 9791 | 23 #include "internal.h" |
| 1 | 24 |
| 1026 | 25 #ifdef USE_SCREENSAVER |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
26 # ifndef _WIN32 |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
27 # include <X11/Xlib.h> |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
28 # include <X11/Xutil.h> |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
29 # include <X11/extensions/scrnsaver.h> |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
30 # include <gdk/gdkx.h> |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
31 # else |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
32 # include "idletrack.h" |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
33 # endif |
| 1026 | 34 #endif /* USE_SCREENSAVER */ |
| 1 | 35 |
|
5615
6500a6c8d679
[gaim-migrate @ 6022]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
36 #include "connection.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
37 #include "debug.h" |
| 11677 | 38 #include "gtkstatusbox.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
39 #include "log.h" |
|
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
40 #include "prefs.h" |
| 11677 | 41 #include "savedstatuses.h" |
|
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
42 #include "signals.h" |
|
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5755
diff
changeset
|
43 |
| 11677 | 44 #define IDLEMARK 600 /* 10 minutes! */ |
| 1 | 45 |
| 10319 | 46 typedef enum |
| 47 { | |
| 48 GAIM_IDLE_NOT_AWAY = 0, | |
| 49 GAIM_IDLE_AUTO_AWAY, | |
| 50 GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY | |
| 51 | |
| 52 } GaimAutoAwayState; | |
| 53 | |
| 54 #ifdef USE_SCREENSAVER | |
| 55 /** | |
| 56 * Get the number of seconds the user has been idle. In Unix-world | |
| 57 * this is based on the X Windows usage. In MS Windows this is based | |
| 58 * on keyboard/mouse usage. | |
| 59 * | |
| 60 * In Debian bug #271639, jwz says: | |
| 61 * | |
| 62 * Gaim should simply ask xscreensaver how long the user has been idle: | |
| 63 * % xscreensaver-command -time | |
| 64 * XScreenSaver 4.18: screen blanked since Tue Sep 14 14:10:45 2004 | |
| 65 * | |
| 66 * Or you can monitor the _SCREENSAVER_STATUS property on root window #0. | |
| 67 * Element 0 is the status (0, BLANK, LOCK), element 1 is the time_t since | |
| 68 * the last state change, and subsequent elements are which hack is running | |
| 69 * on the various screens: | |
| 70 * % xprop -f _SCREENSAVER_STATUS 32ac -root _SCREENSAVER_STATUS | |
| 71 * _SCREENSAVER_STATUS(INTEGER) = BLANK, 1095196626, 10, 237 | |
| 72 * | |
| 73 * See watch() in xscreensaver/driver/xscreensaver-command.c. | |
| 74 * | |
| 75 * @return The number of seconds the user has been idle. | |
| 76 */ | |
| 77 static int | |
| 78 get_idle_time_from_system() | |
| 1 | 79 { |
| 10319 | 80 #ifndef _WIN32 |
| 81 static XScreenSaverInfo *mit_info = NULL; | |
| 82 int event_base, error_base; | |
| 83 if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base)) { | |
| 84 if (mit_info == NULL) { | |
| 85 mit_info = XScreenSaverAllocInfo(); | |
| 86 } | |
| 87 XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info); | |
| 88 return (mit_info->idle) / 1000; | |
| 89 } else | |
| 90 return 0; | |
| 91 #else | |
| 92 return (GetTickCount() - wgaim_get_lastactive()) / 1000; | |
| 93 #endif | |
| 94 } | |
| 95 #endif /* USE_SCREENSAVER */ | |
| 96 | |
| 97 /* | |
| 98 * This function should be called when you think your idle state | |
| 99 * may have changed. Maybe you're over the 10-minute mark and | |
| 100 * Gaim should start reporting idle time to the server. Maybe | |
| 101 * you've returned from being idle. Maybe your auto-away message | |
| 102 * should be set. | |
| 103 * | |
| 104 * There is no harm to calling this many many times, other than | |
| 105 * it will be kinda slow. This is called every 20 seconds by a | |
| 106 * timer set when an account logs in. It is also called when | |
| 107 * you send an IM, a chat, etc. | |
| 108 * | |
| 109 * This function has 3 sections. | |
| 110 * 1. Get your idle time. It will query XScreenSaver or Windows | |
| 111 * or get the Gaim idle time. Whatever. | |
| 112 * 2. Set or unset your auto-away message. | |
| 113 * 3. Report your current idle time to the IM server. | |
| 114 */ | |
| 11677 | 115 /* TODO: This needs to be namespaced or moved elsewhere. */ |
| 10319 | 116 gint |
| 117 check_idle(gpointer data) | |
| 118 { | |
| 119 GaimConnection *gc = data; | |
|
11287
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
120 gboolean report_idle; |
|
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
121 const char *idle_method; |
|
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
122 GaimAccount *account; |
| 1026 | 123 time_t t; |
|
4201
511c2b63caa4
[gaim-migrate @ 4432]
Christian Hammond <chipx86@chipx86.com>
parents:
3905
diff
changeset
|
124 int idle_time; |
| 1 | 125 |
|
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
126 account = gaim_connection_get_account(gc); |
|
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
127 |
|
6485
70d5122bc3ff
[gaim-migrate @ 6999]
Christian Hammond <chipx86@chipx86.com>
parents:
6371
diff
changeset
|
128 gaim_signal_emit(gaim_blist_get_handle(), "update-idle"); |
|
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1141
diff
changeset
|
129 |
| 1026 | 130 time(&t); |
| 1 | 131 |
|
11287
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
132 idle_method = gaim_prefs_get_string("/gaim/gtk/idle/method"); |
|
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
133 report_idle = gaim_prefs_get_bool("/gaim/gtk/idle/report"); |
| 1 | 134 |
| 1026 | 135 #ifdef USE_SCREENSAVER |
|
11287
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
136 if (idle_method != NULL && !strcmp(idle_method, "system")) |
| 10319 | 137 idle_time = get_idle_time_from_system(); |
| 138 else | |
| 1026 | 139 #endif /* USE_SCREENSAVER */ |
|
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
140 idle_time = t - gc->last_sent_time; |
| 1 | 141 |
| 10319 | 142 /* Should be become auto-away? */ |
|
5560
b7319c094153
[gaim-migrate @ 5961]
Christian Hammond <chipx86@chipx86.com>
parents:
5548
diff
changeset
|
143 if (gaim_prefs_get_bool("/core/away/away_when_idle") && |
| 5748 | 144 (idle_time > (60 * gaim_prefs_get_int("/core/away/mins_before_away"))) |
| 9949 | 145 && (!gc->is_auto_away)) |
| 146 { | |
| 147 GaimPresence *presence; | |
| 148 | |
| 149 presence = gaim_account_get_presence(account); | |
|
5560
b7319c094153
[gaim-migrate @ 5961]
Christian Hammond <chipx86@chipx86.com>
parents:
5548
diff
changeset
|
150 |
| 9949 | 151 if (gaim_presence_is_available(presence)) |
| 152 { | |
| 11654 | 153 const char *idleaway_name; |
| 11677 | 154 GaimSavedStatus *saved_status; |
| 6216 | 155 |
| 10319 | 156 gaim_debug_info("idle", "Making %s auto-away\n", |
| 157 gaim_account_get_username(account)); | |
| 158 | |
| 11654 | 159 /* TODO XXX STATUS AWAY CORE/UI */ |
| 160 /* Mark our accounts "away" using the idleaway status */ | |
| 161 idleaway_name = gaim_prefs_get_string("/core/status/idleaway"); | |
| 11677 | 162 saved_status = gaim_savedstatus_find(idleaway_name); |
| 163 gtk_gaim_status_box_activate_saved_status(saved_status); | |
| 6216 | 164 |
| 10319 | 165 gc->is_auto_away = GAIM_IDLE_AUTO_AWAY; |
| 166 } else { | |
| 167 gc->is_auto_away = GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY; | |
| 168 } | |
| 9944 | 169 |
| 10319 | 170 /* Should we return from being auto-away? */ |
| 5748 | 171 } else if (gc->is_auto_away && |
| 172 idle_time < 60 * gaim_prefs_get_int("/core/away/mins_before_away")) { | |
| 10319 | 173 if (gc->is_auto_away == GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY) { |
| 174 gc->is_auto_away = GAIM_IDLE_NOT_AWAY; | |
|
1815
f15d449b3167
[gaim-migrate @ 1825]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1575
diff
changeset
|
175 return TRUE; |
|
1446
38f712e75836
[gaim-migrate @ 1456]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1407
diff
changeset
|
176 } |
| 10319 | 177 gc->is_auto_away = GAIM_IDLE_NOT_AWAY; |
| 9944 | 178 |
| 10319 | 179 /* XXX STATUS AWAY CORE/UI */ |
| 180 /* Need to set this connection to available here */ | |
|
1109
c73736fa0b7c
[gaim-migrate @ 1119]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1062
diff
changeset
|
181 } |
|
c73736fa0b7c
[gaim-migrate @ 1119]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1062
diff
changeset
|
182 |
|
11287
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
183 /* Deal with reporting idleness to the server, if appropriate */ |
|
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
184 if (report_idle && idle_time >= IDLEMARK && !gc->is_idle) { |
| 10319 | 185 gaim_debug_info("idle", "Setting %s idle %d seconds\n", |
|
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
186 gaim_account_get_username(account), idle_time); |
| 1026 | 187 serv_set_idle(gc, idle_time); |
| 188 gc->is_idle = 1; | |
| 7475 | 189 /* LOG system_log(log_idle, gc, NULL, OPT_LOG_BUDDY_IDLE | OPT_LOG_MY_SIGNON); */ |
|
11287
fb6e85c55fb8
[gaim-migrate @ 13484]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10423
diff
changeset
|
190 } else if ((!report_idle || idle_time < IDLEMARK) && gc->is_idle) { |
| 10319 | 191 gaim_debug_info("idle", "Setting %s unidle\n", |
|
5563
9eb5b13fd412
[gaim-migrate @ 5965]
Christian Hammond <chipx86@chipx86.com>
parents:
5560
diff
changeset
|
192 gaim_account_get_username(account)); |
| 1026 | 193 serv_touch_idle(gc); |
| 7475 | 194 /* LOG system_log(log_unidle, gc, NULL, OPT_LOG_BUDDY_IDLE | OPT_LOG_MY_SIGNON); */ |
|
1029
740c6f933fe0
[gaim-migrate @ 1039]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1028
diff
changeset
|
195 } |
| 1 | 196 |
| 1026 | 197 return TRUE; |
| 1 | 198 } |
