xkb: let SendDeviceLedFBs() fill buffer instead of writing directly

Make the code flow a bit easier to understand and allow further simplification
by now just having to write out one additional payload as one block.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-07-16 15:22:36 +02:00
parent bc22eb32ee
commit 580b0b7aff

View File

@ -6264,7 +6264,7 @@ CheckDeviceLedFBs(DeviceIntPtr dev,
}
static int
SendDeviceLedInfo(XkbSrvLedInfoPtr sli, ClientPtr client)
FillDeviceLedInfo(XkbSrvLedInfoPtr sli, char *buffer, ClientPtr client)
{
int length = 0;
xkbDeviceLedsWireDesc wire = {
@ -6284,22 +6284,24 @@ SendDeviceLedInfo(XkbSrvLedInfoPtr sli, ClientPtr client)
swapl(&wire.physIndicators);
swapl(&wire.state);
}
WriteToClient(client, SIZEOF(xkbDeviceLedsWireDesc), &wire);
length += SIZEOF(xkbDeviceLedsWireDesc);
memcpy(buffer, &wire, sizeof(wire));
buffer += sizeof(wire);
length += sizeof(wire);
if (sli->namesPresent | sli->mapsPresent) {
register unsigned i, bit;
if (sli->namesPresent) {
CARD32 awire;
for (i = 0, bit = 1; i < XkbNumIndicators; i++, bit <<= 1) {
if (sli->namesPresent & bit) {
awire = (CARD32) sli->names[i];
CARD32 *val = (CARD32*)buffer;
*val = sli->names[i];
if (client->swapped) {
swapl(&awire);
swapl(val);
}
WriteToClient(client, 4, &awire);
length += 4;
length += sizeof(CARD32);
buffer += sizeof(CARD32);
}
}
}
@ -6316,14 +6318,13 @@ SendDeviceLedInfo(XkbSrvLedInfoPtr sli, ClientPtr client)
.virtualMods = sli->maps[i].mods.vmods,
.ctrls = sli->maps[i].ctrls,
};
if (client->swapped) {
swaps(&iwire.virtualMods);
swapl(&iwire.ctrls);
}
WriteToClient(client, SIZEOF(xkbIndicatorMapWireDesc),
&iwire);
length += sizeof(xkbIndicatorMapWireDesc);
memcpy(buffer, &iwire, sizeof(iwire));
buffer += sizeof(iwire);
length += sizeof(iwire);
}
}
}
@ -6332,8 +6333,8 @@ SendDeviceLedInfo(XkbSrvLedInfoPtr sli, ClientPtr client)
}
static int
SendDeviceLedFBs(DeviceIntPtr dev,
int class, int id, unsigned wantLength, ClientPtr client)
FillDeviceLedFBs(DeviceIntPtr dev, int class, int id, unsigned wantLength,
char *buffer, ClientPtr client)
{
int length = 0;
@ -6350,7 +6351,9 @@ SendDeviceLedFBs(DeviceIntPtr dev,
for (kf = dev->kbdfeed; (kf); kf = kf->next) {
if ((id == XkbAllXIIds) || (id == XkbDfltXIId) ||
(id == kf->ctrl.id)) {
length += SendDeviceLedInfo(kf->xkb_sli, client);
int written = FillDeviceLedInfo(kf->xkb_sli, buffer, client);
buffer += written;
length += written;
if (id != XkbAllXIIds)
break;
}
@ -6363,7 +6366,9 @@ SendDeviceLedFBs(DeviceIntPtr dev,
for (lf = dev->leds; (lf); lf = lf->next) {
if ((id == XkbAllXIIds) || (id == XkbDfltXIId) ||
(id == lf->ctrl.id)) {
length += SendDeviceLedInfo(lf->xkb_sli, client);
int written = FillDeviceLedInfo(lf->xkb_sli, buffer, client);
buffer += written;
length += written;
if (id != XkbAllXIIds)
break;
}
@ -6484,7 +6489,7 @@ ProcXkbGetDeviceInfo(ClientPtr client)
}
WriteToClient(client, SIZEOF(xkbGetDeviceInfoReply), &rep);
int sz = nameLen + rep.nBtnsRtrn * sizeof(xkbActionWireDesc);
int sz = nameLen + rep.nBtnsRtrn * sizeof(xkbActionWireDesc) + led_len;
char *buf = calloc(1, sz);
if (!buf)
return BadAlloc;
@ -6500,11 +6505,10 @@ ProcXkbGetDeviceInfo(ClientPtr client)
walk += sizeof(xkbActionWireDesc)*rep.nBtnsRtrn;
}
WriteToClient(client, sz, buf);
length -= sz;
if (nDeviceLedFBs > 0) {
status = SendDeviceLedFBs(dev, ledClass, ledID, length, client);
status = FillDeviceLedFBs(dev, ledClass, ledID, length, walk, client);
if (status != Success)
return status;
}
@ -6514,6 +6518,8 @@ ProcXkbGetDeviceInfo(ClientPtr client)
length);
return BadLength;
}
WriteToClient(client, sizeof(buf), &buf);
return Success;
}