[hackers] [st] made event.c similiar to that one found in dwm

From: Anselm R. Garbe <arg_AT_suckless.org>
Date: Tue Mar 13 13:08:51 2007

changeset: 65:ac0ccddcf0e4
tag: tip
user: Anselm R. Garbe <arg_AT_suckless.org>
date: Tue Mar 13 13:08:27 2007 +0100
summary: made event.c similiar to that one found in dwm

diff -r f56af848b3b0 -r ac0ccddcf0e4 event.c
--- a/event.c Mon Mar 05 15:55:24 2007 +0100
+++ b/event.c Tue Mar 13 13:08:27 2007 +0100
@@ -38,44 +38,6 @@ static unsigned char *selection_text = N
 #define SELECT_WORD 2
 #define SELECT_LINE 3
 
-static char *eventnames[] = {
- "",
- "",
- "KeyPress",
- "KeyRelease",
- "ButtonPress",
- "ButtonRelease",
- "MotionNotify",
- "EnterNotify",
- "LeaveNotify",
- "FocusIn",
- "FocusOut",
- "KeymapNotify",
- "Expose",
- "GraphicsExpose",
- "NoExpose",
- "VisibilityNotify",
- "CreateNotify",
- "DestroyNotify",
- "UnmapNotify",
- "MapNotify",
- "MapRequest",
- "ReparentNotify",
- "ConfigureNotify",
- "ConfigureRequest",
- "GravityNotify",
- "ResizeRequest",
- "CirculateNotify",
- "CirculateRequest",
- "PropertyNotify",
- "SelectionClear",
- "SelectionRequest",
- "SelectionNotify",
- "ColormapNotify",
- "ClientMessage",
- "MappingNotify"
-};
-
 static void
 selection_paste(Time tm) {
         int nbytes;
@@ -87,7 +49,7 @@ selection_paste(Time tm) {
 }
 
 static void
-handle_keypress(XEvent *xev) {
+keypress(XEvent *xev) {
         char kbuf[KEYSEQLEN];
         KeySym keysym;
         static XComposeStatus compose = {NULL, 0};
@@ -147,7 +109,7 @@ handle_keypress(XEvent *xev) {
 }
 
 static void
-handle_resize(XEvent *xev) {
+configurenotify(XEvent *xev) {
         int new_cols, new_rows;
 
         new_cols = xev->xconfigure.width / dc.font.width;
@@ -238,7 +200,7 @@ selection_select_line(int row, int col,
 }
 
 static void
-handle_buttonpress(XEvent *xev) {
+buttonpress(XEvent *xev) {
         static int lastclick_time = 0;
         static int times_clicked = 0;
         int click_col, click_row;
@@ -282,7 +244,43 @@ handle_buttonpress(XEvent *xev) {
 }
 
 static void
-handle_motionnotify(XEvent *xev) {
+expose(XEvent *xev) {
+ int a,b,c,d;
+ switch(refresh_type) {
+ case REFRESH_FULL:
+ force_redraw_screen();
+ break;
+ case REFRESH_PARTIALLY:
+ a = xev->xexpose.x / dc.font.width + 1;
+ b = xev->xexpose.y / dc.font.height + 1;
+ c = (xev->xexpose.x + xev->xexpose.width) / dc.font.width + 1;
+ d = (xev->xexpose.y + xev->xexpose.height) / dc.font.height + 1;
+ a = a > screen_cols ? screen_cols : a;
+ b = b > screen_rows ? screen_rows : b;
+ c = c > screen_cols ? screen_cols : c;
+ d = d > screen_rows ? screen_rows : d;
+ redraw_region(a,b,c,d);
+ break;
+ }
+}
+
+static void
+visibilitynotify(XEvent *xev) {
+ switch(xev->xvisibility.state) {
+ case VisibilityUnobscured:
+ refresh_type = REFRESH_FULL;
+ break;
+ case VisibilityPartiallyObscured:
+ refresh_type = REFRESH_PARTIALLY;
+ break;
+ default:
+ refresh_type = REFRESH_NO;
+ break;
+ }
+}
+
+static void
+motionnotify(XEvent *xev) {
         int unused;
         unsigned int mask;
         Window dummy;
@@ -293,7 +291,14 @@ handle_motionnotify(XEvent *xev) {
 }
 
 static void
-selection_copy(XEvent *xev) {
+selectionclear(XEvent *xev) {
+ selection_reset();
+ free(selection_text);
+ selection_text = NULL;
+}
+
+static void
+selectionrequest(XEvent *xev) {
         XEvent ev;
         XSelectionRequestEvent *rq = (XSelectionRequestEvent*)xev;
         Atom32 target_list[4];
@@ -346,90 +351,15 @@ selection_copy(XEvent *xev) {
 }
 
 /* extern */
-int
-handle_x_events(void) {
- XEvent xev;
-
- /* Input from the X server */
- while (XPending(dpy)) {
- /* process pending X events */
- XNextEvent(dpy, &xev);
-
- switch(xev.type) {
- default:
- fprintf(stderr, "Got unhandled X event %d (%s)\n", xev.type, eventnames[xev.type]);
- case ButtonPress:
- handle_buttonpress(&xev);
- break;
- case MotionNotify:
- handle_motionnotify(&xev);
- break;
- case KeyPress:
- handle_keypress(&xev);
- break;
- case ConfigureNotify:
- handle_resize(&xev);
- break;
- case SelectionNotify:
- /* Ignore this? */
- break;
- case GraphicsExpose:
- case Expose:
- /* Part of the window needs a redraw */
- switch(refresh_type) {
- case REFRESH_FULL:
- force_redraw_screen();
- break;
- case REFRESH_PARTIALLY:
- {
- int a,b,c,d;
- a = xev.xexpose.x / dc.font.width + 1;
- b = xev.xexpose.y / dc.font.height + 1;
- c = (xev.xexpose.x + xev.xexpose.width) / dc.font.width + 1;
- d = (xev.xexpose.y + xev.xexpose.height) / dc.font.height + 1;
- a = a > screen_cols ? screen_cols : a;
- b = b > screen_rows ? screen_rows : b;
- c = c > screen_cols ? screen_cols : c;
- d = d > screen_rows ? screen_rows : d;
- redraw_region(a,b,c,d);
- }
- break;
- default:
- /* No refreshing needs to be done */
- break;
- }
- break;
- case VisibilityNotify:
- /* Some part of the screen has to be redrawn */
- switch(xev.xvisibility.state) {
- case VisibilityUnobscured:
- refresh_type = REFRESH_FULL;
- break;
- case VisibilityPartiallyObscured:
- refresh_type = REFRESH_PARTIALLY;
- break;
- default:
- refresh_type = REFRESH_NO;
- break;
- }
- break;
- case MapNotify:
- case UnmapNotify:
- case FocusIn:
- case FocusOut:
- case NoExpose:
- /* Silently unhandled for now */
- break;
- case SelectionClear:
- selection_reset();
- free(selection_text);
- selection_text = NULL;
- break;
- case SelectionRequest:
- fprintf(stderr, "SelectionRequest\n");
- selection_copy(&xev);
- break;
- }
- }
- return 0;
-}
+
+void (*handler[LASTEvent]) (XEvent *) = {
+ [ButtonPress] = buttonpress,
+ [ConfigureNotify] = configurenotify,
+ [Expose] = expose,
+ [GraphicsExpose] = expose,
+ [KeyPress] = keypress,
+ [MotionNotify] = motionnotify,
+ [SelectionClear] = selectionclear,
+ [SelectionRequest] = selectionrequest,
+ [VisibilityNotify] = visibilitynotify,
+};
diff -r f56af848b3b0 -r ac0ccddcf0e4 main.c
--- a/main.c Mon Mar 05 15:55:24 2007 +0100
+++ b/main.c Tue Mar 13 13:08:27 2007 +0100
@@ -9,6 +9,17 @@
 #include <X11/cursorfont.h>
 
 /* static */
+
+static char *bgcolors[] = {
+ "black",
+ "#d00000",
+ "#00d000",
+ "#d0d000",
+ "#0000d0",
+ "#d000d0",
+ "#00d0d0",
+ "#d0d0d0"
+};
 
 static char *fgcolors[] = {
         "black",
@@ -20,17 +31,6 @@ static char *fgcolors[] = {
         "#00d0d0",
         "#d0d0d0",
         "#404040"
-};
-
-static char *bgcolors[] = {
- "black",
- "#d00000",
- "#00d000",
- "#d0d000",
- "#0000d0",
- "#d000d0",
- "#00d0d0",
- "#d0d0d0"
 };
 
 static unsigned long
diff -r f56af848b3b0 -r ac0ccddcf0e4 process.c
--- a/process.c Mon Mar 05 15:55:24 2007 +0100
+++ b/process.c Tue Mar 13 13:08:27 2007 +0100
@@ -219,6 +219,7 @@ wait_for_input(void) {
         int r;
         int X_fd;
         int need_redraw = 0;
+ XEvent ev;
 
         X_fd = ConnectionNumber(dpy);
         hide_cursor();
@@ -253,7 +254,11 @@ wait_for_input(void) {
                 }
                 if(FD_ISSET(X_fd, &fset)) {
                         hide_cursor();
- handle_x_events();
+ while(XPending(dpy)) {
+ XNextEvent(dpy, &ev);
+ if(handler[ev.type])
+ (handler[ev.type])(&ev); /* call handler */
+ }
                         need_redraw = 1;
                 }
         }
diff -r f56af848b3b0 -r ac0ccddcf0e4 st.h
--- a/st.h Mon Mar 05 15:55:24 2007 +0100
+++ b/st.h Tue Mar 13 13:08:27 2007 +0100
@@ -53,32 +53,18 @@ typedef struct {
         } font;
 } DC;
 
-extern int application_keypad_mode;
-extern int insert_mode;
-extern int decckm_mode;
-extern int wraparound_mode;
-extern int cursor_visible;
-extern int using_alternate_screen;
-extern int curr_row, curr_col;
-
-extern int screen_cols;
-extern int screen_rows;
-extern int scroll_region_start;
-extern int scroll_region_end ;
-extern int scroll_in_region;
+extern int application_keypad_mode, insert_mode, decckm_mode, wraparound_mode;
+extern int cursor_visible, using_alternate_screen, curr_row, curr_col;
+extern int screen_cols, screen_rows;
+extern int scroll_region_start, scroll_region_end, scroll_in_region;
 extern DC dc;
 extern Glyph text_attrs;
-extern Row *text_screen;
-extern Row *saved_screen;
+extern Row *text_screen, *saved_screen;
 extern Display *dpy;
 extern Window win, root;
 extern Cursor cursor;
-extern int screen;
-extern int ww;
-extern int wh;
-
-/* event.c */
-int handle_x_events(void);
+extern int screen, ww, wh;
+extern void (*handler[LASTEvent])(XEvent *);
 
 /* process.c */
 int resize_app(void);
Received on Tue Mar 13 2007 - 13:08:51 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:56:16 UTC