+
+ /* XXX: Yes, shelling out to system is total cheese. The planned
+ * fix here is to bring graphical display in process, (or at least
+ * have a custom external program that accepts image filenames on
+ * stdin.
+ */
+ switch (challenge_type) {
+ case CHALLENGE_TYPE_TEXT:
+ ASSERT_NOT_REACHED;
+ break;
+ case CHALLENGE_TYPE_IMAGE:
+ program = "xli -gamma 2.2";
+ break;
+ case CHALLENGE_TYPE_AUDIO:
+ program = "play";
+ break;
+ }
+
+ xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
+ program,
+ mnemon->dir_name,
+ challenge);
+ system (command);
+ free (command);
+}
+
+static void
+mnemon_hide_challenge (mnemon_t *mnemon, challenge_type_t challenge_type)
+{
+ char * command;
+
+ if (challenge_type != CHALLENGE_TYPE_IMAGE)
+ return;
+
+ /* XXX: And this is just embarrassing (obviously wrong in several
+ * ways). Hopefully I'll amend away any commit that includes this.
+ */
+ xasprintf (&command, "killall xli");
+ system (command);
+ free (command);
+}
+
+static void
+mnemon_do_challenges (mnemon_t *mnemon)
+{
+ bin_t *bin;
+ int item_index;
+ item_t *item;
+ category_t *category;
+ char *response;
+ int i;
+
+ /* Count the number of items with negative scores. */
+ mnemon->unlearned = 0;
+ for (i = 0; i < mnemon->num_bins; i++) {
+ bin = &mnemon->bins[i];
+ if (bin->score >= 0)
+ break;
+ mnemon->unlearned += bin->num_items;
+ }
+
+ mnemon->to_introduce -= mnemon->unlearned;
+ if (mnemon->to_introduce < 0)
+ mnemon->to_introduce = 0;
+
+ /* Get rid of bin with score of 0 if we aren't going to be
+ * introducing anything from it. */
+ if (mnemon->to_introduce == 0) {
+ bin = mnemon_get_bin (mnemon, 0);
+ mnemon_remove_bin (mnemon, bin);
+ }
+
+ if (mnemon->unlearned) {
+ printf ("You've got %d items to learn already. ", mnemon->unlearned);
+ if (mnemon->to_introduce)
+ printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
+ printf ("\n");
+ } else {
+ printf ("Introducing %d new items.\n", mnemon->to_introduce);
+ }
+ printf ("\n");
+
+ do {
+ struct timeval start, end;
+
+ mnemon_select_item (mnemon, &bin, &item_index, &category);
+ item = bin->items[item_index];
+
+ while (1) {
+ if (category->time_limit > 0.0) {
+ response = readline ("The next one is timed. Press enter when ready:");
+ free (response);
+ }
+
+ mnemon_show_challenge (mnemon, category->challenge_type,
+ item->challenge);
+
+ gettimeofday (&start, NULL);
+ response = readline ("> ");
+ gettimeofday (&end, NULL);
+
+ mnemon_hide_challenge (mnemon, category->challenge_type);
+
+ /* Terminate on EOF */
+ if (response == NULL) {
+ printf ("\n");
+ return;
+ }
+
+ if (response[0] == '/') {
+ mnemon_handle_command (mnemon, response + 1);
+ free (response);
+ } else {
+ break;
+ }
+ }
+
+ mnemon_handle_response (mnemon, bin, item_index,
+ item, response,
+ (end.tv_sec + end.tv_usec / 1e6) -
+ (start.tv_sec + start.tv_usec / 1e6),
+ category->time_limit);
+ free (response);
+ } while (mnemon->to_introduce ||
+ mnemon->unlearned ||
+ mnemon->to_master > 0);