File: thread.c

package info (click to toggle)
irsim 9.7.104-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ansic: 24,763; sh: 7,499; makefile: 418; csh: 269; tcl: 88
file content (264 lines) | stat: -rw-r--r-- 5,463 bytes parent folder | download | duplicates (7)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* thread.c --
 *
 * This file contains functions to run the X11 event loop as a separate
 * thread.  Used by X11.  Compiled only if option "threads" is set in
 * "make config".
 */

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <signal.h>

#include <X11/keysym.h>
#include <pthread.h>

#include "ana.h"
#include "ana_glob.h"
#include "graphics.h"

extern Display *display;
extern Window  window;
extern Func FGetEvent;

pthread_t xloop_thread = 0;
Func EventHandlerPtr = NULL;

/*---------------------------------------------------------
 * Display locking and unlocking
 *
 * Locking the display flushes the X11.  Normally, this is
 * done by XNextEvent(), but with the threaded event watcher,
 * XNextEvent() is only run by one of the two threads.  So
 * X11 calls made by the other thread will not get updated
 * unless XFlush() is called when the thread releases control
 * of the thread lock.
 *---------------------------------------------------------
 */

public void
DisableInput()
{
    if (display)
	XLockDisplay(display);
}

public void
EnableInput()
{
    if (display)
    {
        XFlush(display);
	XUnlockDisplay(display);
    }
}

/*---------------------------------------------------------
 * xloop_begin:  Threaded version of XHelper7 (X11Helper.c)
 *
 *---------------------------------------------------------
 */

int
xloop_begin()
{
    XEvent xevent;
    int parentID;

    /* Set this thread to cancel immediately when requested */
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    while (1) {
	XNextEvent(display, &xevent);
	DisableInput();
        (*EventHandlerPtr)(&xevent);
	EnableInput();
    }
}  

/*----------------------------------------------------------------------*/
/* xloop_create function creates a thread for the X event loop.         */
/* Returns 0 on success of pthread_create, non-zero on failure.         */
/*----------------------------------------------------------------------*/

int
xloop_create()
{
    int status = 0;

    if (xloop_thread == (pthread_t)0)
        status = pthread_create(&xloop_thread, NULL, (void *)xloop_begin,
		NULL);

    return status;
}

/*----------------------------------------------------------------------*/
/* Kill the X event loop thread.					*/
/*----------------------------------------------------------------------*/

void
xloop_end()
{
    pthread_cancel(xloop_thread);
    pthread_join(xloop_thread, NULL);
}

/*------------------------------------------------------*/
/* EventHandler is modified from original in event.c	*/
/*------------------------------------------------------*/

/* Active event handler procedure */

private void EventHandler(event)
    XEvent *event;
{
    switch (event->type)
    {
	case ButtonPress:
	    if (windowState.tooSmall);
	    else if (FGetEvent != NULL)
		(*FGetEvent)(event);
	    else
		HandleButton(&event->xbutton);
	    break;

	case KeyPress:
	    if (windowState.tooSmall);
	    else if (FGetEvent != NULL)
		(*FGetEvent)(&event->xkey);
	    else
		HandleKey(&event->xkey);
	    break;

	case EnterNotify:
	    if (event->xcrossing.window == window)
		WindowCrossed(TRUE);
	    break;

	case LeaveNotify:
	    if (event->xcrossing.window == window)
		WindowCrossed(FALSE);
	    break;

	case Expose:
	    if (event->xexpose.window == iconW)
		RedrawIcon(&event->xexpose);
	    else if (windowState.tooSmall)
		RedrawSmallW();
	    else if (event->xexpose.window == window)
		WindowExposed(&event->xexpose);
	    break;

	case ConfigureNotify:
	    WindowResize(&event->xconfigure);
	    break;

	case UnmapNotify:
	    windowState.iconified = TRUE;
	    break;

	case MapNotify:
	    windowState.iconified = FALSE;
	    windowState.selected = FALSE;
	    (void) WindowChanges();
	    break;

	default:
	    break;
    }
}

/* Inactive event handler procedure */

private void DisabledEventHandler(event)
    XEvent *event;
{
    switch(event->type)
    {
	case EnterNotify:
	    if (event->xcrossing.window == window)
		WindowCrossed(TRUE);
	    break;

	case LeaveNotify:
	    if(event->xcrossing.window == window)
		WindowCrossed(FALSE);
	    break;

	case Expose:
	    if (event->xexpose.window == iconW)
		RedrawIcon(event);
	    else if (windowState.tooSmall)
		RedrawSmallW();
	    break;

	case ConfigureNotify:
	    WindowResize(&event->xconfigure);
	    break;

	case UnmapNotify:
	    windowState.iconified = TRUE;
	    break;

	case MapNotify:
	    windowState.iconified = FALSE;
	    windowState.selected = FALSE;
	    break;
    }
}

/* Initialize handler by installing the active event handler */

public int InitHandler(fd)
    int fd;
{
    int   flags;
    char  *senv;

    EventHandlerPtr = EventHandler;
    return (TRUE);
}

/* Install inactive event handler */

public void DisableAnalyzer()
{
    EventHandlerPtr = DisabledEventHandler;
}

/* Install active event handler */

public void EnableAnalyzer()
{
    int change;

    updatePending = FALSE;

    if (FGetEvent != NULL)
    {
	(*FGetEvent)((XEvent *)NULL);
	FGetEvent = NULL;
    }

    if (windowState.iconified == 0 && windowState.tooSmall == 0)
    {
	change = WindowChanges();
	if (!(change & RESIZED))
	{
	    BBox  box;

	    box.left = box.top = 0;
	    box.right = XWINDOWSIZE - 1;
	    box.bot = YWINDOWSIZE - 1;
	    RedrawWindow( box );
	}
    }
    EventHandlerPtr = EventHandler;
}

/* Do nothing; thread is configured to terminate automatically */

public void TerminateAnalyzer()
{
}