Procházet zdrojové kódy

Option handling, merged app.c & events.c into main.c

master
Bert před 14 roky
rodič
revize
a7e30bb081
8 změnil soubory, kde provedl 143 přidání a 163 odebrání
  1. +0
    -39
      app.h
  2. +0
    -72
      events.c
  3. +9
    -2
      image.c
  4. +2
    -2
      image.h
  5. +83
    -10
      main.c
  6. +34
    -31
      options.c
  7. +14
    -6
      options.h
  8. +1
    -1
      sxiv.h

+ 0
- 39
app.h Zobrazit soubor

@@ -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 */

+ 0
- 72
events.c Zobrazit soubor

@@ -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) {
}

+ 9
- 2
image.c Zobrazit soubor

@@ -19,6 +19,8 @@
#include <stdlib.h>
#include <stdio.h>

#include <Imlib2.h>

#include "sxiv.h"
#include "image.h"

@@ -32,7 +34,12 @@ void imlib_init(win_t *win) {
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)
return;

@@ -53,7 +60,7 @@ void img_render(img_t *img, win_t *win) {
unsigned int sx, sy, sw, sh;
unsigned int dx, dy, dw, dh;

if (!img || !win)
if (!img || !win || !imlib_context_get_image())
return;

/* set zoom level to fit image into window */


+ 2
- 2
image.h Zobrazit soubor

@@ -36,13 +36,13 @@ typedef struct img_s {
int h;
int x;
int y;

Imlib_Image *im;
} img_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*);

#endif /* IMAGE_H */

+ 83
- 10
main.c Zobrazit soubor

@@ -18,20 +18,63 @@

#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/keysym.h>

#include "sxiv.h"
#include "app.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;

app_t app;
while (!XNextEvent(win.env.dpy, &ev)) {
if (handler[ev.type])
handler[ev.type](&ev);
}
}

int main(int argc, char **argv) {
if (parse_options(argc, argv) < 0)
return 1;

if (!options->filecnt) {
print_usage();
exit(1);
}

fileidx = 0;

img.zoom = 1.0;
img.scalemode = SCALE_MODE;

win.w = WIN_WIDTH;
win.h = WIN_HEIGHT;

// TODO: parse cmd line arguments properly
app.filenames = argv + 1;
app.filecnt = argc - 1;
win_open(&win);
imlib_init(&win);

app_init(&app);
app_run(&app);
app_quit(&app);
img_load(&img, options->filenames[fileidx]);
img_render(&img, &win);

run();

cleanup();

return 0;
}
@@ -39,6 +82,36 @@ int main(int argc, char **argv) {
void cleanup() {
static int in = 0;

if (!in++)
app_quit(&app);
if (!in++) {
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) {
}

app.c → options.c Zobrazit soubor

@@ -1,4 +1,4 @@
/* sxiv: app.c
/* sxiv: options.c
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* 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.
*/

#include <X11/Xlib.h>
#define _XOPEN_SOURCE

#include "sxiv.h"
#include "app.h"
#include "events.h"

void app_init(app_t *app) {
if (!app)
return;

app->fileidx = 0;

app->img.zoom = 1.0;
app->img.scalemode = SCALE_MODE;
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

app->win.w = WIN_WIDTH;
app->win.h = WIN_HEIGHT;
#include "sxiv.h"
#include "options.h"

win_open(&app->win);
imlib_init(&app->win);
}
options_t _options;
const options_t *options = (const options_t*) &_options;

void app_run(app_t *app) {
app_load_image(app);
event_loop(app);
void print_usage() {
printf("usage: sxiv [-hv] FILES...\n");
}

void app_quit(app_t *app) {
void print_version() {
printf("sxiv - simple x image viewer\n");
printf("Version %s, written by Bert Muennich\n", VERSION);
}

void app_load_image(app_t *app) {
if (!app || app->fileidx >= app->filecnt || !app->filenames)
return;

img_load(&app->img, app->filenames[app->fileidx]);

img_render(&app->img, &app->win);
int parse_options(int argc, char **argv) {
int opt;

_options.filenames = (const char**) argv + 1;
_options.filecnt = argc - 1;

while ((opt = getopt(argc, argv, "hv")) != -1) {
switch (opt) {
case '?':
return -1;
case 'h':
print_usage();
exit(0);
case 'v':
print_version();
exit(0);
}
}

return 0;
}

events.h → options.h Zobrazit soubor

@@ -1,4 +1,4 @@
/* sxiv: events.h
/* sxiv: options.h
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* 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.
*/

#ifndef EVENTS_H
#define EVENTS_H
#ifndef OPTIONS_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 */

+ 1
- 1
sxiv.h Zobrazit soubor

@@ -21,7 +21,7 @@

#include "config.h"

#define VERSION "git-20110117"
#define VERSION "git-20110119"

#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))


Načítá se…
Zrušit
Uložit