1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
7 * $Id: xcut.c 2336 2009-03-22 18:59:56Z kcr $
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_xcut_c[] = "$Id: xcut.c 2336 2009-03-22 18:59:56Z kcr $";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* Code to deal with handling X events: */
26 /****************************************************************************/
28 #ifndef X_DISPLAY_MISSING
31 #include <X11/Xutil.h>
32 #include <zephyr/zephyr.h>
33 #include "new_memory.h"
34 #include "new_string.h"
40 #include "xrevstack.h"
45 #include "variables.h"
54 static char *selected_text=NULL;
55 static Window selecting_in = 0;
60 return(selected_text);
65 x_gram_to_string(x_gram *gram)
70 string text_so_far = string_Copy("");
74 for (i=0; i<gram->numblocks; i++) {
75 if (last_y != -1 && last_y != gram->blocks[i].y)
76 text_so_far = string_Concat2(text_so_far, "\n");
77 index = gram->blocks[i].strindex;
78 len = gram->blocks[i].strlen;
79 temp = string_CreateFromData(text+index, len);
80 text_so_far = string_Concat2(text_so_far, temp);
82 last_y = gram->blocks[i].y;
85 text_so_far = string_Concat2(text_so_far, "\n");
96 isShiftButton1(Display *dpy,
100 return(event->xbutton.state & (ShiftMask|Button1Mask));
105 isShiftButton3(Display *dpy,
109 return(event->xbutton.state & (ShiftMask|Button3Mask));
113 getLastEvent(Display *dpy,
119 if (state & Button1Mask) {
120 while(XCheckIfEvent(dpy,&xev,isShiftButton1,NULL))
122 } else if (state & Button3Mask) {
123 while(XCheckIfEvent(dpy,&xev,isShiftButton3,NULL))
129 xunmark(Display *dpy,
132 XContext desc_context)
135 if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
139 xmarkRedraw(dpy,w,gram,XMARK_REDRAW_OLD);
142 /* This is out here so xdestroygram can get at it */
144 #define PRESSOP_NONE 0 /* nothing */
145 #define PRESSOP_KILL 1 /* normal click */
146 #define PRESSOP_SEL 2 /* shift left */
147 #define PRESSOP_EXT 3 /* shift right */
148 #define PRESSOP_NUKE 4 /* ctrl */
149 #define PRESSOP_STOP 5 /* pressop cancelled by moving out of window */
151 static int current_pressop = PRESSOP_NONE;
154 xdestroygram(Display *dpy,
156 XContext desc_context,
161 gettimeofday(&now,NULL);
162 if ((gram->can_die.tv_sec == 0) ||
163 (gram->can_die.tv_sec > now.tv_sec) ||
164 ((gram->can_die.tv_sec == now.tv_sec) &&
165 (gram->can_die.tv_usec > now.tv_usec)))
168 if (w == selecting_in) {
172 current_pressop = PRESSOP_NONE;
173 XDeleteContext(dpy, w, desc_context);
174 XDestroyWindow(dpy, w);
180 list_del_notice(gram->notice);
188 if (bottom_gram == NULL && unlinked == NULL) {
189 /* flush colormap here */
196 XContext desc_context)
199 Window w = event->xany.window;
203 * If event is for a window that's not ours anymore (say we're
204 * in the process of deleting it...), ignore it:
206 if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
210 * Dispatch on the event type:
212 switch(event->type) {
219 res = XLookupString(&(event->xkey), &c, 1, NULL, NULL);
220 metaflag = event->xkey.state & Mod1Mask;
222 /* Recheck if zwgcplus is turned on;
223 * Zephyr variables override zwgc variables
227 plusvar = ZGetVariable("zwgcplus")
228 ? ZGetVariable("zwgcplus") : var_get_variable("zwgcplus");
230 if ((plusvar[0]=='\0') || (strcmp(plusvar,"no") == 0))
233 if (strcmp(plusvar,"no") == 0)
235 if (strcmp(plusvar,"new") == 0)
239 if (res != 0 && zwgcplus != 0)
240 plus_retry_notice(gram->notice, c, metaflag);
245 if ((event->xclient.message_type == XA_WM_PROTOCOLS) &&
246 (event->xclient.format == 32) &&
247 (event->xclient.data.l[0] == XA_WM_DELETE_WINDOW))
248 xdestroygram(dpy,w,desc_context,gram);
252 /* I don't like using the local time, but MapNotify events don't
253 * come with a timestamp, and there's no way to query the server
256 if (gram->can_die.tv_sec == 0) {
257 gettimeofday(&(gram->can_die),NULL);
258 gram->can_die.tv_sec += (int) (ttl/1000);
259 gram->can_die.tv_usec += (ttl%1000)*1000;
268 if (current_pressop == PRESSOP_KILL ||
269 current_pressop == PRESSOP_NUKE)
270 current_pressop = PRESSOP_STOP;
274 if (current_pressop == PRESSOP_SEL) {
275 /* getLastEvent(dpy,Button1Mask,event); */
276 changedbound=xmarkExtendFromFirst(gram,event->xmotion.x,
278 xmarkRedraw(dpy,w,gram,changedbound);
279 } else if (current_pressop == PRESSOP_EXT) {
280 /* getLastEvent(dpy,Button3Mask,event); */
281 changedbound=xmarkExtendFromNearest(gram,event->xmotion.x,
283 xmarkRedraw(dpy,w,gram,changedbound);
288 if (current_pressop != PRESSOP_NONE) {
289 current_pressop = PRESSOP_STOP;
290 } else if ((event->xbutton.button==Button4 ||
291 event->xbutton.button==Button5) &&
292 !get_bool_resource("scrollDelete","ScrollDelete",0)) {
293 /* Ignore scroll wheel movement. */
295 } else if ( (event->xbutton.state)&ShiftMask ) {
296 if (event->xbutton.button==Button1) {
298 xunmark(dpy,selecting_in,NULL,desc_context);
299 if (selected_text) free(selected_text);
300 selected_text = NULL;
301 if (! xselGetOwnership(dpy,w,event->xbutton.time)) {
303 ERROR("Unable to get ownership of PRIMARY selection.\n");
305 current_pressop = PRESSOP_STOP;
308 xmarkStart(gram,event->xbutton.x,event->xbutton.y);
309 current_pressop = PRESSOP_SEL;
311 } else if ((event->xbutton.button==Button3) &&
312 (w == selecting_in)) {
313 if (selected_text) free(selected_text);
314 selected_text = NULL;
315 changedbound=xmarkExtendFromNearest(gram,event->xbutton.x,
317 xmarkRedraw(dpy,w,gram,changedbound);
318 selected_text = xmarkGetText();
319 /* this is ok, since to get here, the selection must be owned */
320 current_pressop = PRESSOP_EXT;
323 XStoreBytes(dpy, selected_text, strlen(selected_text)+1);
326 } else if ( (event->xbutton.state)&ControlMask ) {
327 current_pressop = PRESSOP_NUKE;
329 current_pressop = PRESSOP_KILL;
334 if (current_pressop == PRESSOP_KILL) {
335 xdestroygram(dpy,w,desc_context,gram);
336 } else if (current_pressop == PRESSOP_SEL ||
337 current_pressop == PRESSOP_EXT) {
338 if (selected_text) free(selected_text);
339 selected_text = xmarkGetText();
342 XStoreBytes(dpy, selected_text, strlen(selected_text)+1);
344 } else if (current_pressop == PRESSOP_NUKE) {
345 XWindowAttributes wa;
350 for (gram = bottom_gram ; gram ; gram = next) {
351 XGetWindowAttributes(dpy,gram->w,&wa);
352 XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
357 if ((wa.map_state == IsViewable) &&
358 (gx <= event->xbutton.x_root) &&
359 (event->xbutton.x_root < gx+wa.width) &&
360 (gy <= event->xbutton.y_root) &&
361 (event->xbutton.y_root < gy+wa.height)) {
362 xdestroygram(dpy,gram->w,desc_context,gram);
365 for (gram = unlinked ; gram ; gram = next) {
366 XGetWindowAttributes(dpy,gram->w,&wa);
367 XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
372 if ((wa.map_state == IsViewable) &&
373 (gx <= event->xbutton.x_root) &&
374 (event->xbutton.x_root < gx+wa.width) &&
375 (gy <= event->xbutton.y_root) &&
376 (event->xbutton.y_root < gy+wa.height)) {
377 xdestroygram(dpy,gram->w,desc_context,gram);
381 current_pressop = PRESSOP_NONE;
384 case SelectionRequest:
385 xselProcessSelection(dpy,w,event);
389 xselOwnershipLost(event->xselectionclear.time);
390 if (w == selecting_in) {
392 xunmark(dpy,w,gram,desc_context);
393 if (selected_text) free(selected_text);
394 selected_text = NULL;
399 case ConfigureNotify:
402 printf("ConfigureNotify received for wid %lx above wid %lx\n",
403 (long) w,(long) event->xconfigure.above);
405 if (gram->above==gram) {
406 /* a new zgram. Straight to the bottom! */
408 } else if (event->xconfigure.above) {
409 /* some zgram was pulled to the top */
412 /* Some zgram was pushed to the bottom */
413 push_to_bottom(gram);
415 /* Note that there is no option to configure a zgram to the middle */