Initial git import
[sxemacs] / src / events / event-queue.c
1 /*
2   event-queue.c -- New Generation Event Queue
3   Copyright (C) 2006, 2007, 2008 Sebastian Freundt
4
5   Author:  Sebastian Freundt <hroptatyr@sxemacs.org>
6
7   * This file is part of SXEmacs.
8   * 
9   * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in the
18   *    documentation and/or other materials provided with the distribution.
19   *
20   * 3. Neither the name of the author nor the names of any contributors
21   *    may be used to endorse or promote products derived from this
22   *    software without specific prior written permission.
23   *
24   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
25   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27   * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34   * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35   */
36
37 /* Inspired by XEmacs' events.c written by Jamie Zawinski */
38
39 /* Synched up with: Not in FSF. */
40
41 #include <config.h>
42 #include "lisp.h"
43 #include "event-queue.h"
44 #define INCLUDE_EVENTS_H_PRIVATE_SPHERE
45 #include "events.h"
46
47 \f
48 static void
49 finalise_event_queue(void *obj, int for_disksave)
50 {
51         event_queue_t eq = obj;
52         if (eq == NULL)
53                 return;
54
55         SXE_SEMAPH_FINI(&(eq_queue_sem(eq)));
56         free_noseeum_dllist(eq_queue(eq));
57         return;
58 }
59
60 static Lisp_Object
61 mark_event_queue(Lisp_Object obj)
62 {
63         event_queue_t eq = XEVENT_QUEUE(obj);
64         WITH_DLLIST_TRAVERSE(
65                 eq_queue(eq),
66                 if (dllist_item)
67                         mark_object((Lisp_Object)dllist_item));
68         return Qnil;
69 }
70
71 event_queue_t
72 make_noseeum_event_queue(void)
73 {
74         event_queue_t res = xnew(struct event_queue_s);
75
76         SXE_SEMAPH_INIT(&(eq_queue_sem(res)));
77         eq_queue(res) = make_noseeum_dllist();
78
79         return res;
80 }
81
82 static inline event_queue_t
83 allocate_event_queue(void)
84 {
85         event_queue_t res =
86                 alloc_lcrecord_type(struct event_queue_s, &lrecord_event_queue);
87         return res;
88 }
89
90 event_queue_t
91 make_event_queue(void)
92 {
93         event_queue_t res = allocate_event_queue();
94
95         SXE_SEMAPH_INIT(&(eq_queue_sem(res)));
96         eq_queue(res) = make_noseeum_dllist();
97
98         return res;
99 }
100
101 void
102 free_event_queue(event_queue_t eq)
103 {
104         finalise_event_queue(eq, 0);
105         xfree(eq);
106         return;
107 }
108
109 DEFINE_LRECORD_IMPLEMENTATION("event-queue", event_queue,
110                               mark_event_queue, NULL,
111                               finalise_event_queue,
112                               NULL, NULL, 0, struct event_queue_s);
113 \f
114 /* priques */
115 static void
116 finalise_event_prique(void *obj, int for_disksave)
117 {
118         event_prique_t ep = obj;
119         if (ep == NULL)
120                 return;
121
122         SXE_SEMAPH_FINI(&(eq_prique_sem(ep)));
123         free_noseeum_dllist(eq_prique(ep));
124         return;
125 }
126
127 static Lisp_Object
128 mark_event_prique(Lisp_Object obj)
129 {
130         return Qnil;
131 }
132
133 event_prique_t
134 make_event_prique(void)
135 {
136         event_prique_t res = xnew(struct event_prique_s);
137
138         SXE_SEMAPH_INIT(&(eq_prique_sem(res)));
139         eq_prique(res) = NULL;
140
141         return res;
142 }
143
144 void
145 free_event_prique(event_prique_t eq)
146 {
147         finalise_event_prique(eq, 0);
148         xfree(eq);
149         return;
150 }
151
152 DEFINE_LRECORD_IMPLEMENTATION("event-prique", event_prique,
153                               mark_event_prique, NULL,
154                               finalise_event_prique,
155                               NULL, NULL, 0, struct event_prique_s);
156
157 /* stuff with private data aboard */
158 extern void
159 eq_enqueue_event_chain(event_queue_t eq, Lisp_Object ec)
160 {
161         while (!NILP(ec)) {
162                 eq_enqueue(eq, ec);
163                 ec = XEVENT_NEXT(ec);
164         }
165         return;
166 }
167
168
169 \f
170 /************************************************************************/
171 /*                            initialisation                            */
172 /************************************************************************/
173
174 void syms_of_event_queue(void)
175 {
176         INIT_LRECORD_IMPLEMENTATION(event_queue);
177         INIT_LRECORD_IMPLEMENTATION(event_prique);
178 }
179
180 /* event-queue.c ends here */