Option handling, merged app.c & events.c into main.c
This commit is contained in:
		
							parent
							
								
									79c7e6178e
								
							
						
					
					
						commit
						a7e30bb081
					
				
							
								
								
									
										39
									
								
								app.h
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								app.h
									
									
									
									
									
								
							| @ -1,39 +0,0 @@ | |||||||
| /* sxiv: app.h
 |  | ||||||
|  * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |  | ||||||
|  * |  | ||||||
|  * This program is free software; you can redistribute it and/or modify |  | ||||||
|  * it under the terms of the GNU General Public License as published by |  | ||||||
|  * the Free Software Foundation; either version 2 of the License, or |  | ||||||
|  * (at your option) any later version. |  | ||||||
|  *   |  | ||||||
|  * This program is distributed in the hope that it will be useful, |  | ||||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the |  | ||||||
|  * GNU General Public License for more details. |  | ||||||
|  *   |  | ||||||
|  * You should have received a copy of the GNU General Public License |  | ||||||
|  * along with this program; if not, write to the Free Software |  | ||||||
|  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| #ifndef APP_H |  | ||||||
| #define APP_H |  | ||||||
| 
 |  | ||||||
| #include "image.h" |  | ||||||
| #include "window.h" |  | ||||||
| 
 |  | ||||||
| typedef struct app_s { |  | ||||||
| 	char **filenames; |  | ||||||
| 	unsigned int filecnt; |  | ||||||
| 	unsigned int fileidx; |  | ||||||
| 	img_t img; |  | ||||||
| 	win_t win; |  | ||||||
| } app_t; |  | ||||||
| 
 |  | ||||||
| void app_init(app_t*); |  | ||||||
| void app_run(app_t*); |  | ||||||
| void app_quit(app_t*); |  | ||||||
| 
 |  | ||||||
| void app_load_image(app_t*); |  | ||||||
| 
 |  | ||||||
| #endif /* APP_H */ |  | ||||||
							
								
								
									
										72
									
								
								events.c
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								events.c
									
									
									
									
									
								
							| @ -1,72 +0,0 @@ | |||||||
| /* sxiv: events.c
 |  | ||||||
|  * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |  | ||||||
|  * |  | ||||||
|  * This program is free software; you can redistribute it and/or modify |  | ||||||
|  * it under the terms of the GNU General Public License as published by |  | ||||||
|  * the Free Software Foundation; either version 2 of the License, or |  | ||||||
|  * (at your option) any later version. |  | ||||||
|  *   |  | ||||||
|  * This program is distributed in the hope that it will be useful, |  | ||||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the |  | ||||||
|  * GNU General Public License for more details. |  | ||||||
|  *   |  | ||||||
|  * You should have received a copy of the GNU General Public License |  | ||||||
|  * along with this program; if not, write to the Free Software |  | ||||||
|  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| #include <stdlib.h> |  | ||||||
| 
 |  | ||||||
| #include <X11/Xlib.h> |  | ||||||
| #include <X11/keysym.h> |  | ||||||
| 
 |  | ||||||
| #include "events.h" |  | ||||||
| #include "window.h" |  | ||||||
| 
 |  | ||||||
| void on_keypress(app_t*, XEvent*); |  | ||||||
| void on_configurenotify(app_t*, XEvent*); |  | ||||||
| void on_expose(app_t*, XEvent*); |  | ||||||
| 
 |  | ||||||
| static void (*handler[LASTEvent])(app_t*, XEvent*) = { |  | ||||||
| 	[Expose] = on_expose, |  | ||||||
| 	[ConfigureNotify] = on_configurenotify, |  | ||||||
| 	[KeyPress] = on_keypress |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| void event_loop(app_t *app) { |  | ||||||
| 	XEvent ev; |  | ||||||
| 
 |  | ||||||
| 	while (!XNextEvent(app->win.env.dpy, &ev)) { |  | ||||||
| 		if (handler[ev.type]) |  | ||||||
| 			handler[ev.type](app, &ev); |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void on_keypress(app_t *app, XEvent *ev) { |  | ||||||
| 	KeySym keysym; |  | ||||||
| 
 |  | ||||||
| 	if (!app || !ev) |  | ||||||
| 		return; |  | ||||||
| 	 |  | ||||||
| 	keysym = XLookupKeysym(&ev->xkey, 0); |  | ||||||
| 
 |  | ||||||
| 	switch (keysym) { |  | ||||||
| 		case XK_Escape: |  | ||||||
| 			app_quit(app); |  | ||||||
| 			exit(1); |  | ||||||
| 		case XK_q: |  | ||||||
| 			app_quit(app); |  | ||||||
| 			exit(0); |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void on_configurenotify(app_t *app, XEvent *ev) { |  | ||||||
| 	if (!app || !ev) |  | ||||||
| 		return; |  | ||||||
| 	 |  | ||||||
| 	win_configure(&app->win, &ev->xconfigure); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void on_expose(app_t *app, XEvent *ev) { |  | ||||||
| } |  | ||||||
							
								
								
									
										11
									
								
								image.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								image.c
									
									
									
									
									
								
							| @ -19,6 +19,8 @@ | |||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| 
 | 
 | ||||||
|  | #include <Imlib2.h> | ||||||
|  | 
 | ||||||
| #include "sxiv.h" | #include "sxiv.h" | ||||||
| #include "image.h" | #include "image.h" | ||||||
| 
 | 
 | ||||||
| @ -32,7 +34,12 @@ void imlib_init(win_t *win) { | |||||||
| 	imlib_context_set_drawable(win->xwin); | 	imlib_context_set_drawable(win->xwin); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void img_load(img_t *img, char *filename) { | void imlib_destroy() { | ||||||
|  | 	if (imlib_context_get_image()) | ||||||
|  | 		imlib_free_image(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void img_load(img_t *img, const char *filename) { | ||||||
| 	if (!img || !filename) | 	if (!img || !filename) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| @ -53,7 +60,7 @@ void img_render(img_t *img, win_t *win) { | |||||||
| 	unsigned int sx, sy, sw, sh; | 	unsigned int sx, sy, sw, sh; | ||||||
| 	unsigned int dx, dy, dw, dh; | 	unsigned int dx, dy, dw, dh; | ||||||
| 
 | 
 | ||||||
| 	if (!img || !win) | 	if (!img || !win || !imlib_context_get_image()) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	/* set zoom level to fit image into window */ | 	/* set zoom level to fit image into window */ | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								image.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								image.h
									
									
									
									
									
								
							| @ -36,13 +36,13 @@ typedef struct img_s { | |||||||
| 	int h; | 	int h; | ||||||
| 	int x; | 	int x; | ||||||
| 	int y; | 	int y; | ||||||
| 
 |  | ||||||
| 	Imlib_Image *im; | 	Imlib_Image *im; | ||||||
| } img_t; | } img_t; | ||||||
| 
 | 
 | ||||||
| void imlib_init(win_t*); | void imlib_init(win_t*); | ||||||
|  | void imlib_destroy(); | ||||||
| 
 | 
 | ||||||
| void img_load(img_t*, char*); | void img_load(img_t*, const char*); | ||||||
| void img_render(img_t*, win_t*); | void img_render(img_t*, win_t*); | ||||||
| 
 | 
 | ||||||
| #endif /* IMAGE_H */ | #endif /* IMAGE_H */ | ||||||
|  | |||||||
							
								
								
									
										95
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								main.c
									
									
									
									
									
								
							| @ -18,20 +18,63 @@ | |||||||
| 
 | 
 | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| 
 | 
 | ||||||
| #include "sxiv.h" | #include <X11/Xlib.h> | ||||||
| #include "app.h" | #include <X11/keysym.h> | ||||||
| 
 | 
 | ||||||
| app_t app; | #include "sxiv.h" | ||||||
|  | #include "image.h" | ||||||
|  | #include "options.h" | ||||||
|  | #include "window.h" | ||||||
|  | 
 | ||||||
|  | void on_keypress(XEvent*); | ||||||
|  | void on_configurenotify(XEvent*); | ||||||
|  | void on_expose(XEvent*); | ||||||
|  | 
 | ||||||
|  | static void (*handler[LASTEvent])(XEvent*) = { | ||||||
|  | 	[Expose] = on_expose, | ||||||
|  | 	[ConfigureNotify] = on_configurenotify, | ||||||
|  | 	[KeyPress] = on_keypress | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | img_t img; | ||||||
|  | win_t win; | ||||||
|  | unsigned int fileidx; | ||||||
|  | 
 | ||||||
|  | void run() { | ||||||
|  | 	XEvent ev; | ||||||
|  | 
 | ||||||
|  | 	while (!XNextEvent(win.env.dpy, &ev)) { | ||||||
|  | 		if (handler[ev.type]) | ||||||
|  | 			handler[ev.type](&ev); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||||||
|  | 	if (parse_options(argc, argv) < 0) | ||||||
|  | 		return 1; | ||||||
| 
 | 
 | ||||||
| 	// TODO: parse cmd line arguments properly
 | 	if (!options->filecnt) { | ||||||
| 	app.filenames = argv + 1; | 		print_usage(); | ||||||
| 	app.filecnt = argc - 1; | 		exit(1); | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	app_init(&app); | 	fileidx = 0; | ||||||
| 	app_run(&app); | 
 | ||||||
| 	app_quit(&app); | 	img.zoom = 1.0; | ||||||
|  | 	img.scalemode = SCALE_MODE; | ||||||
|  | 
 | ||||||
|  | 	win.w = WIN_WIDTH; | ||||||
|  | 	win.h = WIN_HEIGHT; | ||||||
|  | 
 | ||||||
|  | 	win_open(&win); | ||||||
|  | 	imlib_init(&win); | ||||||
|  | 
 | ||||||
|  | 	img_load(&img, options->filenames[fileidx]); | ||||||
|  | 	img_render(&img, &win); | ||||||
|  | 
 | ||||||
|  | 	run(); | ||||||
|  | 
 | ||||||
|  | 	cleanup(); | ||||||
| 
 | 
 | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| @ -39,6 +82,36 @@ int main(int argc, char **argv) { | |||||||
| void cleanup() { | void cleanup() { | ||||||
| 	static int in = 0; | 	static int in = 0; | ||||||
| 
 | 
 | ||||||
| 	if (!in++) | 	if (!in++) { | ||||||
| 		app_quit(&app); | 		imlib_destroy(); | ||||||
|  | 		win_close(&win); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void on_keypress(XEvent *ev) { | ||||||
|  | 	KeySym keysym; | ||||||
|  | 
 | ||||||
|  | 	if (!ev) | ||||||
|  | 		return; | ||||||
|  | 	 | ||||||
|  | 	keysym = XLookupKeysym(&ev->xkey, 0); | ||||||
|  | 
 | ||||||
|  | 	switch (keysym) { | ||||||
|  | 		case XK_Escape: | ||||||
|  | 			cleanup(); | ||||||
|  | 			exit(1); | ||||||
|  | 		case XK_q: | ||||||
|  | 			cleanup(); | ||||||
|  | 			exit(0); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void on_configurenotify(XEvent *ev) { | ||||||
|  | 	if (!ev) | ||||||
|  | 		return; | ||||||
|  | 	 | ||||||
|  | 	win_configure(&win, &ev->xconfigure); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void on_expose(XEvent *ev) { | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| /* sxiv: app.c
 | /* sxiv: options.c
 | ||||||
|  * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |  * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | ||||||
|  * |  * | ||||||
|  * This program is free software; you can redistribute it and/or modify |  * This program is free software; you can redistribute it and/or modify | ||||||
| @ -16,42 +16,45 @@ | |||||||
|  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include <X11/Xlib.h> | #define _XOPEN_SOURCE | ||||||
|  | 
 | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <unistd.h> | ||||||
| 
 | 
 | ||||||
| #include "sxiv.h" | #include "sxiv.h" | ||||||
| #include "app.h" | #include "options.h" | ||||||
| #include "events.h" |  | ||||||
| 
 | 
 | ||||||
| void app_init(app_t *app) { | options_t _options; | ||||||
| 	if (!app) | const options_t *options = (const options_t*) &_options; | ||||||
| 		return; |  | ||||||
| 
 | 
 | ||||||
| 	app->fileidx = 0; | void print_usage() { | ||||||
| 
 | 	printf("usage: sxiv [-hv] FILES...\n"); | ||||||
| 	app->img.zoom = 1.0; |  | ||||||
| 	app->img.scalemode = SCALE_MODE; |  | ||||||
| 
 |  | ||||||
| 	app->win.w = WIN_WIDTH; |  | ||||||
| 	app->win.h = WIN_HEIGHT; |  | ||||||
| 
 |  | ||||||
| 	win_open(&app->win); |  | ||||||
| 	 |  | ||||||
| 	imlib_init(&app->win); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void app_run(app_t *app) { | void print_version() { | ||||||
| 	app_load_image(app); | 	printf("sxiv - simple x image viewer\n"); | ||||||
| 	event_loop(app); | 	printf("Version %s, written by Bert Muennich\n", VERSION); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void app_quit(app_t *app) { | int parse_options(int argc, char **argv) { | ||||||
| } | 	int opt; | ||||||
| 
 | 
 | ||||||
| void app_load_image(app_t *app) { | 	_options.filenames = (const char**) argv + 1; | ||||||
| 	if (!app || app->fileidx >= app->filecnt || !app->filenames) | 	_options.filecnt = argc - 1; | ||||||
| 		return; | 
 | ||||||
| 
 | 	while ((opt = getopt(argc, argv, "hv")) != -1) { | ||||||
| 	img_load(&app->img, app->filenames[app->fileidx]); | 		switch (opt) { | ||||||
| 
 | 			case '?': | ||||||
| 	img_render(&app->img, &app->win); | 				return -1; | ||||||
|  | 			case 'h': | ||||||
|  | 				print_usage(); | ||||||
|  | 				exit(0); | ||||||
|  | 			case 'v': | ||||||
|  | 				print_version(); | ||||||
|  | 				exit(0); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return 0; | ||||||
| } | } | ||||||
| @ -1,4 +1,4 @@ | |||||||
| /* sxiv: events.h
 | /* sxiv: options.h
 | ||||||
|  * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |  * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | ||||||
|  * |  * | ||||||
|  * This program is free software; you can redistribute it and/or modify |  * This program is free software; you can redistribute it and/or modify | ||||||
| @ -16,11 +16,19 @@ | |||||||
|  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #ifndef EVENTS_H | #ifndef OPTIONS_H | ||||||
| #define EVENTS_H | #define OPTIONS_H | ||||||
| 
 | 
 | ||||||
| #include "app.h" | typedef struct options_s { | ||||||
|  | 	const char **filenames; | ||||||
|  | 	unsigned int filecnt; | ||||||
|  | } options_t; | ||||||
| 
 | 
 | ||||||
| void event_loop(app_t*); | extern const options_t *options; | ||||||
| 
 | 
 | ||||||
| #endif /* EVENTS_H */ | void print_usage(); | ||||||
|  | void print_version(); | ||||||
|  | 
 | ||||||
|  | int parse_options(int, char**); | ||||||
|  | 
 | ||||||
|  | #endif /* OPTIONS_H */ | ||||||
							
								
								
									
										2
									
								
								sxiv.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								sxiv.h
									
									
									
									
									
								
							| @ -21,7 +21,7 @@ | |||||||
| 
 | 
 | ||||||
| #include "config.h" | #include "config.h" | ||||||
| 
 | 
 | ||||||
| #define VERSION "git-20110117" | #define VERSION "git-20110119" | ||||||
| 
 | 
 | ||||||
| #define MIN(a,b) ((a) < (b) ? (a) : (b)) | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | ||||||
| #define MAX(a,b) ((a) > (b) ? (a) : (b)) | #define MAX(a,b) ((a) > (b) ? (a) : (b)) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Bert
						Bert