Path: blob/master/src/jdk.accessibility/windows/native/libwindowsaccessbridge/AccessBridgeMessageQueue.cpp
40957 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* A class to manage queueing of messages for IPC27*/2829#include "AccessBridgeDebug.h"30#include "AccessBridgeMessageQueue.h"31#include "AccessBridgePackages.h" // for debugging only32#include <windows.h>33#include <malloc.h>34#include <new>3536DEBUG_CODE(extern HWND theDialogWindow);37extern "C" {38DEBUG_CODE(void AppendToCallInfo(char *s));39}4041// -------------------424344AccessBridgeQueueElement::AccessBridgeQueueElement(char *buf, int size) {45bufsize = size;46next = (AccessBridgeQueueElement *) 0;47previous = (AccessBridgeQueueElement *) 0;48buffer = (char *) malloc(bufsize);49if (buffer == NULL) {50throw std::bad_alloc();51}52memcpy(buffer, buf, bufsize);53}5455AccessBridgeQueueElement::~AccessBridgeQueueElement() {56// delete buffer;57free(buffer);58}596061// -------------------626364AccessBridgeMessageQueue::AccessBridgeMessageQueue() {65queueLocked = FALSE;66queueRemoveLocked = FALSE;67start = (AccessBridgeQueueElement *) 0;68end = (AccessBridgeQueueElement *) 0;69size = 0;70}7172AccessBridgeMessageQueue::~AccessBridgeMessageQueue() {73// empty queue, then exit74}7576/**77* getEventsWaiting - gets the number of events waiting to fire78*/79int80AccessBridgeMessageQueue::getEventsWaiting() {81return size;82}8384/**85* add - add an element to the queue, which is locked with semaphores86*87*/88QueueReturns89AccessBridgeMessageQueue::add(AccessBridgeQueueElement *element) {90PrintDebugString("[INFO]: in AccessBridgeMessageQueue::add()");91PrintDebugString("[INFO]: queue size = %d", size);9293QueueReturns returnVal = cElementPushedOK;94if (queueLocked) {95PrintDebugString("[WARN]: queue was locked; returning cQueueInUse!");96return cQueueInUse;97}98queueLocked = TRUE;99{100PrintDebugString("[INFO]: adding element to queue!");101if (end == (AccessBridgeQueueElement *) 0) {102if (start == (AccessBridgeQueueElement *) 0 && size == 0) {103start = element;104end = element;105element->previous = (AccessBridgeQueueElement *) 0;106element->next = (AccessBridgeQueueElement *) 0;107size++;108} else {109returnVal = cQueueBroken; // bad voodo!110}111} else {112element->previous = end;113element->next = (AccessBridgeQueueElement *) 0;114end->next = element;115end = element;116size++;117}118}119queueLocked = FALSE;120PrintDebugString("[INFO]: returning from AccessBridgeMessageQueue::add()");121return returnVal;122}123124125/**126* remove - remove an element from the queue, which is locked with semaphores127*128*/129QueueReturns130AccessBridgeMessageQueue::remove(AccessBridgeQueueElement **element) {131PrintDebugString("[INFO]: in AccessBridgeMessageQueue::remove()");132PrintDebugString("[INFO]: queue size = %d", size);133134QueueReturns returnVal = cMoreMessages;135if (queueLocked) {136PrintDebugString("[WARN]: queue was locked; returning cQueueInUse!");137return cQueueInUse;138}139queueLocked = TRUE;140{141PrintDebugString("[INFO]: removing element from queue!");142if (size > 0) {143if (start != (AccessBridgeQueueElement *) 0) {144*element = start;145start = start->next;146if (start != (AccessBridgeQueueElement *) 0) {147start->previous = (AccessBridgeQueueElement *) 0;148} else {149end = (AccessBridgeQueueElement *) 0;150if (size != 1) {151returnVal = cQueueBroken; // bad voodo, should only be 1 in this situation152}153}154size--;155} else {156returnVal = cQueueBroken; // bad voodo!157}158} else {159returnVal = cQueueEmpty;160}161}162queueLocked = FALSE;163PrintDebugString("[INFO]: returning from AccessBridgeMessageQueue::remove()");164return returnVal;165}166167168/**169* setRemoveLock - set the state of the removeLock (TRUE or FALSE)170*171*/172QueueReturns173AccessBridgeMessageQueue::setRemoveLock(BOOL removeLockSetting) {174if (queueLocked) {175return cQueueInUse;176}177queueRemoveLocked = removeLockSetting;178179return cQueueOK;180}181182/**183* setRemoveLock - set the state of the removeLock (TRUE or FALSE)184*185*/186BOOL187AccessBridgeMessageQueue::getRemoveLockSetting() {188return queueRemoveLocked;189}190191192