//---------------------------------------------------------------- CPUTConfigEntryA *CPUTConfigBlockA::GetValueByName(const std::string &szName) { for(int ii=0; ii<mnValueCount; ++ii) { const std::string &valName = mpValues[ii].szName; if(valName.size() != szName.size()) { continue; } size_t j = 0; while (j < valName.size() && tolow(szName[j]) == valName[j]) { ++j; } if (j == valName.size()) // match { return &mpValues[ii]; } } // not found - return an 'empty' object to avoid crashes/extra error checking return const_cast<CPUTConfigEntryA*>(&CPUTConfigEntryA::sNullConfigValue); }
int strlowcmp(const char *s, const char *d) { int rc; char *ss = NULL; char *dd = NULL; ss = strdup(s); dd = strdup(d); if (!ss || !dd) { rc = strcmp(s, d); goto err; } tolow(ss); tolow(dd); rc = strcmp(ss, dd); err: if (ss) free(ss); if (dd) free(dd); return rc; }
int input(struct inp_event *inp, int wait) { int rc; SDL_Event event; SDL_Event peek; memset(&event, 0, sizeof(event)); memset(&peek, 0, sizeof(peek)); if (wait) { rc = SDL_WaitEvent(&event); } else rc = SDL_PollEvent(&event); if (!rc) return 0; inp->sym[0] = 0; inp->type = 0; inp->count = 1; switch(event.type) { #if SDL_VERSION_ATLEAST(2,0,0) case SDL_MULTIGESTURE: case SDL_FINGERMOTION: if (DIRECT_MODE && !game_paused()) return AGAIN; if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, event.type, event.type) > 0) return AGAIN; /* to avoid flickering */ break; case SDL_FINGERUP: #ifdef IOS touch_num = 0; #endif case SDL_FINGERDOWN: #ifdef IOS if (event.type == SDL_FINGERDOWN) { if (gfx_ticks() - touch_stamp > 100) { touch_num = 0; touch_stamp = gfx_ticks(); } touch_num ++; if (touch_num >= 3) { inp->type = KEY_DOWN; inp->code = 0; strncpy(inp->sym, "escape", sizeof(inp->sym)); break; } } #endif gfx_finger_pos_scale(event.tfinger.x, event.tfinger.y, &inp->x, &inp->y); inp->type = (event.type == SDL_FINGERDOWN) ? FINGER_DOWN : FINGER_UP; #ifndef PRIx64 snprintf(inp->sym, sizeof(inp->sym), "%llx:%llx", event.tfinger.fingerId, event.tfinger.touchId); #else snprintf(inp->sym, sizeof(inp->sym), "%"PRIx64":%"PRIx64, event.tfinger.fingerId, event.tfinger.touchId); #endif break; case SDL_WINDOWEVENT: switch (event.window.event) { /* case SDL_WINDOWEVENT_SHOWN: */ case SDL_WINDOWEVENT_EXPOSED: gfx_flip(); gfx_commit(); break; case SDL_WINDOWEVENT_MINIMIZED: case SDL_WINDOWEVENT_RESTORED: m_minimized = (event.window.event == SDL_WINDOWEVENT_MINIMIZED && !opt_fs); snd_pause(!nopause_sw && m_minimized); break; case SDL_WINDOWEVENT_ENTER: case SDL_WINDOWEVENT_FOCUS_GAINED: m_focus = 1; if (opt_fs) mouse_cursor(0); break; case SDL_WINDOWEVENT_LEAVE: m_focus = 0; if (opt_fs) mouse_cursor(1); /* is it hack?*/ break; default: break; } if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT) > 0) return AGAIN; /* to avoid flickering */ return 0; #else case SDL_ACTIVEEVENT: if (event.active.state & SDL_APPACTIVE) { m_minimized = !event.active.gain; snd_pause(!nopause_sw && m_minimized); } if (event.active.state & (SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS)) { if (event.active.gain) { m_focus = 1; if (opt_fs) mouse_cursor(0); } else if (event.active.state & SDL_APPMOUSEFOCUS) { m_focus = 0; if (opt_fs) mouse_cursor(1); /* is it hack?*/ } } #if SDL_VERSION_ATLEAST(1,3,0) if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_ACTIVEEVENT, SDL_ACTIVEEVENT) > 0) #else if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_EVENTMASK(SDL_ACTIVEEVENT)) > 0) #endif return AGAIN; /* to avoid flickering */ return 0; #endif case SDL_USEREVENT: { void (*p) (void*) = (void (*)(void*))event.user.data1; p(event.user.data2); return AGAIN; } case SDL_QUIT: game_running = 0; return -1; case SDL_KEYDOWN: /* A key has been pressed */ #if SDL_VERSION_ATLEAST(2,0,0) if (event.key.repeat) { if (DIRECT_MODE && !game_paused()) /* do not send key repeats */ return AGAIN; if (gfx_ticks() - last_press_ms < INPUT_REP_DELAY_MS) return AGAIN; if ((gfx_ticks() - last_repeat_ms) < INPUT_REP_INTERVAL_MS) return AGAIN; last_repeat_ms = gfx_ticks(); } else { last_press_ms = gfx_ticks(); last_repeat_ms = gfx_ticks(); } #endif inp->type = KEY_DOWN; inp->code = event.key.keysym.scancode; #if SDL_VERSION_ATLEAST(1,3,0) strncpy(inp->sym, SDL_GetScancodeName(inp->code), sizeof(inp->sym)); #else strncpy(inp->sym, SDL_GetKeyName(event.key.keysym.sym), sizeof(inp->sym)); #endif inp->sym[sizeof(inp->sym) - 1] = 0; tolow(inp->sym); #if SDL_VERSION_ATLEAST(1,3,0) key_compat(inp); #endif #if SDL_VERSION_ATLEAST(1,3,0) /* strange bug in some SDL2 env, with up/down events storm */ if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_KEYDOWN, SDL_KEYUP) > 0) { if (peek.key.keysym.scancode == event.key.keysym.scancode && peek.key.repeat == 0) return AGAIN; } #endif break; case SDL_KEYUP: inp->type = KEY_UP; inp->code = event.key.keysym.scancode; #if SDL_VERSION_ATLEAST(1,3,0) strncpy(inp->sym, SDL_GetScancodeName(inp->code), sizeof(inp->sym)); #else strncpy(inp->sym, SDL_GetKeyName(event.key.keysym.sym), sizeof(inp->sym)); #endif inp->sym[sizeof(inp->sym) - 1] = 0; tolow(inp->sym); #if SDL_VERSION_ATLEAST(1,3,0) key_compat(inp); #endif #if SDL_VERSION_ATLEAST(1,3,0) /* strange bug in some SDL2 env, with up/down events storm */ if (SDL_PeepEvents(&peek, 1, SDL_PEEKEVENT, SDL_KEYDOWN, SDL_KEYUP) > 0) { if (event.key.keysym.scancode == peek.key.keysym.scancode && peek.key.repeat == 0) return AGAIN; } #endif break; case SDL_MOUSEMOTION: m_focus = 1; /* ahhh */ if (DIRECT_MODE && !game_paused()) return AGAIN; inp->type = MOUSE_MOTION; inp->x = event.button.x; inp->y = event.button.y; #if SDL_VERSION_ATLEAST(1,3,0) while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) { #else while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_EVENTMASK (SDL_MOUSEMOTION)) > 0) { #endif inp->x = peek.button.x; inp->y = peek.button.y; } break; case SDL_MOUSEBUTTONUP: inp->type = MOUSE_UP; inp->x = event.button.x; inp->y = event.button.y; inp->code = event.button.button; if (event.button.button == 4) inp->type = 0; else if (event.button.button == 5) inp->type = 0; break; #if SDL_VERSION_ATLEAST(2,0,0) case SDL_MOUSEWHEEL: if (DIRECT_MODE && !game_paused()) return AGAIN; inp->type = (event.wheel.y > 0) ? MOUSE_WHEEL_UP : MOUSE_WHEEL_DOWN; while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_MOUSEWHEEL, SDL_MOUSEWHEEL) > 0) { if (!((event.wheel.y > 0 && inp->type == MOUSE_WHEEL_UP) || (event.wheel.y < 0 && inp->type == MOUSE_WHEEL_DOWN))) break; inp->count ++; } break; #endif case SDL_MOUSEBUTTONDOWN: m_focus = 1; /* ahhh */ inp->type = MOUSE_DOWN; inp->x = event.button.x; inp->y = event.button.y; inp->code = event.button.button; if (event.button.button == 4) inp->type = MOUSE_WHEEL_UP; else if (event.button.button == 5) inp->type = MOUSE_WHEEL_DOWN; #if SDL_VERSION_ATLEAST(1,3,0) while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONDOWN) > 0) { #else while (SDL_PeepEvents(&peek, 1, SDL_GETEVENT, SDL_EVENTMASK (SDL_MOUSEBUTTONDOWN)) > 0) { #endif if (!((event.button.button == 4 && inp->type == MOUSE_WHEEL_UP) || (event.button.button == 5 && inp->type == MOUSE_WHEEL_DOWN))) break; inp->count ++; } break; default: break; } return 1; }
/* for load config */ int load_config(const char *path, struct _config *config) { char strbuf[MAXSTRLEN]; FILE *fd = NULL; int n = 0, line = 0, block = 0, index=0; fd = fopen(path, "r"); if (fd == NULL) { fprintf(stderr, "ERROR: Can't open config file \"%s\".\n", path); return(-1); } while(!feof(fd)) { register char *p = strbuf; register char *key = malloc((KEYLEN+1) * sizeof(char)); register char *val = malloc((VALLEN+1) * sizeof(char)); char title[TITLELEN]; *key = '\0'; *val = '\0'; *title = '\0'; line++; if (fgets(strbuf, sizeof(strbuf)-1, fd) == NULL) continue; rmspace(p); /* blank lines and comments get ignored */ if (!*p || *p == '#') continue; if (p[0] == '[' && p[strlen(p)-1] == ']') { sscanf(p, "[%255[^]]", title); block = 1; index = config->index; config->cap[index] = malloc(sizeof(struct _cap)); if (config->index == 0) memset(config->cap[index],0,sizeof(struct _cap)); else { memcpy(config->cap[index],config->cap[index-1],sizeof(struct _cap)); memset(config->cap[index],0,sizeof(struct _cap)); } strcpy(config->cap[index]->title, title); config->cmdl=0; config->index++; continue; } /* parse */ n = sscanf(p, "%255[^=\n\r\t]=%255[^\n\r\t]", key, val); if (n != 2) { fprintf(stderr, "ERROR: Can't parse config file %s at line %d.\n", path, line); continue; } key = tolow(key); if (!strcmp(key,"src")) { if (getmac(val, config->cap[index]->src_interface_mac) != -1) strcpy(config->cap[index]->src_interface, val); else { fprintf(stderr, "ERROR: %s fetching interface information error: Device not found.\n", val); return(-1); } } else if (!strcmp(key,"dst")) { if (getmac(val, config->cap[index]->dst_interface_mac) != -1) strcpy(config->cap[index]->dst_interface, val); else { fprintf(stderr, "ERROR: %s fetching interface information error: Device not found.\n", val); return(-1); } } else if (!strcmp(key,"ip")) { if (inet_aton(val, &config->cap[index]->server_ip) == 0) { fprintf(stderr, "Invalid IP address format \"%s\".\n", val); return(-1); } } else if (!strcmp(key,"port")) { config->cap[index]->port = (uint16_t) atoi(val); if (config->cap[index]->port == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP port value \"%d\".\n", config->cap[index]->port); return(-1); } } else if (!strcmp(key,"dsslport")) { config->cap[index]->dsslport = (uint16_t) atoi(val); if (config->cap[index]->dsslport == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP dsslport value \"%d\".\n", config->cap[index]->dsslport); return(-1); } } else if (!strcmp(key,"key")) { strcpy(config->cap[index]->keyfile, val); } else if (!strcmp(key,"pwd")) { strcpy(config->cap[index]->pwd, val); } // common options else if (!strcmp(key,"loglevel")) { if (block) fprintf(stderr,"WARNING: The option \"loglevel\" is common and must placed not in block \"[title]\".\n"); config->loglevel = atoi(val); } else if (!strcmp(key,"daemon")) { if (block) fprintf(stderr,"WARNING: The option \"daemon\" is common and must placed not in block \"[title]\".\n"); if (!strcmp(tolow(val),"off")) config->daemon = 0; else if (!strcmp(tolow(val),"on")) config->daemon = 1; else fprintf(stderr, "Invalid value in config in option \"%s\".\n", key); } else if (!strcmp(key,"pid")) { if (block) fprintf(stderr,"WARNING: The option \"pid\" is common and must placed not in block \"[title]\".\n"); strcpy(config->pidfilename, val); } free(key); free(val); } // while fclose(fd); return(0); }