[c]configurazione programma

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Avatar utente
cristian_c
Accecante Asceta
Accecante Asceta
Messaggi: 23422
Iscrizione: lunedì 29 ottobre 2007, 11:31

[c]configurazione programma

Messaggio da cristian_c »

ciao,
il mio compito e' fare in modo che il programma inkblot possa essere configurato per diverse configurazioni,a seconda del dispositivo utilizzato.
Per far cio,dovrei ampliare il database esistente,agente sui sorgenti,perche' da quanto ho saputo sembra che si possa configurare il programma solo da sorgenti.
La lista dei file presenti nella cartella src e' questa:

Codice: Seleziona tutto

detect.c       eggtrayicon.h  inkblot.h  Makefile     menu.c
detect.o       eggtrayicon.o  main.c     Makefile.am  menu.o
eggtrayicon.c  inkblot        main.o     Makefile.in
Allora ho deciso di concentrarmi sul file main.c e da li impostare il lavoro.Ecco il contenuto del file:

Codice: Seleziona tutto

/*
 * Copyright (C) 2004 Mike Newman <mikegtn@gnome.org>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gnome.h>
#include <inklevel.h>
#include <glade/glade-xml.h>
#include <gconf/gconf-client.h>
#include "inkblot.h"
#include "eggtrayicon.h"

GtkWidget	*icon_box;
GtkWidget	*icon;
GtkWidget	*dialog;
GtkWidget	*icon_pix;
GtkTooltips	*tooltips = NULL;
gint		visible;
gint		pref_bus;
gint		pref_port;
InkblotStatus	state;
static void	inkblot_assign_tooltip (InkblotStatus);

gint inkblot_check_prefs ( void ) {

  GConfClient *client = gconf_client_get_default ();
  if (! gconf_client_get_int (client, INKBLOT_CONFIG_KEY, NULL)) {
    inkblot_scan_devices();
  }
  pref_bus = gconf_client_get_int (client, INKBLOT_BUS_KEY, NULL);
  pref_port = gconf_client_get_int (client, INKBLOT_PORT_KEY, NULL);
  return 0;
}

int main (int argc, char *argv[]) {

  GnomeClient *client;
  GdkPixbuf *pixbuf = NULL;
#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gnome_program_init (PACKAGE, VERSION, LIBGNOMEUI_MODULE,
		      argc, argv,
		      GNOME_PARAM_APP_DATADIR, PACKAGE_DATA_DIR,
		      NULL);
  client = gnome_master_client ();
  tooltips = gtk_tooltips_new();
  gtk_tooltips_enable(tooltips);
  inkblot_check_prefs();
  g_signal_connect (client, "save-yourself", 
		    G_CALLBACK(inkblot_save_session), (gpointer) argv[0]);
  icon = GTK_WIDGET(egg_tray_icon_new ("inkblot tray icon"));
  icon_box = gtk_event_box_new ();
  state = inkblot_get_overall_status();
  g_signal_connect (G_OBJECT(icon_box), "button_press_event", 
		    G_CALLBACK(inkblot_tray_icon_clicked), NULL);
  gtk_container_add (GTK_CONTAINER(icon), icon_box);
  pixbuf = inkblot_tray_icon_display(state);
  icon_pix = gtk_image_new_from_pixbuf(pixbuf);
  gdk_pixbuf_unref(pixbuf);
  gtk_container_add(GTK_CONTAINER(icon_box),icon_pix); 
  gtk_widget_show_all(icon);
  timer_id = g_timeout_add(3000,inkblot_check_status_event,NULL);
  gtk_main ();
  return 0;
}

gboolean inkblot_check_status_event ( gpointer data ) {
  GdkPixbuf *buf = NULL;
  gint oldstate = state;
  state = inkblot_get_overall_status();
  if (state != oldstate) {
    buf = inkblot_tray_icon_display(state);
    gtk_image_set_from_pixbuf(GTK_IMAGE(icon_pix),buf);
    gdk_pixbuf_unref(buf);
  }
  return TRUE;
}

void inkblot_assign_tooltip (InkblotStatus state) {
  gchar *tip;
  switch (state) {
  case INKBLOT_OK:
    tip = g_strdup (_("Ink level: high"));
    break;
  case INKBLOT_WARN:
    tip = g_strdup (_("Ink level: medium"));
    break;
  case INKBLOT_LOW:
    tip = g_strdup (_("Ink level: low"));
    break;
  case INKBLOT_EMPTY:
    tip = g_strdup (_("Printer off or ink empty"));
    break;
  default:
    tip = g_strdup (_("Error: problem with printer"));
  }
  gtk_tooltips_set_tip(tooltips, GTK_WIDGET(icon_box), tip, NULL);
  g_free(tip);
}
	
GdkPixbuf * inkblot_tray_icon_display ( InkblotStatus state ) {
  GdkPixbuf *pixbuf;
  GError *error = NULL;
  gchar *pixbuf_file = NULL;
  switch (state) {
  case INKBLOT_OK:
    pixbuf_file = g_strdup(PACKAGE_DATA_DIR
			   "/inkblot/pixmaps/printer_green_16.png");
    break;
  case INKBLOT_WARN:
    pixbuf_file = g_strdup(PACKAGE_DATA_DIR
			   "/inkblot/pixmaps/printer_amber_16.png");
    break;
  case INKBLOT_LOW:
    pixbuf_file = g_strdup(PACKAGE_DATA_DIR
			   "/inkblot/pixmaps/printer_red_16.png");
    break;
  case INKBLOT_EMPTY:
    pixbuf_file = g_strdup(PACKAGE_DATA_DIR
			   "/inkblot/pixmaps/printer_black_16.png");
    break;
  default:
    pixbuf_file = g_strdup(PACKAGE_DATA_DIR
			   "/inkblot/pixmaps/printer_error_16.png");
  }
  pixbuf = gdk_pixbuf_new_from_file (pixbuf_file,&error);
  inkblot_assign_tooltip(state);
  g_free (pixbuf_file);
  return pixbuf;
};

void inkblot_tray_icon_clicked ( GtkWidget *event_box, GdkEventButton *event, gpointer data ) {

  if (event->type != GDK_BUTTON_PRESS) {
    return;
  }

  if (event->button == 1 && state != INKBLOT_ERROR) {
    inkblot_toggle_dialog();
  }

  if (event->button == 3) {
    inkblot_tray_menu (event_box, event, data);
  }
}

void inkblot_toggle_dialog ( void ) {
  GladeXML *glade;
  struct ink_level level;
  gchar *black=NULL;
  gchar *color=NULL;
  gchar *cyan=NULL;
  gchar *magenta=NULL;
  gchar *yellow=NULL;
  GtkWidget *b_label;
  GtkWidget *c_label;
  GtkWidget *cy_label;
  GtkWidget *mg_label;
  GtkWidget *yl_label;
  GtkWidget *b_prog;
  GtkWidget *c_prog;
  GtkWidget *cy_prog;
  GtkWidget *mg_prog;
  GtkWidget *yl_prog;
  GtkWidget *expander;
  int i, colorLevel;

  if (visible) {
    inkblot_dialog_response (dialog, -1, NULL);
    return;
  }

  glade = glade_xml_new (PACKAGE_DATA_DIR 
			 "/inkblot/inkblot.glade", NULL, NULL);

  if (glade==NULL) {
    inkblot_error_dialog (INKBLOT_ERROR_GLADE);
    exit (1);
  }

  dialog = glade_xml_get_widget (glade,"inkblot_dialog");
  expander = glade_xml_get_widget (glade,"expander");
  b_label = glade_xml_get_widget (glade,"black_label");
  c_label = glade_xml_get_widget (glade,"color_label");
  cy_label = glade_xml_get_widget (glade,"cy_label");
  mg_label = glade_xml_get_widget (glade,"mg_label");
  yl_label = glade_xml_get_widget (glade,"yl_label");
  b_prog = glade_xml_get_widget (glade,"black_progress");
  c_prog = glade_xml_get_widget (glade,"color_progress");
  cy_prog = glade_xml_get_widget (glade,"cy_prog");
  mg_prog = glade_xml_get_widget (glade,"mg_prog");
  yl_prog = glade_xml_get_widget (glade,"yl_prog");
  get_ink_level (pref_bus, "",pref_port, &level);

  /* First check if the ink level request succeeded */
  if(level.status != RESPONSE_VALID) {
    inkblot_error_dialog ( INKBLOT_ERROR_INVALID_RESPONSE );
    exit(1);
    }

    /* Parse all present cartridges and update labels consequently */
    i=0;
    while( (i<MAX_CARTRIDGE_TYPES) &&
	   (level.levels[i][INDEX_TYPE] != CARTRIDGE_NOT_PRESENT)) {
      switch(level.levels[i][INDEX_TYPE])
	{
	case CARTRIDGE_BLACK:
	case CARTRIDGE_PHOTO: /* Photo seems to be a gray cartridge */
	  black = g_strdup_printf("%d%%",level.levels[i][INDEX_LEVEL]);
	  gtk_label_set_text(GTK_LABEL(b_label),black);
	  gtk_progress_set_value(GTK_PROGRESS(b_prog),
				 level.levels[i][INDEX_LEVEL]);
	  break;
	case CARTRIDGE_COLOR:
	  color = g_strdup_printf("%d%%",level.levels[i][INDEX_LEVEL]);
	  gtk_label_set_text(GTK_LABEL(c_label),color);
	  gtk_progress_set_value(GTK_PROGRESS(c_prog),
				 level.levels[i][INDEX_LEVEL]);
	  break;
	case CARTRIDGE_CYAN:
	case CARTRIDGE_PHOTOCYAN: /* simplification, but a printer can
				     have CYAN and PHOTOCYAN... */
	  cyan = g_strdup_printf("%d%%",level.levels[i][INDEX_LEVEL]);
	  gtk_label_set_text(GTK_LABEL(cy_label),cyan);
	  gtk_progress_set_value(GTK_PROGRESS(cy_prog),
				 level.levels[i][INDEX_LEVEL]);
	  break;
	case CARTRIDGE_MAGENTA:
	case CARTRIDGE_PHOTOMAGENTA:
	  magenta = g_strdup_printf("%d%%",level.levels[i][INDEX_LEVEL]);
	  gtk_label_set_text(GTK_LABEL(mg_label),magenta);
	  gtk_progress_set_value(GTK_PROGRESS(mg_prog),
				 level.levels[i][INDEX_LEVEL]);
	  break;
	case CARTRIDGE_YELLOW:
	case CARTRIDGE_PHOTOYELLOW:
	  yellow = g_strdup_printf("%d%%",level.levels[i][INDEX_LEVEL]);
	  gtk_label_set_text(GTK_LABEL(yl_label),yellow);
	  gtk_progress_set_value(GTK_PROGRESS(yl_prog),
				 level.levels[i][INDEX_LEVEL]);
	  break;
	case CARTRIDGE_RED:
	case CARTRIDGE_GREEN:
	  /* FIXME: not yet supported */
	  break;
	}
      i++;
    }
    /* if no colour level reported, but individual reservoirs
     * available, report an average */
    if (color == NULL) {
      colorLevel = (gtk_progress_get_value(GTK_PROGRESS(cy_prog)) +
		    gtk_progress_get_value(GTK_PROGRESS(mg_prog)) +
		    gtk_progress_get_value(GTK_PROGRESS(yl_prog))  )/3;
      color = g_strdup_printf("%d%%",colorLevel);
      gtk_label_set_text(GTK_LABEL(c_label),color);
      gtk_progress_set_value(GTK_PROGRESS(c_prog), colorLevel);
    }
  gtk_window_set_title(GTK_WINDOW(dialog),level.model);
  if ( (cyan != NULL) || (magenta != NULL) || (yellow != NULL)) {
    gtk_widget_set_sensitive(expander,TRUE);
  } else {
    gtk_widget_set_sensitive(expander,FALSE);
  }
  gtk_widget_show_all(dialog);
  g_signal_connect(G_OBJECT(dialog),"response",
		   G_CALLBACK(inkblot_dialog_response),NULL);
  visible = 1;
  gtk_dialog_run(GTK_DIALOG(dialog));
}

void inkblot_dialog_response ( GtkWidget *widget, gint response, gpointer data) {
  gtk_widget_destroy(widget);
  visible = 0;
}

InkblotStatus inkblot_get_overall_status ( void ) {

  struct ink_level level;
  gint color=0;
  gint black=0;
  gint cyan=0;
  gint magenta=0;
  gint yellow=0;
  gint total=0;
  gint result=0;
  int i;

  result = get_ink_level (pref_bus, "",pref_port, &level);
  if ((result < 0) || (level.status == RESPONSE_INVALID)) return INKBLOT_ERROR;

  /* Parse all present cartridges and update labels consequently */
  i=0;
  while( (i<MAX_CARTRIDGE_TYPES) &&
	 (level.levels[i][INDEX_TYPE] != CARTRIDGE_NOT_PRESENT)) {
    switch(level.levels[i][INDEX_TYPE])
      {
      case CARTRIDGE_BLACK:
      case CARTRIDGE_PHOTO: /* Photo seems to be a gray cartridge */
	black = level.levels[i][INDEX_LEVEL];
	break;
      case CARTRIDGE_COLOR:
	color = level.levels[i][INDEX_LEVEL];
	break;
      case CARTRIDGE_CYAN:
      case CARTRIDGE_PHOTOCYAN: /* simplification, but a printer can
				   have CYAN and PHOTOCYAN... */
	cyan = level.levels[i][INDEX_LEVEL];
	break;
      case CARTRIDGE_MAGENTA:
      case CARTRIDGE_PHOTOMAGENTA:
	magenta = level.levels[i][INDEX_LEVEL];
	break;
      case CARTRIDGE_YELLOW:
      case CARTRIDGE_PHOTOYELLOW:
	yellow = level.levels[i][INDEX_LEVEL];
	break;
      case CARTRIDGE_RED:
      case CARTRIDGE_GREEN:
	/* FIXME: not yet supported */
	break;
      }
    i++;
  }
  /* if no colour level reported, but individual reservoirs
   * available, report an average */
  if (color == 0 && (cyan || magenta || yellow)) {
    color = ( cyan + magenta + yellow ) / 3;
  }
  total = (color+black)/2;
  if (total>66) return INKBLOT_OK;
  if (total>33) return INKBLOT_WARN;
  if (total>0) return INKBLOT_LOW;
  if (total==0) return INKBLOT_EMPTY;
  return INKBLOT_ERROR;
}

static int inkblot_save_session(GnomeClient        *client,
				gint                phase,
				GnomeRestartStyle   save_style,
				gint                shutdown,
				GnomeInteractStyle  interact_style,
				gint                fast,
				gpointer            client_data)  {
                                                                                
  gchar *argv[]= { NULL };
  argv[0] = (gchar*) client_data;
  gnome_client_set_clone_command (client, 1, argv);
  gnome_client_set_restart_command (client, 1, argv);
  return TRUE;
}                                                                               

gint inkblot_error_dialog ( InkblotError err ) {
  GtkWidget       *dialog;
  gchar           *message;
  gint            buttons;
  gint            type;
  gint            response;

  switch (err) {
  case INKBLOT_ERROR_NO_CONFIG:
    message = g_strdup (_("No supported printers found.\n\n"
			  "Please check your printer and cables "
			  "and restart Inkblot."));
    buttons = GTK_BUTTONS_CLOSE;
    type = GTK_MESSAGE_WARNING;
    break;
  case INKBLOT_ERROR_NO_PRINTER:
    message = g_strdup (_("No supported printers found.\n\n"
			  "Please check your printer and cables "
			  "and scan for printers again."));
    buttons = GTK_BUTTONS_CLOSE;
    type = GTK_MESSAGE_WARNING;
    break;
  case INKBLOT_ERROR_GLADE:
    message = g_strdup (_("Inkblot was not installed correctly\n"
			  "and the inkblot.glade file could not "
			  "be found."));
    type = GTK_MESSAGE_ERROR;
    buttons = GTK_BUTTONS_CLOSE;
    break;
  case INKBLOT_ERROR_INVALID_RESPONSE:
    message = g_strdup (_("Inkblot got an invalid response from "
			  "the printer.\n\n"
			  "Please check your printer and check the "
			  "libinklevel homepage if your printer is "
			  "supported."));
    type = GTK_MESSAGE_ERROR;
    buttons = GTK_BUTTONS_CLOSE;
  default:
    message = g_strdup (_("An unexpected error occurred."));
    buttons = GTK_BUTTONS_CLOSE;
    type = GTK_MESSAGE_ERROR;
  }
	
  dialog = gtk_message_dialog_new (NULL, 0, type,
				   buttons,"<b>%s</b>\n\n%s",
				   _("Inkblot Error"), message);
  gtk_label_set_use_markup 
    (GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label),TRUE);
  response = gtk_dialog_run (GTK_DIALOG(dialog));
  g_free(message);

  if (response != GTK_RESPONSE_OK) {
    gtk_widget_destroy (dialog);
  }

  return 0;

}
Voi che siete piu' esperti di me,da dove partireste?
UP! Unreal Project: il futuro inizia qui. Crusade è tra noi
È scienza!
Avatar utente
cristian_c
Accecante Asceta
Accecante Asceta
Messaggi: 23422
Iscrizione: lunedì 29 ottobre 2007, 11:31

Re: [c]configurazione programma

Messaggio da cristian_c »

ciao,
cercavo la definizione della funzione get_ink_level e dopo lunghe ricerche ho trovato la dichiarazione della funzione in un file esterno ai sorgenti del programma.
Infatti ho trovato questa direttiva al preprocessore nei file main.c e detect.c:

Codice: Seleziona tutto

#include <inklevel.h>
Il file si chiama inklevel.h e si trova in /usr/include :

Codice: Seleziona tutto

/* inklevel.h
 *
 * (c) 2003, 2004, 2005, 2006, 2007 Markus Heinz
 *
 * This software is licensed under the terms of the GPL.
 * For details see file COPYING.
 */

#ifndef INKLEVEL_H
#define INKLEVEL_H

//#define DEBUG 1

#ifdef DEBUG
#include <stdio.h> /* for the debug printfs */
#endif

/* public interface */

/* Values for port */

#define PARPORT 1
#define USB 2
#define CUSTOM_PARPORT 3
#define CUSTOM_USB 4

/* Possible return values for get_ink_level() */

#define OK 0
#define ERROR -1
#define DEV_PARPORT_INACCESSIBLE -2
#define DEV_LP_INACCESSIBLE -3
#define COULD_NOT_GET_DEVICE_ID -4
#define DEV_USB_LP_INACCESSIBLE -5
#define UNKNOWN_PORT_SPECIFIED -6
#define NO_PRINTER_FOUND -7
#define NO_DEVICE_CLASS_FOUND -8
#define NO_CMD_TAG_FOUND -9
#define PRINTER_NOT_SUPPORTED -10
#define NO_INK_LEVEL_FOUND -11
#define COULD_NOT_WRITE_TO_PRINTER -12
#define COULD_NOT_READ_FROM_PRINTER -13
#define COULD_NOT_PARSE_RESPONSE_FROM_PRINTER -14
#define COULD_NOT_GET_CREDIT -15
#define DEV_CUSTOM_USB_INACCESSIBLE -16

#define MODEL_NAME_LENGTH 100
#define MAX_CARTRIDGE_TYPES 40

/* Values for ink_level.status */

#define RESPONSE_INVALID 0
#define RESPONSE_VALID 1

/* Values for array index 0 defining the cartridge type */

#define CARTRIDGE_NOT_PRESENT 0
#define CARTRIDGE_BLACK 1
#define CARTRIDGE_COLOR 2
#define CARTRIDGE_PHOTO 3
#define CARTRIDGE_CYAN 4
#define CARTRIDGE_MAGENTA 5
#define CARTRIDGE_YELLOW 6
#define CARTRIDGE_PHOTOBLACK 7
#define CARTRIDGE_PHOTOCYAN 8
#define CARTRIDGE_PHOTOMAGENTA 9
#define CARTRIDGE_PHOTOYELLOW 10
#define CARTRIDGE_RED 11
#define CARTRIDGE_GREEN 12
#define CARTRIDGE_BLUE 13
#define CARTRIDGE_LIGHTBLACK 14
#define CARTRIDGE_LIGHTCYAN 15
#define CARTRIDGE_LIGHTMAGENTA 16
#define CARTRIDGE_LIGHTLIGHTBLACK 17
#define CARTRIDGE_MATTEBLACK 18
#define CARTRIDGE_GLOSSOPTIMIZER 19
#define CARTRIDGE_UNKNOWN 20
#define CARTRIDGE_KCM 21
#define CARTRIDGE_GGK 22
#define CARTRIDGE_KCMY 23
#define CARTRIDGE_LCLM 24
#define CARTRIDGE_YM 25
#define CARTRIDGE_CK 26
#define CARTRIDGE_LGPK 27
#define CARTRIDGE_LG 28
#define CARTRIDGE_G 29
#define CARTRIDGE_PG 30
#define CARTRIDGE_WHITE 31

/* Array indices for ink_level.levels */

#define INDEX_TYPE 0
#define INDEX_LEVEL 1

struct ink_level {
  char model[MODEL_NAME_LENGTH];
  unsigned short status;
  unsigned short levels[MAX_CARTRIDGE_TYPES][2];
};

int get_ink_level(const int port, const char*device_file, 
                  const int portnumber, struct ink_level *level);
char *get_version_string(void);

/* internal */

#define BUFLEN 1024
#define NR_TAGS 15

#define OLD 1
#define NEW 2

#define LIBINKLEVEL_VERSION_STRING "libinklevel v0.7.2"

#endif
In particolare estrapolo le righe da 95 a 102:

Codice: Seleziona tutto

struct ink_level {
  char model[MODEL_NAME_LENGTH];
  unsigned short status;
  unsigned short levels[MAX_CARTRIDGE_TYPES][2];
};

int get_ink_level(const int port, const char*device_file, 
                  const int portnumber, struct ink_level *level);
viene passata una struttura come argomento della funzione get_ink_level, che si chiama ink_level.
Ma c'e' un problema: qui c'e' la dichiarazione della funzione non la sua definizione.
Quando viene chiamata nei file main.c e detect.c(di cui riporto il contenuto più sotto) quali istruzioni esegue il programma se non e' noto il contenuto della funzione?   ??? ::)

Questo e' il file detect.c:

Codice: Seleziona tutto

/*
 * Copyright (C) 2004 Mike Newman <mikegtn@gnome.org> 
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include <inklevel.h>
#include <gconf/gconf-client.h>
#include <glib.h>
#include "inkblot.h"

GtkWidget	*conf_dialog;

gint inkblot_scan_devices ( void ) {

	GConfClient	*client = gconf_client_get_default ();
	GList		*printers = NULL;
        gchar           *message, *bus_d, *port_d;
        gint            buttons;
        gint            type;
        gint            response;
	gint 		bus = 0, port = 0, result, num_printers = 0;
	struct 		ink_level device;
	InkblotPrinter 	*pref_device;

	if (conf_dialog != NULL) return;

	for (bus=0; bus <3; bus++) {
		for (port=0; port<17; port++) {
			result = get_ink_level (bus, "", port, &device);
			if (result == OK) {
				InkblotPrinter *dev = g_new0(InkblotPrinter,1);
				dev->model = g_strdup(device.model);
				dev->bus = bus;
				dev->port = port;
				printers = g_list_prepend (printers, dev);
				num_printers++;
			} else {
				printf("Result was %d\n", result);
			}
		}
	}
	if (num_printers < 1) {
		if (! gconf_client_get_int (client,INKBLOT_CONFIG_KEY, NULL)) {
			inkblot_error_dialog (INKBLOT_ERROR_NO_CONFIG);
			exit (1);
		}
		inkblot_error_dialog (INKBLOT_ERROR_NO_PRINTER);
		return;
	}

	/* FIXME: temp restrict to first printer. need a UI for selection */
	num_printers = 1;

	if (num_printers > 1) {
				g_print(_("Choose Printer Not Implemented"));
	} else {
		pref_device = g_list_first(printers)->data;
	}

	gconf_client_set_int (client, INKBLOT_BUS_KEY,pref_device->bus,NULL);
	gconf_client_set_int (client, INKBLOT_CONFIG_KEY,1,NULL);

	if (pref_device->bus == 1) {
		bus_d = g_strdup (_("parallel port"));
	} else {
		bus_d = g_strdup (_("USB"));
	}
	port_d = g_strdup_printf ("lp%d",pref_device->port);
	message = g_strdup_printf (_("Found <i>%s</i> %s printer on %s\n\n"
				   "Inkblot will report the ink level on "
				   "this device."),
				   pref_device->model,bus_d,port_d);
	g_free(port_d);
	g_free(bus_d);
	g_free(pref_device);
	conf_dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_INFO,
                                         GTK_BUTTONS_OK,
					 "<b><big>%s</big></b>\n\n%s",
                                         _("Printer Detection Complete"), message);
        gtk_label_set_use_markup 
                       (GTK_LABEL(GTK_MESSAGE_DIALOG(conf_dialog)->label),TRUE);
	gtk_window_set_title (GTK_WINDOW(conf_dialog),_("Inkblot Setup"));
        response = gtk_dialog_run (GTK_DIALOG(conf_dialog));
	gtk_widget_destroy (conf_dialog);
        g_free(message);
	conf_dialog = NULL;
}

Altra questione:

io devo svolgere le seguenti operazioni:
- impostare tutte le define per la piedinatura del processore che uso
- impostare i vari input/output
- impostare i registri di configurazione per l'avvio corretto del processore
- devo procurarmi il datasheet del processore

Qualche consiglio?  ???
Ultima modifica di cristian_c il lunedì 15 settembre 2008, 12:05, modificato 1 volta in totale.
UP! Unreal Project: il futuro inizia qui. Crusade è tra noi
È scienza!
Avatar utente
cristian_c
Accecante Asceta
Accecante Asceta
Messaggi: 23422
Iscrizione: lunedì 29 ottobre 2007, 11:31

Re: [c]configurazione programma

Messaggio da cristian_c »

il file inklevel.h contiene i prototipi delle funzioni.
Le funzioni sono definite in inklevel.c ed e' il compilatore che dice ai sorgenti di andare a trovare le definizioni li'.

Ma ho fatto una ricerca nel filesystem e non trovo il file inklevel.c.
Come mai?Dove lo posso trovare?  ::)
UP! Unreal Project: il futuro inizia qui. Crusade è tra noi
È scienza!
Avatar utente
cristian_c
Accecante Asceta
Accecante Asceta
Messaggi: 23422
Iscrizione: lunedì 29 ottobre 2007, 11:31

Re: [c]configurazione programma

Messaggio da cristian_c »

dopo varie peripezie ed installazioni di pacchetti che sarebbe complicato ricostruire ho finalmente in mano il file sorgente inklevel.c.
Lo posto:

Codice: Seleziona tutto

/* libinklevel.c
 *
 * (c) 2003, 2004, 2005, 2006, 2007 Markus Heinz
 *
 * This software is licensed under the terms of the GPL.
 * For details see file COPYING.
 */

#include "inklevel.h"

#include <string.h>

extern int get_device_id(const int port, const char *devive_file, 
                         const int portnumber, char *device_id);
extern int parse_device_id_new_hp(char tags[NR_TAGS][BUFLEN], int n, 
				  struct ink_level *level);
extern int parse_device_id_old_hp(char tags[NR_TAGS][BUFLEN], int n, 
				  struct ink_level *level);
extern int get_ink_level_epson(const int port, const char *device_file, 
                               const int portnumber, 
                               struct ink_level *level);
extern int get_ink_level_canon(const int port, const char* device_file, 
                               const int portnumber, 
                               struct ink_level *level);

int get_ink_level(const int port, const char *device_file, 
                  const int portnumber, struct ink_level *level);
char *get_version_string(void);

static int parse_device_id(const int port, const char *device_file, 
                           const int portnumber, 
                           const char *device_id, struct ink_level *level);

int get_ink_level(const int port, const char *device_file, 
                  const int portnumber, struct ink_level *level) {
  char device_id[BUFLEN];
  int ret;

  memset(level->model, 0, MODEL_NAME_LENGTH);
  memset(level->levels, 0, MAX_CARTRIDGE_TYPES * sizeof(unsigned short) * 2);
  level->status = RESPONSE_INVALID;

  if ((ret = get_device_id(port, device_file, portnumber, device_id)) == OK) {
    if ((ret = parse_device_id(port, device_file, portnumber, device_id, 
                               level)) == OK) {
      return OK;
    }
  }

  return ret;
}

/* This function parses the device id and calls the appropiate function */

static int parse_device_id(int port, const char *device_file, int portnumber, 
                           const char *device_id, struct ink_level *level) {
  int i = 0;
  int j = 0;
  int tag_mfg = -1;
  const char *c;
  char tags[NR_TAGS][BUFLEN];  

  /* init tags */

  memset(tags, 0, NR_TAGS*BUFLEN);

  /* Tokenize the device id */

  i = 0;
  j = 0;
  c = device_id;
  
  while ((*c != '\0') && (i < NR_TAGS)) {
    j = 0;
    while ((*c != '\0') && (*c != ';') && (j < BUFLEN)) {
      tags[i][j] = *c;
      c++;
      j++;
    }
    if (*c == ';') { /* Some printers do not terminate the last tag with ';' */
      c++; /* Skip the ';' */
    }
    i++;
  }

#ifdef DEBUG
  /* print all tags */

  for (i = 0; i < NR_TAGS; i++) {
    printf("%d: %s\n", i, tags[i]);
  }
#endif

  /* Check if we deal with a printer */

  /* Find the "CLS:" tag */

  for (i = 0; i < NR_TAGS; i++) {
    c = tags[i];
    if ((c[0] == 'C') && (c[1] == 'L') && (c[2] == 'S') && (c[3] == ':')) {
      break;
    }
  }

  if (i < NR_TAGS) {

    /* Check if it is "PRINTER" */

    if ((tags[i][4] != 'P') || (tags[i][5] != 'R') || (tags[i][6] != 'I') || 
	(tags[i][7] != 'N') || (tags[i][8] != 'T') || (tags[i][9] != 'E') || 
	(tags[i][10] != 'R')) {
      
#ifdef DEBUG
      printf("No printer found\n");
#endif
      
      return NO_PRINTER_FOUND;
    }
  } else { 
    /* Find the "CLASS:" tag */
    
    for (i = 0; i < NR_TAGS; i++) {
      c = tags[i];
      if ((c[0] == 'C') && (c[1] == 'L') && (c[2] == 'A') && (c[3] == 'S') && 
	  (c[4] == 'S') && (c[5] == ':')) {
	break;
      }
    }

    if (i < NR_TAGS) {
    
      /* Check if it is "PRINTER" */
      
      if ((tags[i][6] != 'P') || (tags[i][7] != 'R') || (tags[i][8] != 'I') || 
	  (tags[i][9] != 'N') || (tags[i][10] != 'T') || 
	  (tags[i][11] != 'E') || (tags[i][12] != 'R')) {
      
#ifdef DEBUG
	printf("No printer found\n");
#endif
	
	return NO_PRINTER_FOUND;
      }
    } else {

#ifdef DEBUG 
      printf("No device class found\n");
#endif

      return NO_DEVICE_CLASS_FOUND;
    }
  }

  /* Insert the name of the printer */

  /* Find the "MFG:" tag */

  for (i = 0; i < NR_TAGS; i++) {
    c = tags[i];
    if ((c[0] == 'M') && (c[1] == 'F') && (c[2] == 'G') && (c[3] == ':')) {
      break;
    }
  }

#ifdef DEBUG
  if (i < NR_TAGS) {
    printf("The \"MFG:\" tag has number %d\n", i);
  } else {
    printf("No \"MFG:\" tag found\n");
  }
#endif

  /* Null-terminate in case of a too large name */
  level->model[MODEL_NAME_LENGTH-1] = '\0';

  if (i < NR_TAGS) {
    tag_mfg = i;
    strncpy(level->model, c + 4, MODEL_NAME_LENGTH-1);
  }

  j = 0;
  while (level->model[j] != '\0') {
    j++;
  }

  /* Find the "MDL:" tag */

  for (i = 0; i < NR_TAGS; i++) {
    c = tags[i];
    if ((c[0] == 'M') && (c[1] == 'D') && (c[2] == 'L') && (c[3] == ':')) {
      break;
    }
  }

#ifdef DEBUG
  if(i < NR_TAGS) {
    printf("The \"MDL:\" tag has number %d\n", i);
  } else {
    printf("No \"MDL:\" tag found\n");
  }
#endif

  if (i < NR_TAGS) {
    level->model[j] = ' ';
    j++;
    strncpy(level->model + j, c + 4, MODEL_NAME_LENGTH-j-1);
  }

  /* Check for a new HP printer */

  /* Find the "S:" tag */

  for (i = 0; i < NR_TAGS; i++) {
    c = tags[i];
    if ((c[0] == 'S') && (c[1] == ':')) {
      break;
    }
  }

#ifdef DEBUG
  if (i < NR_TAGS) {
    printf("The \"S:\" tag has number %d\n", i);
  } else {
    printf("No \"S:\" tag found\n");
  }
#endif

  if (i<NR_TAGS) {
    return parse_device_id_new_hp(tags, i, level);
  }
  
  /* Check for an old HP printer */

  /* Find the "VSTATUS:" tag */

  for (i = 0; i < NR_TAGS; i++) {
    c = tags[i];
    if ((c[0] == 'V') && (c[1] == 'S') && (c[2] == 'T') && (c[3] == 'A') &&
	(c[4] == 'T') && (c[5] == 'U') && (c[6] == 'S') && (c[7] ==':')) {
      break;
    }
  }

#ifdef DEBUG
  if (i < NR_TAGS) {
    printf("The \"VSTATUS:\" tag has number %d\n", i);
  } else {
    printf("No \"VSTATUS:\" tag found\n");
  }
#endif

  if (i<NR_TAGS) {
    return parse_device_id_old_hp(tags, i, level);
  }

  /* Ckeck for a Epson Printer */

  if (tag_mfg >= 0) {
    /* Ckeck if it is "EPSON" */
    c = tags[tag_mfg];
    if ((c[4] == 'E') && (c[5] == 'P') && (c[6] == 'S') && (c[7] == 'O') &&
	(c[8] == 'N')) {

      return get_ink_level_epson(port, device_file, portnumber, level);
    }
  }
  
  /* Ckeck for a Canon Printer */

  if (tag_mfg >= 0) {
    /* Ckeck if it is "Canon" */
    c = tags[tag_mfg];
    if ((c[4] == 'C') && (c[5] == 'a') && (c[6] == 'n') && (c[7] == 'o') &&
	(c[8] == 'n')) {

      return get_ink_level_canon(port, device_file, portnumber, level);
    }
  }
  
  /* Insert code to check for other printers here */
  
  return PRINTER_NOT_SUPPORTED; /* No matching printer was found */
}

char *get_version_string(void) {
  return LIBINKLEVEL_VERSION_STRING;
}
In particolare e' presente la funzione get_ink_level:

Codice: Seleziona tutto

int get_ink_level(const int port, const char *device_file, 
                  const int portnumber, struct ink_level *level) {
  char device_id[BUFLEN];
  int ret;

  memset(level->model, 0, MODEL_NAME_LENGTH);
  memset(level->levels, 0, MAX_CARTRIDGE_TYPES * sizeof(unsigned short) * 2);
  level->status = RESPONSE_INVALID;

  if ((ret = get_device_id(port, device_file, portnumber, device_id)) == OK) {
    if ((ret = parse_device_id(port, device_file, portnumber, device_id, 
                               level)) == OK) {
      return OK;
    }
  }

  return ret;
}
UP! Unreal Project: il futuro inizia qui. Crusade è tra noi
È scienza!
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 2 ospiti