void read_settings() { load_file(settings, SETTINGS_FILE); assure_key(settings, "acpi_location"); assure_key(settings, "poll_timeout"); assure_key(settings, "warning_level"); assure_key(settings, "warning_timeout"); assure_key(settings, "beep_frequency"); assure_key(settings, "beep_duration"); assure_key(settings, "beep_repetitions"); assure_key(settings, "beep_pause"); assure_key(settings, "beep_increment"); assure_key(settings, "bat_state"); assure_key(settings, "bat_capacity"); assure_key(settings, "bat_remaining"); poll_timeout = atoi(hm_get(settings, "poll_timeout")); warning_level = atoi(hm_get(settings, "warning_level")); warning_timeout = atoi(hm_get(settings, "warning_timeout")); beep_frequency = atoi(hm_get(settings, "beep_frequency")); beep_duration = atoi(hm_get(settings, "beep_duration")); beep_repetitions = atoi(hm_get(settings, "beep_repetitions")); beep_pause = atoi(hm_get(settings, "beep_pause")); beep_increment = atoi(hm_get(settings, "beep_increment")); sleep_timeout = gcd(poll_timeout, warning_timeout); }
void read_acpi() { load_file(batt_info, acpi_alarm); load_file(batt_info, acpi_info); load_file(batt_info, acpi_state); assure_key(batt_info, hm_get(settings, "bat_state")); assure_key(batt_info, hm_get(settings, "bat_capacity")); assure_key(batt_info, hm_get(settings, "bat_remaining")); bat_state = hm_get(batt_info, hm_get(settings, "bat_state")); bat_capacity = hm_get(batt_info, hm_get(settings, "bat_capacity")); bat_remaining = hm_get(batt_info, hm_get(settings, "bat_remaining")); capacity_factor = (float)atoi(bat_remaining) / (float)atoi(bat_capacity); }
static void* cbif_service_db_get_(void* cbif_handle, const char* key) { if (cbif_handle && key) { struct connection_st* conn = cbif_handle; return hm_get(conn->db, key); } return NULL; }
static void _service_handler(void* args) { CacheResponse resp; char* rq; void* qb = (void*)malloc(BUFLEN); RpcEndpoint ep; int rc,len,err; char* buf1=(char*)malloc(sizeof(char)*BUFLEN); char retOK[]="OK"; AutomataHandler_t ahandle; for(;;) { printf("waiting on rpc_query...\n"); len = rpc_query(rps, &ep, buf1, BUFLEN); if(len==0) { fprintf(stderr,"query error\n"); exit(1); } rc = rpc_response(rps, &ep, retOK, sizeof(retOK)); if(rc==0) { fprintf(stderr,"response error\n"); exit(1); } resp = newCacheResponse(buf1, BUFLEN); char* id = cache_response_msg(resp); rc = hm_get(service_functions, id, (void**)&ahandle); if(rc==0) { fprintf(stderr,"no handler function for %d\n",id); freeCacheResponse(resp); continue; } err = ahandle(resp); if(err) { fprintf(stderr, "handler for %s had a bad time\n",id); } freeCacheResponse(resp); } free(buf1); }
int main(int argc, char **argv) { (void) signal(SIGINT, sigint_received); settings = hm_create(10, 0.5, 7); batt_info = hm_create(10, 0.5, 7); read_settings(); strcpy(acpi, hm_get(settings, "acpi_location")); strcpy(acpi_alarm, acpi); strcat(acpi_alarm, "/alarm\0"); strcpy(acpi_info, acpi); strcat(acpi_info, "/info\0"); strcpy(acpi_state, acpi); strcat(acpi_state, "/state\0"); read_acpi(); /* Handle main arguments */ struct option opt_list[] = { {"beep", 0, NULL, 'b'}, {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'v'}, {"debug", 0, NULL, 'd'}, {0,0,0,0} }; int debugging = 0; int arg = EOF; while((arg = getopt_long(argc, argv, "bhvd", opt_list, NULL)) != EOF) { switch (arg) { case 'b': beep(beep_frequency, beep_duration, beep_repetitions, beep_pause, beep_increment); exit(EXIT_SUCCESS); case 'h': print_help(); exit(EXIT_SUCCESS); case 'v': print_version(); exit(EXIT_SUCCESS); case 'd': debugging = 1; break; default: print_usage(); exit(EXIT_FAILURE); } } if (debugging) { debug_print(); } else if (!daemonize(PID_FILE, "/", 0)) { exit(EXIT_FAILURE); } unsigned int poll_count = 0; unsigned int warning_count = 0; while (1) { if ((sleep_timeout * (poll_count + 1)) % poll_timeout == 0) { read_acpi(); if (debugging) { printf("> Reading acpi\n"); } } if ((sleep_timeout * (warning_count + 1)) % warning_timeout == 0) { if (capacity_factor <= (float)warning_level / 100.0 && strcmp(bat_state, "discharging") == 0) { beep(beep_frequency, beep_duration, beep_repetitions, beep_pause, beep_increment); } if (debugging) { printf("> Capacity: %.2f%%\tState: %s\n", capacity_factor * 100, bat_state); } } poll_count++; poll_count = poll_count % (poll_timeout / sleep_timeout); warning_count++; warning_count = warning_count % (warning_timeout / sleep_timeout); SLEEP(sleep_timeout); } cleanup(); return EXIT_SUCCESS; }
void append_var_info(compiler_wrapper *cw, char *ch, char load, int lineno) { char needs_close = cw->repl; char already_defined = 0; arraylist list = cw->used_names; int i; for(i = 0; i < list.count; i++) { name_wrapper *w = arr_get(&cw->used_names, i); if(strcmp(w->name, ch)) continue; if(w->owner != cw) { switch_to_close(w->owner, ch, w->idx); needs_close = 1; } else { needs_close = is_close(cw, w->idx); already_defined = 1; } } if(!needs_close && !already_defined && load) needs_close = 1; if(needs_close) { lky_instruction istr = load ? LI_LOAD_CLOSE : LI_SAVE_CLOSE; char *nsid = malloc(strlen(ch) + 1); strcpy(nsid, ch); int i = find_prev_name(cw, nsid); if(i < 0) { i = (int)cw->rnames.count; arr_append(&cw->rnames, nsid); } append_op(cw, istr, lineno); unsigned char buf[4]; int_to_byte_array(buf, i); append_op(cw, buf[0], lineno); append_op(cw, buf[1], lineno); append_op(cw, buf[2], lineno); append_op(cw, buf[3], lineno); return; } lky_instruction istr = load ? LI_LOAD_LOCAL : LI_SAVE_LOCAL; hm_error_t err; int idx = 0; lky_object_builtin *o = hm_get(&cw->saved_locals, ch, &err); if(err == HM_KEY_NOT_FOUND) { idx = get_next_local(cw); lky_object *obj = lobjb_build_int(idx); pool_add(&ast_memory_pool, obj); hm_put(&cw->saved_locals, ch, obj); } else idx = OBJ_NUM_UNWRAP(o); append_op(cw, istr, lineno); unsigned char buf[4]; int_to_byte_array(buf, idx); append_op(cw, buf[0], lineno); append_op(cw, buf[1], lineno); append_op(cw, buf[2], lineno); append_op(cw, buf[3], lineno); name_wrapper *wrap = malloc(sizeof(name_wrapper)); pool_add(&ast_memory_pool, wrap); wrap->idx = cw->rops.count - 5; wrap->name = ch; wrap->owner = cw; arr_append(&cw->used_names, wrap); }
int main(int ac, char **av) { //// // Width, height //// int width = 600; int height = 600; //// // Random generator init //// srand((unsigned) time(0)); //// // Alloc //// vec3_field *vf0 = v3f_random_unit(3, 4, 2), *vf1 = v3f_random_unit(6, 5, 2), *vf2 = v3f_random_unit(8, 11, 2), *vf3 = v3f_random_unit(19, 21, 2); heightmap *hm0 = hm_perlin_noise(width, height, vf0, 0.5), *hm1 = hm_perlin_noise(width, height, vf1, 0.5), *hm2 = hm_perlin_noise(width, height, vf2, 0.5), *hm3 = hm_perlin_noise(width, height, vf3, 0.5); heightmap *hm = hm_new(width, height); //// // Process //// hm_add_scale(hm, hm0, 0.50); hm_add_scale(hm, hm1, 0.25); hm_add_scale(hm, hm2, 0.15); hm_add_scale(hm, hm3, 0.10); //// // SDL TIME! FREE BEERS FOR EVERYONE AND THEIR DOG! // (though, you're the one paying, okay?) //// if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("Error: SDL can't init."); return -1; } SDL_Surface *surface; int running = 1; SDL_Event evt; surface = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); if(surface == NULL) { printf("Error: Can't set video mode."); return -1; } SDL_WM_SetCaption("Perlin noise test", NULL); while(running) { while(SDL_PollEvent(&evt)) { if(evt.type == SDL_QUIT) { running = 0; } } for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) { Uint8 *pixel = (Uint8 *) surface->pixels + y * surface->pitch + x * 4; *(Uint32 *) pixel = to_pixel(surface->format, hm_get(hm, x, y)); } SDL_Flip(surface); } SDL_Quit(); //// // Dealloc //// v3f_destroy(vf0); v3f_destroy(vf1); v3f_destroy(vf2); v3f_destroy(vf3); hm_destroy(hm0); hm_destroy(hm1); hm_destroy(hm2); hm_destroy(hm3); hm_destroy(hm); }
void DispSubsByPosition(gint64 position, CustomData *data) { char curTotlaTime[40], curMin[5], curSec[5], curenthours[5], milsectimeBuf[5]; char endTImeLine[100] = "--> "; guint milSec = 9999999; milSec = position / GST_MSECOND; guint sec = ((milSec % (1000*60*60)) % (1000*60)) / 1000; guint min = (milSec % (1000*60*60)) / (1000*60); guint hours = milSec / (1000 * 60 * 60); guint milsec2 = ((position / GST_MSECOND) % 1000) / 10; sprintf(curenthours, "0%d", hours); if(sec < 10) { sprintf(curSec, "0%d", sec); } else { sprintf(curSec, "%d", sec); } if(min < 10) { sprintf(curMin, "0%d", min); } else { sprintf(curMin, "%d", min); } if(milsec2 < 10) { sprintf(milsectimeBuf, "0%d", milsec2); } else { sprintf(milsectimeBuf, "%d", milsec2); } if(data->IsSubtitlesLoaded) { curTotlaTime[0] = 0; strcat(curTotlaTime, curenthours); strcat(curTotlaTime, ":"); strcat(curTotlaTime, curMin); strcat(curTotlaTime, ":"); strcat(curTotlaTime, curSec); strcat(curTotlaTime, ":"); strcat(curTotlaTime, milsectimeBuf); strcat(endTImeLine, curTotlaTime); int Hresult; hm_get(data->hm, curTotlaTime, data->Hbuf, sizeof(data->Hbuf)); if(hm_exists(data->hm, curTotlaTime)) { Hresult = hm_get(data->hm, curTotlaTime, data->Hbuf, sizeof(data->Hbuf)); if(Hresult == 0) { g_error("Value from hash table not found"); } ClearSubsWindow(data);//Clear subtitles window before new text render rendertext(data, data->subs_window); g_print("%s", data->Hbuf); } else if(hm_exists(data->hm, endTImeLine)) { //We have to use ClearSubsWindow twise to clear subs window ClearSubsWindow(data); strcpy(data->Hbuf, "1");//Must have a string and not an empty one neither spase rendertext(data, data->subs_window); ClearSubsWindow(data); } } }
static int on_each_read_record(const struct jbxm_initconf_st* iconf, struct jbxm_callback_if_st* cbif) { assert(iconf); assert(cbif); assert(cbif->type == JBXM_CBT_EACH_RRECORD); assert(cbif->io_area->avail > 0); struct UserData_st* ud = dbGET(cbif, "user-data"); assert(ud); // available cbif->io_area->buff[0 to cbif->io_area->avail] + '\0' if (ud->bodylen) { // ヘッダの取得は終了し、Content-Length の指定があるので溜め込む struct varmem_st* vm = new_varmem((size_t)cbif->io_area->avail); assert(vm); memcpy(vm->bytes, cbif->io_area->buff, (size_t)cbif->io_area->avail); void* ok = ll_add_tail(ud->body_chunks, vm); assert(ok); ud->body_chunks_sumlen += (size_t)cbif->io_area->avail; return 0; } // HTTP ヘッダの収集 char* buff = (char*)cbif->io_area->buff; ljbx_chomp(buff); // [if buff is string] PRINT("read [%.*s]", (int)cbif->io_area->avail, buff); if (! *buff) { // 空行 if (! ud->method) { // リクエストが設定されていない間は無視 return 0; } //PRINT("headers\n--->>"); //hm_foreach(ud->headers, print_header_, NULL); //PRINT("\n---<<"); const char* clen = hm_get(ud->headers, "content-length"); if (clen) { // add check digit !! const int bodylen = atoi(clen); if (bodylen > 0) { // BODY を収集する ud->bodylen = (size_t)bodylen; } else { if (bodylen) { // bodylen < 0 ud->response = http_CreateError(400); } else { // bodylen == 0 ud->response = http_CreateResponse(ud); } } } else { ud->response = http_CreateResponse(ud); } if (ud->response) { cbif->flow->next_events = EPOLLOUT | EPOLLET; } return 0; } if (ud->method) { // リクエストは解決しているので残りは ud->headers に追加 char* key = NULL; char* val = NULL; int ns = sscanf(buff, "%m[^:]: %m[^\r\n]", &key, &val); if (ns == 2) { ljbx_strtolower(key); void* old_val = NULL; hm_set(ud->headers, key, val, &old_val); free(old_val); // 重複は後勝ち } else { free(val); } free(key); } else { // リクエストを解決 char* method = NULL; char* path = NULL; int major = 0; int minor = 0; int ns = sscanf(buff, "%m[A-Z] %ms HTTP/%d.%d", &method, &path, &major, &minor); if (ns < 2) { free(method); free(path); ud->response = http_CreateError(400); cbif->flow->next_events = EPOLLOUT | EPOLLET; return 0; } ud->method = method; ud->path = path; ud->version.major = major; ud->version.minor = minor; } // set cbif->flow->next_events if necessary return 0; }