File: gtkint.c

package info (click to toggle)
slashem 0.0.7E7F3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 24,312 kB
  • ctags: 23,514
  • sloc: ansic: 263,201; cpp: 7,180; yacc: 2,154; sh: 739; lex: 440; awk: 97; makefile: 61; sed: 11
file content (447 lines) | stat: -rw-r--r-- 10,335 bytes parent folder | download | duplicates (9)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
  $Id: gtkint.c,v 1.6.2.1 2004/11/03 09:32:01 j_ali Exp $
 */
/*
  GTK+ NetHack Copyright (c) Issei Numata 1999-2000
               Copyright (c) Slash'EM Development Team 2001-2003
  GTK+ NetHack may be freely redistributed.  See license for details. 
*/

/*
 * This module contains everything that is only needed for the internal
 * interface (ie., when linking the GTK interface with the game executable).
 */

#include <sys/types.h>
#include <signal.h>
#include "winGTK.h"
#include "nhxdr.h"
#include "proxycom.h"
#include "prxyclnt.h"
#include "wintype.h"
#include "func_tab.h"
#include "dlb.h"
#ifdef SHORT_FILENAMES
#include "patchlev.h"
#else
#include "patchlevel.h"
#endif

static void
hook()
{
}

#ifdef GTK_PROXY

static void FDECL(GTK_proxy_init_nhwindows, (int *, char **));
static void NDECL(GTK_proxy_askname);
static int NDECL(GTK_proxy_nhgetch);
static void FDECL(GTK_proxy_raw_print, (const char *));
static void FDECL(GTK_proxy_raw_print_bold, (const char *));

/* Only the GTK_proxy_... functions are expected to be called.
 * Calling any other of these before GTK_proxy_init_nhwindows()
 * returns is likely to result in undefined behaviour.
 */

struct window_procs GTK_procs = {
    "gtk",
    0,
    0L,
    GTK_proxy_init_nhwindows,
    hook, /* player_selection */
    GTK_proxy_askname,
    GTK_get_nh_event,
    GTK_exit_nhwindows,
    hook, /* suspend_nhwindows */
    hook, /* resume_nhwindows */
    GTK_create_nhwindow,
    hook, /* clear_nhwindow */
    GTK_display_nhwindow,
    GTK_destroy_nhwindow,
    GTK_curs,
    GTK_putstr,
#ifdef FILE_AREAS
    (void (*)(const char *, const char *, BOOLEAN_P))hook, /* display_file */
#else
    (void (*)(const char *, BOOLEAN_P))hook, /* display_file */
#endif
    GTK_start_menu,
    (void (*)(winid, int, const anything *, CHAR_P, CHAR_P, int, const char *,
      BOOLEAN_P))hook, /* add_menu */
    GTK_end_menu,
    (int (*)())hook, /* select_menu */
    genl_message_menu,
    GTK_update_inventory,
    GTK_mark_synch,
    GTK_wait_synch,
#ifdef CLIPPING
    GTK_cliparound,
#endif
#ifdef POSITIONBAR
    hook, /* update_positionbar */
#endif
    (void (*)(winid, XCHAR_P, XCHAR_P, int))hook, /* print_glyph */
    GTK_proxy_raw_print,
    GTK_proxy_raw_print_bold,
    GTK_proxy_nhgetch,
    GTK_nh_poskey,
    hook, /* nhbell */
    GTK_doprev_message,
    (char (*)(const char *, const char *, CHAR_P))hook, /* yn_function */
    hook, /* getlin */
    GTK_get_ext_cmd,
    GTK_number_pad,
    GTK_delay_output,
#ifdef CHANGE_COLOR
    hook, /* change_color */
#ifdef MAC
    hook, /* change_background */
    hook, /* set_font_name */
#endif
    hook, /* get_color_string */
#endif
    hook, /* start_screen,*/
    hook, /* end_screen,*/
    genl_outrip,
    hook, /* preference_update,*/
};

static void
GTK_proxy_askname(void)
{
    strcpy(plname, "games");
}

static int
GTK_proxy_nhgetch(void)
{
    int c;
    do {
	c = getchar();
    } while(c == '\r');
    return c;
}

static void
GTK_proxy_raw_print(const char *str)
{
    fputs(str, stderr); fputc('\n', stderr); (void) fflush(stderr);
}

static void
GTK_proxy_raw_print_bold(const char *str)
{
    fputs(str, stderr); fputc('\n', stderr); (void) fflush(stderr);
}

#ifndef WIN32
static int
GTK_client_read(void *handle, void *buf, unsigned int len)
{
    int nb;
    nb = read((int)handle, buf, len);
    return nb >= 0 ? nb : -1;
}

static int
GTK_client_write(void *handle, void *buf, unsigned int len)
{
    int nb;
    nb = write((int)handle, buf, len);
    return nb >= 0 ? nb : -1;
}
#endif

static void
GTK_proxy_init_nhwindows(int *argcp, char **argv)
{
    int retval;
#ifdef WIN32
    /* Win32 has no concept of fork, so we simply execute ourselves */
    char *s;
    iflags.window_inited = 1;
    proxy_svc_set_ext_procs(win_GTK_init, &GTK_ext_procs);
    s = g_find_program_in_path(argv[0]);
    retval = proxy_connect("file", s ? s : argv[0], argcp, argv);
    if (!retval)
	proxy_start_client_services();
    exit(1);
#else
    int to_game[2],from_game[2];
#ifdef UNIX
    uid_t uid;
    gid_t gid;
#endif
    if (pipe(to_game) || pipe(from_game))
	panic("%s: Can't create NhExt stream", argv[0]);
    iflags.window_inited = 1;
    if (fork()) {
	dup2(to_game[0],0);
	dup2(from_game[1],1);
	close(to_game[1]);
	close(from_game[0]);
	choose_windows("proxy");
	if (strncmp(windowprocs.name, "proxy/", 6))
	    terminate(EXIT_SUCCESS);
	init_nhwindows(argcp, argv);
    } else {
	close(to_game[0]);
	close(from_game[1]);
#ifdef UNIX
	/*
	 * Drop all privileges. For non-Linux systems we just assume that
	 * seteuid()/setuid() suffices. Add more cases as needed.
	 */
	gid = getgid();
	uid = getuid();
#ifdef LINUX
	setresgid(gid, gid, gid);
	setresuid(uid, uid, uid);
#else
	setegid(gid);
	setgid(gid);
	seteuid(uid);
	setuid(uid);
#endif
#endif
	proxy_svc_set_ext_procs(win_GTK_init, &GTK_ext_procs);
	retval = proxy_init_client_services(GTK_client_read,
	  (void *)from_game[0], GTK_client_write, (void *)to_game[1]);
	if (!retval)
	    proxy_start_client_services();
	exit(1);
    }
#endif	/* WIN32 */
}

#else	/* GTK_PROXY */

static void FDECL(GTK_int_init_nhwindows, (int *, char **));
static void NDECL(GTK_int_player_selection);
static void NDECL(GTK_int_askname);
static void FDECL(GTK_int_clear_nhwindow, (winid));
#ifdef FILE_AREAS
static void FDECL(GTK_int_display_file, (const char *, const char *,
		BOOLEAN_P));
#else
static void FDECL(GTK_int_display_file, (const char *, BOOLEAN_P));
#endif
static void FDECL(GTK_int_add_menu, (winid, int, const ANY_P *,
		CHAR_P, CHAR_P, int, const char *, BOOLEAN_P));
static int FDECL(GTK_int_select_menu, (winid, int, MENU_ITEM_P **));
static void FDECL(GTK_int_print_glyph, (winid, XCHAR_P, XCHAR_P, int));
static char FDECL(GTK_int_yn_function, (const char *, const char *, CHAR_P));
static void FDECL(GTK_int_outrip, (winid, int));
static void FDECL(GTK_int_getlin, (const char *, char *));
static void FDECL(GTK_int_preference_update, (const char *));

struct window_procs GTK_procs = {
    "gtk",
    /* If you want to change these you probably also want to change the
     * equivalent values in GTK_capv[].
     */
    WC_COLOR | WC_HILITE_PET |
#if 0
    WC_ASCII_MAP | WC_TILED_MAP | WC_SPLASH_SCREEN | WC_POPUP_DIALOG |
    WC_EIGHT_BIT_IN |
#endif
    WC_PERM_INVENT,
    0L,
    GTK_int_init_nhwindows,
    GTK_int_player_selection,
    GTK_int_askname,
    GTK_get_nh_event,
    GTK_exit_nhwindows,
    hook, /* suspend_nhwindows */
    hook, /* resume_nhwindows */
    GTK_create_nhwindow,
    GTK_int_clear_nhwindow,
    GTK_display_nhwindow,
    GTK_destroy_nhwindow,
    GTK_curs,
    GTK_putstr,
    GTK_int_display_file,
    GTK_start_menu,
    GTK_int_add_menu,
    GTK_end_menu,
    GTK_int_select_menu,
    genl_message_menu,
    GTK_update_inventory,
    GTK_mark_synch,
    GTK_wait_synch,
#ifdef CLIPPING
    GTK_cliparound,
#endif
#ifdef POSITIONBAR
    hook, /* update_positionbar */
#endif
    GTK_int_print_glyph,
    GTK_raw_print,
    GTK_raw_print_bold,
    GTK_nhgetch,
    GTK_nh_poskey,
    hook, /* nhbell */
    GTK_doprev_message,
    GTK_int_yn_function,
    GTK_int_getlin,
    GTK_get_ext_cmd,
    GTK_number_pad,
    GTK_delay_output,
#ifdef CHANGE_COLOR
    hook, /* change_color */
#ifdef MAC
    hook, /* change_background */
    hook, /* set_font_name */
#endif
    hook, /* get_color_string */
#endif
    hook, /* start_screen,*/
    hook, /* end_screen,*/
#ifdef GRAPHIC_TOMBSTONE
    GTK_int_outrip,
#else
    genl_outrip,
#endif
    GTK_int_preference_update,
};

static void
GTK_int_init_nhwindows(int *argcp, char **argv)
{
    char **capv;
    if (GTK_ext_init_nhwindows(argcp, argv, &capv))
	iflags.window_inited = 1;
}

static void
GTK_int_player_selection(void)
{
    /*
     * The GTK interface does not provide a quit option within
     * player selection.
     */
    (void)GTK_ext_player_selection(&flags.initrole, &flags.initrace,
      &flags.initgend, &flags.initalign);
}

static void
GTK_int_askname() {
    char *name;
    name = GTK_ext_askname();
    strcpy(plname, name);
    free(name);
}

static void
GTK_int_clear_nhwindow(window)
winid window;
{
    GTK_ext_clear_nhwindow(window, ROWNO, COLNO, 1);
}

#ifdef FILE_AREAS
static void
GTK_int_display_file(farea, fname, complain)
const char *farea, *fname;
BOOLEAN_P complain;
#else
static void
GTK_int_display_file(fname, complain)
const char *fname;
BOOLEAN_P complain;
#endif
{
    int fh;
#ifdef FILE_AREAS
    fh = dlbh_fopen(farea, fname, "r");
#else
    fh = dlbh_fopen(fname, "r");
#endif
    if (fh < 0) {
	if (complain)  pline("Can't open %s.", fname);
	return;
    }
    GTK_ext_display_file(fh);
    dlbh_fclose(fh);
}

static void
GTK_int_add_menu(window, glyph, identifier, ch, gch, attr, str, preselected)
winid window;
int glyph;
const anything *identifier;
CHAR_P ch, gch;
int attr;
const char *str;
BOOLEAN_P preselected;
{
    int mapping = mapid_map_identifier(window, identifier);
    GTK_ext_add_menu(window, glyph, mapping, ch, gch, attr, str, preselected);
}

int
GTK_int_select_menu(window, how, menu_list)
winid window;
int how;
menu_item **menu_list;
{
    int i, n, retval;
    struct proxy_mi *selected;
    retval = GTK_ext_select_menu(window, how, &selected);
    n = retval <= 0 ? 0 : retval;
    *menu_list = (menu_item *) alloc(n * sizeof(menu_item));
    for(i = 0; i < n; i++) {
	mapid_unmap_identifier(window, selected[i].item, &(*menu_list)[i].item);
	(*menu_list)[i].count = selected[i].count;
    }
    if (n)
	free(selected);
    return retval;
}

static void
GTK_int_print_glyph(window, x, y, glyph)
winid window;
xchar x, y;
int glyph;
{
    GTK_ext_print_glyph(window, x, y, glyph);
}

static char
GTK_int_yn_function(query, resp, def)
const char *query, *resp;
char def;
{
    char ret;
    int count;
    ret = GTK_ext_yn_function(query, resp ? resp : "", def, &count);
    if (ret == '#')
	yn_number = count;
    return ret;
}

static void
GTK_int_outrip(winid id, int how)
{
    if (!GTK_ext_outrip(id, get_killer_string(how)))
	genl_outrip(id, how);
}

static void
GTK_int_getlin(const char *query, char *ret)
{
    char *line;
    line = GTK_ext_getlin(query);
    strcpy(ret, line);
    free(line);
}

static void
GTK_int_preference_update(const char *optnam)
{
    GTK_ext_preference_update(optnam, get_option(optnam));
}
#endif	/* GTK_PROXY */