[hackers] [swk] fix event handling 100% cpu usage || pancake

From: <hg_AT_suckless.org>
Date: Fri, 27 Aug 2010 17:58:43 +0000 (UTC)

changeset: 68:9774662be35c
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Fri Aug 27 19:55:23 2010 +0200
files: gi_x11.c t/test.c t/tlock.c
description:
fix event handling 100% cpu usage
remove unnecessary touchscreen hacks on x11
fix tlock example for small screens

diff -r 56bb495ab32c -r 9774662be35c gi_x11.c
--- a/gi_x11.c Wed Aug 25 05:30:41 2010 +0200
+++ b/gi_x11.c Fri Aug 27 19:55:23 2010 +0200
@@ -1,5 +1,4 @@
 /* See LICENSE file for copyright and license details. */
-#define _BSD_SOURCE // strdup
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -24,14 +23,8 @@
 static int colors[ColorLast] = { FGCOLOR, BGCOLOR, HICOLOR, TFCOLOR, CCCOLOR };
 #define EVENTMASK PointerMotionMask | ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
 
-int
-swk_gi_fontsize(int sz) {
- fs += sz*2;
- /* TODO: resize font */
- return 1;
-}
-
-static Window dc_window(DC *dc, int x, int y, int w, int h) {
+static Window /* TODO: push into libdraw */
+dc_window(DC *dc, int x, int y, int w, int h) {
         Window window;
         int screen = DefaultScreen(dc->dpy);
         window = XCreateSimpleWindow(dc->dpy, RootWindow(dc->dpy, screen),
@@ -42,6 +35,13 @@
 }
 
 int
+swk_gi_fontsize(int sz) {
+ fs += sz*2;
+ /* TODO: resize font */
+ return 1;
+}
+
+int
 swk_gi_init(SwkWindow *w) {
         char buf[128];
         int i;
@@ -75,13 +75,13 @@
 SwkEvent *
 swk_gi_event(SwkWindow *w, int dowait) {
         static int mousedowny, mousedownx, mousedown = 0;
- static int mousemoved = 0;
         KeySym ksym;
         XEvent event;
         SwkEvent *ret = &w->_e;
 
- if(!XCheckMaskEvent(dc->dpy, 0xffff, &event))
+ if(!dowait && !XPending(dc->dpy))
                 return NULL;
+ XNextEvent(dc->dpy, &event);
         switch(event.type) {
         case Expose:
                 ret->type = EExpose;
@@ -92,9 +92,6 @@
                 // TODO: move this stuff into swk.c.. shoudlnt be backend dependent
 // fprintf(stderr, "event: motion (%d,%d)\n", event.motion.x,event.motion.y);
                 if(mousedown) {
- // touchscreen spaguetti trick
- if(mousedowny==-1) mousedowny = event.xmotion.y; else mousemoved = 1;
- if(mousedownx==-1) mousedownx = event.xmotion.x; else mousemoved = 1;
                         if(event.xmotion.y>mousedowny+fs) {
                                 mousedowny = event.xmotion.y;
                                 swk_scroll_up(w);
@@ -125,32 +122,29 @@
         case ButtonRelease:
                 //fprintf(stderr, "event: up %d (%d,%d)\n", event.button.button,event.button.x,event.button.y);
                 mousedown = 0;
- if(!mousemoved) {
- ret->type = EClick;
- switch(event.xbutton.state) {
- case 4096: // 0x1000
- ret->data.click.button = 4;
- break;
- case 2048: // 0x800
- ret->data.click.button = 5;
- break;
- case 1024: // 0x400
- ret->data.click.button = 2;
- break;
- case 512: // 0x200
- ret->data.click.button = 3;
- break;
- case 256: // 0x100
- ret->data.click.button = 1;
- break;
- }
- ret->data.click.point.x = event.xbutton.x / fs;
- ret->data.click.point.y = event.xbutton.y / fs;
+ ret->type = EClick;
+ switch(event.xbutton.state) {
+ case 4096: // 0x1000
+ ret->data.click.button = 4;
+ break;
+ case 2048: // 0x800
+ ret->data.click.button = 5;
+ break;
+ case 1024: // 0x400
+ ret->data.click.button = 2;
+ break;
+ case 512: // 0x200
+ ret->data.click.button = 3;
+ break;
+ case 256: // 0x100
+ ret->data.click.button = 1;
+ break;
                 }
+ ret->data.click.point.x = event.xbutton.x / fs;
+ ret->data.click.point.y = event.xbutton.y / fs;
                 break;
         case ButtonPress:
                 //fprintf(stderr, "event: down %d (%d,%d)\n", event.button.button,event.button.x,event.button.y);
- mousemoved = 0;
                 mousedown = 1;
                 mousedowny = event.xbutton.y;
                 break;
@@ -225,6 +219,10 @@
 void
 swk_gi_line(int x1, int y1, int x2, int y2, int color) {
         Rect r = { x1, y1, x2, y2 };
+XXX
+ XSetForeground(dc->dpy, dc->gc, col[color]);
+XDrawLine(dc->dpy, dc->canvas, dc->gc, x1, y1, x2, y2);
+return;
         if(!x2 || !y2)
                 swk_gi_fill(r, color, 0);
         // TODO: add support for diagonal lines?
@@ -270,11 +268,8 @@
         if(!text||!*text)
                 return;
         XSetForeground(dc->dpy, dc->gc, col[ColorFG]);
-// XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, strlen(text));
         if(dc->font.xfont)
                 XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
- //printf("## %d\n", dc->font.xfont);
- //XmbDrawString(dc->dpy, dc->canvas, dc->font.set, 5+r.x*fs, ((1+r.y)*fs)-3, text, strlen (text));
         XDrawString(dc->dpy, dc->canvas, dc->gc, 5+r.x*fs, ((1+r.y)*fs)-3, text, strlen (text));
 }
 
diff -r 56bb495ab32c -r 9774662be35c t/test.c
--- a/t/test.c Wed Aug 25 05:30:41 2010 +0200
+++ b/t/test.c Fri Aug 27 19:55:23 2010 +0200
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <unistd.h>
 #include <swk.h>
 
 static int count = 3;
diff -r 56bb495ab32c -r 9774662be35c t/tlock.c
--- a/t/tlock.c Wed Aug 25 05:30:41 2010 +0200
+++ b/t/tlock.c Fri Aug 27 19:55:23 2010 +0200
@@ -31,8 +31,8 @@
 
 static void mylocklabel(SwkEvent *e) {
         if(e->type == EMotion) {
- count = 5;
- }
+ count = 3;
+ } else
         if(e->type == EExpose) {
                 int pos = e->box->r.y;
                 if(pos<3 || pos>e->win->r.h) {
@@ -47,7 +47,7 @@
         { .cb=swk_label, .text=timestring },
         { .cb=swk_separator },
         SWK_BOX_NEWLINE(-1),
- SWK_BOX_NEWLINE(4), // Trick to avoid unexpected swkexit
+ SWK_BOX_NEWLINE(6), // Trick to avoid unexpected swkexit
         { .cb=mylocklabel, .text=" slide out to unlock", },
         { .cb=NULL }
 };
Received on Fri Aug 27 2010 - 19:58:43 CEST

This archive was generated by hypermail 2.2.0 : Fri Aug 27 2010 - 20:00:07 CEST