~ubuntu-branches/ubuntu/feisty/elinks/feisty-updates

« back to all changes in this revision

Viewing changes to src/osdep/os2/os2.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Gervai
  • Date: 2004-01-21 22:13:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040121221345-ju33hai1yhhqt6kn
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* OS/2 support fo ELinks. It has pretty different life than rest of ELinks. */
 
2
/* $Id: os2.c,v 1.16 2004/01/05 04:54:31 miciah Exp $ */
 
3
 
 
4
#ifdef HAVE_CONFIG_H
 
5
#include "config.h"
 
6
#endif
 
7
 
 
8
#include "osdep/system.h"
 
9
 
 
10
#ifdef OS2
 
11
 
 
12
#ifdef X2
 
13
/* from xf86sup - XFree86 OS/2 support driver */
 
14
#include <pty.h>
 
15
#endif
 
16
 
 
17
#include <errno.h>
 
18
#include <stdio.h>
 
19
#include <stdlib.h>
 
20
#include <unistd.h>
 
21
 
 
22
#include "elinks.h"
 
23
 
 
24
#include "osdep/os2/os2.h"
 
25
#include "osdep/osdep.h"
 
26
#include "terminal/terminal.h"
 
27
 
 
28
 
 
29
#define INCL_MOU
 
30
#define INCL_VIO
 
31
#define INCL_DOSPROCESS
 
32
#define INCL_DOSERRORS
 
33
#define INCL_WINCLIPBOARD
 
34
#define INCL_WINSWITCHLIST
 
35
#include <os2.h>
 
36
#include <io.h>
 
37
#include <process.h>
 
38
#include <sys/video.h>
 
39
#ifdef HAVE_SYS_FMUTEX_H
 
40
#include <sys/builtin.h>
 
41
#include <sys/fmutex.h>
 
42
#endif
 
43
 
 
44
 
 
45
#define A_DECL(type, var) type var##1, var##2, *var = _THUNK_PTR_STRUCT_OK(&var##1) ? &var##1 : &var##2
 
46
 
 
47
int
 
48
is_xterm(void)
 
49
{
 
50
        static int xt = -1;
 
51
 
 
52
        if (xt == -1) xt = !!getenv("WINDOWID");
 
53
 
 
54
        return xt;
 
55
}
 
56
 
 
57
int winch_pipe[2];
 
58
int winch_thread_running = 0;
 
59
 
 
60
#define WINCH_SLEEPTIME 500 /* time in ms for winch thread to sleep */
 
61
 
 
62
static void
 
63
winch_thread(void)
 
64
{
 
65
        /* A thread which regularly checks whether the size of
 
66
           window has changed. Then raise SIGWINCH or notifiy
 
67
           the thread responsible to handle this. */
 
68
        static int old_xsize, old_ysize;
 
69
        static int cur_xsize, cur_ysize;
 
70
 
 
71
        signal(SIGPIPE, SIG_IGN);
 
72
        if (get_terminal_size(0, &old_xsize, &old_ysize)) return;
 
73
        while (1) {
 
74
                if (get_terminal_size(0, &cur_xsize, &cur_ysize)) return;
 
75
                if ((old_xsize != cur_xsize) || (old_ysize != cur_ysize)) {
 
76
                        old_xsize = cur_xsize;
 
77
                        old_ysize = cur_ysize;
 
78
                        write(winch_pipe[1], "x", 1);
 
79
                        /* Resizing may take some time. So don't send a flood
 
80
                         * of requests?! */
 
81
                        _sleep2(2 * WINCH_SLEEPTIME);
 
82
                } else {
 
83
                        _sleep2(WINCH_SLEEPTIME);
 
84
                }
 
85
        }
 
86
}
 
87
 
 
88
static void
 
89
winch(void *s)
 
90
{
 
91
        unsigned char c;
 
92
 
 
93
        while (can_read(winch_pipe[0]) && safe_read(winch_pipe[0], &c, 1) == 1);
 
94
        ((void (*)())s)();
 
95
}
 
96
 
 
97
void
 
98
handle_terminal_resize(int fd, void (*fn)())
 
99
{
 
100
        if (!is_xterm()) return;
 
101
        if (!winch_thread_running) {
 
102
                if (c_pipe(winch_pipe) < 0) return;
 
103
                winch_thread_running = 1;
 
104
                _beginthread((void (*)(void *))winch_thread, NULL, 0x32000, NULL);
 
105
        }
 
106
        set_handlers(winch_pipe[0], winch, NULL, NULL, fn);
 
107
}
 
108
 
 
109
void
 
110
unhandle_terminal_resize(int fd)
 
111
{
 
112
        set_handlers(winch_pipe[0], NULL, NULL, NULL, NULL);
 
113
}
 
114
 
 
115
int
 
116
get_terminal_size(int fd, int *x, int *y)
 
117
{
 
118
        if (!x || !y) return -1;
 
119
        if (is_xterm()) {
 
120
#ifdef X2
 
121
                /* int fd; */
 
122
                int arc;
 
123
                struct winsize win;
 
124
 
 
125
                /* fd = STDIN_FILENO; */
 
126
                arc = ptioctl(1, TIOCGWINSZ, &win);
 
127
                if (arc) {
 
128
/*
 
129
                        DBG("%d", errno);
 
130
*/
 
131
                        *x = 80;
 
132
                        *y = 24;
 
133
                        return 0;
 
134
                }
 
135
                *y = win.ws_row;
 
136
                *x = win.ws_col;
 
137
/*
 
138
                DBG("%d %d", *x, *y);
 
139
*/
 
140
 
 
141
                return 0;
 
142
#else
 
143
                *x = 80;
 
144
                *y = 24;
 
145
                return 0;
 
146
#endif
 
147
        } else {
 
148
                int a[2] = {0, 0};
 
149
 
 
150
                _scrsize(a);
 
151
                *x = a[0];
 
152
                *y = a[1];
 
153
                if (!*x) {
 
154
                        *x = get_e("COLUMNS");
 
155
                        if (!*x) *x = 80;
 
156
                }
 
157
                if (!*y) {
 
158
                        *y = get_e("LINES");
 
159
                        if (!*y) *y = 24;
 
160
                }
 
161
        }
 
162
 
 
163
        return 0;
 
164
}
 
165
 
 
166
 
 
167
int
 
168
exe(unsigned char *path)
 
169
{
 
170
        int flags = P_SESSION;
 
171
        int pid;
 
172
        int ret = -1;
 
173
        unsigned char *shell;
 
174
 
 
175
        shell = GETSHELL;
 
176
        if (!shell) shell = DEFAULT_SHELL;
 
177
        if (is_xterm()) flags |= P_BACKGROUND;
 
178
 
 
179
        pid = spawnlp(flags, shell, shell, "/c", path, NULL);
 
180
        if (pid != -1)
 
181
                waitpid(pid, &ret, 0);
 
182
 
 
183
        return ret;
 
184
}
 
185
 
 
186
unsigned char *
 
187
get_clipboard_text(void)
 
188
{
 
189
        PTIB tib;
 
190
        PPIB pib;
 
191
        HAB hab;
 
192
        HMQ hmq;
 
193
        ULONG oldType;
 
194
        unsigned char *ret = 0;
 
195
 
 
196
        DosGetInfoBlocks(&tib, &pib);
 
197
 
 
198
        oldType = pib->pib_ultype;
 
199
 
 
200
        pib->pib_ultype = 3;
 
201
 
 
202
        hab = WinInitialize(0);
 
203
        if (hab != NULLHANDLE) {
 
204
                hmq = WinCreateMsgQueue(hab, 0);
 
205
                if (hmq != NULLHANDLE) {
 
206
                        if (WinOpenClipbrd(hab)) {
 
207
                                ULONG fmtInfo = 0;
 
208
 
 
209
                                if (WinQueryClipbrdFmtInfo(hab, CF_TEXT, &fmtInfo) != FALSE) {
 
210
                                        ULONG selClipText = WinQueryClipbrdData(hab, CF_TEXT);
 
211
 
 
212
                                        if (selClipText) {
 
213
                                                PCHAR pchClipText = (PCHAR) selClipText;
 
214
 
 
215
                                                ret = stracpy(pchClipText);
 
216
                                        }
 
217
                                }
 
218
                                WinCloseClipbrd(hab);
 
219
                        }
 
220
                        WinDestroyMsgQueue(hmq);
 
221
                }
 
222
                WinTerminate(hab);
 
223
        }
 
224
 
 
225
        pib->pib_ultype = oldType;
 
226
 
 
227
        return ret;
 
228
}
 
229
 
 
230
void
 
231
set_clipboard_text(unsigned char *data)
 
232
{
 
233
        PTIB tib;
 
234
        PPIB pib;
 
235
        HAB hab;
 
236
        HMQ hmq;
 
237
        ULONG oldType;
 
238
 
 
239
        DosGetInfoBlocks(&tib, &pib);
 
240
 
 
241
        oldType = pib->pib_ultype;
 
242
 
 
243
        pib->pib_ultype = 3;
 
244
 
 
245
        hab = WinInitialize(0);
 
246
        if (hab != NULLHANDLE) {
 
247
                hmq = WinCreateMsgQueue(hab, 0);
 
248
                if (hmq != NULLHANDLE) {
 
249
                        if (WinOpenClipbrd(hab)) {
 
250
                                PVOID pvShrObject = NULL;
 
251
                                if (DosAllocSharedMem(&pvShrObject, NULL, strlen(data) + 1, PAG_COMMIT | PAG_WRITE | OBJ_GIVEABLE) == NO_ERROR) {
 
252
                                        strcpy(pvShrObject, data);
 
253
                                        WinSetClipbrdData(hab, (ULONG)pvShrObject, CF_TEXT, CFI_POINTER);
 
254
                                }
 
255
                                WinCloseClipbrd(hab);
 
256
                        }
 
257
                        WinDestroyMsgQueue(hmq);
 
258
                }
 
259
                WinTerminate(hab);
 
260
        }
 
261
 
 
262
        pib->pib_ultype = oldType;
 
263
}
 
264
 
 
265
unsigned char *
 
266
get_window_title(void)
 
267
{
 
268
#ifndef OS2_DEBUG
 
269
        unsigned char *org_switch_title;
 
270
        unsigned char *org_win_title = NULL;
 
271
        static PTIB tib = NULL;
 
272
        static PPIB pib = NULL;
 
273
        ULONG oldType;
 
274
        HSWITCH hSw = NULLHANDLE;
 
275
        SWCNTRL swData;
 
276
        HAB hab;
 
277
        HMQ hmq;
 
278
 
 
279
        /* save current process title */
 
280
 
 
281
        if (!pib) DosGetInfoBlocks(&tib, &pib);
 
282
        oldType = pib->pib_ultype;
 
283
        memset(&swData, 0, sizeof swData);
 
284
        if (hSw == NULLHANDLE) hSw = WinQuerySwitchHandle(0, pib->pib_ulpid);
 
285
        if (hSw != NULLHANDLE && !WinQuerySwitchEntry(hSw, &swData)) {
 
286
                /*org_switch_title = mem_alloc(strlen(swData.szSwtitle)+1);
 
287
                strcpy(org_switch_title, swData.szSwtitle);*/
 
288
                /* Go to PM */
 
289
                pib->pib_ultype = 3;
 
290
                hab = WinInitialize(0);
 
291
                if (hab != NULLHANDLE) {
 
292
                        hmq = WinCreateMsgQueue(hab, 0);
 
293
                        if (hmq != NULLHANDLE) {
 
294
                                org_win_title = mem_alloc(MAXNAMEL + 1);
 
295
                                if (org_win_title)
 
296
                                        WinQueryWindowText(swData.hwnd,
 
297
                                                           MAXNAMEL + 1,
 
298
                                                           org_win_title);
 
299
                                        org_win_title[MAXNAMEL] = 0;
 
300
                                /* back From PM */
 
301
                                WinDestroyMsgQueue(hmq);
 
302
                        }
 
303
                        WinTerminate(hab);
 
304
                }
 
305
                pib->pib_ultype = oldType;
 
306
        }
 
307
        return org_win_title;
 
308
#else
 
309
        return NULL;
 
310
#endif
 
311
}
 
312
 
 
313
void
 
314
set_window_title(unsigned char *title)
 
315
{
 
316
#ifndef OS2_DEBUG
 
317
        static PTIB tib;
 
318
        static PPIB pib;
 
319
        ULONG oldType;
 
320
        static HSWITCH hSw;
 
321
        SWCNTRL swData;
 
322
        HAB hab;
 
323
        HMQ hmq;
 
324
        unsigned char new_title[MAXNAMEL];
 
325
 
 
326
        if (!title) return;
 
327
        if (!pib) DosGetInfoBlocks(&tib, &pib);
 
328
        oldType = pib->pib_ultype;
 
329
        memset(&swData, 0, sizeof swData);
 
330
        if (hSw == NULLHANDLE) hSw = WinQuerySwitchHandle(0, pib->pib_ulpid);
 
331
        if (hSw != NULLHANDLE && !WinQuerySwitchEntry(hSw, &swData)) {
 
332
                unsigned char *p;
 
333
 
 
334
                safe_strncpy(new_title, title, MAXNAMEL - 1);
 
335
                while ((p = strchr(new_title, 1))) *p = ' ';
 
336
                safe_strncpy(swData.szSwtitle, new_title, MAXNAMEL - 1);
 
337
                WinChangeSwitchEntry(hSw, &swData);
 
338
                /* Go to PM */
 
339
                pib->pib_ultype = 3;
 
340
                hab = WinInitialize(0);
 
341
                if (hab != NULLHANDLE) {
 
342
                        hmq = WinCreateMsgQueue(hab, 0);
 
343
                        if (hmq != NULLHANDLE) {
 
344
                                if (swData.hwnd)
 
345
                                        WinSetWindowText(swData.hwnd, new_title);
 
346
                                /* back From PM */
 
347
                                WinDestroyMsgQueue(hmq);
 
348
                        }
 
349
                        WinTerminate(hab);
 
350
                }
 
351
        }
 
352
        pib->pib_ultype = oldType;
 
353
#endif
 
354
}
 
355
 
 
356
#if 0
 
357
void
 
358
set_window_title(int init, const char *url)
 
359
{
 
360
        static char *org_switch_title;
 
361
        static char *org_win_title;
 
362
        static PTIB tib;
 
363
        static PPIB pib;
 
364
        static ULONG oldType;
 
365
        static HSWITCH hSw;
 
366
        static SWCNTRL swData;
 
367
        HAB hab;
 
368
        HMQ hmq;
 
369
        char new_title[MAXNAMEL];
 
370
 
 
371
        switch (init) {
 
372
        case 1:
 
373
                DosGetInfoBlocks(&tib, &pib);
 
374
                oldType = pib->pib_ultype;
 
375
                memset(&swData, 0, sizeof swData);
 
376
                hSw = WinQuerySwitchHandle(0, pib->pib_ulpid);
 
377
                if (hSw != NULLHANDLE && !WinQuerySwitchEntry(hSw, &swData)) {
 
378
                        org_switch_title = mem_alloc(strlen(swData.szSwtitle) + 1);
 
379
                        strcpy(org_switch_title, swData.szSwtitle);
 
380
                        pib->pib_ultype = 3;
 
381
                        hab = WinInitialize(0);
 
382
                        hmq = WinCreateMsgQueue(hab, 0);
 
383
                        if (hab != NULLHANDLE && hmq != NULLHANDLE) {
 
384
                                org_win_title = mem_alloc(MAXNAMEL + 1);
 
385
                                WinQueryWindowText(swData.hwnd, MAXNAMEL + 1, org_win_title);
 
386
                                WinDestroyMsgQueue(hmq);
 
387
                                WinTerminate(hab);
 
388
                        }
 
389
                        pib->pib_ultype = oldType;
 
390
                }
 
391
                break;
 
392
        case -1:
 
393
                pib->pib_ultype = 3;
 
394
                hab = WinInitialize(0);
 
395
                hmq = WinCreateMsgQueue(hab, 0);
 
396
                if (hSw != NULLHANDLE && hab != NULLHANDLE && hmq != NULLHANDLE) {
 
397
                        safe_strncpy(swData.szSwtitle, org_switch_title, MAXNAMEL);
 
398
                        WinChangeSwitchEntry(hSw, &swData);
 
399
 
 
400
                        if (swData.hwnd)
 
401
                                WinSetWindowText(swData.hwnd, org_win_title);
 
402
                        WinDestroyMsgQueue(hmq);
 
403
                        WinTerminate(hab);
 
404
                }
 
405
                pib->pib_ultype = oldType;
 
406
                mem_free(org_switch_title);
 
407
                mem_free(org_win_title);
 
408
                break;
 
409
        case 0:
 
410
                if (url && *url) {
 
411
                        safe_strncpy(new_title, url, MAXNAMEL - 10);
 
412
                        strcat(new_title, " - Links");
 
413
                        pib->pib_ultype = 3;
 
414
                        hab = WinInitialize(0);
 
415
                        hmq = WinCreateMsgQueue(hab, 0);
 
416
                        if (hSw != NULLHANDLE && hab != NULLHANDLE && hmq != NULLHANDLE) {
 
417
                                safe_strncpy(swData.szSwtitle, new_title, MAXNAMEL);
 
418
                                WinChangeSwitchEntry(hSw, &swData);
 
419
 
 
420
                                if (swData.hwnd)
 
421
                                        WinSetWindowText(swData.hwnd, new_title);
 
422
                                WinDestroyMsgQueue(hmq);
 
423
                                WinTerminate(hab);
 
424
                        }
 
425
                        pib->pib_ultype = oldType;
 
426
                }
 
427
                break;
 
428
        }
 
429
}
 
430
#endif
 
431
 
 
432
int
 
433
resize_window(int x, int y)
 
434
{
 
435
        A_DECL(VIOMODEINFO, vmi);
 
436
 
 
437
        resize_count++;
 
438
        if (is_xterm()) return -1;
 
439
        vmi->cb = sizeof(*vmi);
 
440
        if (VioGetMode(vmi, 0)) return -1;
 
441
        vmi->col = x;
 
442
        vmi->row = y;
 
443
        if (VioSetMode(vmi, 0)) return -1;
 
444
#if 0
 
445
        unsigned char cmdline[16];
 
446
        sprintf(cmdline, "mode ");
 
447
        snprint(cmdline + 5, 5, x);
 
448
        strcat(cmdline, ",");
 
449
        snprint(cmdline + strlen(cmdline), 5, y);
 
450
#endif
 
451
        return 0;
 
452
}
 
453
 
 
454
 
 
455
#if OS2_MOUSE
 
456
 
 
457
#ifdef HAVE_SYS_FMUTEX_H
 
458
_fmutex mouse_mutex;
 
459
int mouse_mutex_init = 0;
 
460
#endif
 
461
int mouse_h = -1;
 
462
 
 
463
struct os2_mouse_spec {
 
464
        int p[2];
 
465
        void (*fn)(void *, unsigned char *, int);
 
466
        void *data;
 
467
        unsigned char buffer[sizeof(struct term_event)];
 
468
        int bufptr;
 
469
        int terminate;
 
470
};
 
471
 
 
472
void
 
473
mouse_thread(void *p)
 
474
{
 
475
        int status;
 
476
        struct os2_mouse_spec *oms = p;
 
477
        A_DECL(HMOU, mh);
 
478
        A_DECL(MOUEVENTINFO, ms);
 
479
        A_DECL(USHORT, rd);
 
480
        A_DECL(USHORT, mask);
 
481
        struct term_event ev;
 
482
 
 
483
        signal(SIGPIPE, SIG_IGN);
 
484
        ev.ev = EV_MOUSE;
 
485
        if (MouOpen(NULL, mh)) goto ret;
 
486
        mouse_h = *mh;
 
487
        *mask = MOUSE_MOTION_WITH_BN1_DOWN | MOUSE_BN1_DOWN |
 
488
                MOUSE_MOTION_WITH_BN2_DOWN | MOUSE_BN2_DOWN |
 
489
                MOUSE_MOTION_WITH_BN3_DOWN | MOUSE_BN3_DOWN |
 
490
                MOUSE_MOTION;
 
491
        MouSetEventMask(mask, *mh);
 
492
        *rd = MOU_WAIT;
 
493
        status = -1;
 
494
 
 
495
        while (1) {
 
496
                int w, ww;
 
497
 
 
498
                if (MouReadEventQue(ms, rd, *mh)) break;
 
499
#ifdef HAVE_SYS_FMUTEX_H
 
500
                _fmutex_request(&mouse_mutex, _FMR_IGNINT);
 
501
#endif
 
502
                if (!oms->terminate) MouDrawPtr(*mh);
 
503
#ifdef HAVE_SYS_FMUTEX_H
 
504
                _fmutex_release(&mouse_mutex);
 
505
#endif
 
506
                ev.x = ms->col;
 
507
                ev.y = ms->row;
 
508
                /*DBG("status: %d %d %d", ms->col, ms->row, ms->fs);*/
 
509
                if (ms->fs & (MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN))
 
510
                        ev.b = status = B_DOWN | (ms->fs & MOUSE_BN1_DOWN ? B_LEFT : ms->fs & MOUSE_BN2_DOWN ? B_MIDDLE : B_RIGHT);
 
511
                else if (ms->fs & (MOUSE_MOTION_WITH_BN1_DOWN | MOUSE_MOTION_WITH_BN2_DOWN | MOUSE_MOTION_WITH_BN3_DOWN)) {
 
512
                        int b = ms->fs & MOUSE_MOTION_WITH_BN1_DOWN ? B_LEFT : ms->fs & MOUSE_MOTION_WITH_BN2_DOWN ? B_MIDDLE : B_RIGHT;
 
513
 
 
514
                        if (status == -1)
 
515
                                b |= B_DOWN;
 
516
                        else
 
517
                                b |= B_DRAG;
 
518
                        ev.b = status = b;
 
519
                } else {
 
520
                        if (status == -1) continue;
 
521
                        ev.b = (status & BM_BUTT) | B_UP;
 
522
                        status = -1;
 
523
                }
 
524
                if (hard_write(oms->p[1], (unsigned char *)&ev, sizeof(struct term_event)) != sizeof(struct term_event)) break;
 
525
        }
 
526
#ifdef HAVE_SYS_FMUTEX_H
 
527
        _fmutex_request(&mouse_mutex, _FMR_IGNINT);
 
528
#endif
 
529
        mouse_h = -1;
 
530
        MouClose(*mh);
 
531
#ifdef HAVE_SYS_FMUTEX_H
 
532
        _fmutex_release(&mouse_mutex);
 
533
#endif
 
534
 
 
535
ret:
 
536
        close(oms->p[1]);
 
537
        /*free(oms);*/
 
538
}
 
539
 
 
540
void
 
541
mouse_handle(struct os2_mouse_spec *oms)
 
542
{
 
543
        int r = safe_read(oms->p[0], oms->buffer + oms->bufptr,
 
544
                          sizeof(struct term_event) - oms->bufptr);
 
545
 
 
546
        if (r <= 0) {
 
547
                unhandle_mouse(oms);
 
548
                return;
 
549
        }
 
550
 
 
551
        oms->bufptr += r;
 
552
        if (oms->bufptr == sizeof(struct term_event)) {
 
553
                oms->bufptr = 0;
 
554
                oms->fn(oms->data, oms->buffer, sizeof(struct term_event));
 
555
        }
 
556
}
 
557
 
 
558
void *
 
559
handle_mouse(int cons, void (*fn)(void *, unsigned char *, int),
 
560
             void *data)
 
561
{
 
562
        struct os2_mouse_spec *oms;
 
563
 
 
564
        if (is_xterm()) return NULL;
 
565
#ifdef HAVE_SYS_FMUTEX_H
 
566
        if (!mouse_mutex_init) {
 
567
                if (_fmutex_create(&mouse_mutex, 0)) return NULL;
 
568
                mouse_mutex_init = 1;
 
569
        }
 
570
#endif
 
571
                /* This is never freed but it's allocated only once */
 
572
        oms = malloc(sizeof(struct os2_mouse_spec));
 
573
        if (!oms) return NULL;
 
574
        oms->fn = fn;
 
575
        oms->data = data;
 
576
        oms->bufptr = 0;
 
577
        oms->terminate = 0;
 
578
        if (c_pipe(oms->p)) {
 
579
                free(oms);
 
580
                return NULL;
 
581
        }
 
582
        _beginthread(mouse_thread, NULL, 0x10000, (void *)oms);
 
583
        set_handlers(oms->p[0], (void (*)(void *))mouse_handle, NULL, NULL, oms);
 
584
 
 
585
        return oms;
 
586
}
 
587
 
 
588
void
 
589
unhandle_mouse(void *om)
 
590
{
 
591
        struct os2_mouse_spec *oms = om;
 
592
 
 
593
        want_draw();
 
594
        oms->terminate = 1;
 
595
        set_handlers(oms->p[0], NULL, NULL, NULL, NULL);
 
596
        close(oms->p[0]);
 
597
        done_draw();
 
598
}
 
599
 
 
600
void
 
601
suspend_mouse(void *om)
 
602
{
 
603
}
 
604
 
 
605
void
 
606
resume_mouse(void *om)
 
607
{
 
608
}
 
609
 
 
610
void
 
611
want_draw(void)
 
612
{
 
613
        A_DECL(NOPTRRECT, pa);
 
614
#ifdef HAVE_SYS_FMUTEX_H
 
615
        if (mouse_mutex_init) _fmutex_request(&mouse_mutex, _FMR_IGNINT);
 
616
#endif
 
617
        if (mouse_h != -1) {
 
618
                static int x = -1, y = -1;
 
619
                static int c = -1;
 
620
 
 
621
                if (x == -1 || y == -1 || (c != resize_count)) {
 
622
                        get_terminal_size(1, &x, &y);
 
623
                        c = resize_count;
 
624
                }
 
625
 
 
626
                pa->row = 0;
 
627
                pa->col = 0;
 
628
                pa->cRow = y - 1;
 
629
                pa->cCol = x - 1;
 
630
                MouRemovePtr(pa, mouse_h);
 
631
        }
 
632
}
 
633
 
 
634
void
 
635
done_draw(void)
 
636
{
 
637
#ifdef HAVE_SYS_FMUTEX_H
 
638
        if (mouse_mutex_init) _fmutex_release(&mouse_mutex);
 
639
#endif
 
640
}
 
641
 
 
642
#endif /* OS2_MOUSE */
 
643
 
 
644
 
 
645
int
 
646
get_ctl_handle(void)
 
647
{
 
648
        return get_input_handle();
 
649
}
 
650
 
 
651
 
 
652
int
 
653
get_system_env(void)
 
654
{
 
655
        int env = get_common_env();
 
656
 
 
657
        /* !!! FIXME: telnet */
 
658
        if (!is_xterm()) env |= ENV_OS2VIO;
 
659
 
 
660
        return env;
 
661
}
 
662
 
 
663
 
 
664
void
 
665
open_in_new_vio(struct terminal *term, unsigned char *exe_name,
 
666
                unsigned char *param)
 
667
{
 
668
        exec_new_elinks(term, DEFAULT_OS2_WINDOW_CMD, exe_name, param);
 
669
}
 
670
 
 
671
void
 
672
open_in_new_fullscreen(struct terminal *term, unsigned char *exe_name,
 
673
                       unsigned char *param)
 
674
{
 
675
        exec_new_elinks(term, DEFAULT_OS2_FULLSCREEN_CMD, exe_name, param);
 
676
}
 
677
 
 
678
 
 
679
void
 
680
set_highpri(void)
 
681
{
 
682
        DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
 
683
}
 
684
 
 
685
 
 
686
int
 
687
can_open_os_shell(int environment)
 
688
{
 
689
        if (environment & ENV_XWIN) return 0;
 
690
        return 1;
 
691
}
 
692
 
 
693
 
 
694
#if defined(HAVE_BEGINTHREAD)
 
695
 
 
696
int
 
697
start_thread(void (*fn)(void *, int), void *ptr, int l)
 
698
{
 
699
        int p[2];
 
700
        struct tdata *t;
 
701
 
 
702
        if (c_pipe(p) < 0) return -1;
 
703
        if (set_nonblocking_fd(p[0]) < 0) return -1;
 
704
        if (set_nonblocking_fd(p[1]) < 0) return -1;
 
705
 
 
706
        t = malloc(sizeof(struct tdata) + l);
 
707
        if (!t) return -1;
 
708
        t->fn = fn;
 
709
        t->h = p[1];
 
710
        memcpy(t->data, ptr, l);
 
711
        if (_beginthread((void (*)(void *))bgt, NULL, 65536, t) == -1) {
 
712
                close(p[0]);
 
713
                close(p[1]);
 
714
                mem_free(t);
 
715
                return -1;
 
716
        }
 
717
 
 
718
        return p[0];
 
719
}
 
720
 
 
721
#if defined(HAVE_READ_KBD)
 
722
 
 
723
int tp = -1;
 
724
int ti = -1;
 
725
 
 
726
void
 
727
input_thread(void *p)
 
728
{
 
729
        unsigned char c[2];
 
730
        int h = (int) p;
 
731
 
 
732
        signal(SIGPIPE, SIG_IGN);
 
733
        while (1) {
 
734
#if 0
 
735
                c[0] = _read_kbd(0, 1, 1);
 
736
                if (c[0]) if (safe_write(h, c, 1) <= 0) break;
 
737
                else {
 
738
                        int w;
 
739
                        printf("1");fflush(stdout);
 
740
                        c[1] = _read_kbd(0, 1, 1);
 
741
                        printf("2");fflush(stdout);
 
742
                        w = safe_write(h, c, 2);
 
743
                        printf("3");fflush(stdout);
 
744
                        if (w <= 0) break;
 
745
                        if (w == 1) if (safe_write(h, c+1, 1) <= 0) break;
 
746
                        printf("4");fflush(stdout);
 
747
                }
 
748
#endif
 
749
                /* for the records:
 
750
                 * _read_kbd(0, 1, 1) will
 
751
                 * read a char, don't echo it, wait for one available and
 
752
                 * accept CTRL-C.
 
753
                 * Knowing that, I suggest we replace this call completly!
 
754
                 */
 
755
                *c = _read_kbd(0, 1, 1);
 
756
                write(h, c, 1);
 
757
        }
 
758
        close(h);
 
759
}
 
760
 
 
761
int
 
762
get_input_handle(void)
 
763
{
 
764
        int fd[2];
 
765
 
 
766
        if (ti != -1) return ti;
 
767
        if (is_xterm()) return 0;
 
768
        if (c_pipe(fd) < 0) return 0;
 
769
        ti = fd[0];
 
770
        tp = fd[1];
 
771
        _beginthread(input_thread, NULL, 0x10000, (void *)tp);
 
772
        return fd[0];
 
773
}
 
774
 
 
775
#endif
 
776
 
 
777
#endif
 
778
 
 
779
 
 
780
#ifdef USE_OPEN_PREALLOC
 
781
int
 
782
open_prealloc(unsigned char *name, int flags, int mode, int siz)
 
783
{
 
784
        /* This is good for OS/2, where this will prevent file fragmentation,
 
785
         * preallocating the desired file size upon open(). */
 
786
        return open(name, flags | O_SIZE, mode, (unsigned long) siz);
 
787
}
 
788
 
 
789
void
 
790
prealloc_truncate(int h, int siz)
 
791
{
 
792
        ftruncate(h, siz);
 
793
}
 
794
#endif
 
795
 
 
796
#endif