spk_thread.c 21.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2018 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.com/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include <stdio.h>
#include <string.h>

#include "parameters.h"
#include "log.h"
#include "strfmt.h"
#include "prefs.h"
#include "spk_thread.h"
#include "spk.h"
#include "async_wait.h"
#include "async_event.h"
#include "thread.h"
#include "queue.h"

#ifdef ENABLE_SPEECH_SUPPORT
typedef enum {
  THD_CONSTRUCTING,
  THD_STARTING,
  THD_READY,
  THD_STOPPING,
  THD_FINISHED
} ThreadState;

typedef struct {
  const char *name;
} ThreadStateEntry;

static const ThreadStateEntry threadStateTable[] = {
  [THD_CONSTRUCTING] = {
    .name = "constructing"
  },

  [THD_STARTING] = {
    .name = "starting"
  },

  [THD_READY] = {
    .name = "ready"
  },

  [THD_STOPPING] = {
    .name = "stopping"
  },

  [THD_FINISHED] = {
    .name = "finished"
  },
};

static inline const ThreadStateEntry *
getThreadStateEntry (ThreadState state) {
  if (state >= ARRAY_COUNT(threadStateTable)) return NULL;
  return &threadStateTable[state];
}

typedef enum {
  RSP_PENDING,
  RSP_INTEGER
} SpeechResponseType;

struct SpeechDriverThreadStruct {
  ThreadState threadState;
  Queue *requestQueue;

  volatile SpeechSynthesizer *speechSynthesizer;
  char **driverParameters;

#ifdef GOT_PTHREADS
  pthread_t threadIdentifier;
  AsyncEvent *requestEvent;
  AsyncEvent *messageEvent;
  unsigned isBeingDestroyed:1;
#endif /* GOT_PTHREADS */

  struct {
    SpeechResponseType type;

    union {
      int INTEGER;
    } value;
  } response;
};

typedef enum {
  REQ_SAY_TEXT,
  REQ_MUTE_SPEECH,
  REQ_DRAIN_SPEECH,

  REQ_SET_VOLUME,
  REQ_SET_RATE,
  REQ_SET_PITCH,
  REQ_SET_PUNCTUATION
} SpeechRequestType;

static const char *const speechRequestNames[] = {
  [REQ_SAY_TEXT] = "say text",
  [REQ_MUTE_SPEECH] = "mute speech",
  [REQ_DRAIN_SPEECH] = "drain speech",

  [REQ_SET_VOLUME] = "set volume",
  [REQ_SET_RATE] = "set rate",
  [REQ_SET_PITCH] = "set pitch",
  [REQ_SET_PUNCTUATION] = "set punctuation"
};

typedef struct {
  SpeechRequestType type;

  union {
    struct {
      const unsigned char *text;
      size_t length;
      size_t count;
      const unsigned char *attributes;
      SayOptions options;
    } sayText;

    struct {
      unsigned char setting;
    } setVolume;

    struct {
      unsigned char setting;
    } setRate;

    struct {
      unsigned char setting;
    } setPitch;

    struct {
      SpeechPunctuation setting;
    } setPunctuation;
  } arguments;

  unsigned char data[0];
} SpeechRequest;

typedef struct {
  const void *address;
  size_t size;
  unsigned end:1;
} SpeechDatum;

#define BEGIN_SPEECH_DATA SpeechDatum data[] = {
#define END_SPEECH_DATA {.end=1} };

typedef enum {
  MSG_REQUEST_FINISHED,
  MSG_SPEECH_FINISHED,
  MSG_SPEECH_LOCATION
} SpeechMessageType;

static const char *const speechMessageNames[] = {
  [MSG_REQUEST_FINISHED] = "request finished",
  [MSG_SPEECH_FINISHED] = "speech finished",
  [MSG_SPEECH_LOCATION] = "speech location"
};

typedef struct {
  SpeechMessageType type;

  union {
    struct {
      int result;
    } requestFinished;

    struct {
      int location;
    } speechLocation;
  } arguments;

  unsigned char data[0];
} SpeechMessage;

static const char *
getActionName (unsigned int action, const char *const *names, size_t count) {
  return (action < count)? names[action]: NULL;
}

typedef struct {
  const char *action;
  const char *type;
  const char *name;
  unsigned int value;
} LogSpeechActionData;

static
STR_BEGIN_FORMATTER(formatLogSpeechActionData, const void *data)
  const LogSpeechActionData *lsa = data;

  STR_PRINTF("%s speech %s: ", lsa->action, lsa->type);

  if (lsa->name) {
    STR_PRINTF("%s", lsa->name);
  } else {
    STR_PRINTF("%u", lsa->value);
  }
STR_END_FORMATTER

static void
logSpeechAction (const LogSpeechActionData *lsa) {
  logData(LOG_CATEGORY(SPEECH_EVENTS), formatLogSpeechActionData, lsa);
}

static void
logSpeechRequest (SpeechRequest *req, const char *action) {
  const LogSpeechActionData lsa = {
    .action = action,
    .type = "request",
    .name = req? getActionName(req->type, speechRequestNames, ARRAY_COUNT(speechRequestNames)): "stop",
    .value = req? req->type: 0
  };

  logSpeechAction(&lsa);
}

static void
logSpeechMessage (SpeechMessage *msg, const char *action) {
  const LogSpeechActionData lsa = {
    .action = action,
    .type = "message",
    .name = getActionName(msg->type, speechMessageNames, ARRAY_COUNT(speechMessageNames)),
    .value = msg->type
  };

  logSpeechAction(&lsa);
}

static int
testThreadValidity (volatile SpeechDriverThread *sdt) {
  if (!sdt) return 0;

#ifdef GOT_PTHREADS
  if (sdt->isBeingDestroyed) return 0;
#endif /* GOT_PTHREADS */

  volatile SpeechSynthesizer *spk = sdt->speechSynthesizer;
  if (!spk) return 0;
  if (sdt != spk->driver.thread) return 0;

  if (sdt->threadState != THD_READY) return 0;
  return 1;
}

static void
setThreadState (volatile SpeechDriverThread *sdt, ThreadState state) {
  const ThreadStateEntry *entry = getThreadStateEntry(state);
  const char *name = entry? entry->name: NULL;

  if (!name) name = "?";
  logMessage(LOG_CATEGORY(SPEECH_EVENTS), "driver thread %s", name);
  sdt->threadState = state;
}

static size_t
getSpeechDataSize (const SpeechDatum *data) {
  size_t size = 0;

  if (data) {
    const SpeechDatum *datum = data;

    while (!datum->end) {
      if (datum->address) size += datum->size;
      datum += 1;
    }
  }

  return size;
}

static void
moveSpeechData (unsigned char *target, SpeechDatum *data) {
  if (data) {
    SpeechDatum *datum = data;

    while (!datum->end) {
      if (datum->address) {
        memcpy(target, datum->address, datum->size);
        datum->address = target;
        target += datum->size;
      }

      datum += 1;
    }
  }
}

static inline void
setResponsePending (volatile SpeechDriverThread *sdt) {
  sdt->response.type = RSP_PENDING;
}

static void
setIntegerResponse (volatile SpeechDriverThread *sdt, int value) {
  sdt->response.type = RSP_INTEGER;
  sdt->response.value.INTEGER = value;
}

ASYNC_CONDITION_TESTER(testSpeechResponseReceived) {
  volatile SpeechDriverThread *sdt = data;

  return sdt->response.type != RSP_PENDING;
}

static int
awaitSpeechResponse (volatile SpeechDriverThread *sdt, int timeout) {
  return asyncAwaitCondition(timeout, testSpeechResponseReceived, (void *)sdt);
}

static void sendSpeechRequest (volatile SpeechDriverThread *sdt);

static void
handleSpeechMessage (volatile SpeechDriverThread *sdt, SpeechMessage *msg) {
  logSpeechMessage(msg, "handling");

  if (msg) {
    switch (msg->type) {
      case MSG_REQUEST_FINISHED:
        setIntegerResponse(sdt, msg->arguments.requestFinished.result);
        sendSpeechRequest(sdt);
        break;

      case MSG_SPEECH_FINISHED: {
        volatile SpeechSynthesizer *spk = sdt->speechSynthesizer;
        SetSpeechFinishedMethod *setFinished = spk->setFinished;

        if (setFinished) setFinished(spk);
        break;
      }

      case MSG_SPEECH_LOCATION: {
        volatile SpeechSynthesizer *spk = sdt->speechSynthesizer;
        SetSpeechLocationMethod *setLocation = spk->setLocation;

        if (setLocation) setLocation(spk, msg->arguments.speechLocation.location);
        break;
      }

      default:
        logMessage(LOG_CATEGORY(SPEECH_EVENTS), "unimplemented message: %u", msg->type);
        break;
    }

    free(msg);
  }
}

static int
sendSpeechMessage (volatile SpeechDriverThread *sdt, SpeechMessage *msg) {
  logSpeechMessage(msg, "sending");

#ifdef GOT_PTHREADS
  return asyncSignalEvent(sdt->messageEvent, msg);
#else /* GOT_PTHREADS */
  handleSpeechMessage(sdt, msg);
  return 1;
#endif /* GOT_PTHREADS */
}

static SpeechMessage *
newSpeechMessage (SpeechMessageType type, SpeechDatum *data) {
  SpeechMessage *msg;
  size_t size = sizeof(*msg) + getSpeechDataSize(data);

  if ((msg = malloc(size))) {
    memset(msg, 0, sizeof(*msg));
    msg->type = type;
    moveSpeechData(msg->data, data);
    return msg;
  } else {
    logMallocError();
  }

  return NULL;
}

static int
speechMessage_requestFinished (
  volatile SpeechDriverThread *sdt,
  int result
) {
  SpeechMessage *msg;

  if ((msg = newSpeechMessage(MSG_REQUEST_FINISHED, NULL))) {
    msg->arguments.requestFinished.result = result;
    if (sendSpeechMessage(sdt, msg)) return 1;

    free(msg);
  }

  return 0;
}

int
speechMessage_speechFinished (
  volatile SpeechDriverThread *sdt
) {
  SpeechMessage *msg;

  if ((msg = newSpeechMessage(MSG_SPEECH_FINISHED, NULL))) {
    if (sendSpeechMessage(sdt, msg)) return 1;

    free(msg);
  }

  return 0;
}

int
speechMessage_speechLocation (
  volatile SpeechDriverThread *sdt,
  int location
) {
  SpeechMessage *msg;

  if ((msg = newSpeechMessage(MSG_SPEECH_LOCATION, NULL))) {
    msg->arguments.speechLocation.location = location;
    if (sendSpeechMessage(sdt, msg)) return 1;

    free(msg);
  }

  return 0;
}

static int
sendIntegerResponse (volatile SpeechDriverThread *sdt, int result) {
  return speechMessage_requestFinished(sdt, result);
}

static void
handleSpeechRequest (volatile SpeechDriverThread *sdt, SpeechRequest *req) {
  volatile SpeechSynthesizer *spk = sdt->speechSynthesizer;

  logSpeechRequest(req, "handling");

  if (req) {
    switch (req->type) {
      case REQ_SAY_TEXT: {
        SayOptions options = req->arguments.sayText.options;
        int restorePitch = 0;
        int restorePunctuation = 0;

        if (options & SAY_OPT_MUTE_FIRST) speech->mute(spk);

        if (options & SAY_OPT_HIGHER_PITCH) {
          if (spk->setPitch) {
            unsigned char pitch = prefs.speechPitch + 7;

            if (pitch > SPK_PITCH_MAXIMUM) pitch = SPK_PITCH_MAXIMUM;

            if (pitch != prefs.speechPitch) {
              spk->setPitch(spk, pitch);
              restorePitch = 1;
            }
          }
        }

        if (options & SAY_OPT_ALL_PUNCTUATION) {
          if (spk->setPunctuation) {
            unsigned char punctuation = SPK_PUNCTUATION_ALL;

            if (punctuation != prefs.speechPunctuation) {
              spk->setPunctuation(spk, punctuation);
              restorePunctuation = 1;
            }
          }
        }

        speech->say(spk,
          req->arguments.sayText.text, req->arguments.sayText.length,
          req->arguments.sayText.count, req->arguments.sayText.attributes
        );

        if (restorePunctuation) spk->setPunctuation(spk, prefs.speechPunctuation);
        if (restorePitch) spk->setPitch(spk, prefs.speechPitch);

        sendIntegerResponse(sdt, 1);
        break;
      }

      case REQ_MUTE_SPEECH: {
        speech->mute(spk);

        sendIntegerResponse(sdt, 1);
        break;
      }

      case REQ_DRAIN_SPEECH: {
        spk->drain(spk);

        sendIntegerResponse(sdt, 1);
        break;
      }

      case REQ_SET_VOLUME: {
        spk->setVolume(spk, req->arguments.setVolume.setting);

        sendIntegerResponse(sdt, 1);
        break;
      }

      case REQ_SET_RATE: {
        spk->setRate(spk, req->arguments.setRate.setting);

        sendIntegerResponse(sdt, 1);
        break;
      }

      case REQ_SET_PITCH: {
        spk->setPitch(spk, req->arguments.setPitch.setting);

        sendIntegerResponse(sdt, 1);
        break;
      }

      case REQ_SET_PUNCTUATION: {
        spk->setPunctuation(spk, req->arguments.setPunctuation.setting);

        sendIntegerResponse(sdt, 1);
        break;
      }

      default:
        logMessage(LOG_CATEGORY(SPEECH_EVENTS), "unimplemented request: %u", req->type);
        sendIntegerResponse(sdt, 0);
        break;
    }

    free(req);
  } else {
    setThreadState(sdt, THD_STOPPING);
    sendIntegerResponse(sdt, 1);
  }
}

typedef struct {
  SpeechRequestType const type;
} TestSpeechRequestData;

static int
testSpeechRequest (const void *item, void *data) {
  const SpeechRequest *req = item;
  const TestSpeechRequestData *tsr = data;

  return req->type == tsr->type;
}

static Element *
findSpeechRequestElement (volatile SpeechDriverThread *sdt, SpeechRequestType type) {
  TestSpeechRequestData tsr = {
    .type = type
  };

  if (!testThreadValidity(sdt)) return NULL;
  return findElement(sdt->requestQueue, testSpeechRequest, &tsr);
}

static void
removeSpeechRequests (volatile SpeechDriverThread *sdt, SpeechRequestType type) {
  Element *element;

  while ((element = findSpeechRequestElement(sdt, type))) deleteElement(element);
}

static void
muteSpeechRequestQueue (volatile SpeechDriverThread *sdt) {
  removeSpeechRequests(sdt, REQ_SAY_TEXT);
  removeSpeechRequests(sdt, REQ_MUTE_SPEECH);
}

static void
sendSpeechRequest (volatile SpeechDriverThread *sdt) {
  while (getQueueSize(sdt->requestQueue) > 0) {
    SpeechRequest *req = dequeueItem(sdt->requestQueue);

    logSpeechRequest(req, "sending");
    setResponsePending(sdt);

#ifdef GOT_PTHREADS
    if (!asyncSignalEvent(sdt->requestEvent, req)) {
      if (req) free(req);
      setIntegerResponse(sdt, 0);
      continue;
    }
#else /* GOT_PTHREADS */
    handleSpeechRequest(sdt, req);
#endif /* GOT_PTHREADS */

    break;
  }
}

static int
enqueueSpeechRequest (volatile SpeechDriverThread *sdt, SpeechRequest *req) {
  if (testThreadValidity(sdt)) {
    logSpeechRequest(req, "enqueuing");

    if (enqueueItem(sdt->requestQueue, req)) {
      if (sdt->response.type != RSP_PENDING) {
        if (getQueueSize(sdt->requestQueue) == 1) {
          sendSpeechRequest(sdt);
        }
      }

      return 1;
    }
  }

  return 0;
}

static SpeechRequest *
newSpeechRequest (SpeechRequestType type, SpeechDatum *data) {
  SpeechRequest *req;
  size_t size = sizeof(*req) + getSpeechDataSize(data);

  if ((req = malloc(size))) {
    memset(req, 0, sizeof(*req));
    req->type = type;
    moveSpeechData(req->data, data);
    return req;
  } else {
    logMallocError();
  }

  return NULL;
}

int
speechRequest_sayText (
  volatile SpeechDriverThread *sdt,
  const char *text, size_t length,
  size_t count, const unsigned char *attributes,
  SayOptions options
) {
  SpeechRequest *req;

  BEGIN_SPEECH_DATA
    {.address=text, .size=length+1},
    {.address=attributes, .size=count},
  END_SPEECH_DATA

  if ((req = newSpeechRequest(REQ_SAY_TEXT, data))) {
    req->arguments.sayText.text = data[0].address;
    req->arguments.sayText.length = length;
    req->arguments.sayText.count = count;
    req->arguments.sayText.attributes = data[1].address;
    req->arguments.sayText.options = options;

    if (options & SAY_OPT_MUTE_FIRST) muteSpeechRequestQueue(sdt);
    if (enqueueSpeechRequest(sdt, req)) return 1;

    free(req);
  }

  return 0;
}

int
speechRequest_muteSpeech (
  volatile SpeechDriverThread *sdt
) {
  SpeechRequest *req;

  if ((req = newSpeechRequest(REQ_MUTE_SPEECH, NULL))) {
    muteSpeechRequestQueue(sdt);
    if (enqueueSpeechRequest(sdt, req)) return 1;

    free(req);
  }

  return 0;
}

int
speechRequest_drainSpeech (
  volatile SpeechDriverThread *sdt
) {
  SpeechRequest *req;

  if ((req = newSpeechRequest(REQ_DRAIN_SPEECH, NULL))) {
    if (enqueueSpeechRequest(sdt, req)) {
      awaitSpeechResponse(sdt, SPEECH_RESPONSE_WAIT_TIMEOUT);
      return 1;
    }

    free(req);
  }

  return 0;
}

int
speechRequest_setVolume (
  volatile SpeechDriverThread *sdt,
  unsigned char setting
) {
  SpeechRequest *req;

  if ((req = newSpeechRequest(REQ_SET_VOLUME, NULL))) {
    req->arguments.setVolume.setting = setting;
    if (enqueueSpeechRequest(sdt, req)) return 1;

    free(req);
  }

  return 0;
}

int
speechRequest_setRate (
  volatile SpeechDriverThread *sdt,
  unsigned char setting
) {
  SpeechRequest *req;

  if ((req = newSpeechRequest(REQ_SET_RATE, NULL))) {
    req->arguments.setRate.setting = setting;
    if (enqueueSpeechRequest(sdt, req)) return 1;

    free(req);
  }

  return 0;
}

int
speechRequest_setPitch (
  volatile SpeechDriverThread *sdt,
  unsigned char setting
) {
  SpeechRequest *req;

  if ((req = newSpeechRequest(REQ_SET_PITCH, NULL))) {
    req->arguments.setPitch.setting = setting;
    if (enqueueSpeechRequest(sdt, req)) return 1;

    free(req);
  }

  return 0;
}

int
speechRequest_setPunctuation (
  volatile SpeechDriverThread *sdt,
  SpeechPunctuation setting
) {
  SpeechRequest *req;

  if ((req = newSpeechRequest(REQ_SET_PUNCTUATION, NULL))) {
    req->arguments.setPunctuation.setting = setting;
    if (enqueueSpeechRequest(sdt, req)) return 1;

    free(req);
  }

  return 0;
}

static void
setThreadReady (volatile SpeechDriverThread *sdt) {
  setThreadState(sdt, THD_READY);
  sendIntegerResponse(sdt, 1);
}

static int
startSpeechDriver (volatile SpeechDriverThread *sdt) {
  logMessage(LOG_CATEGORY(SPEECH_EVENTS), "starting driver");
  return speech->construct(sdt->speechSynthesizer, sdt->driverParameters);
}

static void
stopSpeechDriver (volatile SpeechDriverThread *sdt) {
  logMessage(LOG_CATEGORY(SPEECH_EVENTS), "stopping driver");
  speech->destruct(sdt->speechSynthesizer);
}

#ifdef GOT_PTHREADS
ASYNC_CONDITION_TESTER(testSpeechDriverThreadStopping) {
  volatile SpeechDriverThread *sdt = data;

  return sdt->threadState == THD_STOPPING;
}

ASYNC_EVENT_CALLBACK(handleSpeechMessageEvent) {
  volatile SpeechDriverThread *sdt = parameters->eventData;
  SpeechMessage *msg = parameters->signalData;

  handleSpeechMessage(sdt, msg);
}

ASYNC_EVENT_CALLBACK(handleSpeechRequestEvent) {
  volatile SpeechDriverThread *sdt = parameters->eventData;
  SpeechRequest *req = parameters->signalData;

  handleSpeechRequest(sdt, req);
}

static void
awaitSpeechDriverThreadTermination (volatile SpeechDriverThread *sdt) {
  void *result;

  pthread_join(sdt->threadIdentifier, &result);
}

THREAD_FUNCTION(runSpeechDriverThread) {
  volatile SpeechDriverThread *sdt = argument;

  setThreadState(sdt, THD_STARTING);

  if ((sdt->requestEvent = asyncNewEvent(handleSpeechRequestEvent, (void *)sdt))) {
    if (startSpeechDriver(sdt)) {
      setThreadReady(sdt);
      asyncWaitFor(testSpeechDriverThreadStopping, (void *)sdt);
      stopSpeechDriver(sdt);
    } else {
      logMessage(LOG_CATEGORY(SPEECH_EVENTS), "driver construction failure");
    }

    asyncDiscardEvent(sdt->requestEvent);
    sdt->requestEvent = NULL;
  } else {
    logMessage(LOG_CATEGORY(SPEECH_EVENTS), "request event construction failure");
  }

  {
    int ok = sdt->threadState == THD_STOPPING;

    sendIntegerResponse(sdt, ok);
  }

  setThreadState(sdt, THD_FINISHED);
  return NULL;
}
#endif /* GOT_PTHREADS */

static void
deallocateSpeechRequest (void *item, void *data) {
  SpeechRequest *req = item;

  logSpeechRequest(req, "unqueuing");
  free(req);
}

int
constructSpeechDriverThread (
  volatile SpeechSynthesizer *spk,
  char **parameters
) {
  volatile SpeechDriverThread *sdt;

  if ((sdt = malloc(sizeof(*sdt)))) {
    memset((void *)sdt, 0, sizeof(*sdt));
    setThreadState(sdt, THD_CONSTRUCTING);
    setResponsePending(sdt);

    sdt->speechSynthesizer = spk;
    sdt->driverParameters = parameters;

    if ((sdt->requestQueue = newQueue(deallocateSpeechRequest, NULL))) {
      spk->driver.thread = sdt;

#ifdef GOT_PTHREADS
      if ((sdt->messageEvent = asyncNewEvent(handleSpeechMessageEvent, (void *)sdt))) {
        pthread_t threadIdentifier;
        int createError = createThread("speech-driver",
                                       &threadIdentifier, NULL,
                                       runSpeechDriverThread, (void *)sdt);

        if (!createError) {
          sdt->threadIdentifier = threadIdentifier;

          if (awaitSpeechResponse(sdt, SPEECH_DRIVER_THREAD_START_TIMEOUT)) {
            if (sdt->response.type == RSP_INTEGER) {
              if (sdt->response.value.INTEGER) {
                return 1;
              }
            }

            logMessage(LOG_CATEGORY(SPEECH_EVENTS), "driver thread initialization failure");
            awaitSpeechDriverThreadTermination(sdt);
          } else {
            logMessage(LOG_CATEGORY(SPEECH_EVENTS), "driver thread initialization timeout");
          }
        } else {
          logMessage(LOG_CATEGORY(SPEECH_EVENTS), "driver thread creation failure: %s", strerror(createError));
        }

        asyncDiscardEvent(sdt->messageEvent);
        sdt->messageEvent = NULL;
      } else {
        logMessage(LOG_CATEGORY(SPEECH_EVENTS), "response event construction failure");
      }
#else /* GOT_PTHREADS */
      if (startSpeechDriver(sdt)) {
        setThreadReady(sdt);
        return 1;
      }
#endif /* GOT_PTHREADS */

      spk->driver.thread = NULL;
      deallocateQueue(sdt->requestQueue);
    }

    free((void *)sdt);
  } else {
    logMallocError();
  }

  spk->driver.thread = NULL;
  return 0;
}

void
destroySpeechDriverThread (volatile SpeechSynthesizer *spk) {
  volatile SpeechDriverThread *sdt = spk->driver.thread;

  deleteElements(sdt->requestQueue);

#ifdef GOT_PTHREADS
  if (enqueueSpeechRequest(sdt, NULL)) {
    sdt->isBeingDestroyed = 1;
    awaitSpeechResponse(sdt, SPEECH_DRIVER_THREAD_STOP_TIMEOUT);

    setResponsePending(sdt);
    awaitSpeechResponse(sdt, SPEECH_DRIVER_THREAD_STOP_TIMEOUT);

    awaitSpeechDriverThreadTermination(sdt);
  }

  if (sdt->messageEvent) asyncDiscardEvent(sdt->messageEvent);
#else /* GOT_PTHREADS */
  stopSpeechDriver(sdt);
  setThreadState(sdt, THD_FINISHED);
#endif /* GOT_PTHREADS */

  sdt->speechSynthesizer->driver.thread = NULL;
  deallocateQueue(sdt->requestQueue);
  free((void *)sdt);
}
#endif /* ENABLE_SPEECH_SUPPORT */