Krolyxon
Commit 12a94cc

Branches

Refs →

nsxiv12a94cc

Manual event handling in main.c

Author
Bert
Date
2011-02-03 10:55 +0100
Parent
e8cf8da
Commit
12a94cc40ef0f155a9c99dea915cd3807d760441

+142 −155

1 file changed

  1. M main.c +142 −155
M main.c View
@@ -32,17 +32,10 @@
3232 #include "util.h"
3333 #include "window.h"
3434
35−void on_keypress(XEvent*);
36−void on_buttonpress(XEvent*);
37−void on_buttonrelease(XEvent*);
38−void on_motionnotify(XEvent*);
39−void on_configurenotify(XEvent*);
40−
4135 void update_title();
4236 void check_append(const char*);
4337 void read_dir_rec(const char*);
44−
45−static void (*handler[LASTEvent])(XEvent*);
38+void run();
4639
4740 img_t img;
4841 win_t win;
@@ -52,11 +45,6 @@ win_t win;
5245 const char **filenames;
5346 int filecnt, fileidx;
5447
55−unsigned char timeout;
56−
57−int mox;
58−int moy;
59−
6048 #define TITLE_LEN 256
6149 char win_title[TITLE_LEN];
6250
@@ -69,39 +57,6 @@ void cleanup() {
6957 }
7058 }
7159
72−void run() {
73− int xfd;
74− fd_set fds;
75− struct timeval t;
76− XEvent ev;
77−
78− handler[KeyPress] = on_keypress;
79− handler[ButtonPress] = on_buttonpress;
80− handler[ButtonRelease] = on_buttonrelease;
81− handler[MotionNotify] = on_motionnotify;
82− handler[ConfigureNotify] = on_configurenotify;
83−
84− timeout = 0;
85−
86− while (1) {
87− if (timeout) {
88− t.tv_sec = 0;
89− t.tv_usec = 250;
90− xfd = ConnectionNumber(win.env.dpy);
91− FD_ZERO(&fds);
92− FD_SET(xfd, &fds);
93−
94− if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
95− img_render(&img, &win);
96− timeout = 0;
97− }
98− }
99−
100− if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type])
101− handler[ev.type](&ev);
102− }
103−}
104−
10560 int main(int argc, char **argv) {
10661 int i;
10762 const char *filename;
@@ -158,15 +113,102 @@ int main(int argc, char **argv) {
158113 return 0;
159114 }
160115
161−void on_keypress(XEvent *ev) {
116+void update_title() {
117+ int n;
118+
119+ n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
120+ filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
121+
122+ if (n >= TITLE_LEN) {
123+ win_title[TITLE_LEN - 2] = '.';
124+ win_title[TITLE_LEN - 3] = '.';
125+ win_title[TITLE_LEN - 4] = '.';
126+ }
127+
128+ win_set_title(&win, win_title);
129+}
130+
131+void check_append(const char *filename) {
132+ if (!filename)
133+ return;
134+
135+ if (img_check(filename)) {
136+ if (fileidx == filecnt) {
137+ filecnt *= 2;
138+ filenames = (const char**) s_realloc(filenames,
139+ filecnt * sizeof(const char*));
140+ }
141+ filenames[fileidx++] = filename;
142+ }
143+}
144+
145+void read_dir_rec(const char *dirname) {
146+ char *filename;
147+ const char **dirnames;
148+ int dircnt, diridx;
149+ unsigned char first;
150+ size_t len;
151+ DIR *dir;
152+ struct dirent *dentry;
153+ struct stat fstats;
154+
155+ if (!dirname)
156+ return;
157+
158+ dircnt = DNAME_CNT;
159+ diridx = first = 1;
160+ dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
161+ dirnames[0] = dirname;
162+
163+ while (diridx > 0) {
164+ dirname = dirnames[--diridx];
165+ if (!(dir = opendir(dirname)))
166+ die("could not open directory: %s", dirname);
167+ while ((dentry = readdir(dir))) {
168+ if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
169+ continue;
170+ len = strlen(dirname) + strlen(dentry->d_name) + 2;
171+ filename = (char*) s_malloc(len * sizeof(char));
172+ snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
173+ if (stat(filename, &fstats)) {
174+ warn("could not stat file: %s", filename);
175+ free(filename);
176+ } else if (S_ISDIR(fstats.st_mode)) {
177+ if (diridx == dircnt) {
178+ dircnt *= 2;
179+ dirnames = (const char**) s_realloc(dirnames,
180+ dircnt * sizeof(const char*));
181+ }
182+ dirnames[diridx++] = filename;
183+ } else {
184+ check_append(filename);
185+ }
186+ }
187+ closedir(dir);
188+ if (!first)
189+ free((void*) dirname);
190+ else
191+ first = 0;
192+ }
193+
194+ free(dirnames);
195+}
196+
197+
198+/* event handling */
199+
200+unsigned char timeout;
201+int mox, moy;
202+
203+void on_keypress(XKeyEvent *kev) {
162204 char key;
163205 KeySym ksym;
164206 int changed;
165207
166− if (!ev)
208+ if (!kev)
167209 return;
168210
169− XLookupString(&ev->xkey, &key, 1, &ksym, NULL);
211+ XLookupString(kev, &key, 1, &ksym, NULL);
170212 changed = 0;
171213
172214 switch (ksym) {
@@ -271,17 +313,17 @@ void on_keypress(XEvent *ev) {
271313 }
272314 }
273315
274−void on_buttonpress(XEvent *ev) {
316+void on_buttonpress(XButtonEvent *bev) {
275317 int changed;
276318 unsigned int mask;
277319
278− if (!ev)
320+ if (!bev)
279321 return;
280322
281− mask = CLEANMASK(ev->xbutton.state);
323+ mask = CLEANMASK(bev->state);
282324 changed = 0;
283325
284− switch (ev->xbutton.button) {
326+ switch (bev->button) {
285327 case Button1:
286328 if (fileidx + 1 < filecnt) {
287329 img_load(&img, filenames[++fileidx]);
@@ -289,8 +331,8 @@ void on_buttonpress(XEvent *ev) {
289331 }
290332 break;
291333 case Button2:
292− mox = ev->xbutton.x;
293− moy = ev->xbutton.y;
334+ mox = bev->x;
335+ moy = bev->y;
294336 win_set_cursor(&win, CURSOR_HAND);
295337 break;
296338 case Button3:
@@ -330,118 +372,63 @@ void on_buttonpress(XEvent *ev) {
330372 }
331373 }
332374
333−void on_buttonrelease(XEvent *ev) {
334− if (!ev)
375+void on_motionnotify(XMotionEvent *mev) {
376+ if (!mev)
335377 return;
336378
337− if (ev->xbutton.button == Button2)
338− win_set_cursor(&win, CURSOR_ARROW);
339−}
340−
341−void on_motionnotify(XEvent *ev) {
342− XMotionEvent *m;
343−
344− if (!ev)
345− return;
346−
347− m = &ev->xmotion;
348−
349− if (m->x >= 0 && m->x <= win.w && m->y >= 0 && m->y <= win.h) {
350− if (img_move(&img, &win, m->x - mox, m->y - moy))
379+ if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
380+ if (img_move(&img, &win, mev->x - mox, mev->y - moy))
351381 timeout = 1;
352382
353− mox = m->x;
354− moy = m->y;
383+ mox = mev->x;
384+ moy = mev->y;
355385 }
356386 }
357387
358−void on_configurenotify(XEvent *ev) {
359− if (!ev)
360− return;
361−
362− if (win_configure(&win, &ev->xconfigure)) {
363− img.checkpan = 1;
364− timeout = 1;
365− }
366−}
367−
368−void update_title() {
369− int n;
370−
371− n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
372− filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
373−
374− if (n >= TITLE_LEN) {
375− win_title[TITLE_LEN - 2] = '.';
376− win_title[TITLE_LEN - 3] = '.';
377− win_title[TITLE_LEN - 4] = '.';
378− }
379−
380− win_set_title(&win, win_title);
381−}
388+void run() {
389+ int xfd;
390+ fd_set fds;
391+ struct timeval t;
392+ XEvent ev;
382393
383−void check_append(const char *filename) {
384− if (!filename)
385− return;
394+ timeout = 0;
386395
387− if (img_check(filename)) {
388− if (fileidx == filecnt) {
389− filecnt *= 2;
390− filenames = (const char**) s_realloc(filenames,
391− filecnt * sizeof(const char*));
396+ while (1) {
397+ if (timeout) {
398+ t.tv_sec = 0;
399+ t.tv_usec = 250;
400+ xfd = ConnectionNumber(win.env.dpy);
401+ FD_ZERO(&fds);
402+ FD_SET(xfd, &fds);
403+
404+ if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
405+ img_render(&img, &win);
406+ timeout = 0;
407+ }
392408 }
393− filenames[fileidx++] = filename;
394− }
395−}
396409
397−void read_dir_rec(const char *dirname) {
398− char *filename;
399− const char **dirnames;
400− int dircnt, diridx;
401− unsigned char first;
402− size_t len;
403− DIR *dir;
404− struct dirent *dentry;
405− struct stat fstats;
406−
407− if (!dirname)
408− return;
409−
410− dircnt = DNAME_CNT;
411− diridx = first = 1;
412− dirnames = (const char**) s_malloc(dircnt * sizeof(const char*));
413− dirnames[0] = dirname;
414−
415− while (diridx > 0) {
416− dirname = dirnames[--diridx];
417− if (!(dir = opendir(dirname)))
418− die("could not open directory: %s", dirname);
419− while ((dentry = readdir(dir))) {
420− if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
421− continue;
422− len = strlen(dirname) + strlen(dentry->d_name) + 2;
423− filename = (char*) s_malloc(len * sizeof(char));
424− snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
425− if (stat(filename, &fstats)) {
426− warn("could not stat file: %s", filename);
427− free(filename);
428− } else if (S_ISDIR(fstats.st_mode)) {
429− if (diridx == dircnt) {
430− dircnt *= 2;
431− dirnames = (const char**) s_realloc(dirnames,
432− dircnt * sizeof(const char*));
433− }
434− dirnames[diridx++] = filename;
435− } else {
436− check_append(filename);
410+ if (!XNextEvent(win.env.dpy, &ev)) {
411+ switch (ev.type) {
412+ case KeyPress:
413+ on_keypress(&ev.xkey);
414+ break;
415+ case ButtonPress:
416+ on_buttonpress(&ev.xbutton);
417+ break;
418+ case ButtonRelease:
419+ if (ev.xbutton.button == Button2)
420+ win_set_cursor(&win, CURSOR_ARROW);
421+ break;
422+ case MotionNotify:
423+ on_motionnotify(&ev.xmotion);
424+ break;
425+ case ConfigureNotify:
426+ if (win_configure(&win, &ev.xconfigure)) {
427+ img.checkpan = 1;
428+ timeout = 1;
429+ }
430+ break;
437431 }
438432 }
439− closedir(dir);
440− if (!first)
441− free((void*) dirname);
442− else
443− first = 0;
444433 }
445−
446− free(dirnames);
447434 }