File: vtmouse.c

package info (click to toggle)
splitvt 1.6.6-7
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 340 kB
  • ctags: 393
  • sloc: ansic: 4,811; sh: 99; makefile: 19; perl: 15
file content (298 lines) | stat: -rw-r--r-- 7,376 bytes parent folder | download | duplicates (5)
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

/* This is a program to show how mouse events can be handled by programs
   running under an xterm window.

		-Sam Lantinga		5/11/94
*/

#define SPLITVT_SOURCE

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vtmouse.h"
#include "splitvt.h"

extern FILE *safe_popen();		/* From misc.c */

#ifdef MAIN
void event_loop()
{
	int c, event_flag;
	struct event X_event;

	while ( (c=event_getc(&X_event)) != EOF ) {
		if ( X_event.happening ) {
			printf("\r\n************\r\n");
			printf("Mouse Event: 0x%x\r\n", X_event.button_state);
			printf("\tX coordinate: %d\r\n", X_event.x);
			printf("\tY coordinate: %d\r\n", X_event.y);
			/* One example of getting the mouse state */
			printf("\tX Event: ");
			switch(X_event.button_state&BUTTON_MASK) {
				case RELEASE:	printf("Button was released!\r\n");
						break;
				default:	printf("Button %d was pressed!\r\n", 
							(X_event.button_state&BUTTON_MASK)+1);
						break;
			}
			/* Another example of getting the mouse state */
			printf("\tMouse %s buttons <%c|%c|%c>\r\n",
				(BUTTON_ISSET(X_event, RELEASE) ? "released" : "pressed"),
				(BUTTON_ISSET(X_event, BUTTON_1) ? 'X' : ' '),
				(BUTTON_ISSET(X_event, BUTTON_2) ? 'X' : ' '),
				(BUTTON_ISSET(X_event, BUTTON_3) ? 'X' : ' '));
#ifdef REPORT_SELECTION
			if ( (X_event.button_state&SELECTED) == SELECTED )
				printf("Selection: Start x=%d, y=%d; End x=%d, y=%d\r\n",
						X_event.selection.begin_col,
						X_event.selection.begin_row,
						X_event.selection.end_col,
						X_event.selection.end_row);
#endif /* REPORT_SELECTION */
			printf("************\r\n");
		} else {
			if ( c == '\r' ) {
				putc('\r', stdout);
				putc('\n', stdout);
			} else if ( c < ' ' ) {
				putc('^', stdout);
				putc(c+'@', stdout);
			} else if ( c == 'Q' )
				return;
			else
				putc(c, stdout);
		}
	}
}

int main(int argc, char *argv[])
{
	if ( event_init(stdin, stdout, NULL) < 0 ) {
		printf("Not running under an xterm.\n");
		exit(2);
	}
	system("stty -echo raw");

	event_loop();

	system("stty -raw echo");
	event_quit();

	exit(0);
}
#endif

/* I/O streams default to stdin and stdout. */
static FILE *xt_input, *xt_output;
static int have_xterm=0;
static int set_title=0;
static char *old_title=NULL;
int terminal_input=0;		/* Is there pending terminal input? */

static char *get_xtitle()
{
	char buffer[512], *title;
	FILE *pipe;

	strcpy(buffer, "xterm");
	if ( (title=(char *)getenv("WINDOWID")) ) {
		if ( (strlen("xprop -id ")+strlen(title)+1) > 512 )
			goto NoTitle;
		sprintf(buffer, "xprop -id %s", title);
		if ( (pipe=safe_popen(buffer, "r")) ) {
			while ( fgets(buffer, 511, pipe) );
			/* Last line should be 'WM_NAME(STRING) = "title"' */
			if (strlen(buffer) > strlen("WM_NAME(STRING) = \"")) {
				buffer[strlen(buffer)-2]='\0';
				title=(buffer+strlen("WM_NAME(STRING) = \""));
				strcpy(buffer, title);
			} else
				strcpy(buffer, "xterm");
			safe_pclose(pipe);
		} else NoTitle:
			strcpy(buffer, "xterm");
	}
	if ( (title=(char *)malloc(strlen(buffer)+1)) == NULL )
		return(NULL);
	strcpy(title, buffer);
	return(title);
}
static void set_xtitle(titlebar)
char *titlebar;
{
	fprintf(xt_output, "\033]0;%s\07", titlebar);
	fflush(xt_output);
}

int event_init(input, output, titlebar)
FILE *input, *output;
char *titlebar;
{
	char *termtype;

	/* A program can assume that input is unbuffered after this routine */
	setbuf(input, NULL);
	xt_input=input;
	xt_output=output;

	/* Check for xterm terminal type */
	if ( (termtype=(char *)getenv("TERM")) && strcmp(termtype, "xterm") == 0 ) {
#ifdef REPORT_SELECTION
		fprintf(xt_output, "\033[?1001h");
#else
		fprintf(xt_output, "\033[?1000h");
#endif /* REPORT_SELECTION */
		fflush(xt_output);
		have_xterm=1;
		old_title=get_xtitle();
		if ( titlebar ) {
			set_xtitle(titlebar);
			set_title=1;
		}
	} else {
		return(-1);
	}
	return(0);
}

int event_getc(X_event)
struct event *X_event;
{
#ifdef REPORT_SELECTION
	static int last_row, last_col;
#endif
	int c;
	static char prefix[8], *next;
#ifdef SPLITVT_SOURCE
	extern struct physical physical;
	window *thiswin;
#endif

	X_event->happening=0;

	if ( have_xterm ) {
		if ( terminal_input == 0 ) { /* Brand new input */
			strcpy(prefix, "\033[");	/* Sequence prefix */
			next=prefix;
			while ( (c=getc(xt_input)) != EOF ) {
				if ( *next ) {
					if ( c != *next ) {
						*next = c;
						terminal_input=(next-prefix);
						next=prefix;
						return(*(next++));
					} else
						++next;
				} else
					break;
			}
		} else { /* We already have input to return */
			--terminal_input;
			return(*(next++));
		}

		/* If we got here, we got prefix + c */
		switch (c) {
			case 'M':	/* ^[[M%d%d%d  (assume getc() is ok) */
					X_event->button_state =
							(getc(xt_input)-' ');
					X_event->y=(getc(xt_input)-' ');
					last_col=X_event->y;
					X_event->x=(getc(xt_input)-' ');
					last_row=X_event->x;
#ifdef REPORT_SELECTION
					if (BUTTON_ISSET((*X_event), BUTTON_1))
					{ /* Tell xterm to start selection */
#ifdef SPLITVT_SOURCE
        	if ( X_event->x < (physical.subwins[LOWER])->row_offset )
			thiswin=physical.subwins[UPPER];
		else if ( X_event->x > (physical.subwins[LOWER])->row_offset )
			thiswin=physical.subwins[LOWER];
		else { /* On separator bar -- don't start cut-paste there */
			fprintf(xt_output, "\033[0;0;0;0;0T");
			fflush(xt_output);
			X_event->happening=1;
			break;
		}
		fprintf(xt_output, "\033[1;%d;%d;%d;%dT",
					/* startcol, startrow, */
					X_event->y, X_event->x,
					/* first row */
					1 + thiswin->row_offset,
					/* last row */
					thiswin->rows + thiswin->row_offset+1);
#else
						fprintf(xt_output, 
							"\033[1;%d;%d;%d;%dT",
						/* startcol, startrow, */
							X_event->y, X_event->x,
						/* firstrow, lastrow */
							1, 24);
#endif /* SPLITVT_SOURCE */
						fflush(xt_output);
					}
#endif /* REPORT_SELECTION */
					X_event->happening=1;
					break;
#ifdef REPORT_SELECTION
			case 't':	/* ^[[t%d%d */
					X_event->button_state = 
							(RELEASE|SELECTED);
					X_event->selection.begin_row=last_row;
					X_event->selection.begin_col=last_col;
					X_event->selection.end_col =
							(getc(xt_input)-' ');
					X_event->selection.end_row =
							(getc(xt_input)-' ');
					X_event->happening=1;
					break;
			case 'T':	/* ^[[T%d%d%d%d%d%d */
					X_event->y=(getc(xt_input)-' ');
					X_event->x=(getc(xt_input)-' ');
					X_event->button_state = 
							(RELEASE|SELECTED);
					X_event->selection.begin_col =
							(getc(xt_input)-' ');
					X_event->selection.begin_row =
							(getc(xt_input)-' ');
					X_event->selection.end_col =
							(getc(xt_input)-' ');
					X_event->selection.end_row =
							(getc(xt_input)-' ');
					X_event->happening=1;
					break;
#endif /* REPORT_SELECTION */
			case EOF:	/* We got EOF, return EOF */
					return(EOF);
					break;
			default:	/* It's not an event sequence */
					*next = c;
					terminal_input=(next-prefix);
					next=prefix;
					return(*(next++));
					break;
		}
	} else
		return(getc(xt_input));
	return(0);
}

void event_quit()
{
	if ( have_xterm ) {
#ifdef REPORT_SELECTION
		fprintf(xt_output, "\033[?1001l");
#else
		fprintf(xt_output, "\033[?1000l");
#endif /* REPORT_SELECTION */
		fflush(xt_output);
		if ( set_title && old_title ) {
			set_xtitle(old_title);
			(void) free(old_title);
		}
	}
}