<commit_before>/**
 * @file clientlist.c
 * @author <NAME>
 * @date 2007
 *
 * @brief Functions to manage lists of clients.
 *
 */

#include "jwm.h"
#include "clientlist.h"
#include "client.h"
#include "main.h"
#include "key.h"

ClientNode *nodes[LAYER_COUNT];
ClientNode *nodeTail[LAYER_COUNT];

static Window *windowStack = NULL;  /**< Image of the window stack. */
static int windowStackSize = 0;     /**< Size of the image. */
static int windowStackCurrent = 0;  /**< Current location in the image. */

/** Determine if a client is allowed focus. */
int ShouldFocus(const ClientNode *np) {

   if(np->state.desktop != currentDesktop
      && !(np->state.status & STAT_STICKY)) {
      return 0;
   }

   if(np->state.status & STAT_NOLIST) {
      return 0;
   }

   if(np->owner != None) {
      return 0;
   }

   return 1;

}

/** Start walking the window stack. */
void StartWindowStackWalk() {

   /* Get an image of the window stack.
    * Here we get the Window IDs rather than client pointers so
    * clients can be added/removed without disrupting the stack walk.
    */

   ClientNode *np;
   int layer;
   int count;

   /* If we are already walking the stack, just return. */
   if(windowStack != NULL) {
      return;
   }

   /* First determine how much space to allocate for windows. */
   count = 0;
   for(layer = LAYER_TOP; layer >= LAYER_BOTTOM; layer--) {
      for(np = nodes[layer]; np; np = np->next) {
         if(ShouldFocus(np)) {
            ++count;
         }
      }
   }

   /* If there were no windows to walk, don't even start. */
   if(count == 0) {
      return;
   }

   /* Allocate space for the windows. */
   windowStack = Allocate(sizeof(Window) * count);

   /* Copy windows into the array. */
   windowStackSize = 0;
   for(layer = LAYER_TOP; layer >= LAYER_BOTTOM; layer--) {
      for(np = nodes[layer]; np; np = np->next) {
         if(ShouldFocus(np)) {
            windowStack[windowStackSize++] = np->window;
         }
      }
   }

   Assert(windowStackSize == count);

   windowStackCurrent = 0;

}

/** Move to the next window in the window stack. */
void WalkWindowStack(int forward) {

   ClientNode *np;
   int x;

   if(windowStack != NULL) {

      /* Loop until we either raise a window or go through them all. */
      for(x = 0; x < windowStackSize; x++) {

         /* Move to the next/previous window (wrap if needed). */
         if(forward) {
             windowStackCurrent = (windowStackCurrent + 1) % windowStackSize;
         } else {
             if(windowStackCurrent == 0) {
                 windowStackCurrent = windowStackSize;
             }
             --windowStackCurrent;
         }

         /* Look up the window. */
         np = FindClientByWindow(windowStack[windowStackCurrent]);

         /* Skip this window if it no longer exists or is currently in
          * a state that doesn't allow focus.
          */
         if(np == NULL || !ShouldFocus(np)) {
            continue;
         }

         /* Focus the window. We only raise the client when the
          * stack walk completes. */
         FocusClient(np);
         break;

      }

   }

}

/** Stop walking the window stack. */
void StopWindowStackWalk() {

   ClientNode *np;

   /* Raise the selected window and free the window array. */
   if(windowStack != NULL) {

      /* Look up the current window. */
      np = FindClientByWindow(windowStack[windowStackCurrent]);
      if(np) {
         if(np->state.status & STAT_MINIMIZED) {
            RestoreClient(np, 1);
         } else {
            RaiseClient(np);
         }
      }

      Release(windowStack);
      windowStack = NULL;

      windowStackSize = 0;
      windowStackCurrent = 0;

   }

}

/** Focus the next client in the stacking order. */
void FocusNextStacked(ClientNode *np) {

   int x;
   ClientNode *tp;

   Assert(np);

   for(tp = np->next; tp; tp = tp->next) {
      if((tp->state.status & (STAT_MAPPED | STAT_SHADED))
         && !(tp->state.status & STAT_HIDDEN)) {
         FocusClient(tp);
         return;
      }
   }
   for(x = np->state.layer - 1; x >= LAYER_BOTTOM; x--) {
      for(tp = nodes[x]; tp; tp = tp->next) {
         if((tp->state.status & (STAT_MAPPED | STAT_SHADED))
            && !(tp->state.status & STAT_HIDDEN)) {
            FocusClient(tp);
            return;
         }
      }
   }

}<commit_msg>Attempt to fix an issue with walking the window stack.<commit_after>/**
 * @file clientlist.c
 * @author <NAME>
 * @date 2007
 *
 * @brief Functions to manage lists of clients.
 *
 */

#include "jwm.h"
#include "clientlist.h"
#include "client.h"
#include "main.h"
#include "key.h"

ClientNode *nodes[LAYER_COUNT];
ClientNode *nodeTail[LAYER_COUNT];

static Window *windowStack = NULL;  /**< Image of the window stack. */
static int windowStackSize = 0;     /**< Size of the image. */
static int windowStackCurrent = 0;  /**< Current location in the image. */

/** Determine if a client is allowed focus. */
int ShouldFocus(const ClientNode *np) {

   if(np->state.desktop != currentDesktop
      && !(np->state.status & STAT_STICKY)) {
      return 0;
   }

   if(np->state.status & STAT_NOLIST) {
      return 0;
   }

   if(np->owner != None) {
      return 0;
   }

   return 1;

}

/** Start walking the window stack. */
void StartWindowStackWalk() {

   /* Get an image of the window stack.
    * Here we get the Window IDs rather than client pointers so
    * clients can be added/removed without disrupting the stack walk.
    */

   ClientNode *np;
   int layer;
   int count;

   /* If we are already walking the stack, just return. */
   if(windowStack != NULL) {
      return;
   }

   /* First determine how much space to allocate for windows. */
   count = 0;
   for(layer = LAYER_TOP; layer >= LAYER_BOTTOM; layer--) {
      for(np = nodes[layer]; np; np = np->next) {
         if(ShouldFocus(np)) {
            ++count;
         }
      }
   }

   /* If there were no windows to walk, don't even start. */
   if(count == 0) {
      return;
   }

   /* Allocate space for the windows. */
   windowStack = Allocate(sizeof(Window) * count);

   /* Copy windows into the array. */
   windowStackSize = 0;
   for(layer = LAYER_TOP; layer >= LAYER_BOTTOM; layer--) {
      for(np = nodes[layer]; np; np = np->next) {
         if(ShouldFocus(np)) {
            windowStack[windowStackSize++] = np->window;
         }
      }
   }

   Assert(windowStackSize == count);

   windowStackCurrent = 0;

   JXGrabKeyboard(display, rootWindow, False, GrabModeAsync,
                  GrabModeAsync, CurrentTime);

}

/** Move to the next window in the window stack. */
void WalkWindowStack(int forward) {

   ClientNode *np;
   int x;

   if(windowStack != NULL) {

      /* Loop until we either raise a window or go through them all. */
      for(x = 0; x < windowStackSize; x++) {

         /* Move to the next/previous window (wrap if needed). */
         if(forward) {
             windowStackCurrent = (windowStackCurrent + 1) % windowStackSize;
         } else {
             if(windowStackCurrent == 0) {
                 windowStackCurrent = windowStackSize;
             }
             --windowStackCurrent;
         }

         /* Look up the window. */
         np = FindClientByWindow(windowStack[windowStackCurrent]);

         /* Skip this window if it no longer exists or is currently in
          * a state that doesn't allow focus.
          */
         if(np == NULL || !ShouldFocus(np)) {
            continue;
         }

         /* Focus the window. We only raise the client when the
          * stack walk completes. */
         FocusClient(np);
         break;

      }

   }

}

/** Stop walking the window stack. */
void StopWindowStackWalk() {

   ClientNode *np;

   /* Raise the selected window and free the window array. */
   if(windowStack != NULL) {

      /* Look up the current window. */
      np = FindClientByWindow(windowStack[windowStackCurrent]);
      if(np) {
         if(np->state.status & STAT_MINIMIZED) {
            RestoreClient(np, 1);
         } else {
            RaiseClient(np);
         }
      }

      Release(windowStack);
      windowStack = NULL;

      windowStackSize = 0;
      windowStackCurrent = 0;

      JXUngrabKeyboard(display, CurrentTime);

   }

}

/** Focus the next client in the stacking order. */
void FocusNextStacked(ClientNode *np) {

   int x;
   ClientNode *tp;

   Assert(np);

   for(tp = np->next; tp; tp = tp->next) {
      if((tp->state.status & (STAT_MAPPED | STAT_SHADED))
         && !(tp->state.status & STAT_HIDDEN)) {
         FocusClient(tp);
         return;
      }
   }
   for(x = np->state.layer - 1; x >= LAYER_BOTTOM; x--) {
      for(tp = nodes[x]; tp; tp = tp->next) {
         if((tp->state.status & (STAT_MAPPED | STAT_SHADED))
            && !(tp->state.status & STAT_HIDDEN)) {
            FocusClient(tp);
            return;
         }
      }
   }

}
{'fraction_non_alphanumeric': 0.12633112879722005, 'fraction_numerical': 0.004707992377536151, 'mean_word_length': 2.072314049586777, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1152522', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2737, 'n_words': 787}