summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/libx11.cc
blob: aec3c5322100ede00c9e716ff012f8de94c1c7b8 (plain) (blame)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 *  File:       guic-win.cc
 *  Created by: ennewalker on Sat Jan 5 01:33:53 2008 UTC
 *
 *  Modified for Crawl Reference by $Author: j-p-e-g $ on $Date: 2008-03-07 $
 */

#include "AppHdr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef USE_X11 //Sys dep
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/Xlocale.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include <X11/Xmd.h>
#endif

#include "cio.h"
#include "enum.h"
#include "externs.h"
#include "guic.h"
#include "libutil.h"

/*
 * Tile related stuff
 */
#ifdef USE_TILE

#include "tiles.h"

static Display *display=NULL;
static int screen;

static int x11_keypress(XKeyEvent *xev);
static void x11_check_exposure(XEvent *xev);

extern WinClass *win_main;

void GetNextEvent(int *etype, int *key, bool *shift, bool *ctrl,
                  int *x1, int *y1, int *x2, int *y2)
{
    XEvent xev;

    while (true)
    {
        XNextEvent(display, &xev);

        if (xev.type == KeyPress)
        {
            *etype = EV_KEYIN;
            *key = x11_keypress(&(xev.xkey));
            break;
        }
        else if (xev.type == Expose)
        {
            x11_check_exposure(&xev);
        }
        else if (xev.type == ConfigureNotify)
        {
            win_main->ox = xev.xconfigure.x;
            win_main->oy = xev.xconfigure.y;
            break;
        }
        else if (xev.type == ButtonPress)
        {
            *etype = EV_BUTTON;
            int button = xev.xbutton.button;
            *shift = (xev.xkey.state & ShiftMask)   ? true : false;
            *ctrl  = (xev.xkey.state & ControlMask) ? true : false;
            *x1 = xev.xbutton.x;
            *y1 = xev.xbutton.y;

            if (button == 3)
                button = 2;
            else if (button == 2)
                button = 3;

            *key = button;
            break;
        }
        else if (xev.type == MotionNotify || xev.type == EnterNotify)
        {
            *etype = EV_MOVE;
            *x1 = xev.xbutton.x;
            *y1 = xev.xbutton.y;
            break;
        }
        else if (xev.type == LeaveNotify)
        {
            *etype = EV_MOVE;
            *x1 = -100;
            *y1 = -100;
            break;
        }
        else if (xev.type == ButtonRelease)
        {
            *etype = EV_UNBUTTON;
            int button = xev.xbutton.button;
            if (button == 3)
                button = 2;
            else if (button == 2)
                button = 3;

            *x1 = xev.xbutton.x;
            *y1 = xev.xbutton.y;
            *key = button;
            break;
        }
    }
}

char *my_getenv(const char *envname, const char *def)
{
    const char *result = getenv(envname);
    if (!result)
        result = def;

    return (char *)result;
}

int my_getenv_int(const char *envname, int def)
{
    const char *rstr = getenv(envname);
    if (!rstr)
        return def;

    return atoi(rstr);
}

extern WinClass *win_main;
extern TextRegionClass *region_tip;
extern TextRegionClass *region_crt;
void update_tip_text(const char *tip)
{
    const unsigned int tip_size = 512;
    static char old_tip[tip_size];
    char new_tip[tip_size];

    if (strncmp(old_tip, tip, tip_size) == 0)
        return;

    const bool is_main_screen = (win_main->active_layer == 0);
    const unsigned int height = is_main_screen ? region_tip->my : 1;

    const unsigned int width  = is_main_screen ? region_tip->mx
                                               : region_crt->mx;

    strncpy(new_tip, tip, tip_size);
    strncpy(old_tip, new_tip, tip_size);

    if (is_main_screen)
        region_tip->clear();

    char *next_tip = new_tip;
    for (unsigned int i = 0; next_tip && i < height; i++)
    {
        char *this_tip = next_tip;

        // find end of line
        char *pc = strchr(next_tip, '\n');
        if (pc)
        {
            *pc = 0;
            next_tip = pc + 1;
        }
        else
            next_tip = 0;

        if (strlen(this_tip) > width)
        {
            // try to remove inscriptions
            char *ins = strchr(this_tip, '{');
            if (ins && ins[-1] == ' ')
                ins--;
            if (ins && (ins - this_tip <= (int)width))
            {
                *ins = 0;
            }
            else
            {
                // try to remove state, e.g. "(worn)"
                char *state = strchr(this_tip, '(');
                if (state && state[-1] == ' ')
                    state--;
                if (state && (state - this_tip <= (int)width))
                {
                    *state = 0;
                }
                else
                {
                    // if nothing else...
                    this_tip[width]   = 0;
                    this_tip[width-1] = '.';
                    this_tip[width-2] = '.';
                }
            }
        }

        if (is_main_screen)
        {
            region_tip->cgotoxy(1, 1 + i);
            region_tip->addstr(this_tip);
            region_tip->make_active();
        }
        else
        {
            ASSERT(i == 0);
            textattr(WHITE);
            cgotoxy(1, get_number_of_lines() + 1);
            cprintf("%s", this_tip);
            clear_to_end_of_line();
        }
    }
}

void TileDrawDungeonAux()
{
}

#endif  /* USE_TILE */

/* X11 */
void x11_check_exposure(XEvent *xev)
{
    int sx, sy, ex, ey;

    sx = xev->xexpose.x;
    ex = xev->xexpose.x + (xev->xexpose.width)  - 1;
    sy = xev->xexpose.y;
    ey = xev->xexpose.y + (xev->xexpose.height) - 1;

    if (xev->xany.window != win_main->win)
        return;

    win_main->redraw(sx,sy,ex,ey);
}

/* X11 */
// This routine is taken from Angband main-x11.c
int x11_keypress(XKeyEvent *xev)
{

#define IsSpecialKey(keysym) \
  ((unsigned)(keysym) >= 0xFF00)

    const unsigned int ck_table[9] =
    {
        CK_END,  CK_DOWN,   CK_PGDN,
        CK_LEFT, CK_INSERT, CK_RIGHT,
        CK_HOME, CK_UP,     CK_PGUP
    };

    int dir, base;

    int n;
    bool mc, ms, ma;
    unsigned int ks1;

    XKeyEvent *ev = (XKeyEvent*)(xev);
    KeySym ks;
    char buf[256];

    n = XLookupString(ev, buf, 125, &ks, NULL);
    buf[n] = '\0';

    if (IsModifierKey(ks)) return 0;

    /* Extract "modifier flags" */
    mc = (ev->state & ControlMask) ? true : false;
    ms = (ev->state & ShiftMask)   ? true : false;
    ma = (ev->state & Mod1Mask)    ? true : false;

    /* Normal keys */
    if (n &&  !IsSpecialKey(ks))
    {
        buf[n] = 0;

        // Hack Ctrl + [0-9] etc.
        if (mc && ((ks >= '0' && ks <= '9') || buf[0] >= ' '))
            return 1024 | ks;

        if (!ma)
            return buf[0];

        /* Alt + ? */
        return 2048|buf[0];
    }

    /* Hack -- convert into an unsigned int */
    ks1 = (uint)(ks);

    /* Handle a few standard keys (bypass modifiers) XXX XXX XXX */
    base = dir = 0;
    switch (ks1)
    {
        case XK_Escape:
            base = 0x1b;
            break;
        case XK_Return:
            base = '\r';
            break;
        case XK_Tab:
            base = '\t';
            break;
        case XK_Delete:
        case XK_BackSpace:
            base = '\010';
            break;

        // for menus
        case XK_Down:
           return CK_DOWN;
        case XK_Up:
           return CK_UP;
        case XK_Left:
           return CK_LEFT;
        case XK_Right:
           return CK_RIGHT;

        // Keypad
        case XK_KP_1:
        case XK_KP_End:
            dir = 1;
            break;

        case XK_KP_2:
        case XK_KP_Down:
            dir = 2;
            break;

        case XK_KP_3:
        case XK_KP_Page_Down:
            dir = 3;
            break;

        case XK_KP_6:
        case XK_KP_Right:
            dir = 6;
            break;

        case XK_KP_9:
        case XK_KP_Page_Up:
            dir = 9;
            break;

        case XK_KP_8:
        case XK_KP_Up:
            dir = 8;
            break;

        case XK_KP_7:
        case XK_KP_Home:
            dir = 7;
            break;

        case XK_KP_4:
        case XK_KP_Left:
            dir = 4;
            break;

        case XK_KP_5:
            dir = 5;
            break;
    }

    //Handle keypad first
    if (dir != 0)
    {
        int result = ck_table[dir-1];

        if (ms) result += CK_SHIFT_UP - CK_UP;
        if (mc) result += CK_CTRL_UP  - CK_UP;

        return result;
    }

    if (base != 0)
    {
       if (ms) base |= 1024;
       if (mc) base |= 2048;
       if (ma) base |= 4096;

       return base;
    }

    //Hack Special key
    if (ks1 >=
0xff00)
    {
        base = 512 + ks1 - 0xff00;

        if (ms) base |= 1024;
        if (mc) base |= 2048;
        if (ma) base |= 4096;

        return base;
    }

    return 0;
}

void libgui_init_sys()
{
    GuicInit(&display, &screen);
}

void libgui_shutdown_sys()
{
    GuicDeinit();
}

void update_screen()
{
    XFlush(display);
}

int kbhit()
{
    XEvent xev;

    if (XCheckMaskEvent(display, KeyPressMask | ButtonPressMask, &xev))
    {
        XPutBackEvent(display, &xev);
        return 1;
    }
    return 0;
}

void delay( unsigned long time )
{
    usleep( time * 1000 );
}

/* Convert value to string */
int itoa(int value, char *strptr, int radix)
{
    unsigned int bitmask = 32768;
    int ctr = 0;
    int startflag = 0;

    if (radix == 10)
    {
        sprintf(strptr, "%i", value);
    }
    else if (radix == 2)             /* int to "binary string" */
    {
        while (bitmask)
        {
            if (value & bitmask)
            {
                startflag = 1;
                sprintf(strptr + ctr, "1");
            }
            else if (startflag)
            {
                sprintf(strptr + ctr, "0");
            }

            bitmask = bitmask >> 1;
            if (startflag)
                ctr++;
        }

        if (!startflag)         /* Special case if value == 0 */
            sprintf((strptr + ctr++), "0");

        strptr[ctr] = (char) NULL;
    }
    return (0);                /* Me? Fail? Nah. */
}


// Convert string to lowercase.
char *strlwr(char *str)
{
    unsigned int i;

    for (i = 0; i < strlen(str); i++)
        str[i] = tolower(str[i]);

    return (str);
}

int stricmp( const char *str1, const char *str2 )
{
    return (strcmp(str1, str2));
}