void get_cpu_count() { int cpu_count = 1; /* default to 1 cpu */ #ifndef OLDCPU int mib[2] = { CTL_HW, HW_NCPU }; size_t len = sizeof(cpu_count); if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) { NORM_ERR("error getting cpu count, defaulting to 1"); } #endif info.cpu_count = cpu_count; info.cpu_usage = malloc(info.cpu_count * sizeof(float)); if (info.cpu_usage == NULL) { CRIT_ERR(NULL, NULL, "malloc"); } #ifndef OLDCPU assert(fresh == NULL); /* XXX Is this leaking memory? */ /* XXX Where shall I free this? */ if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) { CRIT_ERR(NULL, NULL, "calloc"); } #endif }
int get_ibm_acpi_fan(struct text_object *obj, char *p, int p_max_size) { FILE *fp; unsigned int speed = 0; char fan[128]; (void)obj; if (!p || p_max_size <= 0) { return 0; } snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR); fp = fopen(fan, "r"); if (fp != NULL) { while (!feof(fp)) { char line[256]; if (fgets(line, 255, fp) == NULL) { break; } if (sscanf(line, "speed: %u", &speed)) { break; } } } else { CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove " "ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno)); } fclose(fp); snprintf(p, p_max_size, "%d", speed); return 0; }
void get_ibm_acpi_brightness(struct text_object *obj, char *p, int p_max_size) { FILE *fp; unsigned int brightness = 0; char filename[128]; (void)obj; if (!p || p_max_size <= 0) { return; } snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR); fp = fopen(filename, "r"); if (fp != NULL) { while (!feof(fp)) { char line[256]; if (fgets(line, 255, fp) == NULL) { break; } if (sscanf(line, "level: %u", &brightness)) { break; } } } else { CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove " "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno)); } fclose(fp); snprintf(p, p_max_size, "%d", brightness); }
int get_ibm_acpi_temps(void) { FILE *fp; char thermal[128]; snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR); fp = fopen(thermal, "r"); if (fp != NULL) { while (!feof(fp)) { char line[256]; if (fgets(line, 255, fp) == NULL) { break; } if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d", &ibm_acpi_temps[0], &ibm_acpi_temps[1], &ibm_acpi_temps[2], &ibm_acpi_temps[3], &ibm_acpi_temps[4], &ibm_acpi_temps[5], &ibm_acpi_temps[6], &ibm_acpi_temps[7])) { break; } } } else { CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove " "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno)); } fclose(fp); return 0; }
/* fanspeed in SONY_LAPTOP_DIR contains an integer value for fanspeed (0~255). * I don't know the exact measurement unit, though. I may assume that 0 for * 'fan stopped' and 255 for 'maximum fan speed'. */ void get_sony_fanspeed(char *p_client_buffer, size_t client_buffer_size) { FILE *fp; unsigned int speed = 0; char fan[128]; if (!p_client_buffer || client_buffer_size <= 0) { return; } snprintf(fan, 127, "%s/fanspeed", SONY_LAPTOP_DIR); fp = fopen(fan, "r"); if (fp != NULL) { while (!feof(fp)) { char line[256]; if (fgets(line, 255, fp) == NULL) { break; } if (sscanf(line, "%u", &speed)) { break; } } } else { CRIT_ERR(NULL, NULL, "can't open '%s': %s\nEnable sony support or remove " "sony* from your "PACKAGE_NAME" config file.", fan, strerror(errno)); } fclose(fp); snprintf(p_client_buffer, client_buffer_size, "%d", speed); }
/* push an ifblock object onto the stack * in fact, this does a lot more: * - IFBLOCK_IF is just pushed onto the stack * - IFBLOCK_ELSE updates the "next" pointer of the upmost * object in the stack and is then pushed onto the stack * - IFBLOCK_ENDIF updates the "next" pointer of the upmost * object in the stack and then triggers stack popping of * any optional IFBLOCK_ELSE along with it's IFBLOCK_IF */ static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top, struct text_object *obj, enum ifblock_type type) { struct ifblock_stack_obj *stackobj; switch (type) { case IFBLOCK_ENDIF: if (!(*ifblock_stack_top)) CRIT_ERR(NULL, NULL, "got an endif without matching if"); (*ifblock_stack_top)->obj->ifblock_next = obj; /* if there's some else in between, remove and free it */ if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) { stackobj = *ifblock_stack_top; *ifblock_stack_top = stackobj->next; free(stackobj); } /* finally remove and free the if object */ stackobj = *ifblock_stack_top; *ifblock_stack_top = stackobj->next; free(stackobj); break; case IFBLOCK_ELSE: if (!(*ifblock_stack_top)) CRIT_ERR(NULL, NULL, "got an else without matching if"); (*ifblock_stack_top)->obj->ifblock_next = obj; /* fall through */ case IFBLOCK_IF: stackobj = malloc(sizeof(struct ifblock_stack_obj)); stackobj->type = type; stackobj->obj = obj; stackobj->next = *ifblock_stack_top; *ifblock_stack_top = stackobj; break; default: CRIT_ERR(NULL, NULL, "push_ifblock() missuse detected!"); } return 0; }
void get_cpu_count() { int cpu_count = 1; /* default to 1 cpu */ static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; int mib[2] = { CTL_HW, HW_NCPU }; size_t len = sizeof(cpu_count); if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) { NORM_ERR("error getting cpu count, defaulting to 1"); } pthread_mutex_lock(&count_mutex); if (info.cpu_count == cpu_count) { pthread_mutex_unlock(&count_mutex); return; } info.cpu_count = cpu_count; free(info.cpu_usage); info.cpu_usage = malloc(info.cpu_count * sizeof(float)); if (info.cpu_usage == NULL) { CRIT_ERR(NULL, NULL, "malloc"); } free(fresh); /* XXX Where shall I free this? */ if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) { CRIT_ERR(NULL, NULL, "calloc"); } pthread_mutex_unlock(&count_mutex); }
void scan_cmdline_to_pid_arg(struct text_object *obj, const char *arg, void* free_at_crash) { unsigned int i; if(strlen(arg) > 0) { obj->data.s = strdup(arg); for(i = 0; obj->data.s[i] != 0; i++) { while(obj->data.s[i] == ' ' && obj->data.s[i + 1] == ' ') { memmove(obj->data.s + i, obj->data.s + i + 1, strlen(obj->data.s + i + 1) + 1); } } if(obj->data.s[i - 1] == ' ') { obj->data.s[i - 1] = 0; } } else { CRIT_ERR(obj, free_at_crash, "${cmdline_to_pid commandline}"); } }
/* append an object to the given root object's list */ int append_object(struct text_object *root, struct text_object *obj) { struct text_object *end; end = root->prev; obj->prev = end; obj->next = NULL; if (end) { if (end->next) CRIT_ERR(NULL, NULL, "huston, we have a lift-off"); end->next = obj; } else { root->next = obj; } root->prev = obj; return 0; }
void get_ibm_acpi_volume(struct text_object *obj, char *p, int p_max_size) { FILE *fp; char volume[128]; unsigned int vol = -1; char mute[3] = ""; (void)obj; if (!p || p_max_size <= 0) { return; } snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR); fp = fopen(volume, "r"); if (fp != NULL) { while (!feof(fp)) { char line[256]; unsigned int read_vol = -1; if (fgets(line, 255, fp) == NULL) { break; } if (sscanf(line, "level: %u", &read_vol)) { vol = read_vol; continue; } if (sscanf(line, "mute: %s", mute)) { break; } } } else { CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove " "ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno)); } fclose(fp); if (strcmp(mute, "on") == 0) snprintf(p, p_max_size, "%s", "mute"); else snprintf(p, p_max_size, "%d", vol); }
struct net_stat *get_net_stat(const char *dev) { unsigned int i; if (!dev) return 0; /* find interface stat */ for (i=0; i<16; i++) { if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) return &netstats[i]; } /* wasn't found? add it */ if (i == 16) { for (i=0; i<16; i++) { if (netstats[i].dev == 0) { netstats[i].dev = strdup(dev); return &netstats[i]; } } } CRIT_ERR("too many interfaces used (limit is 16)"); return 0; }
static void mbox_scan(char *args, char *output, size_t max_len) { int i, u, flag; int force_rescan = 0; char buf[text_buffer_size]; struct stat statbuf; struct ring_list *curr = 0, *prev = 0, *startlist = 0; FILE *fp; /* output was set to 1 after malloc'ing in conky.c */ /* -> beeing able to test it here for catching SIGUSR1 */ if (output[0] == 1) { force_rescan = 1; output[0] = '\0'; } if (!args_ok || force_rescan) { char *substr = strstr(args, "-n"); if (substr) { if (sscanf(substr, "-n %i", &print_num_mails) != 1) { print_num_mails = PRINT_MAILS; } } else { print_num_mails = PRINT_MAILS; } if (print_num_mails < 1) { print_num_mails = 1; } substr = strstr(args, "-t"); if (substr) { if (sscanf(substr, "-t %i", &time_delay) != 1) { time_delay = TIME_DELAY; } } else { time_delay = TIME_DELAY; } substr = strstr(args, "-fw"); if (substr) { if (sscanf(substr, "-fw %i", &from_width) != 1) { from_width = FROM_WIDTH; } } else { from_width = FROM_WIDTH; } substr = strstr(args, "-sw"); if (substr) { if (sscanf(substr, "-sw %i", &subject_width) != 1) { subject_width = SUBJECT_WIDTH; } } else { subject_width = SUBJECT_WIDTH; } /* encapsulated with "'s find first occurrence of " */ if (args[strlen(args) - 1] == '"') { char *start; strncpy(mbox_mail_spool, args, DEFAULT_TEXT_BUFFER_SIZE); start = strchr(mbox_mail_spool, '"') + 1; start[(long) (strrchr(mbox_mail_spool, '"') - start)] = '\0'; strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE); } else { char *copy_args = strndup(args, text_buffer_size); char *tmp = strtok(copy_args, " "); char *start = tmp; while (tmp) { tmp = strtok(NULL, " "); if (tmp) { start = tmp; } } strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE); free(copy_args); } if (strlen(mbox_mail_spool) < 1) { CRIT_ERR(NULL, NULL, "Usage: ${mboxscan [-n <number of messages to print>] " "[-fw <from width>] [-sw <subject width>] " "[-t <delay in sec> mbox]}"); } /* allowing $MAIL in the config */ if (!strcmp(mbox_mail_spool, "$MAIL")) { strcpy(mbox_mail_spool, current_mail_spool); } if (stat(mbox_mail_spool, &statbuf)) { CRIT_ERR(NULL, NULL, "can't stat %s: %s", mbox_mail_spool, strerror(errno)); } args_ok = 1; /* args-computing necessary only once */ } /* if time_delay not yet reached, then return */ if (current_update_time - last_update < time_delay && !force_rescan) { return; } last_update = current_update_time; /* mbox still exists? and get stat-infos */ if (stat(mbox_mail_spool, &statbuf)) { NORM_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno)); output[0] = '\0'; /* delete any output */ return; } /* modification time has not changed, so skip scanning the box */ if (statbuf.st_ctime == last_ctime && statbuf.st_mtime == last_mtime && !force_rescan) { return; } last_ctime = statbuf.st_ctime; last_mtime = statbuf.st_mtime; /* build up double-linked ring-list to hold data, while scanning down the * mbox */ for (i = 0; i < print_num_mails; i++) { curr = (struct ring_list *) malloc(sizeof(struct ring_list)); curr->from = (char *) malloc(sizeof(char[from_width + 1])); curr->subject = (char *) malloc(sizeof(char[subject_width + 1])); curr->from[0] = '\0'; curr->subject[0] = '\0'; if (i == 0) { startlist = curr; } if (i > 0) { curr->previous = prev; prev->next = curr; } prev = curr; } /* connect end to start for an endless loop-ring */ startlist->previous = curr; curr->next = startlist; /* mbox */ fp = fopen(mbox_mail_spool, "r"); if (!fp) { return; } /* first find a "From " to set it to 0 for header-sarchings */ flag = 1; while (!feof(fp)) { if (fgets(buf, text_buffer_size, fp) == NULL) { break; } if (strncmp(buf, "From ", 5) == 0) { curr = curr->next; /* skip until \n */ while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); } flag = 0; /* in the headers now */ continue; } if (flag == 1) { /* in the body, so skip */ continue; } if (buf[0] == '\n') { /* beyond the headers now (empty line), skip until \n */ /* then search for new mail ("From ") */ while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); } flag = 1; /* in the body now */ continue; } if ((strncmp(buf, "X-Status: ", 10) == 0) || (strncmp(buf, "Status: R", 9) == 0)) { /* Mail was read or something, so skip that message */ flag = 1; /* search for next From */ curr->subject[0] = '\0'; curr->from[0] = '\0'; /* (will get current again on new 'From ' finding) */ curr = curr->previous; /* Skip until \n */ while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); } continue; } /* that covers ^From: and ^from: ^From:<tab> */ if (strncmp(buf + 1, "rom:", 4) == 0) { i = 0; u = 6; /* no "From: " string needed, so skip */ while (1) { if (buf[u] == '"') { /* no quotes around names */ u++; continue; } /* some are: From: <*****@*****.**> */ if (buf[u] == '<' && i > 1) { curr->from[i] = '\0'; /* skip until \n */ while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); } break; } if (buf[u] == '\n') { curr->from[i] = '\0'; break; } if (buf[u] == '\0') { curr->from[i] = '\0'; break; } if (i >= from_width) { curr->from[i] = '\0'; /* skip until \n */ while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); } break; } /* nothing special so just set it */ curr->from[i++] = buf[u++]; } } /* that covers ^Subject: and ^subject: and ^Subjec:<tab> */ if (strncmp(buf + 1, "ubject:", 7) == 0) { i = 0; u = 9; /* no "Subject: " string needed, so skip */ while (1) { if (buf[u] == '\n') { curr->subject[i] = '\0'; break; } if (buf[u] == '\0') { curr->subject[i] = '\0'; break; } if (i >= subject_width) { curr->subject[i] = '\0'; /* skip until \n */ while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); } break; } /* nothing special so just set it */ curr->subject[i++] = buf[u++]; } } } fclose(fp); output[0] = '\0'; i = print_num_mails; while (i) { struct ring_list *tmp; if (curr->from[0] != '\0') { if (i != print_num_mails) { snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width, curr->from, subject_width, curr->subject); } else { /* first time - no \n in front */ snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width, curr->from, subject_width, curr->subject); } } else { snprintf(buf, text_buffer_size, "\n"); } strncat(output, buf, max_len - strlen(output)); tmp = curr; curr = curr->previous; free(tmp->from); free(tmp->subject); free(tmp); i--; } }
static void parse_keys(const int key_in, const unsigned char * const key_ptr=NULL, const int keys=1) { int i; for (i=0; i<keys; i++) { int rescale=0; // Used by 'o' and 'O'. // Default is the 'no rescale' of 'o'. int key; if (key_ptr==NULL) // Now 'keys' should be 1! key=key_in; else // // First, we try to detect the "pseudocharacter" '<ESC>'. Or '<TAB>'. // 010508: Trying to add <CTRL> // { #define ESC_STRING "<ESC>" #define TAB_STRING "<TAB>" #define CTRL_STRING "<CTRL>" const int esc_len=strlen(ESC_STRING); const int tab_len=strlen(TAB_STRING); const int ctrl_len=strlen(CTRL_STRING); if ((keys-i>=esc_len+2) && (strncmp((const char *)key_ptr+i, ESC_STRING, esc_len)==0)) { key = (27<<16) + (key_ptr[i+esc_len]<<8) + key_ptr[i+esc_len+1]; // 011211: Format seems to be: 27*65536+code1*256+code2. i+=esc_len+1; } else if ((keys-i>=tab_len) && (strncmp((const char *)key_ptr+i, TAB_STRING, tab_len)==0)) { key=9; i+=tab_len-1; } else if ((keys-i>=ctrl_len+1) && (strncmp((const char *)key_ptr+i, CTRL_STRING, ctrl_len)==0)) { key = tolower(key_ptr[i+ctrl_len])-'a'+1; i+=ctrl_len; } else key=key_ptr[i]; } // printf("key=%d (%c)\n", key, key); switch (key) { // // 021203: What was this? Only place obj_trans (from old mouse.h or something // was used...?!) // // case 0: // @H@ Toggle mouse-translation mode. // obj_trans=1-obj_trans; // printf("obj_trans=%d\n", obj_trans); // break; //-------------------------------------------------- // // Selection, enabling etc. of surfaces. // //-------------------------------------------------- case 9: // (tab) Select surface by cycling through all surfaces. printf("Surfaces=%d\n", surfaces); if ((surfaces>0) /* && (surface_highlights==0) */ ) { selected_surface++; if (selected_surface==surfaces) selected_surface=0; printf("Surface selected is '%s', which has dim=%d.\n", surface_name[selected_surface], surface[selected_surface]->idim); // // Now, we signal highlighting of the // surface for a few frames. // surface_highlights=50; } break; case 'e': // Enable/Disable the selected surface. if (selected_surface>-1) surf_enabled[selected_surface]= 1-surf_enabled[selected_surface]; if (surf_enabled[selected_surface]) printf("Enabled surface '%s'.\n", surface_name[selected_surface]); else printf("Disabled surface '%s'.\n", surface_name[selected_surface]); break; case 'd': // Disable all other surfaces than this. if (selected_surface>-1) { int i; for (i=0; i<surfaces; i++) surf_enabled[i]=(i==selected_surface); } printf("Enabled surface '%s'.\n", surface_name[selected_surface]); break; case 'a': // Enable all surfaces. { int i; for (i=0; i<surfaces; i++) surf_enabled[i]=1; } puts("All surfaces enabled."); break; //-------------------------------------------------- // // Selection, enabling etc. of curves. // //-------------------------------------------------- case ' ': // (space) Select curve by cycling through all of them. if ((curves>0) && (curve_highlights==0)) { selected_curve++; if (selected_curve==curves) selected_curve=0; printf("Curve selected is '%s'.\n", curve_name[selected_curve]); surface_highlights=5; } break; case 5: // (ctrl-e) Enable/Disable the selected curve. if (selected_curve>-1) curve_enabled[selected_curve]=1-curve_enabled[selected_curve]; if (curve_enabled[selected_curve]) printf("Enabled curve '%s'.\n", curve_name[selected_curve]); else printf("Disabled curve '%s'.\n", curve_name[selected_curve]); break; case 4: // (ctrl-d) Disable all other curves than this. if (selected_curve>-1) { int i; for (i=0; i<curves; i++) curve_enabled[i]=(i==selected_curve); } printf("Enabled curve '%s'.\n", curve_name[selected_curve]); break; case 1: // (ctrl-a) Enable all curves. { int i; for (i=0; i<curves; i++) curve_enabled[i]=1; } puts("All curves enabled."); break; // // 001206: Hmm... How should we handle the centering and rescaling // for inifinte straight lines? Could use the point closest // to origo, perhaps??? // case 'O': // Center and rescale all objects around origo. rescale=1; // // Note how we simply continue into the code for 'o'... // case 'o': // Center all objects around origo. Possibly rescale. center_and_scale(rescale); break; case 'q': // Quit. exit(0); //-------------------------------------------------- // // Colour stuff. // //-------------------------------------------------- case 'b': // Toggle between black and white for wireframes. wire_col=1.0-wire_col; break; case 'B': // Toggle between black and white for the background. background_col=1.0-background_col; glClearColor(background_col, background_col, background_col, 0.0); break; //-------------------------------------------------- case '+': // Increase thickness of axes. axis_thickness*=1.1; //printf("axis_thickness=%f\n", axis_thickness); break; case '-': // Decrease thickness of axes. if (axis_thickness>0.1) axis_thickness*=0.9; //printf("axis_thickness=%f\n", axis_thickness); break; case '>': // Increase size of point markers. marker_size*=1.1; //printf("marker_size=%.5f\n", marker_size); break; case '<': // Decrease size of point markers. if (marker_size>0.00001) marker_size*=0.9; //printf("marker_size=%.5f\n", marker_size); break; case '*': // Increase length of axes. axis_length*=1.1; //printf("axis_length=%f\n", axis_length); break; case '/': // Decrease length of axes. if (axis_length>0.1) axis_length*=0.9; //printf("axis_length=%f\n", axis_length); break; case 'A': // Toggle axes on/off. draw_axes=1-draw_axes; //printf("draw_axes=%d\n", draw_axes); break; case 'w': // Toggle wireframe mode on/off, no hidden line removal. draw_edges=1-draw_edges; //printf("draw_edges=%d\n", draw_edges); break; //-------------------------------------------------- // // Reading and writing of viewing parameters. // //-------------------------------------------------- case (27<<16)+('w'<<8)+'1': // (Esc-w-<n>) Storing viewing parameters in slot <n>. case (27<<16)+('w'<<8)+'2': case (27<<16)+('w'<<8)+'3': case (27<<16)+('w'<<8)+'4': case (27<<16)+('w'<<8)+'5': case (27<<16)+('w'<<8)+'6': case (27<<16)+('w'<<8)+'7': case (27<<16)+('w'<<8)+'8': case (27<<16)+('w'<<8)+'9': { int slot=(key&255)-'1'+1; printf("Storing viewing parameters in slot %d.\n", slot); char tmp[1000]; #ifdef _MSC_VER // don't know which headerfile to use for VC++... // Also: Using root instead of home directory... sprintf(tmp, "view%d", slot); #else snprintf(tmp, 1000, "view%d", slot); #endif FILE *f=fopen(tmp, "w"); if (f==NULL) CRIT_ERR(printf("Error opening file '%s' for writing.\n", tmp)); write_gl_matrices(f); fprintf(f, "%.15e %.15e %.15e\n", xtrans, ytrans, ztrans); fprintf(f, "%.15e %.15e %.15e\n", xscale, yscale, zscale); fclose(f); } break; case (27<<16)+('r'<<8)+'1': // (Esc-r-<n>) Retrieving viewing parameters in slot <n>. case (27<<16)+('r'<<8)+'2': case (27<<16)+('r'<<8)+'3': case (27<<16)+('r'<<8)+'4': case (27<<16)+('r'<<8)+'5': case (27<<16)+('r'<<8)+'6': case (27<<16)+('r'<<8)+'7': case (27<<16)+('r'<<8)+'8': case (27<<16)+('r'<<8)+'9': { int slot=(key&255)-'1'+1; printf("Retrieving viewing parameters from slot %d.\n", slot); char tmp[1000]; #ifdef _MSC_VER // don't know which headerfile to use for VC++... // Also: Using root instead of home directory... sprintf(tmp, "/view%d", slot); #else snprintf(tmp, 1000, "view%d", slot); #endif FILE *f=fopen(tmp, "r"); if (f==NULL) printf("Error opening file '%s' for reading, skipping.\n", tmp); else { read_gl_matrices(f); fscanf(f, "%lf %lf %lf\n", &xtrans, &ytrans, &ztrans); fscanf(f, "%lf %lf %lf\n", &xscale, &yscale, &zscale); fclose(f); } } break; default: ; } } }
void draw_all_curves(void) { int i, j; for (i=0; i<curves; i++) { if (curve_enabled[i]) { // // As a first brute force solution, we simply evaluate the curves // in 100 points and draw straight line segments. // All evaluations are done once, since the curves are not evolving // in any way. // #define CURVE_EVALUATIONS 500 if (discr_curve[i]==NULL) { discr_curve[i]=new double[3*CURVE_EVALUATIONS]; int left=0; for (j=0; j<CURVE_EVALUATIONS; j++) { double t= curve[i]->et[curve[i]->ik-1]+ (curve[i]->et[curve[i]->in]-curve[i]->et[curve[i]->ik-1])* j/(CURVE_EVALUATIONS-1.0); { int stat; s1227(curve[i], 0, t, &left, discr_curve[i]+3*j, &stat); if (stat!=0) CRIT_ERR(printf("s1227 returned status %d.\n", stat)); } } } // // Now we draw all the line segments. // glLineWidth(1.0); glDisable(GL_LIGHTING); glBegin(GL_LINE_STRIP); { // int c=i % predef_colours; for (j=0; j<CURVE_EVALUATIONS; j++) { //glColor3dv(col_setting+3*c); //glColor3f(0.0, 0.0, 0.0); glColor3f(1.0-background_col, 1.0-background_col, 1.0-background_col); glVertex3dv(discr_curve[i]+3*j); // printf("%4d: %f %f %f\n", j, discr_curve[i][3*j], // discr_curve[i][3*j+1], discr_curve[i][3*j+2]); } } glEnd(); glEnable(GL_LIGHTING); } // end of if(curve_enabled[i]) ... } }
static void read_curves_and_surfaces(int argc, char *argv[]) { xscale=yscale=zscale=1.0; int ref=DEFAULT_REF; // Number of new knots between old ones. int maxref=DEFAULT_MAX_REF; // Maximal number of coeffs in any given direction. // (n, n) makes sure new knots are inserted close to max limit = n... int i; // // This must be reset every time we change control vertices, since // the discretization is done in the drawing routine. // for (i=0; i<curves; i++) { if (discr_curve[i]!=NULL) { delete discr_curve[i]; discr_curve[i]=NULL; } } // @HH@ // @HH@ Use the following optional command line options: // @HH@ surfaces=0; curves=0; for (i=1; i<argc; i++) { switch (argv[i][0]) { case 's': { // Next string is filename for surface. (One surface.) puts("Reading surfaces"); // surface[surfaces-1]=read_nurbs_sf(argv[i+1]); // old format std::vector<SISLSurf*> tmp; std::ifstream is(argv[i+1]); if (!is) { CRIT_ERR(printf("Could not open file: '%s'.\n", argv[i+1])); } try { eatwhite(is); while (!is.eof()) { //surface[surfaces-1] = readGoSurface(is); tmp.push_back(readGoSurface(is)); eatwhite(is); } } catch (std::exception& e) { CRIT_ERR(printf("Error occured while reading surface: %s\n", e.what())); } is.close(); int num_surfaces = tmp.size(); if (surfaces + num_surfaces > MAX_SURFACES) { CRIT_ERR(puts("Increase MAX_SURFACES.")); } for (int k = 0; k < num_surfaces; ++k) { // // 010116: This should be a quick fix for periodic // surfaces... // if (tmp[k] == NULL) { CRIT_ERR(printf("Couldn't read SISLSurf '%s'.\n", argv[i+1])); } if ((tmp[k]->cuopen_1 == SISL_CRV_PERIODIC || tmp[k]->cuopen_2 == SISL_CRV_PERIODIC)) { int kstat; SISLSurf *tmp_surf; make_sf_kreg(tmp[k], &tmp_surf, &kstat); if (kstat < 0) { CRIT_ERR(printf("make_sf_kreg failed!\n")); } freeSurf(tmp[k]); tmp[k] = tmp_surf; } if (tmp[k]->idim != 3) { CRIT_ERR(printf("Dimension of surface is %d and not 3!\n", tmp[k]->idim)); } if (surface[surfaces]) { // deleting old surface freeSurf(surface[surfaces]); } surface[surfaces] = tmp[k]; surface_name[surfaces] = argv[i+1]; surf_enabled[surfaces] = 1; // Generating an approximating polygon. lower_degree_and_subdivide(surface + surfaces, ref, maxref); // evaluating normals (normalized) delete normal[surfaces]; compute_surface_normals(surface[surfaces], normal + surfaces); ++surfaces; } } i++; break; case 'c': { // Next string is filename for file containing 1 curve. printf("Reading a single curve into slot %d.\n", curves); std::vector<SISLCurve*> tmp; //n=get_curve_set(argv[i+1], &tmp, &stat); //get_single_curve(argv[i+1], &tmp, &stat); // old format std::ifstream is(argv[i+1]); if (!is) { CRIT_ERR(printf("Could not open file: '%s'.\n", argv[i+1])); } try { eatwhite(is); while (!is.eof()) { tmp.push_back(readGoCurve(is)); eatwhite(is); } } catch (std::exception& e) { CRIT_ERR(printf("Error occured while reading curve: %s\n", e.what())); } is.close(); int num_curves = tmp.size(); if (curves + num_curves > MAX_CURVES) { CRIT_ERR(puts("Increase MAX_CURVES.")); } for(int k = 0; k < num_curves; ++k) { if (curve[curves + k] != NULL) { freeCurve(curve[curves + k]); } curve[curves + k] = tmp[k]; // // 001206: If the dimension is 2, we set up a // new curve, filling in zeros. // if (curve[curves + k]->idim==2) { double *new_coeffs= new double[3*curve[curves + k]->in]; if (new_coeffs==NULL) CRIT_ERR(puts("Couldn't allocate memory.")); int j; for (j=0; j<curve[curves + k]->in; j++) { new_coeffs[3*j+0]= curve[curves + k]->ecoef[2*j+0]; new_coeffs[3*j+1]= curve[curves + k]->ecoef[2*j+1]; new_coeffs[3*j+2]=0.0; } SISLCurve *tmp2=curve[curves + k]; curve[curves + k]= newCurve(tmp2->in, tmp2->ik, tmp2->et, new_coeffs, tmp2->ikind, 3, 1); freeCurve(tmp2); } if (curve[curves + k]->idim!=3) { CRIT_ERR(printf("Dimension of curve is %d and not 3?!\n", curve[curves + k]->idim)); } curve_name[curves + k]=argv[i+1]; curve_enabled[curves + k]=1; } curves+=num_curves; } i++; break; case 'p': { // Next string is filename for file containing a point cloud. printf("Reading a point cloud.\n"); std::ifstream is(argv[i+1]); if (!is) { CRIT_ERR(printf("Could not open file: '%s'.\n", argv[i+1])); } try { vector<double> coords; readGoPoints(coords, is); int num_points = int(coords.size()) / 3; printf("Number of vertices: %d\n", num_points); for (int i = 0; i < num_points; ++i) { pcloud.push_back(vector3t<float>(coords[3 * i], coords[3 * i + 1], coords[3 * i + 2])); } // eatwhite(is); // int tmp; // is >> tmp; // is >> tmp; // is >> tmp; // is >> tmp; // is >> tmp; // is >> tmp; // is >> tmp; // is >> tmp; // eatwhite(is); // is >> tmp; // printf("Number of vertices: %d\n", tmp); // while (!is.eof()) { // double x, y, z; // is >> x; // is >> y; // is >> z; // //printf("point: %f %f %f\n", x, y, z); // pcloud.push_back(vector3t<float>(x, y, z)); // eatwhite(is); // } } catch (std::exception& e) { CRIT_ERR(printf("Error occured while reading curve: %s\n", e.what())); } is.close(); printf("pcloud size is now %d\n", pcloud.size()); } i++; break; // case 'r': // // Set refinement factor. Default value is ???. // puts("Reading surface refinement factor"); // ref=atoi(argv[i+1]); // i++; // break; // case 'R': case 'r': // Set max refinement factor. Default value is ???. puts("Reading upper bound for surface refinement."); maxref=atoi(argv[i+1]); i++; break; case 'e': // String with keypresses to execute follows. Not // everything will work! printf("Executing '%s'.\n", argv[i+1]); strncpy(init_key_string, argv[i+1], 1000); i++; break; default: puts("Huh?! Unknown option."); exit(0); } } }