~ubuntu-branches/ubuntu/gutsy/splitvt/gutsy

« back to all changes in this revision

Viewing changes to vtmouse.c

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2001-10-05 20:09:25 UTC
  • Revision ID: james.westby@ubuntu.com-20011005200925-1crvu3veaaofavdb
Tags: upstream-1.6.5
ImportĀ upstreamĀ versionĀ 1.6.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* This is a program to show how mouse events can be handled by programs
 
3
   running under an xterm window.
 
4
 
 
5
                -Sam Lantinga           5/11/94
 
6
*/
 
7
 
 
8
#define SPLITVT_SOURCE
 
9
 
 
10
#include <stdio.h>
 
11
#include "vtmouse.h"
 
12
 
 
13
extern FILE *safe_popen();              /* From misc.c */
 
14
 
 
15
#ifdef MAIN
 
16
void event_loop()
 
17
{
 
18
        int c, event_flag;
 
19
        struct event X_event;
 
20
 
 
21
        while ( (c=event_getc(&X_event)) != EOF ) {
 
22
                if ( X_event.happening ) {
 
23
                        printf("\r\n************\r\n");
 
24
                        printf("Mouse Event: 0x%x\r\n", X_event.button_state);
 
25
                        printf("\tX coordinate: %d\r\n", X_event.x);
 
26
                        printf("\tY coordinate: %d\r\n", X_event.y);
 
27
                        /* One example of getting the mouse state */
 
28
                        printf("\tX Event: ");
 
29
                        switch(X_event.button_state&BUTTON_MASK) {
 
30
                                case RELEASE:   printf("Button was released!\r\n");
 
31
                                                break;
 
32
                                default:        printf("Button %d was pressed!\r\n", 
 
33
                                                        (X_event.button_state&BUTTON_MASK)+1);
 
34
                                                break;
 
35
                        }
 
36
                        /* Another example of getting the mouse state */
 
37
                        printf("\tMouse %s buttons <%c|%c|%c>\r\n",
 
38
                                (BUTTON_ISSET(X_event, RELEASE) ? "released" : "pressed"),
 
39
                                (BUTTON_ISSET(X_event, BUTTON_1) ? 'X' : ' '),
 
40
                                (BUTTON_ISSET(X_event, BUTTON_2) ? 'X' : ' '),
 
41
                                (BUTTON_ISSET(X_event, BUTTON_3) ? 'X' : ' '));
 
42
#ifdef REPORT_SELECTION
 
43
                        if ( (X_event.button_state&SELECTED) == SELECTED )
 
44
                                printf("Selection: Start x=%d, y=%d; End x=%d, y=%d\r\n",
 
45
                                                X_event.selection.begin_col,
 
46
                                                X_event.selection.begin_row,
 
47
                                                X_event.selection.end_col,
 
48
                                                X_event.selection.end_row);
 
49
#endif /* REPORT_SELECTION */
 
50
                        printf("************\r\n");
 
51
                } else {
 
52
                        if ( c == '\r' ) {
 
53
                                putc('\r', stdout);
 
54
                                putc('\n', stdout);
 
55
                        } else if ( c < ' ' ) {
 
56
                                putc('^', stdout);
 
57
                                putc(c+'@', stdout);
 
58
                        } else if ( c == 'Q' )
 
59
                                return;
 
60
                        else
 
61
                                putc(c, stdout);
 
62
                }
 
63
        }
 
64
}
 
65
 
 
66
int main(int argc, char *argv[])
 
67
{
 
68
        if ( event_init(stdin, stdout, NULL) < 0 ) {
 
69
                printf("Not running under an xterm.\n");
 
70
                exit(2);
 
71
        }
 
72
        system("stty -echo raw");
 
73
 
 
74
        event_loop();
 
75
 
 
76
        system("stty -raw echo");
 
77
        event_quit();
 
78
 
 
79
        exit(0);
 
80
}
 
81
#endif
 
82
 
 
83
/* I/O streams default to stdin and stdout. */
 
84
static FILE *xt_input, *xt_output;
 
85
static int have_xterm=0;
 
86
static int set_title=0;
 
87
static char *old_title=NULL;
 
88
int terminal_input=0;           /* Is there pending terminal input? */
 
89
 
 
90
static char *get_xtitle()
 
91
{
 
92
        char buffer[512], *title;
 
93
        FILE *pipe;
 
94
 
 
95
        strcpy(buffer, "xterm");
 
96
        if ( (title=(char *)getenv("WINDOWID")) ) {
 
97
                if ( (strlen("xprop -id ")+strlen(title)+1) > 512 )
 
98
                        goto NoTitle;
 
99
                sprintf(buffer, "xprop -id %s", title);
 
100
                if ( (pipe=safe_popen(buffer, "r")) ) {
 
101
                        while ( fgets(buffer, 511, pipe) );
 
102
                        /* Last line should be 'WM_NAME(STRING) = "title"' */
 
103
                        if (strlen(buffer) > strlen("WM_NAME(STRING) = \"")) {
 
104
                                buffer[strlen(buffer)-2]='\0';
 
105
                                title=(buffer+strlen("WM_NAME(STRING) = \""));
 
106
                                strcpy(buffer, title);
 
107
                        } else
 
108
                                strcpy(buffer, "xterm");
 
109
                        safe_pclose(pipe);
 
110
                } else NoTitle:
 
111
                        strcpy(buffer, "xterm");
 
112
        }
 
113
        if ( (title=(char *)malloc(strlen(buffer)+1)) == NULL )
 
114
                return(NULL);
 
115
        strcpy(title, buffer);
 
116
        return(title);
 
117
}
 
118
static void set_xtitle(titlebar)
 
119
char *titlebar;
 
120
{
 
121
        fprintf(xt_output, "\033]0;%s\07", titlebar);
 
122
        fflush(xt_output);
 
123
}
 
124
 
 
125
int event_init(input, output, titlebar)
 
126
FILE *input, *output;
 
127
char *titlebar;
 
128
{
 
129
        char *termtype;
 
130
 
 
131
        /* A program can assume that input is unbuffered after this routine */
 
132
        setbuf(input, NULL);
 
133
        xt_input=input;
 
134
        xt_output=output;
 
135
 
 
136
        /* Check for xterm terminal type */
 
137
        if ( (termtype=(char *)getenv("TERM")) && strcmp(termtype, "xterm") == 0 ) {
 
138
#ifdef REPORT_SELECTION
 
139
                fprintf(xt_output, "\033[?1001h");
 
140
#else
 
141
                fprintf(xt_output, "\033[?1000h");
 
142
#endif /* REPORT_SELECTION */
 
143
                fflush(xt_output);
 
144
                have_xterm=1;
 
145
                old_title=get_xtitle();
 
146
                if ( titlebar ) {
 
147
                        set_xtitle(titlebar);
 
148
                        set_title=1;
 
149
                }
 
150
        } else {
 
151
                return(-1);
 
152
        }
 
153
        return(0);
 
154
}
 
155
 
 
156
int event_getc(X_event)
 
157
struct event *X_event;
 
158
{
 
159
#ifdef REPORT_SELECTION
 
160
        static int last_row, last_col;
 
161
#endif
 
162
        int c;
 
163
        static char prefix[8], *next;
 
164
#ifdef SPLITVT_SOURCE
 
165
        extern struct physical physical;
 
166
        window *thiswin;
 
167
#endif
 
168
 
 
169
        X_event->happening=0;
 
170
 
 
171
        if ( have_xterm ) {
 
172
                if ( terminal_input == 0 ) { /* Brand new input */
 
173
                        strcpy(prefix, "\033[");        /* Sequence prefix */
 
174
                        next=prefix;
 
175
                        while ( (c=getc(xt_input)) != EOF ) {
 
176
                                if ( *next ) {
 
177
                                        if ( c != *next ) {
 
178
                                                *next = c;
 
179
                                                terminal_input=(next-prefix);
 
180
                                                next=prefix;
 
181
                                                return(*(next++));
 
182
                                        } else
 
183
                                                ++next;
 
184
                                } else
 
185
                                        break;
 
186
                        }
 
187
                } else { /* We already have input to return */
 
188
                        --terminal_input;
 
189
                        return(*(next++));
 
190
                }
 
191
 
 
192
                /* If we got here, we got prefix + c */
 
193
                switch (c) {
 
194
                        case 'M':       /* ^[[M%d%d%d  (assume getc() is ok) */
 
195
                                        X_event->button_state =
 
196
                                                        (getc(xt_input)-' ');
 
197
                                        X_event->y=(getc(xt_input)-' ');
 
198
                                        last_col=X_event->y;
 
199
                                        X_event->x=(getc(xt_input)-' ');
 
200
                                        last_row=X_event->x;
 
201
#ifdef REPORT_SELECTION
 
202
                                        if (BUTTON_ISSET((*X_event), BUTTON_1))
 
203
                                        { /* Tell xterm to start selection */
 
204
#ifdef SPLITVT_SOURCE
 
205
                if ( X_event->x < (physical.subwins[LOWER])->row_offset )
 
206
                        thiswin=physical.subwins[UPPER];
 
207
                else if ( X_event->x > (physical.subwins[LOWER])->row_offset )
 
208
                        thiswin=physical.subwins[LOWER];
 
209
                else { /* On separator bar -- don't start cut-paste there */
 
210
                        fprintf(xt_output, "\033[0;0;0;0;0T");
 
211
                        fflush(xt_output);
 
212
                        X_event->happening=1;
 
213
                        break;
 
214
                }
 
215
                fprintf(xt_output, "\033[1;%d;%d;%d;%dT",
 
216
                                        /* startcol, startrow, */
 
217
                                        X_event->y, X_event->x,
 
218
                                        /* first row */
 
219
                                        1 + thiswin->row_offset,
 
220
                                        /* last row */
 
221
                                        thiswin->rows + thiswin->row_offset+1);
 
222
#else
 
223
                                                fprintf(xt_output, 
 
224
                                                        "\033[1;%d;%d;%d;%dT",
 
225
                                                /* startcol, startrow, */
 
226
                                                        X_event->y, X_event->x,
 
227
                                                /* firstrow, lastrow */
 
228
                                                        1, 24);
 
229
#endif /* SPLITVT_SOURCE */
 
230
                                                fflush(xt_output);
 
231
                                        }
 
232
#endif /* REPORT_SELECTION */
 
233
                                        X_event->happening=1;
 
234
                                        break;
 
235
#ifdef REPORT_SELECTION
 
236
                        case 't':       /* ^[[t%d%d */
 
237
                                        X_event->button_state = 
 
238
                                                        (RELEASE|SELECTED);
 
239
                                        X_event->selection.begin_row=last_row;
 
240
                                        X_event->selection.begin_col=last_col;
 
241
                                        X_event->selection.end_col =
 
242
                                                        (getc(xt_input)-' ');
 
243
                                        X_event->selection.end_row =
 
244
                                                        (getc(xt_input)-' ');
 
245
                                        X_event->happening=1;
 
246
                                        break;
 
247
                        case 'T':       /* ^[[T%d%d%d%d%d%d */
 
248
                                        X_event->y=(getc(xt_input)-' ');
 
249
                                        X_event->x=(getc(xt_input)-' ');
 
250
                                        X_event->button_state = 
 
251
                                                        (RELEASE|SELECTED);
 
252
                                        X_event->selection.begin_col =
 
253
                                                        (getc(xt_input)-' ');
 
254
                                        X_event->selection.begin_row =
 
255
                                                        (getc(xt_input)-' ');
 
256
                                        X_event->selection.end_col =
 
257
                                                        (getc(xt_input)-' ');
 
258
                                        X_event->selection.end_row =
 
259
                                                        (getc(xt_input)-' ');
 
260
                                        X_event->happening=1;
 
261
                                        break;
 
262
#endif /* REPORT_SELECTION */
 
263
                        case EOF:       /* We got EOF, return EOF */
 
264
                                        return(EOF);
 
265
                                        break;
 
266
                        default:        /* It's not an event sequence */
 
267
                                        *next = c;
 
268
                                        terminal_input=(next-prefix);
 
269
                                        next=prefix;
 
270
                                        return(*(next++));
 
271
                                        break;
 
272
                }
 
273
        } else
 
274
                return(getc(xt_input));
 
275
        return(0);
 
276
}
 
277
 
 
278
void event_quit()
 
279
{
 
280
        if ( have_xterm ) {
 
281
#ifdef REPORT_SELECTION
 
282
                fprintf(xt_output, "\033[?1001l");
 
283
#else
 
284
                fprintf(xt_output, "\033[?1000l");
 
285
#endif /* REPORT_SELECTION */
 
286
                fflush(xt_output);
 
287
                if ( set_title && old_title ) {
 
288
                        set_xtitle(old_title);
 
289
                        (void) free(old_title);
 
290
                }
 
291
        }
 
292
}
 
293