(1623) 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 d624638fed
commit 5499a2999a

View File

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