aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/alarm.c
blob: ae76a0ce738687f05c2b60234629392018d173d3 (plain) (blame)
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
/*
 * Alarm handling for the GNOME Calendar.
 *
 * (C) 1998 the Free Software Foundation
 *
 * Author: Miguel de Icaza (miguel@kernel.org)
 */
#include <config.h>
#include <time.h>
#include <gnome.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <cal-util/calobj.h>
#include "alarm.h"

/* The pipes used to notify about an alarm */
int alarm_pipes [2];

/* The list of pending alarms */
static GList *alarms;

static void *head_alarm;

typedef struct {
    time_t        activation_time;
    AlarmFunction fn;
    void          *closure;
    CalendarAlarm *alarm;
} AlarmRecord;

enum DebugAction {
    ALARM_ACTIVATED,
    ALARM_ADDED,
    ALARM_NOT_ADDED
};

void debug_alarm (AlarmRecord* ar, enum DebugAction action);
void calendar_notify (time_t time, CalendarAlarm *which, void *data);
extern int debug_alarms;

/*
 * SIGALRM handler.  Notifies the callback about the alarm
 */
static void
alarm_activate ()
{
    char c = 0;

    write (alarm_pipes [1], &c, 1);
}

/*
 * SIGUSR1 handler.  Toggles debugging output
 */
static void
toggle_debugging ()
{
    debug_alarms = !debug_alarms;
}

static void
alarm_ready (void *closure, int fd, GdkInputCondition cond)
{
    AlarmRecord *ar = head_alarm;
    time_t now = time (NULL);
    char c;

    if (read (alarm_pipes [0], &c, 1) != 1)
        return;

    if (ar == NULL){
        g_warning ("Empty events.  This should not happen\n");
        return;
    }

    while (head_alarm){
        if (debug_alarms)
            debug_alarm (ar, ALARM_ACTIVATED);
        (*ar->fn)(ar->activation_time, ar->alarm, ar->closure);
        alarms = g_list_remove (alarms, head_alarm);

        /* Schedule next alarm */
        if (alarms){
            AlarmRecord *next;
            
            head_alarm = alarms->data;
            next = head_alarm;

            if (next->activation_time > now){
                struct itimerval itimer;
                
                itimer.it_interval.tv_sec = 0;
                itimer.it_interval.tv_usec = 0;
                itimer.it_value.tv_sec = next->activation_time - now;
                itimer.it_value.tv_usec = 0;
                setitimer (ITIMER_REAL, &itimer, NULL);
                break;
            } else {
                g_free (ar);
                ar = next;
            }
        } else
            head_alarm = NULL;
    }
    g_free (ar);
}

static int
alarm_compare_by_time (gconstpointer a, gconstpointer b)
{
    const AlarmRecord *ara = a;
    const AlarmRecord *arb = b;
    time_t diff;
    
    diff = ara->activation_time - arb->activation_time;
    return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
}

/**
 * alarm_add:
 *
 * Tries to schedule @alarm.
 *
 * Returns TRUE if the alarm was scheduled.
 */
gboolean
alarm_add (CalendarAlarm *alarm, AlarmFunction fn, void *closure)
{
    time_t now = time (NULL);
    AlarmRecord *ar;
    time_t alarm_time = alarm->trigger;

    ar = g_new0 (AlarmRecord, 1);
    ar->activation_time = alarm_time;
    ar->fn = fn;
    ar->closure = closure;
    ar->alarm = alarm;

    /* If it already expired, do not add it */
    if (alarm_time < now) {
        if (debug_alarms)
            debug_alarm (ar, ALARM_NOT_ADDED);
        return FALSE;
    }

    alarms = g_list_insert_sorted (alarms, ar, alarm_compare_by_time);

    /* If first alarm is not the previous first alarm, reschedule SIGALRM */
    if (head_alarm != alarms->data){
        struct itimerval itimer;
        int v;
        
        /* Set the timer to disable upon activation */
        itimer.it_interval.tv_sec = 0;
        itimer.it_interval.tv_usec = 0;
        itimer.it_value.tv_sec = alarm_time - now;
        itimer.it_value.tv_usec = 0;
        v = setitimer (ITIMER_REAL, &itimer, NULL);
        head_alarm = alarms->data;
    }
    if (debug_alarms)
        debug_alarm (ar, ALARM_ADDED);
    return TRUE;
}

int 
alarm_kill (void *closure_key)
{
    GList *p;

    for (p = alarms; p; p = p->next){
        AlarmRecord *ar = p->data;
        
        if (ar->closure == closure_key){
            alarms = g_list_remove (alarms, p->data);
            if (alarms)
                head_alarm = alarms->data;
            else
                head_alarm = NULL;
            return 1;
        }
    }
    return 0;
}

void
alarm_init (void)
{
    struct sigaction sa;
    struct sigaction debug_sa;
    int flags = 0;
    
    pipe (alarm_pipes);
    
    /* set non blocking mode */
    fcntl (alarm_pipes [0], F_GETFL, &flags);
    fcntl (alarm_pipes [0], F_SETFL, flags | O_NONBLOCK);
    gdk_input_add (alarm_pipes [0], GDK_INPUT_READ, alarm_ready, 0);

    /* Setup the signal handler */
    sa.sa_handler = alarm_activate;
    sigemptyset (&sa.sa_mask);
    sa.sa_flags   = SA_RESTART;
    sigaction (SIGALRM, &sa, NULL);
    
    /* Setup a signal handler to toggle debugging */
    debug_sa.sa_handler = toggle_debugging;
    sigemptyset (&debug_sa.sa_mask);
    debug_sa.sa_flags = SA_RESTART;
    sigaction (SIGUSR1, &debug_sa, NULL);
}

void 
debug_alarm (AlarmRecord* ar, enum DebugAction action)
{
    time_t now = time (NULL);
    iCalObject *ico = ar->closure;
    printf ("%s", ctime(&now));
    switch (action) {
    case ALARM_ADDED:
        printf ("Added alarm for %s", ctime(&ar->activation_time));
        break;
    case ALARM_NOT_ADDED:
        printf ("Alarm not added for %s", ctime(&ar->activation_time));
        break;
    case ALARM_ACTIVATED:
        printf ("Activated alarm\n");
        break;
    }

    if (ar->fn!=&calendar_notify) return;
    printf ("--- Summary: %s\n", ico->summary);
    switch (ar->alarm->type) {
    case ALARM_MAIL:
        printf ("--- Type: Mail\n");
        break;
    case ALARM_PROGRAM:
        printf ("--- Type: Program\n");
        break;
    case ALARM_DISPLAY:
        printf ("--- Type: Display\n");
        break;
    case ALARM_AUDIO:
        printf ("--- Type: Audio\n");
        break;
    }
}