Cleanup some jack server interactions
[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
53         if (eq == NULL || for_disksave) {
54                 return;
55         }
56
57         SXE_SEMAPH_FINI(&(eq_queue_sem(eq)));
58         free_noseeum_dllist(eq_queue(eq));
59         return;
60 }
61
62 static Lisp_Object
63 mark_event_queue(Lisp_Object obj)
64 {
65         event_queue_t eq = XEVENT_QUEUE(obj);
66         WITH_DLLIST_TRAVERSE(
67                 eq_queue(eq),
68                 if (dllist_item)
69                         mark_object((Lisp_Object)dllist_item));
70         return Qnil;
71 }
72
73 event_queue_t
74 make_noseeum_event_queue(void)
75 {
76         event_queue_t res = xnew(struct event_queue_s);
77
78         SXE_SEMAPH_INIT(&(eq_queue_sem(res)));
79         eq_queue(res) = make_noseeum_dllist();
80
81         return res;
82 }
83
84 static inline event_queue_t
85 allocate_event_queue(void)
86 {
87         event_queue_t res =
88                 alloc_lcrecord_type(struct event_queue_s, &lrecord_event_queue);
89         return res;
90 }
91
92 event_queue_t
93 make_event_queue(void)
94 {
95         event_queue_t res = allocate_event_queue();
96
97         SXE_SEMAPH_INIT(&(eq_queue_sem(res)));
98         eq_queue(res) = make_noseeum_dllist();
99
100         return res;
101 }
102
103 void
104 free_event_queue(event_queue_t eq)
105 {
106         finalise_event_queue(eq, 0);
107         xfree(eq);
108         return;
109 }
110
111 DEFINE_LRECORD_IMPLEMENTATION("event-queue", event_queue,
112                               mark_event_queue, NULL,
113                               finalise_event_queue,
114                               NULL, NULL, 0, struct event_queue_s);
115 \f
116 /* priques */
117 static void
118 finalise_event_prique(void *obj, int for_disksave)
119 {
120         event_prique_t ep = obj;
121         if (ep == NULL)
122                 return;
123
124         SXE_SEMAPH_FINI(&(eq_prique_sem(ep)));
125         free_noseeum_dllist(eq_prique(ep));
126         return;
127 }
128
129 static Lisp_Object
130 mark_event_prique(Lisp_Object obj)
131 {
132         return Qnil;
133 }
134
135 event_prique_t
136 make_event_prique(void)
137 {
138         event_prique_t res = xnew(struct event_prique_s);
139
140         SXE_SEMAPH_INIT(&(eq_prique_sem(res)));
141         eq_prique(res) = NULL;
142
143         return res;
144 }
145
146 void
147 free_event_prique(event_prique_t eq)
148 {
149         finalise_event_prique(eq, 0);
150         xfree(eq);
151         return;
152 }
153
154 DEFINE_LRECORD_IMPLEMENTATION("event-prique", event_prique,
155                               mark_event_prique, NULL,
156                               finalise_event_prique,
157                               NULL, NULL, 0, struct event_prique_s);
158
159 /* stuff with private data aboard */
160 extern void
161 eq_enqueue_event_chain(event_queue_t eq, Lisp_Object ec)
162 {
163         while (!NILP(ec)) {
164                 eq_enqueue(eq, ec);
165                 ec = XEVENT_NEXT(ec);
166         }
167         return;
168 }
169
170
171 \f
172 /************************************************************************/
173 /*                            initialisation                            */
174 /************************************************************************/
175
176 void syms_of_event_queue(void)
177 {
178         INIT_LRECORD_IMPLEMENTATION(event_queue);
179         INIT_LRECORD_IMPLEMENTATION(event_prique);
180 }
181
182 /* event-queue.c ends here */