summaryrefslogtreecommitdiff
path: root/clients/zmailnotify/zmwnotify.c
blob: 6395fd9ff868bdff7a973935c1278f52ffb38fcd (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *	$Source$
 *	$Header$
 */

#ifndef lint
static char *rcsid_mwnotify_c = "$Header$";
#endif	lint

#include <sys/types.h>
#include <sys/uio.h>
#include <sys/syslog.h>
#include <sys/wait.h>
#include <ttyent.h>
#include <pwd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sgtty.h>
#include <signal.h>
#include <errno.h>
#include <netdb.h>
#include <X/Xlib.h>

extern char **environ;

short cursor[] = {0x0000, 0x7ffe, 0x4fc2, 0x4ffe, 0x7ffe,
		  0x7ffe, 0x781e, 0x7ffe , 0x7ffe, 0x0000};

#define NW_TOP 5
#define DEFAULT_WINDOWS 16
#define MAX_WINDOWS 32

WindowInfo Winfo;
FontInfo Finfo;
Font NoteFont;
int Timeout = 0;
int Bwidth = 2;
int Inner = 2;
int Volume = 0;
int Forepix = BlackPixel;
int Backpix = WhitePixel;
int Brdrpix = BlackPixel;
int Mouspix = BlackPixel;
int Offset = NW_TOP;
int VPos = NW_TOP;
int WindowMask = 0;
int WindowCount;
int WindowMax = DEFAULT_WINDOWS;

struct wsav {
    Window w_window;
    struct iovec *w_iov;
    int w_iovcnt;
    int w_flags;
} Wsav[MAX_WINDOWS];

#define W_MAPPED 1

extern int errno;

notify_user(iov, iovcnt)
register struct iovec *iov;
register int iovcnt;
{
    register int i;
    register struct wsav *w;

    if (WindowCount == WindowMax)
	return(1);

    w = &Wsav[WindowCount];
    w->w_flags = 0;
    w->w_iov = (struct iovec *)malloc(sizeof (struct iovec) * iovcnt);
    w->w_iovcnt = iovcnt;
    for (i = iovcnt; --i >= 0; ) {
	w->w_iov[i].iov_base = (char *)malloc(iov[i].iov_len+1);
	bcopy(iov[i].iov_base, w->w_iov[i].iov_base, iov[i].iov_len+1);
	w->w_iov[i].iov_len = iov[i].iov_len;
    }
    w->w_window = XNotifySetup(iov, iovcnt, &VPos);
    WindowMask |= (1 << WindowCount);
    WindowCount++;
    return(0);
}

XProcessEvent()
{
    XEvent rep;
    register struct wsav *w;
    register int i;

    do {
	XNextEvent(&rep);

	/* find the window */
	w = Wsav;
	for (i = 0; i < WindowMax; w++, i++)
	    if (rep.window == w->w_window) break;
	if (i == WindowMax) return;

	/* process the event */
	switch (rep.type) {
	case ButtonPressed:
	    for (; i >= 0; w--, i--) {
		if (w->w_window != NULL) {
		    XDestroyWindow(w->w_window);
		    w->w_window = NULL;
		    free_iov(w->w_iov, w->w_iovcnt);
		    WindowMask &= ~(1 << i);
		}
	    }
	    if (WindowMask == 0) {
		VPos = Offset;
		WindowCount = 0;
	    }
	    break;

	case ExposeWindow:
	case ExposeRegion:
	    XClear(w->w_window);
	    display_notice(w->w_window, w->w_iov, w->w_iovcnt);
	    XFlush();
	    break;

	}
    } while (XPending() > 0);
}

free_iov(iov, iovcnt)
register struct iovec *iov;
register int iovcnt;
{
    register struct iovec *iovbase = iov;

    while (--iovcnt >= 0) {
	free(iov->iov_base);
	iov++;
    }
    free(iovbase);
}
	

XNotifyInit(dname)
char *dname;
{
    struct passwd *pwent;
    char *envbuf[2];
    char homebuf[280];
    int reverse = 0;
    char *option;
    char *font_name = "6x13";
    char *fore_color = NULL;
    char *back_color = NULL;
    char *brdr_color = NULL;
    char *mous_color = NULL;
    Color cdef;
    char *getlogin();

    if (!XOpenDisplay(dname))
	exit(0);
    if (pwent = getpwnam(getlogin())) {
	strcpy(homebuf, "HOME=");
	strcat(homebuf, pwent->pw_dir);
	envbuf[0] = homebuf;
	envbuf[1] = NULL;
	environ = envbuf;
	if (option = XGetDefault("mailwatch", "BodyFont"))
	    font_name = option;
	fore_color = XGetDefault("mailwatch", "Foreground");
	back_color = XGetDefault("mailwatch", "Background");
	brdr_color = XGetDefault("mailwatch", "Border");
	mous_color = XGetDefault("mailwatch", "Mouse");
	if (option = XGetDefault("mailwatch", "BorderWidth"))
	    Bwidth = atoi(option);
	if (option = XGetDefault("mailwatch", "InternalBorder"))
	    Inner = atoi(option);
	if (option = XGetDefault("mailwatch", "Timeout"))
	    Timeout = atoi(option);
	if (option = XGetDefault("mailwatch", "Volume"))
	    Volume = atoi(option);
	if (option = XGetDefault("mailwatch", "Offset"))
	    Offset = atoi(option);
	if ((option = XGetDefault("mailwatch", "ReverseVideo")) &&
	    strcmp(option, "on") == 0)
	    reverse = 1;
	if (option = XGetDefault("mailwatch", "MaxNotices"))
	    WindowMax = atoi(option);
    }
    if (reverse) {
	Brdrpix = Backpix;
	Backpix = Forepix;
	Forepix = Brdrpix;
	Mouspix = Forepix;
    }

    if ((NoteFont = XGetFont(font_name)) == NULL)
	exit(0);
    if (DisplayCells() > 2) {
	if (back_color && XParseColor(back_color, &cdef) &&
	    XGetHardwareColor(&cdef))
	    Backpix = cdef.pixel;
	if (fore_color && XParseColor(fore_color, &cdef) &&
	    XGetHardwareColor(&cdef))
	    Forepix = cdef.pixel;
	if (brdr_color && XParseColor(brdr_color, &cdef) &&
	    XGetHardwareColor(&cdef))
	    Brdrpix = cdef.pixel;
	if (mous_color && XParseColor(mous_color, &cdef) &&
	    XGetHardwareColor(&cdef))
	    Mouspix = cdef.pixel;
    }
    XQueryFont(NoteFont, &Finfo);
    XQueryWindow (RootWindow, &Winfo);
    VPos = Offset;
    return(dpyno());
}

XNotifyClose()
{
    register int i;
    register struct wsav *w;

    for (i = WindowMax, w = Wsav; --i >= 0; w++)
	if (w->w_window != NULL) XDestroyWindow(w->w_window);

}

XDisplayNewWindows()
{
    register int i;
    register struct wsav *w;
    register int feepcount = 0;

    for (i = 0, w = Wsav; i < WindowMax; w++, i++)
	if (w->w_window != NULL && !(w->w_flags & W_MAPPED)) {
	    XMapWindow(w->w_window);
	    feepcount++;
	    w->w_flags |= W_MAPPED;
	}
    XFlush();
    /* Now feep for each window, s l o w l y */
    while (--feepcount >= 0) {
	XFeep(Volume);
	XFlush();
    }
}

XNotifySetup (iov, iovcnt, vpos)
struct iovec *iov;
int iovcnt;
register int *vpos;
{
    register int i;
    register int n;
    register int width;
    register int height;
    int vertical;
    register Window w;
    
    width = 0;
    for (i = iovcnt; --i >= 0; ) {
	n = XQueryWidth (iov[i].iov_base, NoteFont);
	if (n > width) width = n;
    }

    width += Inner * 2;
    height = iovcnt * Finfo.height + (Inner * 2);
    vertical = *vpos;
    *vpos += height + (Finfo.height / 2);

    w = XCreateWindow(RootWindow, (Winfo.width - width - (Bwidth * 2)) / 2,
		      vertical, width, height, Bwidth,
		      XMakeTile(Brdrpix), XMakeTile(Backpix));
    XStoreName(w, "mail-notice");
    XSelectInput(w, ButtonPressed|ButtonReleased|ExposeWindow|ExposeRegion);
    XDefineCursor(w, XCreateCursor(16, 10, cursor, NULL, 7, 5,
				   Mouspix, Backpix, GXcopy));
    return(w);
}

display_notice(w, iov, iovcnt)
Window w;
register struct iovec *iov;
register int iovcnt;
{
    register int in;
    register int y;
    register int height;

    in = Inner - 1;
    if (in <= 0) in = 1;

    height = Finfo.height;
    y = in;
    while (--iovcnt >= 0) {
	XText(w, in, y, iov->iov_base, iov->iov_len,
	      NoteFont, Forepix, Backpix);
	y += height;
	iov++;
    }
}