Anonymous avatar Anonymous committed 6b25c2f

refactoring: for a candy code :)

- display code sections
- add somme comments
- put a message error to stderr instead of stdout
- move vt_open

Some stuff still remains :P

Comments (0)

Files changed (1)

 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */
 
+/* INCLUDES
+ * ======== */
 #include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE, getenv, abs */
 #include <unistd.h> /* fork, setsid */
 #include <stdio.h> /* printf, fprintf, stderr, stdout, fflush */
 #include "cairo.h"
 #include "poppler-clib.h"
 
+/* DEFINES
+ * ======= */
 #define VTNAME "/dev/tty%d"
 #define VTNO 0
 #define MAX_CHAR_LEN 512
 #define VTACQUIRED 1
 #define VTRELEASED 0
 
+/* STRUCTS
+ * ======= */
 struct option_panel{
     unsigned char visible;
     int height;
     struct poppler_clib_view view;
 };
 
+/* GLOBALS
+ * ======= */
 static int vtfd;
 static struct fb_screen fbscreen;
 static struct page_info page;
 static struct option_panel panel;
 int vtstatus = VTRELEASED;
 
-
+/* RENDERING
+ * ========= */
 static inline void update_page_boundaries(struct page_info* page,
                                           struct fb_screen* fbscreen)
 {
     cairo_restore(ctx);
 }
 
+/* Open a virtual terminal (file descriptor set in ``fd`` argument) and get
+ * state (state set in ``vtstate`` argument). 
+ * Close virtual terminal file descriptor at any exception raised.
+ */
+static int vt_open(char *vtname, struct vt_stat *vtstat, int *fd){
+     *fd = open(vtname, O_RDWR);
+     if ((*fd) < 0) {
+         fprintf(stderr, "Failed to open '%s'\n", vtname);
+         close(*fd);
+         return -1;
+     }
+     if (ioctl(*fd, VT_GETSTATE, &vtstat) < 0){
+         fprintf(stderr, "Could not get state of '%s'", vtname);
+         close(*fd);
+         return -1;
+     }
+     return 0;
+ }
+
+/* TERMINAL SWITCHING HANDLING
+ * Leaving: set console mode to KD_TEXT to re-enable text features (pushing a
+ *          keybord key let the corresponding key to appear on the screen ...)
+ * acquiring: set console mode to KD_GRAPHICS attaching the frame buffer which
+ *            permits to "draw" on the screen. Then re-render the last page with
+ *            last settings (zoom, position ...) to force pdf to be visible.
+ * The only way a found to differenciate leaving/acquiring is to set a sentinel
+ * (``vtstatus``). I'm not sure that is robust.
+ *
+ * */
 static void fb_switch_sig(int signal)
 {
     if (signal == SIGUSR1) { /* RELEASE */
     fprintf(stderr, "Usage: %s [-t tty] [-c vtnumber] [-p pagenumber] document\n", progname);
 }
 
-static int vt_open(char *vtname, struct vt_stat *vtstat, int *fd){
-     *fd = open(vtname, O_RDWR);
-     if ((*fd) < 0) {
-         fprintf(stderr, "Failed to open '%s'\n", vtname);
-         close(*fd);
-         return -1;
-     }
-     if (ioctl(*fd, VT_GETSTATE, &vtstat) < 0){
-         fprintf(stderr, "Could not get state of '%s'", vtname);
-         close(*fd);
-         return -1;
-     }
-     return 0;
- }
-
+/* MAIN
+ * ==== */
 int main(int argc, char* const argv [])
 {
     int opt, orig_vtfd, vtno=-1, status, page_idx, ret;
          sprintf(current_vtname, VTNAME, VTNO);
          fprintf(stdout, ", so use '%s' instead. \n", current_vtname);
          if ( vt_open(current_vtname, &vtstat, &orig_vtfd)<0 ){
-             fprintf(stdout, "... Terminate the process.");
-             fprintf(stdout, "Use the '-t' option to manually specify one.");
+             fprintf(stderr, "... Terminate the process.");
+             fprintf(stderr, "Use the '-t' option to manually specify one.");
              return EXIT_FAILURE;
          }
     }
     if (vtno == -1) {
-        /* vtno = vtstat.v_active; */
         ret = ioctl( orig_vtfd, VT_OPENQRY, &vtno );
         if ( (ret<0) || (vtno == -1) ){
             fprintf(stderr, "Unable to query a new terminal.");
             return EXIT_FAILURE;
         }
     }
+    /* Explicitly and imeediatly say which terminal is used for rendering. */
     fprintf(stdout, "Use terminal '#%i' for graphics.\n", vtno);
     fflush(stdout);
+    /* use a fork to render the pdf. */
     if (fork() == 0) {
         read_document(argv[optind], vtno, page_idx);
     }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.